View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Builder object used to populate a tab with controls.
65   *
66   * For a complete reference of dialog control settings see: http://documentation.magnolia-cms.com/reference/dialogs/controls.html.
67   *
68   * @see info.magnolia.cms.gui.dialog.Dialog
69   * @see DialogTab
70   * @since 0.5
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             //Snippet stolen from DialogButtonSet public void setOption(Content context.getConfigNode(),) and slightly modified
213             List options = new ArrayList();
214             Button button = new Button(buttonSet.getName() + "_dummy", StringUtils.EMPTY); //$NON-NLS-1$
215             button.setLabel(buttonLabel);
216             button.setValue("true"); //$NON-NLS-1$
217             button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + buttonSet.getName() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
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      * Convenience-method to create a radio-buttonset from a map of options.
229      *
230      * @param name         name of nodeData
231      * @param label        label
232      * @param options      map of options where key is label and value is value. Use a sorted Map-implementation like LinkedHashMap if order is important.
233      * @param defaultValue the value of the item that should be selected by default. Must be in the map to be selected.
234      * @return
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 }