BrightSide Workbench Full Report + Source Code
HTMLForm.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.html;
19 
20 import java.io.IOException;
21 import java.util.Iterator;
22 import java.util.Map;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.context.IElement;
25 
30 public class HTMLForm extends HTMLComponent {
31 
35  public HTMLForm(IElement iel) throws IOException {
36  super(iel);
37  }
38 
39  public void startForm(String id, String method, String actionElement) {
40  startForm(id, method, actionElement, "class='htmlform'");
41  }
42 
43  public void startForm(String id, String method, String actionElement, String attributes) {
44  String formAction = ElephantContext.getRootWebPath() +
45  "/xpaction/" + actionElement;
46  write("<form id='" + id + "' action='" + formAction + "' method='" + method + "' " + attributes + ">");
47  createHiddenElement();
48  }
49 
50  public void startMultipartForm(String id, String method, String actionElement) {
51  String formAction = ElephantContext.getRootWebPath() +
52  "/xpaction/" + actionElement;
53  write("<form id='" + id + "' action='" + formAction + "' method='" + method + "' enctype='multipart/form-data' class='htmlform'>");
54  createHiddenElement();
55  }
56 
57  public void endForm() {
58  writeln("</form>");
59  }
60 
61  public void command(String command, String value, String style) {
62  doTag("input", new String[] {
63  "type='submit'",
64  "class='hf_command'",
65  "name='action_" + command + "'",
66  "style='" + style + "'",
67  "value=\"" + value + "\""
68  });
69  }
70 
71  public void command(String command, String value, String style, String onClick) {
72  doTag("input", new String[] {
73  "type='submit'",
74  "class='hf_command'",
75  "name='action_" + command + "'",
76  "style='" + style + "'",
77  "onclick=\"javascript:" + onClick + "\"",
78  "value=\"" + value + "\""
79  });
80  }
81 
82  public void clientCommand(String command, String value, String style) {
83  doTag("input", new String[] {
84  "type='button'",
85  "class='hf_command'",
86  "onclick='javascript:" + command + "'",
87  "style='" + style + "'",
88  "value=\"" + value + "\""
89  });
90  }
91 
92  public void inPlaceTextInput(String idShow, String id, String value, String style) {
93  doTag("img",
94  new String[] {
95  "onclick='showEdit" + id + "(this)'",
96  "alt='Edit'",
97  "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/inplaceedit.gif'",
98  "style='cursor:pointer'"
99  });
100  textInput(id, value, style + ";display:none");
101  startJavaScript();
102  write(
103  "function showEdit" + id + "(el) {" +
104  "el.hide();" +
105  "$('" + idShow + "').hide();" +
106  "$('" + id + "').show();" +
107  "}"
108  );
109  endJavaScript();
110  }
111 
112  public void textInput(String id, String value, String style) {
113  if(value == null) value = "";
114  write("<input type='text' name='" + id + "' id='" + id + "' value='" + value + "' style='" + style + "' class='hf_input'/>");
115  }
116 
117  public void passwordInput(String id, String value, String style) {
118  if(value == null) value = "";
119  write("<input type='password' name='" + id + "' id='" + id + "' value='" + value + "' style='" + style + "' class='hf_input'/>");
120  }
121 
122  public void checkBox(String id, String value, String style, boolean checked) {
123  if(value == null) value = "";
124  write("<input type='checkbox' name='" + id + "' id='" + id + "' value='" + value + "' style='" + style + "' class='hf_check'" + (checked ? " checked" : "") + "/>");
125  }
126 
127  public void inPlaceTextArea(String idShow, String id, String value, String style) {
128  doTag("img",
129  new String[] {
130  "onclick='showEdit" + id + "(this)'",
131  "alt='Edit'",
132  "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/inplaceedit.gif'",
133  "style='cursor:pointer'"
134  });
135  textArea(id, value, style + ";display:none");
136  startJavaScript();
137  write(
138  "function showEdit" + id + "(el) {" +
139  "el.hide();" +
140  "$('" + idShow + "').hide();" +
141  "$('" + id + "').show();" +
142  "}"
143  );
144  endJavaScript();
145  }
146 
147  public void textArea(String id, String value, String style) {
148  if(value == null) value = "";
149  write("<textarea name='" + id + "' id='" + id + "' style='" + style + "' class='hf_editor'>" + value + "</textarea>");
150  }
151 
152  public void selectList(String id, String value, Map options, String style) {
153  write("<select name='" + id + "' id='" + id + "' style='" + style + "' class='hf_select'>");
154  Iterator it = options.keySet().iterator();
155  String key;
156  while(it.hasNext()) {
157  key = (String)it.next();
158  write("<option value='" + key + "'");
159  if(key.equals(value)) {
160  write(" selected");
161  }
162  write(">");
163  write((String)options.get(key));
164  write("</option>");
165  }
166  write("</select>");
167  }
168 
169  public void hidden(String id, String value) {
170  write("<input type='hidden' name='" + id + "' value='" + value + "'/>");
171  }
172 
173  public void inPlaceAutocompleter(String idShow, String id, String value, String style, String actionElement, String action, String extraParam, String tokenizer) {
174  doTag("img",
175  new String[] {
176  "onclick='showEdit" + id + "(this)'",
177  "alt='Edit'",
178  "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/inplaceedit.gif'",
179  "style='cursor:pointer'"
180  });
181  autocompleter(id, value, style + ";display:none", actionElement, action, extraParam, tokenizer);
182  startJavaScript();
183  write(
184  "function showEdit" + id + "(el) {" +
185  "el.hide();" +
186  (idShow != null ? "$('" + idShow + "').hide();" : "") +
187  "$('" + id + "').show();" +
188  "}"
189  );
190  endJavaScript();
191  }
192 
193  public void autocompleter(String id, String value, String style, String actionElement, String action, String extraParam, String tokenizer) {
194  String formAction = ElephantContext.getRootWebPath() +
195  "/xpaction/" + actionElement +
196  "?action=" + action +
197  "&context=" + iel.getContext().getPath() +
198  "&idel=" + iel.getId() +
199  "&field=" + id +
200  extraParam;
201  if(value == null) value = "";
202  write("<input type='text' id='" + id + "' name='" + id + "' value='" + value + "' style='" + style + "' class='hf_autocompleter'/>");
203  String idList = "ac_" + id;
204  startTag("div", new String[] { "id='" + idList + "'", "class='autolist'" });
205  endTag();
206  startJavaScript();
207  write(
208  "new Ajax.Autocompleter(\n" +
209  "\"" + id + "\", \"" + idList + "\"," +
210  "\"" + formAction + "\"" +
211  (tokenizer != null ? ", {tokens:\"" + tokenizer + "\"}" : "") +
212  ");"
213  );
214  endJavaScript();
215  }
216 
217  public void fileUpload(String id, String style) {
218  write("<input type='file' name='" + id + "' style='" + style + "' class='hf_upload'/>");
219  }
220 
221  private void createHiddenElement() {
222  hidden("context", iel.getContext().getPath());
223  hidden("idel", (iel.getId() == null ? "" : iel.getId()));
224  }
225 }
void startForm(String id, String method, String actionElement)
Definition: HTMLForm.java:39
void textInput(String id, String value, String style)
Definition: HTMLForm.java:112
HTMLForm(IElement iel)
Definition: HTMLForm.java:35
void inPlaceTextInput(String idShow, String id, String value, String style)
Definition: HTMLForm.java:92
void command(String command, String value, String style)
Definition: HTMLForm.java:61
void textArea(String id, String value, String style)
Definition: HTMLForm.java:147
void passwordInput(String id, String value, String style)
Definition: HTMLForm.java:117
void checkBox(String id, String value, String style, boolean checked)
Definition: HTMLForm.java:122
void inPlaceTextArea(String idShow, String id, String value, String style)
Definition: HTMLForm.java:127
void clientCommand(String command, String value, String style)
Definition: HTMLForm.java:82
void fileUpload(String id, String style)
Definition: HTMLForm.java:217
void command(String command, String value, String style, String onClick)
Definition: HTMLForm.java:71
void selectList(String id, String value, Map options, String style)
Definition: HTMLForm.java:152
void autocompleter(String id, String value, String style, String actionElement, String action, String extraParam, String tokenizer)
Definition: HTMLForm.java:193
void startForm(String id, String method, String actionElement, String attributes)
Definition: HTMLForm.java:43
void startMultipartForm(String id, String method, String actionElement)
Definition: HTMLForm.java:50
void hidden(String id, String value)
Definition: HTMLForm.java:169
void inPlaceAutocompleter(String idShow, String id, String value, String style, String actionElement, String action, String extraParam, String tokenizer)
Definition: HTMLForm.java:173
HTMLGenerator doTag(String tag)
HTMLGenerator startTag(String tag)
void writeln(String value)
Definition: HTMLHelper.java:90
HTMLGenerator write(String value)
Definition: HTMLHelper.java:69