KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > button > JahiaButton


1 package org.jahia.taglibs.button;
2
3 import java.io.IOException JavaDoc;
4 import java.util.MissingResourceException JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.JspWriter JavaDoc;
9 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
10
11 import org.jahia.data.JahiaData;
12 import org.jahia.params.ParamBean;
13 import org.jahia.resourcebundle.JahiaResourceBundle;
14 import org.jahia.taglibs.resourcebundle.EngineResourceBundleTag;
15 import org.jahia.utils.JahiaConsole;
16
17 /**
18  * Create a graphic rollover button defining by three parameters.
19  *
20  * All these following parameters are required ('JahiaTags.tld' file) :
21  *
22  * img : The image button start name defined in the resource bundle properties
23             files. For example if the button defined in the resource bundle file
24             is 'sortOffButtonImg' and 'sortOnButtonIMg', the start name is 'sort'.
25             This tag will retrieve automatically the end of string identifier.
26  * href : This is the anchor tag <A> "href" parameter.
27  * alt : Correspond to the <IMG> tag "alt" parameter.
28  */

29 public class JahiaButton extends BodyTagSupport JavaDoc {
30
31     private static org.apache.log4j.Logger logger =
32             org.apache.log4j.Logger.getLogger(JahiaButton.class);
33
34     public void setImg(String JavaDoc img) {
35         this.img = img;
36     }
37
38     public void setHref(String JavaDoc href) {
39         this.href = href;
40     }
41
42     public void setAlt(String JavaDoc alt) {
43         this.alt = alt;
44     }
45
46     public void setAltKey(String JavaDoc altKey) {
47         this.altKey = altKey;
48     }
49
50     public void setAltBundle(String JavaDoc altBundle) {
51         this.altBundle = altBundle;
52     }
53
54     public void setOnclick(String JavaDoc onclick) {
55         this.onclick = onclick;
56     }
57
58
59     public int doStartTag() {
60
61         // Recover 'jData'
62
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
63         JahiaData jData = (JahiaData)request.getAttribute("org.jahia.data.JahiaData");
64
65         // Build the resource bundle image parameters
66
String JavaDoc bImageOff = "";
67         String JavaDoc bImageOn = "";
68         try {
69             bImageOff = EngineResourceBundleTag.parseResourceValue(
70                             JahiaResourceBundle.getEngineResource("org.jahia." + img + "Off.button",
71                                                                   jData.params(),
72                                                                   jData.params().getLocale()),jData.params());
73             bImageOn = EngineResourceBundleTag.parseResourceValue(
74                             JahiaResourceBundle.getEngineResource("org.jahia." + img + "On.button",
75                                                                   jData.params(),
76                                                                   jData.params().getLocale()),jData.params());
77
78         } catch (MissingResourceException JavaDoc mre) {
79             logger.debug("Error while trying to retrieve resource for image :" + img, mre);
80         }
81
82         // FIXME : Why does the "JahiaResourceBundle.getEngineResource" method
83
// return a null String when resource is not found ????
84
if (bImageOff == null) bImageOff = "";
85         if (bImageOn == null) bImageOn = "";
86         bImageOff = getServerHttpPath(jData.params()) + bImageOff;
87         bImageOn = getServerHttpPath(jData.params()) + bImageOn;
88         // Define a unique ID that identify the rollover
89
String JavaDoc sImgID = "img" + String.valueOf(imgID++);
90
91         // now let's resolve the alt text if resource bundle keys are being used.
92
if (altKey != null) {
93             alt = JahiaResourceBundle.getResource(altBundle, altKey, jData.params().getLocale(), jData.params());
94         }
95
96         // Produce the HTML code
97
try {
98             JspWriter JavaDoc out = pageContext.getOut();
99             StringBuffer JavaDoc str = new StringBuffer JavaDoc("\n");
100             if (debug) {
101                 str.append("<!-- ============================================================= -->\n");
102                 str.append("<!-- The following HTML code is generated by 'jahiaButton' taglib -->\n");
103                 str.append("<!-- Parameters : img = ");
104                     str.append(img); str.append("\n");
105                 str.append(" : href = ");
106                     str.append(href); str.append("\n");
107                 str.append(" : alt = ");
108                     str.append(alt);
109                     str.append("\n--------------------------------------------------------------------->\n");
110             }
111             str.append("<a HREF=\"");
112                 str.append(href);
113                 str.append("\"\n");
114             str.append(" onMouseOut=\"MM_swapImgRestore()\"\n");
115             str.append(" onMouseOver=\"MM_swapImage('");
116                 str.append(sImgID);
117                 str.append("','','");
118                 str.append(bImageOn);
119                 str.append("',1)\"\n");
120             if (onclick != null) {
121                 str.append(" onclick=\"");
122                 str.append(onclick);
123                 str.append("\"\n");
124             }
125             str.append(" ><img name=\"");
126                 str.append(sImgID);
127                 str.append("\" alt=\"");
128                 str.append(alt);
129                 str.append("\"\n");
130             str.append(" SRC=\"");
131                 str.append(bImageOff);
132                 str.append("\" border=\"0\"></a>");
133             // @todo : an optional focus parameter (yes/no) are to be implemented
134
// 1. give a "name" the anchor <a>.
135
// 2. use this name to implement the following focus script.
136
// str.append(<script language="javascript">a62.focus();</script>);
137
if (debug) {
138                 str.append("<!-- End 'jahiaButton' taglib ===== -->");
139             }
140             out.print(str.toString());
141         } catch (IOException JavaDoc ioe) {
142             JahiaConsole.println("JahiaButton.doStartTag", ioe.toString());
143         }
144         return SKIP_BODY;
145     }
146
147     /**
148      * Build an http path containing the server name and port,
149      * instead of the path from JahiaPrivateSettings.
150      *
151      * @return An http path leading to Jahia, built with the server name, and
152      * the server port if nonstandard.
153      *
154      * FIXME : This method duplicate the method already defined in ServerHttpPathTag
155      */

156     private final String JavaDoc getServerHttpPath (ParamBean jParams)
157     {
158         return jParams.getRequest().getContextPath();
159     }
160
161     public int doEndTag() throws JspException JavaDoc {
162         // let's reinitialize the tag variables to allow tag object reuse in
163
// pooling.
164
img = "";
165         href = "";
166         alt = "";
167         onclick = null;
168         altKey = null;
169         altBundle = null;
170         return EVAL_PAGE;
171     }
172
173     // Taglib parameters
174
private String JavaDoc img = "";
175     private String JavaDoc href = "";
176     private String JavaDoc alt = "";
177     private String JavaDoc onclick = null;
178
179     private String JavaDoc altKey = null;
180     private String JavaDoc altBundle = null;
181
182     // Internal members
183
private static long imgID = 0;
184     private boolean debug = true;
185
186
187 }
188
Popular Tags