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 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   * Builder object used to populate a tab with controls.
66   *
67   * For a complete reference of dialog control settings see: http://documentation.magnolia-cms.com/reference/dialogs/controls.html.
68   *
69   * @see info.magnolia.cms.gui.dialog.Dialog
70   * @see DialogTab
71   * @since 0.5
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             //Snippet stolen from DialogButtonSet public void setOption(Content context.getConfigNode(),) and slightly modified
214             List options = new ArrayList();
215             Button button = new Button(buttonSet.getName() + "_dummy", StringUtils.EMPTY); //$NON-NLS-1$
216             button.setLabel(buttonLabel);
217             button.setValue("true"); //$NON-NLS-1$
218             button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + buttonSet.getName() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
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      * Convenience-method to create a radio-buttonset from a map of options.
230      *
231      * @param name         name of nodeData
232      * @param label        label
233      * @param options      map of options where key is label and value is value. Use a sorted Map-implementation like LinkedHashMap if order is important.
234      * @param defaultValue the value of the item that should be selected by default. Must be in the map to be selected.
235      * @return
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 }