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 org.apache.commons.lang.StringUtils;
37  
38  
39  /**
40   * @author Vinzenz Wyser
41   * @version 2.0
42   */
43  public class Button extends ControlImpl {
44  
45      /**
46       * html before.
47       */
48      private static final String HTML_PRE_DIVIDED = "<table cellpadding=0 cellspacing=0 border=0><tr><td>"; //$NON-NLS-1$
49  
50      private String label;
51  
52      private String iconSrc;
53  
54      private String onclick;
55  
56      private int state = BUTTONSTATE_NORMAL;
57  
58      private int buttonType = BUTTONTYPE_PUSHBUTTON;
59  
60      private String pushButtonTag = "span"; //$NON-NLS-1$
61  
62      private boolean small;
63  
64      public Button() {
65      }
66  
67      public Button(String name, String value) {
68          super(name, value);
69      }
70  
71      public void setLabel(String s) {
72          this.label = s;
73      }
74  
75      public String getLabel() {
76          if (this.label != null) {
77              return this.label;
78          }
79  
80          return this.getValue();
81      }
82  
83      /**
84       * Sets the source path for the image. URI must contain the context path.
85       * @param s image source, with context path
86       */
87      public void setIconSrc(String s) {
88          this.iconSrc = s;
89      }
90  
91      public String getIconSrc() {
92          if (iconSrc == null) {
93              return StringUtils.EMPTY;
94          }
95  
96          // iconSrc already has context path
97          return "<img src=\"" + this.iconSrc + "\" />"; //$NON-NLS-1$ //$NON-NLS-2$
98      }
99  
100     public void setOnclick(String s) {
101         this.onclick = s;
102     }
103 
104     public String getOnclick() {
105         return this.onclick;
106     }
107 
108     public void setHtmlPre() {
109         if (super.getHtmlPre(null) == null) {
110             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
111                 this.setHtmlPre(StringUtils.EMPTY);
112             }
113             else {
114                 this.setHtmlPre(HTML_PRE_DIVIDED);
115             }
116         }
117     }
118 
119     public void setHtmlInter() {
120         if (super.getHtmlInter(null) == null) {
121             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
122                 this.setHtmlInter(StringUtils.EMPTY);
123             }
124             else {
125                 this.setHtmlInter("</td><td>"); //$NON-NLS-1$
126             }
127         }
128     }
129 
130     public void setHtmlPost() {
131         if (super.getHtmlPost(null) == null) {
132             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
133                 this.setHtmlPost(StringUtils.EMPTY);
134             }
135             else {
136                 this.setHtmlPost("</td></tr></table>"); //$NON-NLS-1$
137             }
138         }
139     }
140 
141     public void setSmall(boolean b) {
142         this.small = b;
143     }
144 
145     public boolean getSmall() {
146         return this.small;
147     }
148 
149     public void setPushButtonTag(String s) {
150         this.pushButtonTag = s;
151     }
152 
153     public String getPushButtonTag() {
154         return this.pushButtonTag;
155     }
156 
157     public String getHtml() {
158         StringBuffer html = new StringBuffer();
159         this.setHtmlPre();
160         this.setHtmlInter();
161         this.setHtmlPost();
162         html.append(this.getHtmlPre());
163         if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
164             html.append(this.getHtmlPushbutton());
165         }
166         else {
167             html.append(this.getHtmlDividedbutton());
168         }
169         html.append(this.getHtmlPost());
170         return html.toString();
171     }
172 
173     public String getHtmlDividedbutton() {
174         StringBuffer html = new StringBuffer();
175         String buttonType;
176         if (this.getButtonType() == BUTTONTYPE_RADIO) {
177             buttonType = "radio"; //$NON-NLS-1$
178         }
179         else {
180             buttonType = "checkbox"; //$NON-NLS-1$
181         }
182         html.append("<input type=\"" + buttonType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
183         html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
184         html.append(" value=\"" + ControlImpl.escapeHTML(this.getValue()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
185         html.append(" id=\"" + this.getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
186         if (StringUtils.isNotEmpty(this.getOnclick())) {
187             html.append(" onclick=\"" + this.getOnclick() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
188         }
189         if (this.getState() == BUTTONSTATE_PUSHED) {
190             html.append(" checked=\"checked\""); //$NON-NLS-1$
191         }
192         html.append(this.getHtmlCssClass());
193         html.append(this.getHtmlCssStyles());
194         html.append(" />"); //$NON-NLS-1$
195         if (this.getSaveInfo()) {
196             html.append(this.getHtmlSaveInfo());
197         }
198         html.append(this.getHtmlInter());
199         html.append("<a href=\"javascript:mgnlShiftDividedButton('" + this.getId() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
200         if (StringUtils.isNotEmpty(this.getOnclick())) {
201             html.append(this.getOnclick());
202         }
203         html.append("\" " + this.getHtmlCssClass() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
204 
205         html.append(this.getLabel());
206         html.append(this.getIconSrc());
207 
208         html.append("</a>"); //$NON-NLS-1$
209         return html.toString();
210     }
211 
212     public String getHtmlPushbutton() {
213         StringBuffer html = new StringBuffer();
214         html.append("<" + this.getPushButtonTag()); //$NON-NLS-1$
215         if (StringUtils.isEmpty(this.getCssClass())) {
216             if (this.getSmall()) {
217                 this.setCssClass(CSSCLASS_CONTROLBUTTONSMALL);
218             }
219             else {
220                 this.setCssClass(CSSCLASS_CONTROLBUTTON);
221             }
222         }
223         if (this.getState() == BUTTONSTATE_PUSHED) {
224             this.setCssClass(this.getCssClass() + "_PUSHED"); //$NON-NLS-1$
225         }
226 
227         this.setEvent("onclick", "mgnlShiftPushButtonClick(this);"); //$NON-NLS-1$ //$NON-NLS-2$
228         if (StringUtils.isNotEmpty(this.getOnclick())) {
229             this.setEvent("onclick", this.getOnclick()); //$NON-NLS-1$
230         }
231         this.setEvent("onmousedown", "mgnlShiftPushButtonDown(this);"); //$NON-NLS-1$ //$NON-NLS-2$
232         this.setEvent("onmouseout", "mgnlShiftPushButtonOut(this);"); //$NON-NLS-1$ //$NON-NLS-2$
233 
234         html.append(this.getHtmlEvents());
235         html.append(this.getHtmlId());
236         html.append(this.getHtmlCssClass());
237         html.append(this.getHtmlCssStyles());
238         html.append(">"); //$NON-NLS-1$
239 
240         html.append(this.getIconSrc());
241         html.append(this.getLabel());
242         html.append("</" + this.getPushButtonTag() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
243         return html.toString();
244     }
245 
246     public void setState(int i) {
247         this.state = i;
248     }
249 
250     public int getState() {
251         return this.state;
252     }
253 
254     public void setButtonType(int i) {
255         this.buttonType = i;
256     }
257 
258     public int getButtonType() {
259         return this.buttonType;
260     }
261 
262 }