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.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 ButtonSet extends ControlImpl {
50  
51      // default values for divided button (checkbox, radio)
52      private static final String HTML_PRE_DIVIDED = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; //$NON-NLS-1$
53  
54      private static final String HTML_POST_DIVIDED = "</table>"; //$NON-NLS-1$
55  
56      private static final String BUTTONHTML_PRE_DIVIDED = "<tr><td>"; //$NON-NLS-1$
57  
58      private static final String BUTTONHTML_INTER_DIVIDED = "</td><td>"; //$NON-NLS-1$
59  
60      private static final String BUTTONHTML_POST_DIVIDED = "</td></tr>"; //$NON-NLS-1$
61  
62      // default values for push button
63  
64      private static final String HTML_INTER_PUSH = " "; //$NON-NLS-1$
65  
66      private List buttons = new ArrayList();
67  
68      private int buttonType = BUTTONTYPE_RADIO;
69  
70      private String buttonHtmlPre; // html before each button
71  
72      private String buttonHtmlInter; // html between each button and label (not available for push button)
73  
74      private String buttonHtmlPost; // html after each label
75  
76      public ButtonSet() {
77      }
78  
79      public ButtonSet(String name, String value) {
80          super(name, value);
81      }
82  
83      public ButtonSet(String name, List values) {
84          super(name, values);
85      }
86  
87      public ButtonSet(String name, Content websiteNode) {
88          super(name, websiteNode);
89      }
90  
91      public void setButtons(List buttons) {
92          this.buttons = buttons;
93      }
94  
95      public void setButtons(Button button) {
96          this.getButtons().add(button);
97      }
98  
99      public List getButtons() {
100         return this.buttons;
101     }
102 
103     public void setButtonHtmlPre(String s) {
104         this.buttonHtmlPre = s;
105     }
106 
107     public String getButtonHtmlPre() {
108         if (this.buttonHtmlPre == null) {
109             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
110                 return StringUtils.EMPTY;
111             }
112             return BUTTONHTML_PRE_DIVIDED;
113         }
114         return this.buttonHtmlPre;
115     }
116 
117     public void setButtonHtmlInter(String s) {
118         this.buttonHtmlInter = s;
119     }
120 
121     public String getButtonHtmlInter() {
122         if (this.buttonHtmlInter == null) {
123             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
124                 return StringUtils.EMPTY;
125             }
126             return BUTTONHTML_INTER_DIVIDED;
127         }
128         return this.buttonHtmlInter;
129     }
130 
131     public void setButtonHtmlPost(String s) {
132         this.buttonHtmlPost = s;
133     }
134 
135     public String getButtonHtmlPost() {
136         if (this.buttonHtmlPost == null) {
137             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
138                 return StringUtils.EMPTY;
139             }
140             return BUTTONHTML_POST_DIVIDED;
141         }
142         return buttonHtmlPost;
143 
144     }
145 
146     public String getHtmlPre() {
147         if (super.getHtmlPre(null) == null) {
148             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
149                 return StringUtils.EMPTY;
150             }
151             return HTML_PRE_DIVIDED;
152         }
153         return super.getHtmlPre();
154     }
155 
156     public String getHtmlInter() {
157         if (super.getHtmlInter(null) == null) {
158             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
159                 return HTML_INTER_PUSH;
160             }
161             return StringUtils.EMPTY;
162         }
163         return super.getHtmlInter();
164     }
165 
166     public String getHtmlPost() {
167         if (super.getHtmlPost(null) == null) {
168             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
169                 return StringUtils.EMPTY;
170             }
171             return HTML_POST_DIVIDED;
172         }
173         return super.getHtmlPost();
174 
175     }
176 
177     public void setButtonType(int i) {
178         this.buttonType = i;
179     }
180 
181     public int getButtonType() {
182         return this.buttonType;
183     }
184 
185     public String getHtml() {
186         StringBuffer html = new StringBuffer();
187         html.append(this.getHtmlPre());
188         Iterator it = this.getButtons().iterator();
189         int i = 0;
190         while (it.hasNext()) {
191             Button b = (Button) it.next();
192             if (b.getName() == null) {
193                 b.setName(this.getName());
194             }
195             b.setButtonType(this.getButtonType());
196             b.setSaveInfo(false);
197             if (b.getHtmlPre(null) == null) {
198                 b.setHtmlPre(this.getButtonHtmlPre());
199             }
200             if (b.getHtmlInter(null) == null) {
201                 b.setHtmlInter(this.getButtonHtmlInter());
202             }
203             if (b.getHtmlPost(null) == null) {
204                 b.setHtmlPost(this.getButtonHtmlPost());
205             }
206             if (StringUtils.isEmpty(b.getCssClass())) {
207                 b.setCssClass(this.getCssClass());
208             }
209             b.setId(this.getName() + "_SETBUTTON_" + i); //$NON-NLS-1$
210             if (this.getValueType() == ControlImpl.VALUETYPE_MULTIPLE) {
211                 if (this.getValues().size() != 0) {
212                     if (this.getValues().contains(b.getValue())) {
213                         b.setState(BUTTONSTATE_PUSHED);
214                     }
215                     else {
216                         b.setState(BUTTONSTATE_NORMAL);
217                     }
218                 }
219             }
220             else {
221                 if (StringUtils.isNotEmpty(this.getValue())) {
222                     if (this.getValue().equals(b.getValue())) {
223                         b.setState(BUTTONSTATE_PUSHED);
224                     }
225                     else {
226                         b.setState(BUTTONSTATE_NORMAL);
227                     }
228                 }
229             }
230             html.append(b.getHtml());
231             if (it.hasNext()) {
232                 html.append(this.getHtmlInter());
233             }
234             i++;
235         }
236         html.append(this.getHtmlPost());
237         html.append(this.getHtmlSaveInfo());
238         return html.toString();
239     }
240 }