1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.dialog;
35
36 import info.magnolia.cms.gui.control.Button;
37 import info.magnolia.cms.gui.control.ControlImpl;
38 import info.magnolia.freemarker.FreemarkerUtil;
39
40 import java.io.IOException;
41 import java.io.Writer;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
48
49 import org.apache.commons.lang.BooleanUtils;
50 import org.apache.commons.lang.StringEscapeUtils;
51 import org.apache.commons.lang.StringUtils;
52
53
54
55
56
57
58
59 public class DialogMultiSelect extends DialogBox {
60
61
62
63
64 public static final String SAVE_MODE_MULTIPLE = "multiple";
65
66
67
68
69 public static final String SAVE_MODE_JSON = "json";
70
71
72
73
74 public static final String SAVE_MODE_LIST = "list";
75
76
77
78
79 public static final String CONFIG_SAVE_MODE = "saveMode";
80
81
82
83
84 private static final String CONFIG_CHOOSE_ONCLICK = "chooseOnclick";
85
86
87
88
89 private static final String CONFIG_TREE = "tree";
90
91
92
93
94 @Override
95 public void drawHtml(Writer out) throws IOException {
96 this.drawHtmlPre(out);
97
98
99
100 out.write(FreemarkerUtil.process(DialogMultiSelect.class, this));
101 this.drawHtmlPost(out);
102 }
103
104
105
106
107 public String getAddButton() {
108 Button add = new Button();
109 add.setLabel(getMessage("buttons.add"));
110 add.setOnclick(this.getName() + "DynamicTable.addNew();");
111 add.setSmall(true);
112 return add.getHtml();
113 }
114
115
116
117
118 public String getChooseButton() {
119
120 String chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK);
121 if(StringUtils.isEmpty(chooseOnclick)){
122 String tree = this.getConfigValue(CONFIG_TREE);
123 if(StringUtils.isNotEmpty(tree)){
124 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');";
125
126 }
127 }
128
129 if (StringUtils.isNotEmpty(chooseOnclick)) {
130 Button choose = new Button();
131 choose.setLabel(this.getMessage("buttons.choose"));
132 choose.setOnclick(chooseOnclick);
133
134 choose.setSmall(true);
135 return choose.getHtml();
136 }
137 return "";
138 }
139
140
141
142
143 public String getDeleteButton() {
144 Button delete = new Button();
145 delete.setLabel(this.getMessage("buttons.delete"));
146 delete
147 .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
148 delete.setSmall(true);
149 return delete.getHtml();
150 }
151
152
153
154
155 public String getInnerHtml() {
156
157
158
159 String name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html";
160 Map map = new HashMap();
161 map.put("this", this);
162 return FreemarkerUtil.process(name, map);
163 }
164
165
166
167
168 public String getGetObjectFunction() {
169 return "function(prefix, index){return {value: $(prefix).value }}";
170 }
171
172
173
174
175 public String getNewObjectFunction() {
176 return "function(){return {value: ''}}";
177 }
178
179
180
181
182 public String getJSON() {
183 if (this.isSaveAsJSON()) {
184 return this.getValue();
185 }
186
187 List values;
188 if (this.isSaveAsList()) {
189 values = Arrays.asList(this.getValue().split(","));
190 }
191 else {
192 values = this.getValues();
193 }
194
195 if (values.size() == 0) {
196 return "[{value:''}]";
197 }
198
199 List objects = new ArrayList();
200 for (Iterator iter = values.iterator(); iter.hasNext();) {
201 String value = (String) iter.next();
202 objects.add("{value: '" + StringEscapeUtils.escapeJavaScript(value) + "'}");
203 }
204 return "[" + StringUtils.join(objects.iterator(), ",") + "]";
205 }
206
207 public String getSaveInfo() {
208 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
209 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
210 ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
211 if (!isSaveAsList() && !isSaveAsJSON()) {
212 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
213 }
214 return dummy.getHtmlSaveInfo();
215 }
216
217 return "";
218 }
219
220 public boolean isSaveAsList() {
221 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST);
222 }
223
224 public boolean isSaveAsJSON() {
225 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON);
226 }
227
228
229
230
231
232
233 public String getHiddenFieldName() {
234 if (this.isSaveAsList()) {
235 return this.getName();
236 }
237
238 return this.getName() + "Persisted";
239 }
240
241 }