View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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     public String getHtml() {
103         StringBuffer html = new StringBuffer();
104         html.append("<select"); //$NON-NLS-1$
105         html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
106         html.append(" id=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
107         if ("true".equalsIgnoreCase(this.getMultiple())) {
108             html.append(" multiple=\"multiple\""); //$NON-NLS-1$
109         }
110         html.append(this.getHtmlCssClass());
111         html.append(this.getHtmlCssStyles());
112         html.append(this.getHtmlEvents());
113         html.append(">"); //$NON-NLS-1$
114         Iterator it = this.getOptions().iterator();
115         while (it.hasNext()) {
116             SelectOption o = (SelectOption) it.next();
117             if (this.getValueType() == ControlImpl.VALUETYPE_MULTIPLE) {
118                 if (this.getValues().size() != 0) {
119                     if (this.getValues().contains(o.getValue())) {
120                         o.setSelected(true);
121                     }
122                     else {
123                         o.setSelected(false);
124                     }
125                 }
126             }
127             else {
128                 if (StringUtils.isNotEmpty(this.getValue())) {
129                     if (this.getValue().equals(o.getValue())) {
130                         o.setSelected(true);
131                     }
132                     else {
133                         o.setSelected(false);
134                     }
135                 }
136             }
137             html.append(o.getHtml());
138         }
139         html.append("</select>"); //$NON-NLS-1$
140         if (this.getSaveInfo()) {
141             html.append(this.getHtmlSaveInfo());
142         }
143         return html.toString();
144     }
145 }