KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > control > Button


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.control;
14
15 import org.apache.commons.lang.StringUtils;
16
17
18 /**
19  * @author Vinzenz Wyser
20  * @version 2.0
21  */

22 public class Button extends ControlSuper {
23
24     /**
25      * html before.
26      */

27     private static final String JavaDoc HTML_PRE_DIVIDED = "<table cellpadding=0 cellspacing=0 border=0><tr><td>"; //$NON-NLS-1$
28

29     private String JavaDoc label;
30
31     private String JavaDoc iconSrc;
32
33     private String JavaDoc onclick;
34
35     private int state = BUTTONSTATE_NORMAL;
36
37     private int buttonType = BUTTONTYPE_PUSHBUTTON;
38
39     private String JavaDoc pushButtonTag = "span"; //$NON-NLS-1$
40

41     private boolean small;
42
43     public Button() {
44     }
45
46     public Button(String JavaDoc name, String JavaDoc value) {
47         super(name, value);
48     }
49
50     public void setLabel(String JavaDoc s) {
51         this.label = s;
52     }
53
54     public String JavaDoc getLabel() {
55         if (this.label != null) {
56             return this.label;
57         }
58
59         return this.getValue();
60     }
61
62     /**
63      * Sets the source path for the image. URI must contain the context path.
64      * @param s image source, with context path
65      */

66     public void setIconSrc(String JavaDoc s) {
67         this.iconSrc = s;
68     }
69
70     public String JavaDoc getIconSrc() {
71         if (iconSrc == null) {
72             return StringUtils.EMPTY;
73         }
74
75         // iconSrc already has context path
76
return "<img SRC=\"" + this.iconSrc + "\" />"; //$NON-NLS-1$ //$NON-NLS-2$
77
}
78
79     public void setOnclick(String JavaDoc s) {
80         this.onclick = s;
81     }
82
83     public String JavaDoc getOnclick() {
84         return this.onclick;
85     }
86
87     public void setHtmlPre() {
88         if (super.getHtmlPre(null) == null) {
89             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
90                 this.setHtmlPre(StringUtils.EMPTY);
91             }
92             else {
93                 this.setHtmlPre(HTML_PRE_DIVIDED);
94             }
95         }
96     }
97
98     public void setHtmlInter() {
99         if (super.getHtmlInter(null) == null) {
100             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
101                 this.setHtmlInter(StringUtils.EMPTY);
102             }
103             else {
104                 this.setHtmlInter("</td><td>"); //$NON-NLS-1$
105
}
106         }
107     }
108
109     public void setHtmlPost() {
110         if (super.getHtmlPost(null) == null) {
111             if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
112                 this.setHtmlPost(StringUtils.EMPTY);
113             }
114             else {
115                 this.setHtmlPost("</td></tr></table>"); //$NON-NLS-1$
116
}
117         }
118     }
119
120     public void setSmall(boolean b) {
121         this.small = b;
122     }
123
124     public boolean getSmall() {
125         return this.small;
126     }
127
128     public void setPushButtonTag(String JavaDoc s) {
129         this.pushButtonTag = s;
130     }
131
132     public String JavaDoc getPushButtonTag() {
133         return this.pushButtonTag;
134     }
135
136     public String JavaDoc getHtml() {
137         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
138         this.setHtmlPre();
139         this.setHtmlInter();
140         this.setHtmlPost();
141         html.append(this.getHtmlPre());
142         if (this.getButtonType() == BUTTONTYPE_PUSHBUTTON) {
143             html.append(this.getHtmlPushbutton());
144         }
145         else {
146             html.append(this.getHtmlDividedbutton());
147         }
148         html.append(this.getHtmlPost());
149         return html.toString();
150     }
151
152     public String JavaDoc getHtmlDividedbutton() {
153         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
154         String JavaDoc buttonType;
155         if (this.getButtonType() == BUTTONTYPE_RADIO) {
156             buttonType = "radio"; //$NON-NLS-1$
157
}
158         else {
159             buttonType = "checkbox"; //$NON-NLS-1$
160
}
161         html.append("<input type=\"" + buttonType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
162
html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
163
html.append(" value=\"" + this.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
164
html.append(" id=\"" + this.getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
165
if (StringUtils.isNotEmpty(this.getOnclick())) {
166             html.append(" onclick=\"" + this.getOnclick() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
167
}
168         if (this.getState() == BUTTONSTATE_PUSHED) {
169             html.append(" checked"); //$NON-NLS-1$
170
}
171         html.append(this.getHtmlCssClass());
172         html.append(this.getHtmlCssStyles());
173         html.append(" />"); //$NON-NLS-1$
174
if (this.getSaveInfo()) {
175             html.append(this.getHtmlSaveInfo());
176         }
177         html.append(this.getHtmlInter());
178         html.append("<a HREF=\"javascript:mgnlShiftDividedButton('" + this.getId() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
179
if (StringUtils.isNotEmpty(this.getOnclick())) {
180             html.append(this.getOnclick());
181         }
182         html.append("\" " + this.getHtmlCssClass() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
183

184         html.append(this.getLabel());
185         html.append(this.getIconSrc());
186
187         html.append("</a>"); //$NON-NLS-1$
188
return html.toString();
189     }
190
191     public String JavaDoc getHtmlPushbutton() {
192         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
193         html.append("<" + this.getPushButtonTag()); //$NON-NLS-1$
194
if (StringUtils.isEmpty(this.getCssClass())) {
195             if (this.getSmall()) {
196                 this.setCssClass(CSSCLASS_CONTROLBUTTONSMALL);
197             }
198             else {
199                 this.setCssClass(CSSCLASS_CONTROLBUTTON);
200             }
201         }
202         if (this.getState() == BUTTONSTATE_PUSHED) {
203             this.setCssClass(this.getCssClass() + "_PUSHED"); //$NON-NLS-1$
204
}
205         html.append(" onclick=\"mgnlShiftPushButtonClick(this);"); //$NON-NLS-1$
206

207         if (StringUtils.isNotEmpty(this.getOnclick())) {
208             html.append(this.getOnclick());
209         }
210         html.append("\""); //$NON-NLS-1$
211

212         html.append(" onmousedown=\"mgnlShiftPushButtonDown(this);\""); //$NON-NLS-1$
213
html.append(" onmouseout=\"mgnlShiftPushButtonOut(this);\""); //$NON-NLS-1$
214
html.append(this.getHtmlId());
215         html.append(this.getHtmlCssClass());
216         html.append(this.getHtmlCssStyles());
217         html.append(">"); //$NON-NLS-1$
218

219         html.append(this.getIconSrc());
220         html.append(this.getLabel());
221         html.append("</" + this.getPushButtonTag() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
222
return html.toString();
223     }
224
225     public void setState(int i) {
226         this.state = i;
227     }
228
229     public int getState() {
230         return this.state;
231     }
232
233     public void setButtonType(int i) {
234         this.buttonType = i;
235     }
236
237     public int getButtonType() {
238         return this.buttonType;
239     }
240
241     /**
242      * @deprecated do nothing
243      * @param i ignored
244      */

245     public void setLabelNbspPadding(int i) {
246         // do nothing
247
}
248 }
249
Popular Tags