View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.control;
35  
36  import info.magnolia.cms.core.Content;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  
45  /**
46   * @author Vinzenz Wyser
47   * @version 2.0
48   */
49  public class Select extends ControlImpl {
50  
51      private List options = new ArrayList();
52  
53      private String multiple = "false"; //$NON-NLS-1$
54  
55      public Select() {
56      }
57  
58      public Select(String name, String value) {
59          super(name, value);
60      }
61  
62      public Select(String name, List values) {
63          super(name, values);
64      }
65  
66      public Select(String name, Content websiteNode) {
67          super(name, websiteNode);
68      }
69  
70      public void setOptions(List l) {
71          this.options = l;
72      }
73  
74      public void setOptions(SelectOption option) {
75          this.getOptions().add(option);
76      }
77  
78      public void setOptions(String label, String value) {
79          this.getOptions().add(new SelectOption(label, value));
80      }
81  
82      public List getOptions() {
83          return this.options;
84      }
85  
86      /**
87       * Sets the multiple.
88       * @param multiple the multiple to set
89       */
90      public void setMultiple(String multiple) {
91          this.multiple = multiple;
92      }
93  
94      /**
95       * Returns the multiple.
96       * @return the multiple
97       */
98      public String getMultiple() {
99          return multiple;
100     }
101 
102     @Override
103     public String getHtml() {
104         StringBuffer html = new StringBuffer();
105         html.append("<select"); //$NON-NLS-1$
106         html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
107         html.append(" id=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
108         if ("true".equalsIgnoreCase(this.getMultiple())) {
109             html.append(" multiple=\"multiple\""); //$NON-NLS-1$
110         }
111         html.append(this.getHtmlCssClass());
112         html.append(this.getHtmlCssStyles());
113         html.append(this.getHtmlEvents());
114         html.append(">"); //$NON-NLS-1$
115         Iterator it = this.getOptions().iterator();
116         while (it.hasNext()) {
117             SelectOption o = (SelectOption) it.next();
118             if (this.getValueType() == ControlImpl.VALUETYPE_MULTIPLE) {
119                 if (this.getValues().size() != 0) {
120                     if (this.getValues().contains(o.getValue())) {
121                         o.setSelected(true);
122                     }
123                     else {
124                         o.setSelected(false);
125                     }
126                 }
127             }
128             else {
129                 if (StringUtils.isNotEmpty(this.getValue())) {
130                     if (this.getValue().equals(o.getValue())) {
131                         o.setSelected(true);
132                     }
133                     else {
134                         o.setSelected(false);
135                     }
136                 }
137             }
138             html.append(o.getHtml());
139         }
140         html.append("</select>"); //$NON-NLS-1$
141         if (this.getSaveInfo()) {
142             html.append(this.getHtmlSaveInfo());
143         }
144         return html.toString();
145     }
146 }