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.module.blossom.dialog;
35
36 import info.magnolia.cms.gui.control.Button;
37 import info.magnolia.cms.gui.control.SelectOption;
38 import info.magnolia.cms.gui.dialog.Dialog;
39 import info.magnolia.cms.gui.dialog.DialogButtonSet;
40 import info.magnolia.cms.gui.dialog.DialogControlImpl;
41 import info.magnolia.cms.gui.dialog.DialogDate;
42 import info.magnolia.cms.gui.dialog.DialogEdit;
43 import info.magnolia.cms.gui.dialog.DialogFactory;
44 import info.magnolia.cms.gui.dialog.DialogFile;
45 import info.magnolia.cms.gui.dialog.DialogHidden;
46 import info.magnolia.cms.gui.dialog.DialogInclude;
47 import info.magnolia.cms.gui.dialog.DialogLink;
48 import info.magnolia.cms.gui.dialog.DialogMultiSelect;
49 import info.magnolia.cms.gui.dialog.DialogPassword;
50 import info.magnolia.cms.gui.dialog.DialogSelect;
51 import info.magnolia.cms.gui.dialog.DialogStatic;
52 import info.magnolia.cms.gui.dialog.DialogTab;
53 import info.magnolia.cms.gui.dialog.DialogUUIDLink;
54 import info.magnolia.module.fckeditor.dialogs.FckEditorDialog;
55 import org.apache.commons.lang.StringUtils;
56
57 import javax.jcr.RepositoryException;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.List;
61 import java.util.Map;
62
63
64
65
66
67
68
69
70
71
72 public class TabBuilder {
73
74 private DialogCreationContext context;
75 private DialogTab tab;
76
77 public TabBuilder(DialogCreationContext context, String label) {
78 this.context = context;
79 this.tab = context.getDialog().addTab(label);
80 this.tab.setName(label);
81 }
82
83 public TabBuilder(DialogCreationContext context, DialogTab tab) {
84 this.context = context;
85 this.tab = tab;
86 }
87
88 public DialogTab getTab() {
89 return this.tab;
90 }
91
92 public Dialog getDialog() {
93 return context.getDialog();
94 }
95
96 public DialogCreationContext getContext() {
97 return context;
98 }
99
100 public void addValidator(ValidationCallback validator) {
101 context.addValidator(validator);
102 }
103
104 public DialogEdit addEdit(String name, String label, String description) {
105 try {
106 DialogEdit edit = DialogFactory.getDialogEditInstance(
107 context.getRequest(),
108 context.getResponse(),
109 context.getWebsiteNode(),
110 null);
111 edit.setName(name);
112 edit.setLabel(label);
113 edit.setDescription(description);
114 tab.addSub(edit);
115 return edit;
116 } catch (RepositoryException e) {
117 throw new RuntimeRepositoryException(e);
118 }
119 }
120
121 public DialogEdit addTextArea(String name, String label, String description, int rows) {
122 try {
123 DialogEdit edit = DialogFactory.getDialogEditInstance(
124 context.getRequest(),
125 context.getResponse(),
126 context.getWebsiteNode(),
127 null);
128 edit.setName(name);
129 edit.setLabel(label);
130 edit.setDescription(description);
131 edit.setConfig("rows", rows);
132 tab.addSub(edit);
133 return edit;
134 } catch (RepositoryException e) {
135 throw new RuntimeRepositoryException(e);
136 }
137 }
138
139 public FckEditorDialog addFckEditor(String name, String label, String description) {
140 try {
141 FckEditorDialog dialog = new FckEditorDialog();
142 dialog.init(context.getRequest(), context.getResponse(), context.getWebsiteNode(), null);
143 dialog.setName(name);
144 dialog.setLabel(label);
145 dialog.setDescription(description);
146 tab.addSub(dialog);
147 return dialog;
148 } catch (RepositoryException e) {
149 throw new RuntimeRepositoryException(e);
150 }
151 }
152
153 public DialogSelect addSelect(String name, String label, String description, Map<String, String> options) {
154 try {
155 DialogSelect select = DialogFactory.getDialogSelectInstance(
156 context.getRequest(),
157 context.getResponse(),
158 context.getWebsiteNode(),
159 null);
160 select.setName(name);
161 select.setLabel(label);
162 select.setDescription(description);
163 for (Map.Entry<String, String> entry : options.entrySet()) {
164 select.addOption(new SelectOption(entry.getKey(), entry.getValue()));
165 }
166 tab.addSub(select);
167 return select;
168 } catch (RepositoryException e) {
169 throw new RuntimeRepositoryException(e);
170 }
171 }
172
173 public DialogSelect addSelect(String name, String label, String description, Collection<String> options) {
174 try {
175 DialogSelect select = DialogFactory.getDialogSelectInstance(
176 context.getRequest(),
177 context.getResponse(),
178 context.getWebsiteNode(),
179 null);
180 select.setName(name);
181 select.setLabel(label);
182 select.setDescription(description);
183 for (String option : options) {
184 select.addOption(new SelectOption(option, option));
185 }
186 tab.addSub(select);
187 return select;
188 } catch (RepositoryException e) {
189 throw new RuntimeRepositoryException(e);
190 }
191 }
192
193 public DialogLink addLink(String name, String label, String description) {
194 return (DialogLink) addControl("link", name, label, description);
195 }
196
197 public DialogUUIDLink addUuidLink(String name, String label, String description) {
198 return (DialogUUIDLink) addControl("uuidLink", name, label, description);
199 }
200
201 public DialogButtonSet addCheckbox(String name, String label, String buttonLabel) {
202 try {
203 DialogButtonSet buttonSet = DialogFactory.getDialogButtonSetInstance(
204 context.getRequest(),
205 context.getResponse(),
206 context.getWebsiteNode(),
207 null);
208 buttonSet.setButtonType(Button.BUTTONTYPE_CHECKBOX);
209 buttonSet.setName(name);
210 buttonSet.setLabel(label);
211
212
213 List options = new ArrayList();
214 Button button = new Button(buttonSet.getName() + "_dummy", StringUtils.EMPTY);
215 button.setLabel(buttonLabel);
216 button.setValue("true");
217 button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + buttonSet.getName() + "');");
218 options.add(button);
219 buttonSet.setOptions(options);
220 tab.addSub(buttonSet);
221 return buttonSet;
222 } catch (RepositoryException e) {
223 throw new RuntimeRepositoryException(e);
224 }
225 }
226
227
228
229
230
231
232
233
234
235
236 public DialogButtonSet addRadio(String name, String label, Map<String, String> options, String defaultValue) {
237 try {
238 DialogButtonSet buttonSet = DialogFactory.getDialogButtonSetInstance(
239 context.getRequest(),
240 context.getResponse(),
241 context.getWebsiteNode(),
242 null);
243 buttonSet.setButtonType(Button.BUTTONTYPE_RADIO);
244 buttonSet.setName(name);
245 buttonSet.setLabel(label);
246 List optionList = new ArrayList();
247 for (Map.Entry<String, String> e : options.entrySet()) {
248 String value = e.getValue();
249 String buttonLabel = e.getKey();
250
251 Button button = new Button(name, value);
252 button.setLabel(buttonLabel);
253
254 if (StringUtils.equals(value, defaultValue)) {
255 button.setState(Button.BUTTONSTATE_PUSHED);
256 }
257 optionList.add(button);
258 }
259 buttonSet.setOptions(optionList);
260 tab.addSub(buttonSet);
261 return buttonSet;
262
263 } catch (RepositoryException e) {
264 throw new RuntimeRepositoryException(e);
265 }
266 }
267
268 public DialogHidden addHidden(String name, String value) {
269 try {
270 DialogHidden hidden = DialogFactory.getDialogHiddenInstance(
271 context.getRequest(),
272 context.getResponse(),
273 context.getWebsiteNode(),
274 null);
275 hidden.setName(name);
276 hidden.setValue(value);
277 tab.addSub(hidden);
278 return hidden;
279 } catch (RepositoryException e) {
280 throw new RuntimeRepositoryException(e);
281 }
282 }
283
284 public DialogStatic addStatic(String text) {
285 try {
286 DialogStatic control = DialogFactory.getDialogStaticInstance(
287 context.getRequest(),
288 context.getResponse(),
289 context.getWebsiteNode(),
290 null);
291 control.setValue(text);
292 tab.addSub(control);
293 return control;
294 } catch (RepositoryException e) {
295 throw new RuntimeRepositoryException(e);
296 }
297 }
298
299 public DialogDate addDate(String name, String label, String description) {
300 return (DialogDate) addControl("date", name, label, description);
301 }
302
303 public DialogDate addDateAndTime(String name, String label, String description) {
304 DialogDate date = addDate(name, label, description);
305 date.setConfig("time", "true");
306 return date;
307 }
308
309 public DialogFile addFile(String name, String label, String description) {
310 DialogFile file = (DialogFile) addControl("file", name, label, description);
311 file.setConfig("type", "Binary");
312 return file;
313 }
314
315 public DialogPassword addPassword(String name, String label, String description) {
316 try {
317 DialogPassword password = DialogFactory.getDialogPasswordInstance(
318 context.getRequest(),
319 context.getResponse(),
320 context.getWebsiteNode(),
321 null);
322 password.setName(name);
323 password.setLabel(label);
324 password.setDescription(description);
325 tab.addSub(password);
326 return password;
327 } catch (RepositoryException e) {
328 throw new RuntimeRepositoryException(e);
329 }
330 }
331
332 public DialogMultiSelect addMultiSelect(String name, String label, String description) {
333 return (DialogMultiSelect) addControl("multiselect", name, label, description);
334 }
335
336 public DialogInclude addInclude(String file) {
337 try {
338 DialogInclude include = DialogFactory.getDialogIncludeInstance(
339 context.getRequest(),
340 context.getResponse(),
341 context.getWebsiteNode(),
342 null);
343 include.setConfig("file", file);
344 tab.addSub(include);
345 return include;
346 } catch (RepositoryException e) {
347 throw new RuntimeRepositoryException(e);
348 }
349 }
350
351 public DialogControlImpl addControl(String controlType, String name, String label, String description) {
352 try {
353 DialogControlImpl control = (DialogControlImpl) DialogFactory.getDialogControlInstanceByName(
354 context.getRequest(),
355 context.getResponse(),
356 context.getWebsiteNode(),
357 null,
358 controlType);
359 control.setName(name);
360 control.setLabel(label);
361 control.setDescription(description);
362 tab.addSub(control);
363 return control;
364 } catch (RepositoryException e) {
365 throw new RuntimeRepositoryException(e);
366 }
367 }
368 }