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 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     @Override
147     public String getHtmlPre() {
148         if (super.getHtmlPre(null) == null) {
149             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
150                 return StringUtils.EMPTY;
151             }
152             return HTML_PRE_DIVIDED;
153         }
154         return super.getHtmlPre();
155     }
156 
157     @Override
158     public String getHtmlInter() {
159         if (super.getHtmlInter(null) == null) {
160             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
161                 return HTML_INTER_PUSH;
162             }
163             return StringUtils.EMPTY;
164         }
165         return super.getHtmlInter();
166     }
167 
168     @Override
169     public String getHtmlPost() {
170         if (super.getHtmlPost(null) == null) {
171             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
172                 return StringUtils.EMPTY;
173             }
174             return HTML_POST_DIVIDED;
175         }
176         return super.getHtmlPost();
177 
178     }
179 
180     public void setButtonType(int i) {
181         this.buttonType = i;
182     }
183 
184     public int getButtonType() {
185         return this.buttonType;
186     }
187 
188     @Override
189     public String getHtml() {
190         StringBuffer html = new StringBuffer();
191         html.append(this.getHtmlPre());
192         Iterator it = this.getButtons().iterator();
193         int i = 0;
194         while (it.hasNext()) {
195             Button b = (Button) it.next();
196             if (b.getName() == null) {
197                 b.setName(this.getName());
198             }
199             b.setButtonType(this.getButtonType());
200             b.setSaveInfo(false);
201             if (b.getHtmlPre(null) == null) {
202                 b.setHtmlPre(this.getButtonHtmlPre());
203             }
204             if (b.getHtmlInter(null) == null) {
205                 b.setHtmlInter(this.getButtonHtmlInter());
206             }
207             if (b.getHtmlPost(null) == null) {
208                 b.setHtmlPost(this.getButtonHtmlPost());
209             }
210             if (StringUtils.isEmpty(b.getCssClass())) {
211                 b.setCssClass(this.getCssClass());
212             }
213             b.setId(this.getName() + "_SETBUTTON_" + i); //$NON-NLS-1$
214             if (this.getValueType() == ControlImpl.VALUETYPE_MULTIPLE) {
215                 if (this.getValues().size() != 0) {
216                     if (this.getValues().contains(b.getValue())) {
217                         b.setState(BUTTONSTATE_PUSHED);
218                     }
219                     else {
220                         b.setState(BUTTONSTATE_NORMAL);
221                     }
222                 }
223             }
224             else {
225                 if (StringUtils.isNotEmpty(this.getValue())) {
226                     if (this.getValue().equals(b.getValue())) {
227                         b.setState(BUTTONSTATE_PUSHED);
228                     }
229                     else {
230                         b.setState(BUTTONSTATE_NORMAL);
231                     }
232                 }
233             }
234             html.append(b.getHtml());
235             if (it.hasNext()) {
236                 html.append(this.getHtmlInter());
237             }
238             i++;
239         }
240         html.append(this.getHtmlPost());
241         html.append(this.getHtmlSaveInfo());
242         return html.toString();
243     }
244 }