View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.control;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.util.NodeDataUtil;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.WebContext;
41  
42  import java.util.ArrayList;
43  import java.util.Hashtable;
44  import java.util.Iterator;
45  import java.util.List;
46  import java.util.Map;
47  import java.io.Writer;
48  import java.io.IOException;
49  
50  import javax.jcr.PropertyType;
51  import javax.servlet.http.HttpServletRequest;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  
56  /**
57   * @author Vinzenz Wyser
58   * @version 2.0
59   */
60  public class ControlImpl implements Control {
61  
62      public static final int BUTTONTYPE_PUSHBUTTON = 0;
63  
64      public static final int BUTTONTYPE_CHECKBOX = 1;
65  
66      public static final int BUTTONTYPE_RADIO = 2;
67  
68      public static final int BUTTONSTATE_NORMAL = 0;
69  
70      public static final int BUTTONSTATE_MOUSEOVER = 1;
71  
72      public static final int BUTTONSTATE_MOUSEDOWN = 2;
73  
74      public static final int BUTTONSTATE_PUSHED = 3;
75  
76      public static final int BUTTONSTATE_DISABLED = 4; // not yet supported
77  
78      public static final int VALUETYPE_SINGLE = 0;
79  
80      public static final int VALUETYPE_MULTIPLE = 1;
81  
82      public static final int ENCODING_NO = 0;
83  
84      public static final int ENCODING_BASE64 = 1;
85  
86      public static final int ENCODING_UNIX = 2;
87  
88      public static final int RICHEDIT_NONE = 0;
89  
90      public static final int RICHEDIT_KUPU = 1;
91  
92      public static final int RICHEDIT_FCK = 2;
93  
94      public static final String CSSCLASS_CONTROLBUTTON = "mgnlControlButton"; //$NON-NLS-1$
95  
96      public static final String CSSCLASS_CONTROLBUTTONSMALL = "mgnlControlButtonSmall"; //$NON-NLS-1$
97  
98      public static final String CSSCLASS_CONTROLBAR = "mgnlControlBar"; //$NON-NLS-1$
99  
100     public static final String CSSCLASS_CONTROLBARSMALL = "mgnlControlBarSmall"; //$NON-NLS-1$
101 
102     private int valueType = VALUETYPE_SINGLE;
103 
104     private int encoding = ENCODING_NO;
105 
106     private int isRichEditValue = RICHEDIT_NONE;
107 
108     private String label;
109 
110     private String name;
111 
112     private String id;
113 
114     private String value;
115 
116     private List values = new ArrayList(); // mulitple values (checkbox)
117 
118     private Map events = new Hashtable();
119 
120     private Content websiteNode;
121 
122     private String htmlPre;
123 
124     private String htmlInter;
125 
126     private String htmlPost;
127 
128     private int type = PropertyType.STRING;
129 
130     private boolean saveInfo = true;
131 
132     private String cssClass = StringUtils.EMPTY;
133 
134     private Map cssStyles = new Hashtable();
135 
136     private String path;
137 
138     private String nodeCollectionName;
139 
140     private String nodeName;
141 
142     private final String newLine;
143 
144     public ControlImpl() {
145         this.newLine = System.getProperty("line.separator");
146     }
147 
148     public ControlImpl(String name, String value) {
149         this();
150         this.setName(name);
151         this.setValue(value);
152     }
153 
154     public ControlImpl(String name, List values) {
155         this();
156         this.setName(name);
157         this.setValues(values);
158     }
159 
160     public ControlImpl(String name, Content websiteNode) {
161         this();
162         this.setName(name);
163         this.setWebsiteNode(websiteNode);
164     }
165 
166     public void setPath(String path) {
167         this.path = path;
168     }
169 
170     public String getPath() {
171         return this.path;
172     }
173 
174     public void setNodeCollectionName(String nodeCollectionName) {
175         this.nodeCollectionName = nodeCollectionName;
176     }
177 
178     public String getNodeCollectionName() {
179         return this.nodeCollectionName;
180     }
181 
182     public String getNodeCollectionName(String nullOrEmptyValue) {
183         if (StringUtils.isEmpty(this.getNodeCollectionName())) {
184             return nullOrEmptyValue;
185         }
186 
187         return this.getNodeCollectionName();
188     }
189 
190     public void setNodeName(String nodeName) {
191         this.nodeName = nodeName;
192     }
193 
194     public String getNodeName() {
195         return this.nodeName;
196     }
197 
198     public String getNodeName(String nullOrEmptyValue) {
199         if (StringUtils.isEmpty(this.getNodeName())) {
200             return nullOrEmptyValue;
201         }
202 
203         return this.getNodeName();
204     }
205 
206     /**
207      * @deprecated
208      */
209     @Deprecated
210     public void setRequest(HttpServletRequest request) {
211     }
212 
213     /**
214      * @return
215      */
216     public HttpServletRequest getRequest() {
217         return ((WebContext)MgnlContext.getInstance()).getRequest();
218     }
219 
220     public void setName(String s) {
221         this.name = s;
222     }
223 
224     public String getName() {
225         return this.name;
226     }
227 
228     public void setId(String s) {
229         this.id = s;
230     }
231 
232     public String getId() {
233         return this.id;
234     }
235 
236     public String getHtmlId() {
237         if (StringUtils.isNotEmpty(this.getId())) {
238             return " id=\"" + this.getId() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
239         }
240 
241         return StringUtils.EMPTY;
242     }
243 
244     public void setValue(String value) {
245         this.value = value;
246     }
247 
248     public String getValue() {
249         if (this.value != null) {
250             return this.value;
251         }
252 
253         try {
254             return this.getWebsiteNode().getNodeData(this.getName()).getString();
255         }
256         catch (Exception e) {
257             return StringUtils.EMPTY;
258         }
259 
260     }
261 
262     public void setValues(List values) {
263         this.values = values;
264     }
265 
266     public List getValues() {
267         if (this.values.size() != 0) {
268             return this.values;
269         }
270         try {
271             NodeData node = this.getWebsiteNode().getNodeData(this.getName());
272             if(node.isMultiValue() != NodeData.MULTIVALUE_FALSE) {
273                 return NodeDataUtil.getValuesStringList(node.getValues());
274             } else {
275                 Iterator it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator();
276                 List l = new ArrayList();
277                 while (it.hasNext()) {
278                     NodeData data = (NodeData) it.next();
279                     l.add(data.getString());
280                 }
281                 return l;
282             }
283         }
284         catch (Exception re) {
285             return this.values;
286         }
287     }
288 
289     public void setWebsiteNode(Content c) {
290         this.websiteNode = c;
291     }
292 
293     public Content getWebsiteNode() {
294         return this.websiteNode;
295     }
296 
297     public void setLabel(String label) {
298         this.label = label;
299     }
300 
301     public String getLabel() {
302         return this.label;
303     }
304 
305     public void setEvent(String event, String action) {
306         setEvent(event, action, false);
307     }
308 
309     public void setEvent(String event, String action, boolean removeExisting) {
310         String eventLower = event.toLowerCase();
311         String existing = null;
312         if (!removeExisting) {
313             existing = (String) this.getEvents().get(eventLower);
314         }
315         if (existing == null) {
316             existing = StringUtils.EMPTY;
317         }
318 
319         this.getEvents().put(eventLower, existing + action);
320     }
321 
322     public void setEvents(Map h) {
323         this.events = h;
324     }
325 
326     public Map getEvents() {
327         return this.events;
328     }
329 
330     public String getHtmlEvents() {
331         StringBuffer html = new StringBuffer();
332         Iterator en = this.getEvents().keySet().iterator();
333         while (en.hasNext()) {
334             String key = (String) en.next();
335             html.append(" " + key + "=\"" + this.getEvents().get(key) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
336         }
337         return html.toString();
338     }
339 
340     /**
341      * Returns an empty string.
342      * @see info.magnolia.cms.gui.control.Control#getHtml()
343      */
344     public String getHtml() {
345         return StringUtils.EMPTY;
346     }
347 
348     public void setHtmlPre(String s) {
349         this.htmlPre = s;
350     }
351 
352     public String getHtmlPre() {
353         return this.getHtmlPre(StringUtils.EMPTY);
354     }
355 
356     public String getHtmlPre(String nullValue) {
357         if (this.htmlPre != null) {
358             return this.htmlPre;
359         }
360 
361         return nullValue;
362     }
363 
364     public void setHtmlInter(String s) {
365         this.htmlInter = s;
366     }
367 
368     public String getHtmlInter() {
369         return this.getHtmlInter(StringUtils.EMPTY);
370     }
371 
372     public String getHtmlInter(String nullValue) {
373         if (this.htmlInter != null) {
374             return this.htmlInter;
375         }
376 
377         return nullValue;
378     }
379 
380     public void setHtmlPost(String s) {
381         this.htmlPost = s;
382     }
383 
384     public String getHtmlPost() {
385         return this.getHtmlPost(StringUtils.EMPTY);
386     }
387 
388     public String getHtmlPost(String nullValue) {
389         if (this.htmlPost != null) {
390             return this.htmlPost;
391         }
392 
393         return nullValue;
394     }
395 
396     public void setType(int i) {
397         this.type = i;
398     }
399 
400     public void setType(String s) {
401         this.type = PropertyType.valueFromName(s);
402     }
403 
404     public int getType() {
405         return this.type;
406     }
407 
408     public void setSaveInfo(boolean b) {
409         this.saveInfo = b;
410     }
411 
412     public boolean getSaveInfo() {
413         return this.saveInfo;
414     }
415 
416     public void setCssClass(String s) {
417         this.cssClass = s;
418     }
419 
420     public String getCssClass() {
421         return this.cssClass;
422     }
423 
424     public String getHtmlCssClass() {
425         if (StringUtils.isNotEmpty(this.getCssClass())) {
426             return " class=\"" + this.getCssClass() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
427         }
428 
429         return StringUtils.EMPTY;
430     }
431 
432     public String getHtmlSaveInfo() {
433         StringBuffer html = new StringBuffer();
434         if (this.getSaveInfo()) {
435             html.append("<input type=\"hidden\""); //$NON-NLS-1$
436             html.append(" name=\"mgnlSaveInfo\""); //$NON-NLS-1$
437             html.append(" value=\"" //$NON-NLS-1$
438                     + ControlImpl.escapeHTML(this.getName())
439                     + "," //$NON-NLS-1$
440                     + PropertyType.nameFromValue(this.getType())
441                     + "," //$NON-NLS-1$
442                     + this.getValueType()
443                     + "," //$NON-NLS-1$
444                     + this.getIsRichEditValue()
445                     + "," //$NON-NLS-1$
446                     + this.getEncoding()
447                     + "\""); //$NON-NLS-1$
448             html.append(" />"); //$NON-NLS-1$
449         }
450         return html.toString();
451     }
452 
453     public void setCssStyles(Map h) {
454         this.cssStyles = h;
455     }
456 
457     public void setCssStyles(String key, String value) {
458         this.getCssStyles().put(key, value);
459     }
460 
461     public Map getCssStyles() {
462         return this.cssStyles;
463     }
464 
465     public String getCssStyles(String key, String nullValue) {
466         if (this.getCssStyles().containsKey(key)) {
467             return (String) this.getCssStyles().get(key);
468         }
469         return nullValue;
470     }
471 
472     public String getCssStyles(String key) {
473         return this.getCssStyles(key, StringUtils.EMPTY);
474     }
475 
476     public String getHtmlCssStyles() {
477         StringBuffer html = new StringBuffer();
478         Iterator en = this.getCssStyles().keySet().iterator();
479         while (en.hasNext()) {
480             String key = (String) en.next();
481             html.append(key + ":" + this.getCssStyles().get(key) + ";"); //$NON-NLS-1$ //$NON-NLS-2$
482         }
483         if (html.length() > 0) {
484             return " style=\"" + html + "\""; //$NON-NLS-1$ //$NON-NLS-2$
485         }
486         return StringUtils.EMPTY;
487     }
488 
489     public void setValueType(int i) {
490         this.valueType = i;
491     }
492 
493     public int getValueType() {
494         return this.valueType;
495     }
496 
497     public void setEncoding(int i) {
498         this.encoding = i;
499     }
500 
501     public int getEncoding() {
502         return this.encoding;
503     }
504 
505     public void setIsRichEditValue(int i) {
506         this.isRichEditValue = i;
507     }
508 
509     public int getIsRichEditValue() {
510         return this.isRichEditValue;
511     }
512 
513     public static String escapeHTML(String str) {
514         return str.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
515     }
516 
517     protected void println(Writer out, String s) throws IOException {
518         out.write(s);
519         out.write(newLine);
520     }
521 
522 }