View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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     public void setRequest(HttpServletRequest request) {
210     }
211 
212     /**
213      * @return
214      */
215     public HttpServletRequest getRequest() {
216         return ((WebContext)MgnlContext.getInstance()).getRequest();
217     }
218 
219     public void setName(String s) {
220         this.name = s;
221     }
222 
223     public String getName() {
224         return this.name;
225     }
226 
227     public void setId(String s) {
228         this.id = s;
229     }
230 
231     public String getId() {
232         return this.id;
233     }
234 
235     public String getHtmlId() {
236         if (StringUtils.isNotEmpty(this.getId())) {
237             return " id=\"" + this.getId() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
238         }
239 
240         return StringUtils.EMPTY;
241     }
242 
243     public void setValue(String value) {
244         this.value = value;
245     }
246 
247     public String getValue() {
248         if (this.value != null) {
249             return this.value;
250         }
251 
252         try {
253             return this.getWebsiteNode().getNodeData(this.getName()).getString();
254         }
255         catch (Exception e) {
256             return StringUtils.EMPTY;
257         }
258 
259     }
260 
261     public void setValues(List values) {
262         this.values = values;
263     }
264 
265     public List getValues() {
266         if (this.values.size() != 0) {
267             return this.values;
268         }
269         try {
270             NodeData node = this.getWebsiteNode().getNodeData(this.getName());
271             if(node.isMultiValue() != NodeData.MULTIVALUE_FALSE) {
272                 return NodeDataUtil.getValuesStringList(node.getValues());
273             } else {
274                 Iterator it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator();
275                 List l = new ArrayList();
276                 while (it.hasNext()) {
277                     NodeData data = (NodeData) it.next();
278                     l.add(data.getString());
279                 }
280                 return l;
281             }
282         }
283         catch (Exception re) {
284             return this.values;
285         }
286     }
287 
288     public void setWebsiteNode(Content c) {
289         this.websiteNode = c;
290     }
291 
292     public Content getWebsiteNode() {
293         return this.websiteNode;
294     }
295 
296     public void setLabel(String label) {
297         this.label = label;
298     }
299 
300     public String getLabel() {
301         return this.label;
302     }
303 
304     public void setEvent(String event, String action) {
305         setEvent(event, action, false);
306     }
307 
308     public void setEvent(String event, String action, boolean removeExisting) {
309         String eventLower = event.toLowerCase();
310         String existing = null;
311         if (!removeExisting) {
312             existing = (String) this.getEvents().get(eventLower);
313         }
314         if (existing == null) {
315             existing = StringUtils.EMPTY;
316         }
317 
318         this.getEvents().put(eventLower, existing + action);
319     }
320 
321     public void setEvents(Map h) {
322         this.events = h;
323     }
324 
325     public Map getEvents() {
326         return this.events;
327     }
328 
329     public String getHtmlEvents() {
330         StringBuffer html = new StringBuffer();
331         Iterator en = this.getEvents().keySet().iterator();
332         while (en.hasNext()) {
333             String key = (String) en.next();
334             html.append(" " + key + "=\"" + this.getEvents().get(key) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
335         }
336         return html.toString();
337     }
338 
339     /**
340      * Returns an empty string.
341      * @see info.magnolia.cms.gui.control.Control#getHtml()
342      */
343     public String getHtml() {
344         return StringUtils.EMPTY;
345     }
346 
347     public void setHtmlPre(String s) {
348         this.htmlPre = s;
349     }
350 
351     public String getHtmlPre() {
352         return this.getHtmlPre(StringUtils.EMPTY);
353     }
354 
355     public String getHtmlPre(String nullValue) {
356         if (this.htmlPre != null) {
357             return this.htmlPre;
358         }
359 
360         return nullValue;
361     }
362 
363     public void setHtmlInter(String s) {
364         this.htmlInter = s;
365     }
366 
367     public String getHtmlInter() {
368         return this.getHtmlInter(StringUtils.EMPTY);
369     }
370 
371     public String getHtmlInter(String nullValue) {
372         if (this.htmlInter != null) {
373             return this.htmlInter;
374         }
375 
376         return nullValue;
377     }
378 
379     public void setHtmlPost(String s) {
380         this.htmlPost = s;
381     }
382 
383     public String getHtmlPost() {
384         return this.getHtmlPost(StringUtils.EMPTY);
385     }
386 
387     public String getHtmlPost(String nullValue) {
388         if (this.htmlPost != null) {
389             return this.htmlPost;
390         }
391 
392         return nullValue;
393     }
394 
395     public void setType(int i) {
396         this.type = i;
397     }
398 
399     public void setType(String s) {
400         this.type = PropertyType.valueFromName(s);
401     }
402 
403     public int getType() {
404         return this.type;
405     }
406 
407     public void setSaveInfo(boolean b) {
408         this.saveInfo = b;
409     }
410 
411     public boolean getSaveInfo() {
412         return this.saveInfo;
413     }
414 
415     public void setCssClass(String s) {
416         this.cssClass = s;
417     }
418 
419     public String getCssClass() {
420         return this.cssClass;
421     }
422 
423     public String getHtmlCssClass() {
424         if (StringUtils.isNotEmpty(this.getCssClass())) {
425             return " class=\"" + this.getCssClass() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
426         }
427 
428         return StringUtils.EMPTY;
429     }
430 
431     public String getHtmlSaveInfo() {
432         StringBuffer html = new StringBuffer();
433         if (this.getSaveInfo()) {
434             html.append("<input type=\"hidden\""); //$NON-NLS-1$
435             html.append(" name=\"mgnlSaveInfo\""); //$NON-NLS-1$
436             html.append(" value=\"" //$NON-NLS-1$
437                 + ControlImpl.escapeHTML(this.getName())
438                 + "," //$NON-NLS-1$
439                 + PropertyType.nameFromValue(this.getType())
440                 + "," //$NON-NLS-1$
441                 + this.getValueType()
442                 + "," //$NON-NLS-1$
443                 + this.getIsRichEditValue()
444                 + "," //$NON-NLS-1$
445                 + this.getEncoding()
446                 + "\""); //$NON-NLS-1$
447             html.append(" />"); //$NON-NLS-1$
448         }
449         return html.toString();
450     }
451 
452     public void setCssStyles(Map h) {
453         this.cssStyles = h;
454     }
455 
456     public void setCssStyles(String key, String value) {
457         this.getCssStyles().put(key, value);
458     }
459 
460     public Map getCssStyles() {
461         return this.cssStyles;
462     }
463 
464     public String getCssStyles(String key, String nullValue) {
465         if (this.getCssStyles().containsKey(key)) {
466             return (String) this.getCssStyles().get(key);
467         }
468         return nullValue;
469     }
470 
471     public String getCssStyles(String key) {
472         return this.getCssStyles(key, StringUtils.EMPTY);
473     }
474 
475     public String getHtmlCssStyles() {
476         StringBuffer html = new StringBuffer();
477         Iterator en = this.getCssStyles().keySet().iterator();
478         while (en.hasNext()) {
479             String key = (String) en.next();
480             html.append(key + ":" + this.getCssStyles().get(key) + ";"); //$NON-NLS-1$ //$NON-NLS-2$
481         }
482         if (html.length() > 0) {
483             return " style=\"" + html + "\""; //$NON-NLS-1$ //$NON-NLS-2$
484         }
485         return StringUtils.EMPTY;
486     }
487 
488     public void setValueType(int i) {
489         this.valueType = i;
490     }
491 
492     public int getValueType() {
493         return this.valueType;
494     }
495 
496     public void setEncoding(int i) {
497         this.encoding = i;
498     }
499 
500     public int getEncoding() {
501         return this.encoding;
502     }
503 
504     public void setIsRichEditValue(int i) {
505         this.isRichEditValue = i;
506     }
507 
508     public int getIsRichEditValue() {
509         return this.isRichEditValue;
510     }
511 
512     public static String escapeHTML(String str) {
513         return str.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
514     }
515 
516     protected void println(Writer out, String s) throws IOException {
517         out.write(s);
518         out.write(newLine);
519     }
520 
521 }