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 public class DialogMultiSelect extends DialogBox {
59
60
61
62
63 public static final String SAVE_MODE_MULTIPLE = "multiple";
64
65
66
67
68 public static final String SAVE_MODE_JSON = "json";
69
70
71
72
73 public static final String SAVE_MODE_LIST = "list";
74
75
76
77
78 public static final String CONFIG_SAVE_MODE = "saveMode";
79
80
81
82
83 private static final String CONFIG_CHOOSE_ONCLICK = "chooseOnclick";
84
85
86
87
88 private static final String CONFIG_TREE = "tree";
89
90
91
92
93 @Override
94 public void drawHtml(Writer out) throws IOException {
95 this.drawHtmlPre(out);
96
97
98
99 out.write(FreemarkerUtil.process(DialogMultiSelect.class, this));
100 this.drawHtmlPost(out);
101 }
102
103
104
105
106 public String getAddButton() {
107 Button add = new Button();
108 add.setLabel(getMessage("buttons.add"));
109 add.setOnclick(this.getName() + "DynamicTable.addNew();");
110 add.setSmall(true);
111 return add.getHtml();
112 }
113
114
115
116
117 public String getChooseButton() {
118
119 String chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK);
120 if (StringUtils.isEmpty(chooseOnclick)) {
121 String tree = this.getConfigValue(CONFIG_TREE);
122 if (StringUtils.isNotEmpty(tree)) {
123 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');";
124
125 }
126 }
127
128 if (StringUtils.isNotEmpty(chooseOnclick)) {
129 Button choose = new Button();
130 choose.setLabel(this.getMessage("buttons.choose"));
131 choose.setOnclick(chooseOnclick);
132
133 choose.setSmall(true);
134 return choose.getHtml();
135 }
136 return "";
137 }
138
139
140
141
142 public String getDeleteButton() {
143 Button delete = new Button();
144 delete.setLabel(this.getMessage("buttons.delete"));
145 delete
146 .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
147 delete.setSmall(true);
148 return delete.getHtml();
149 }
150
151
152
153
154 public String getInnerHtml() {
155
156
157
158 String name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html";
159 Map map = new HashMap();
160 map.put("this", this);
161 return FreemarkerUtil.process(name, map);
162 }
163
164
165
166
167 public String getGetObjectFunction() {
168 return "function(prefix, index){return {value: $(prefix).value }}";
169 }
170
171
172
173
174 public String getNewObjectFunction() {
175 return "function(){return {value: ''}}";
176 }
177
178
179
180
181 public String getJSON() {
182 if (this.isSaveAsJSON()) {
183 return this.getValue();
184 }
185
186 List values;
187 if (this.isSaveAsList()) {
188 values = Arrays.asList(this.getValue().split(","));
189 }
190 else {
191 values = this.getValues();
192 }
193
194 if (values.size() == 0) {
195 return "[{value:'" + this.getConfigValue(DialogMultiSelect.DEFAULT_VALUE_PROPERTY) + "'}]";
196 }
197
198 List objects = new ArrayList();
199 for (Iterator iter = values.iterator(); iter.hasNext();) {
200 String value = (String) iter.next();
201 objects.add("{value: '" + StringEscapeUtils.escapeJavaScript(value) + "'}");
202 }
203 return "[" + StringUtils.join(objects.iterator(), ",") + "]";
204 }
205
206 public String getSaveInfo() {
207 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
208 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
209 ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
210 if (!isSaveAsList() && !isSaveAsJSON()) {
211 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
212 }
213 return dummy.getHtmlSaveInfo();
214 }
215
216 return "";
217 }
218
219 public boolean isSaveAsList() {
220 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST);
221 }
222
223 public boolean isSaveAsJSON() {
224 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON);
225 }
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 }