KickJava   Java API By Example, From Geeks To Geeks.

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


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

147     private final String JavaDoc getServerHttpPath (ParamBean jParams)
148     {
149         return jParams.getRequest().getContextPath();
150     }
151
152     public int doEndTag() throws JspException JavaDoc {
153         // let's reinitialize the tag variables to allow tag object reuse in
154
// pooling.
155
img = "";
156         href = "";
157         alt = "";
158         altKey = null;
159         altBundle = null;
160         return EVAL_PAGE;
161     }
162
163     // Taglib parameters
164
private String JavaDoc img = "";
165     private String JavaDoc href = "";
166     private String JavaDoc alt = "";
167
168     private String JavaDoc altKey = null;
169     private String JavaDoc altBundle = null;
170
171     // Internal members
172
private static long imgID = 0;
173     private boolean debug = true;
174
175
176 }
177
Popular Tags