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