View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.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  
40  import java.io.IOException;
41  import java.io.Writer;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.HashMap;
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.Map;
48  
49  import org.apache.commons.lang.BooleanUtils;
50  import org.apache.commons.lang.StringEscapeUtils;
51  import org.apache.commons.lang.StringUtils;
52  
53  /**
54   * Control to select multiple values. The values can get stored as list, in JSON format or as a multiple values
55   * 
56   * @version $Id$
57   */
58  public class DialogMultiSelect extends DialogBox {
59  
60      /**
61       * The values are saved a properties
62       */
63      public static final String SAVE_MODE_MULTIPLE = "multiple";
64  
65      /**
66       * The values are saved as a single json string
67       */
68      public static final String SAVE_MODE_JSON = "json";
69  
70      /**
71       * The values are saved as semicolon separated list
72       */
73      public static final String SAVE_MODE_LIST = "list";
74  
75      /**
76       * Set the save mode
77       */
78      public static final String CONFIG_SAVE_MODE = "saveMode";
79  
80      /**
81       * Set the onclick js code of the coose button. If non is set the button is not rendered.
82       */
83      private static final String CONFIG_CHOOSE_ONCLICK = "chooseOnclick";
84  
85      /**
86       * If you like to select the data from a tree, just define the config value tree instead of chooseOnclick
87       */
88      private static final String CONFIG_TREE = "tree";
89  
90      /**
91       * Render the Html using a template
92       */
93      @Override
94      public void drawHtml(Writer out) throws IOException {
95          this.drawHtmlPre(out);
96          // this could be replaced by
97          // out.write(FreemarkerUtil.process(this));
98          // except this could cause problems with subclasses
99          out.write(FreemarkerUtil.process(DialogMultiSelect.class, this));
100         this.drawHtmlPost(out);
101     }
102 
103     /**
104      * The button to add a new row
105      */
106     public String getAddButton() {
107         Button add = new Button();
108         add.setLabel(getMessage("buttons.add"));
109         add.setOnclick(this.getName() + "DynamicTable.addNew();");
110         add.setSmall(true);
111         return add.getHtml();
112     }
113 
114     /**
115      * If this control has a choose button
116      */
117     public String getChooseButton() {
118 
119         String chooseOnclick = this.getConfigValue(CONFIG_CHOOSE_ONCLICK);
120         if (StringUtils.isEmpty(chooseOnclick)) {
121             String tree = this.getConfigValue(CONFIG_TREE);
122             if (StringUtils.isNotEmpty(tree)) {
123                 chooseOnclick = "mgnlOpenTreeBrowserWithControl($('${prefix}'), '" + tree + "');";
124 
125             }
126         }
127 
128         if (StringUtils.isNotEmpty(chooseOnclick)) {
129             Button choose = new Button();
130             choose.setLabel(this.getMessage("buttons.choose"));
131             choose.setOnclick(chooseOnclick);
132 
133             choose.setSmall(true);
134             return choose.getHtml();
135         }
136         return "";
137     }
138 
139     /**
140      * Button for deleting a row
141      */
142     public String getDeleteButton() {
143         Button delete = new Button();
144         delete.setLabel(this.getMessage("buttons.delete"));
145         delete
146                 .setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
147         delete.setSmall(true);
148         return delete.getHtml();
149     }
150 
151     /**
152      * Called by the template. It renders the dynamic inner row using trimpaths templating mechanism.
153      */
154     public String getInnerHtml() {
155         // TODO - this could potentially be replaced by
156         // return FreemarkerUtil.process(this, "Inner", "html");
157         // except this might cause problems with subclasses
158         String name = "/" + StringUtils.replace(DialogMultiSelect.class.getName(), ".", "/") + "Inner.html";
159         Map map = new HashMap();
160         map.put("this", this);
161         return FreemarkerUtil.process(name, map);
162     }
163 
164     /**
165      * JS function used to create an object out of the input fields
166      */
167     public String getGetObjectFunction() {
168         return "function(prefix, index){return {value: $(prefix).value }}";
169     }
170 
171     /**
172      * JS function used to create a new empty object
173      */
174     public String getNewObjectFunction() {
175         return "function(){return {value: ''}}";
176     }
177 
178     /**
179      * Create the object to initialize the table
180      */
181     public String getJSON() {
182         if (this.isSaveAsJSON()) {
183             return this.getValue();
184         }
185 
186         List values;
187         if (this.isSaveAsList()) {
188             values = Arrays.asList(this.getValue().split(","));
189         }
190         else {
191             values = this.getValues();
192         }
193 
194         if (values.size() == 0) {
195             return "[{value:'" + this.getConfigValue(DialogMultiSelect.DEFAULT_VALUE_PROPERTY) + "'}]";
196         }
197 
198         List objects = new ArrayList();
199         for (Iterator iter = values.iterator(); iter.hasNext();) {
200             String value = (String) iter.next();
201             objects.add("{value: '" + StringEscapeUtils.escapeJavaScript(value) + "'}");
202         }
203         return "[" + StringUtils.join(objects.iterator(), ",") + "]";
204     }
205 
206     public String getSaveInfo() {
207         Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
208         if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
209             ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
210             if (!isSaveAsList() && !isSaveAsJSON()) {
211                 dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
212             }
213             return dummy.getHtmlSaveInfo();
214         }
215         // don' create the save info
216         return "";
217     }
218 
219     public boolean isSaveAsList() {
220         return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_LIST);
221     }
222 
223     public boolean isSaveAsJSON() {
224         return StringUtils.equals(this.getConfigValue(CONFIG_SAVE_MODE), SAVE_MODE_JSON);
225     }
226 
227     /**
228      * If the values are saved using the valueType multiple, we can not use the same name for the hidden field we use
229      * for persisting the data.
230      * 
231      * @return the name of the hidden field
232      */
233     public String getHiddenFieldName() {
234         if (this.isSaveAsList()) {
235             return this.getName();
236         }
237 
238         return this.getName() + "Persisted";
239     }
240 
241 }