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