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