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 import org.apache.commons.lang.BooleanUtils;
40 import org.apache.commons.lang.StringEscapeUtils;
41 import org.apache.commons.lang.StringUtils;
42
43 import java.io.IOException;
44 import java.io.Writer;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.HashMap;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Map;
51
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 public void drawHtml(Writer out) throws IOException {
94 this.drawHtmlPre(out);
95
96
97
98 out.write(FreemarkerUtil.process(DialogMultiSelect.class, this));
99 this.drawHtmlPost(out);
100 }
101
102
103
104
105 public String getAddButton() {
106 Button add = new Button();
107 add.setLabel(getMessage("buttons.add"));
108 add.setOnclick(this.getName() + "DynamicTable.addNew();");
109 add.setSmall(true);
110 return add.getHtml();
111 }
112
113
114
115
116 public String getChooseButton() {
117
118 String chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK);
119 if(StringUtils.isEmpty(chooseOnclick)){
120 String tree = this.getConfigValue(CONFIG_TREE);
121 if(StringUtils.isNotEmpty(tree)){
122 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');";
123
124 }
125 }
126
127 if (StringUtils.isNotEmpty(chooseOnclick)) {
128 Button choose = new Button();
129 choose.setLabel(this.getMessage("buttons.choose"));
130 choose.setOnclick(chooseOnclick);
131
132 choose.setSmall(true);
133 return choose.getHtml();
134 }
135 return "";
136 }
137
138
139
140
141 public String getDeleteButton() {
142 Button delete = new Button();
143 delete.setLabel(this.getMessage("buttons.delete"));
144 delete
145 .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
146 delete.setSmall(true);
147 return delete.getHtml();
148 }
149
150
151
152
153 public String getInnerHtml() {
154
155
156
157 String name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html";
158 Map map = new HashMap();
159 map.put("this", this);
160 return FreemarkerUtil.process(name, map);
161 }
162
163
164
165
166 public String getGetObjectFunction() {
167 return "function(prefix, index){return {value: $(prefix).value }}";
168 }
169
170
171
172
173 public String getNewObjectFunction() {
174 return "function(){return {value: ''}}";
175 }
176
177
178
179
180 public String getJSON() {
181 if (this.isSaveAsJSON()) {
182 return this.getValue();
183 }
184
185 List values;
186 if (this.isSaveAsList()) {
187 values = Arrays.asList(this.getValue().split(","));
188 }
189 else {
190 values = this.getValues();
191 }
192
193 if (values.size() == 0) {
194 return "[{value:''}]";
195 }
196
197 List objects = new ArrayList();
198 for (Iterator iter = values.iterator(); iter.hasNext();) {
199 String value = (String) iter.next();
200 objects.add("{value: '" + StringEscapeUtils.escapeJavaScript(value) + "'}");
201 }
202 return "[" + StringUtils.join(objects.iterator(), ",") + "]";
203 }
204
205 public String getSaveInfo() {
206 Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
207 if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
208 ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
209 if (!isSaveAsList() && !isSaveAsJSON()) {
210 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
211 }
212 return dummy.getHtmlSaveInfo();
213 }
214
215 return "";
216 }
217
218 public boolean isSaveAsList() {
219 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST);
220 }
221
222 public boolean isSaveAsJSON() {
223 return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON);
224 }
225
226
227
228
229
230
231 public String getHiddenFieldName() {
232 if (this.isSaveAsList()) {
233 return this.getName();
234 }
235
236 return this.getName() + "Persisted";
237 }
238
239 }