KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > taglib > JonasButtonTag


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JonasButtonTag.java,v 1.8 2005/04/01 09:57:23 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.taglib;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29
30 import javax.servlet.jsp.JspException JavaDoc;
31
32 import org.apache.commons.beanutils.PropertyUtils;
33 import org.apache.struts.taglib.html.LinkTag;
34 import org.apache.struts.taglib.TagUtils;
35 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
36
37 /**
38  * @author Michel-Ange ANTON
39  */

40 public class JonasButtonTag extends LinkTag {
41
42 // ----------------------------------------------------- Manifest Constants
43

44     private static final String JavaDoc s_ImageBorder = "dot.gif";
45
46 // ----------------------------------------------------- Instance Variables
47

48     protected String JavaDoc ms_PathImage = null;
49     protected String JavaDoc ms_LastStyleClass = null;
50     protected String JavaDoc ms_StyleClassButton = "btn";
51
52 // ------------------------------------------------------------- Properties
53

54     private int widthBorder = 1;
55     private int heightBorder = 1;
56     private String JavaDoc imagesDir = null;
57     private String JavaDoc styleClassBorderLight = "btnBorderLight";
58     private String JavaDoc styleClassBorderShadow = "btnBorderShadow";
59     private String JavaDoc styleClassBackground = "btn";
60     private String JavaDoc styleClassDisabled = "btnDisabled";
61     private String JavaDoc disabledName = null;
62     private String JavaDoc disabledProperty = null;
63     private String JavaDoc disabledValueEqual = null;
64     private String JavaDoc disabledValueNotEqual = null;
65
66     public int getHeightBorder() {
67         return heightBorder;
68     }
69
70     public void setHeightBorder(int heightBorder) {
71         this.heightBorder = heightBorder;
72     }
73
74     public String JavaDoc getImagesDir() {
75         return imagesDir;
76     }
77
78     public void setImagesDir(String JavaDoc imagesDir) {
79         this.imagesDir = imagesDir;
80     }
81
82     public int getWidthBorder() {
83         return widthBorder;
84     }
85
86     public void setWidthBorder(int widthBorder) {
87         this.widthBorder = widthBorder;
88     }
89
90     public String JavaDoc getStyleClassBorderLight() {
91         return styleClassBorderLight;
92     }
93
94     public void setStyleClassBorderLight(String JavaDoc styleClassBorderLight) {
95         this.styleClassBorderLight = styleClassBorderLight;
96     }
97
98     public String JavaDoc getStyleClassBorderShadow() {
99         return styleClassBorderShadow;
100     }
101
102     public void setStyleClassBorderShadow(String JavaDoc styleClassBorderShadow) {
103         this.styleClassBorderShadow = styleClassBorderShadow;
104     }
105
106     public String JavaDoc getStyleClassBackground() {
107         return styleClassBackground;
108     }
109
110     public void setStyleClassBackground(String JavaDoc styleClassBackground) {
111         this.styleClassBackground = styleClassBackground;
112     }
113
114     public String JavaDoc getStyleClassDisabled() {
115         return styleClassDisabled;
116     }
117
118     public void setStyleClassDisabled(String JavaDoc styleClassDisabled) {
119         this.styleClassDisabled = styleClassDisabled;
120     }
121
122     public String JavaDoc getDisabledName() {
123         return disabledName;
124     }
125
126     public void setDisabledName(String JavaDoc disabledName) {
127         this.disabledName = disabledName;
128     }
129
130     public String JavaDoc getDisabledProperty() {
131         return disabledProperty;
132     }
133
134     public void setDisabledProperty(String JavaDoc disabledProperty) {
135         this.disabledProperty = disabledProperty;
136     }
137
138     public String JavaDoc getDisabledValueEqual() {
139         return disabledValueEqual;
140     }
141
142     public void setDisabledValueEqual(String JavaDoc disabledValueEqual) {
143         this.disabledValueEqual = disabledValueEqual;
144     }
145
146     public String JavaDoc getDisabledValueNotEqual() {
147         return disabledValueNotEqual;
148     }
149
150     public void setDisabledValueNotEqual(String JavaDoc disabledValueNotEqual) {
151         this.disabledValueNotEqual = disabledValueNotEqual;
152     }
153
154 // ------------------------------------------------------------- Public methods
155

156     /**
157      * Render the beginning of the hyperlink.
158      *
159      * @exception JspException if a JSP exception has occurred
160      */

161     public int doStartTag()
162         throws JspException JavaDoc {
163
164         // Test if button disabled
165
boolean bDisabled = getDisabled();
166         TagUtils tagUtils = TagUtils.getInstance();
167         if ((getDisabledName() != null) &&
168             ((getDisabledValueEqual() != null) || (getDisabledValueNotEqual() != null))) {
169             String JavaDoc sValue = lookupProperty(getDisabledName(), getDisabledProperty());
170             if (getDisabledValueEqual() != null) {
171                 bDisabled = getDisabledValueEqual().equals(sValue);
172             } else {
173                 bDisabled = !getDisabledValueNotEqual().equals(sValue);
174             }
175         }
176
177         // Force style class to the Link Tag
178
ms_LastStyleClass = getStyleClass();
179         if (getStyleClass() == null) {
180             setStyleClass(ms_StyleClassButton);
181         }
182         // Prepare path image
183
makePathImage();
184         // Add the begining of render button
185
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(
186             "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
187         sb.append(prepareBorderTop());
188         sb.append("<tr>");
189         sb.append(prepareBorderColumn(styleClassBorderLight));
190         sb.append("<td nowrap class=\"");
191         if (bDisabled == false) {
192             sb.append(styleClassBackground);
193         } else {
194             sb.append(styleClassDisabled);
195         }
196         sb.append("\">");
197         tagUtils.write(pageContext, sb.toString());
198
199         if (bDisabled == false) {
200             // Add link if enabled
201
return (super.doStartTag());
202         }
203         return (EVAL_BODY_BUFFERED);
204     }
205
206     /**
207      * Render the end of the hyperlink.
208      *
209      * @exception JspException if a JSP exception has occurred
210      */

211     public int doEndTag()
212         throws JspException JavaDoc {
213         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
214         int iRet = EVAL_PAGE;
215         TagUtils tagUtils = TagUtils.getInstance();
216
217         // If body text exist pre-fix and post-fix with space
218
if (text != null) {
219             StringBuffer JavaDoc sbText = new StringBuffer JavaDoc("&nbsp;");
220             sbText.append(text);
221             sbText.append("&nbsp;");
222             text = sbText.toString();
223         }
224
225         if (getDisabled() == false) {
226             // Add link if enabled
227
iRet = super.doEndTag();
228         } else {
229             sb.append(text);
230         }
231
232         // Add the end of render button
233
sb.append("</td>");
234         sb.append(prepareBorderColumn(styleClassBorderShadow));
235         sb.append("</tr>");
236         sb.append(prepareBorderBottom());
237         sb.append("</table>");
238         tagUtils.write(pageContext, sb.toString());
239
240         // Force last style
241
setStyleClass(ms_LastStyleClass);
242         ms_LastStyleClass = null;
243
244         return (iRet);
245     }
246
247     public String JavaDoc getImagesRoot() {
248         String JavaDoc sImagesRoot = null;
249         WhereAreYou oWhere = getWhereAreYouInstance();
250         if (oWhere != null) {
251             sImagesRoot = oWhere.getImagesRoot();
252         }
253         return sImagesRoot;
254     }
255
256     public void release() {
257         super.release();
258         ms_PathImage = null;
259         imagesDir = null;
260         ms_StyleClassButton = null;
261         styleClassBorderLight = null;
262         styleClassBorderShadow = null;
263         styleClassBackground = null;
264         styleClassDisabled = null;
265     }
266
267 // --------------------------------------------------------- Protected Methods
268
protected void makePathImage() {
269         // Prepare image separator
270
if (imagesDir != null) {
271             ms_PathImage = imagesDir + "/" + s_ImageBorder;
272         }
273         else if (getImagesRoot() != null) {
274             ms_PathImage = getImagesRoot() + "/" + s_ImageBorder;
275         }
276         else {
277             ms_PathImage = s_ImageBorder;
278         }
279     }
280
281     /**
282      * Return render image.
283      * @return
284      */

285     protected String JavaDoc prepareImage() {
286         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<img SRC=\"");
287         sb.append(ms_PathImage);
288         sb.append("\" width=\"");
289         sb.append(widthBorder);
290         sb.append("\" height=\"");
291         sb.append(heightBorder);
292         sb.append("\" border=\"0\">");
293         return sb.toString();
294     }
295
296     /**
297      * Return render image.
298      * @return
299      */

300     protected String JavaDoc prepareBorderColumn(String JavaDoc ps_Class) {
301         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<td width=\"");
302         sb.append(widthBorder);
303         sb.append("\" height=\"");
304         sb.append(heightBorder);
305         sb.append("\" class=\"");
306         //btnBorderLight
307
sb.append(ps_Class);
308         sb.append("\">");
309         sb.append(prepareImage());
310         sb.append("</td>");
311         return sb.toString();
312     }
313
314     protected String JavaDoc prepareBorderTop() {
315         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<tr>");
316         sb.append(prepareBorderColumn(styleClassBorderLight));
317         sb.append(prepareBorderColumn(styleClassBorderLight));
318         sb.append(prepareBorderColumn(styleClassBorderLight));
319         sb.append("</tr>");
320         return sb.toString();
321     }
322
323     protected String JavaDoc prepareBorderBottom() {
324         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<tr>");
325         sb.append(prepareBorderColumn(styleClassBorderLight));
326         sb.append(prepareBorderColumn(styleClassBorderShadow));
327         sb.append(prepareBorderColumn(styleClassBorderShadow));
328         sb.append("</tr>");
329         return sb.toString();
330     }
331
332     protected WhereAreYou getWhereAreYouInstance() {
333         return (WhereAreYou) pageContext.getSession().getAttribute(WhereAreYou.SESSION_NAME);
334     }
335
336     protected String JavaDoc lookupProperty(String JavaDoc beanName, String JavaDoc property)
337         throws JspException JavaDoc {
338
339         TagUtils tagUtils = TagUtils.getInstance();
340         Object JavaDoc bean = tagUtils.lookup(this.pageContext, beanName, null);
341         if (bean == null) {
342             throw new JspException JavaDoc(messages.getMessage("getter.bean", beanName));
343         }
344         if (property == null) {
345             return bean.toString();
346         }
347         try {
348             return (PropertyUtils.getProperty(bean, property)).toString();
349         } catch (IllegalAccessException JavaDoc e) {
350             throw new JspException JavaDoc(messages.getMessage("getter.access", property, beanName));
351
352         } catch (InvocationTargetException JavaDoc e) {
353             Throwable JavaDoc t = e.getTargetException();
354             throw new JspException JavaDoc(messages.getMessage("getter.result", property, t.toString()));
355
356         } catch (NoSuchMethodException JavaDoc e) {
357             throw new JspException JavaDoc(messages.getMessage("getter.method", property, beanName));
358         }
359     }
360 }
Popular Tags