KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogMultiSelect


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.gui.control.Button;
16 import info.magnolia.cms.gui.control.ControlImpl;
17 import info.magnolia.cms.util.FreeMarkerUtil;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.commons.lang.BooleanUtils;
29 import org.apache.commons.lang.StringUtils;
30
31
32 /**
33  * Control to select multiple values. The values can get stored as list, in JSON format or as a multiple values
34  * @author Philipp Bracher
35  * @version $Revision: 6341 $ ($Author: philipp $)
36  */

37 public class DialogMultiSelect extends DialogBox {
38
39     /**
40      * The values are saved a properties
41      */

42     public static final String JavaDoc SAVE_MODE_MULTIPLE = "multiple";
43
44     /**
45      * The values are saved as a single json string
46      */

47     public static final String JavaDoc SAVE_MODE_JSON = "json";
48
49     /**
50      * The values are saved as semicolon separated list
51      */

52     public static final String JavaDoc SAVE_MODE_LIST = "list";
53
54     /**
55      * Set the save mode
56      */

57     public static final String JavaDoc CONFIG_SAVE_MODE = "saveMode";
58
59     /**
60      * Set the onclick js code of the coose button. If non is set the button is not rendered.
61      */

62     private static final String JavaDoc CONFIG_CHOOSE_ONCLICK = "chooseOnclick";
63     
64     /**
65      * If you like to select the data from a tree, just define the config value tree instead of chooseOnclick
66      */

67     private static final String JavaDoc CONFIG_TREE = "tree";
68
69     /**
70      * Render the Html using a template
71      */

72     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
73         this.drawHtmlPre(out);
74         out.write(FreeMarkerUtil.process(DialogMultiSelect.class, this));
75         this.drawHtmlPost(out);
76     }
77
78     /**
79      * The button to add a new row
80      */

81     public String JavaDoc getAddButton() {
82         Button add = new Button();
83         add.setLabel(getMessage("buttons.add")); //$NON-NLS-1$
84
add.setOnclick(this.getName() + "DynamicTable.addNew();"); //$NON-NLS-1$
85
add.setSmall(true);
86         return add.getHtml();
87     }
88
89     /**
90      * If this control has a choose button
91      */

92     public String JavaDoc getChooseButton() {
93         
94         String JavaDoc chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK);
95         if(StringUtils.isEmpty(chooseOnclick)){
96             String JavaDoc tree = this.getConfigValue(CONFIG_TREE);
97             if(StringUtils.isNotEmpty(tree)){
98                 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');";
99
100             }
101         }
102         
103         if (StringUtils.isNotEmpty(chooseOnclick)) {
104             Button choose = new Button();
105             choose.setLabel(this.getMessage("buttons.choose")); //$NON-NLS-1$
106
choose.setOnclick(chooseOnclick);
107
108             choose.setSmall(true);
109             return choose.getHtml();
110         }
111         return "";
112     }
113
114     /**
115      * Button for deleting a row
116      */

117     public String JavaDoc getDeleteButton() {
118         Button delete = new Button();
119         delete.setLabel(this.getMessage("buttons.delete")); //$NON-NLS-1$
120
delete
121             .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();"); //$NON-NLS-1$
122
delete.setSmall(true);
123         return delete.getHtml();
124     }
125
126     /**
127      * Called by the template. It renders the dynamic inner row using trimpaths templating mechanism.
128      */

129     public String JavaDoc getInnerHtml() {
130         String JavaDoc name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html";
131         Map JavaDoc map = new HashMap JavaDoc();
132         map.put("this", this);
133         return FreeMarkerUtil.process(name, map);
134     }
135
136     /**
137      * JS function used to create an object out of the input fields
138      */

139     public String JavaDoc getGetObjectFunction() {
140         return "function(prefix, index){return {value: $(prefix).value }}";
141     }
142
143     /**
144      * JS function used to create a new empty object
145      */

146     public String JavaDoc getNewObjectFunction() {
147         return "function(){return {value: ''}}";
148     }
149
150     /**
151      * Create the object to initialize the table
152      */

153     public String JavaDoc getJSON() {
154         if (this.isSaveAsJSON()) {
155             return this.getValue();
156         }
157
158         List JavaDoc values;
159         if (this.isSaveAsList()) {
160             values = Arrays.asList(this.getValue().split(","));
161         }
162         else {
163             values = this.getValues();
164         }
165
166         if (values.size() == 0) {
167             return "[{value:''}]";
168         }
169
170         List JavaDoc objects = new ArrayList JavaDoc();
171         for (Iterator JavaDoc iter = values.iterator(); iter.hasNext();) {
172             String JavaDoc value = (String JavaDoc) iter.next();
173             objects.add("{value: '" + value + "'}");
174         }
175         return "[" + StringUtils.join(objects.iterator(), ",") + "]";
176     }
177
178     public String JavaDoc getSaveInfo() {
179         Boolean JavaDoc renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
180         if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
181             ControlImpl dummy = new ControlImpl(this.getName(), (String JavaDoc) null);
182             if (!isSaveAsList() && !isSaveAsJSON()) {
183                 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
184             }
185             return dummy.getHtmlSaveInfo();
186         }
187         // don' create the save info
188
return "";
189     }
190
191     public boolean isSaveAsList() {
192         return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST);
193     }
194
195     public boolean isSaveAsJSON() {
196         return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON);
197     }
198
199     /**
200      * If the values are saved using the valueType multiple, we can not use the same name for the hidden field we use
201      * for persisting the data.
202      * @return the name of the hidden field
203      */

204     public String JavaDoc getHiddenFieldName() {
205         if (this.isSaveAsList()) {
206             return this.getName();
207         }
208
209         return this.getName() + "Persisted";
210     }
211
212 }
213
Popular Tags