KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ____.
2
// __/\ ______| |__/\. _______
3
// __ .____| | \ | +----+ \
4
// _______| /--| | | - \ _ | : - \_________
5
// \\______: :---| : : | : | \________>
6
// |__\---\_____________:______: :____|____:_____\
7
// /_____|
8
//
9
// . . . i n j a h i a w e t r u s t . . .
10
//
11
/*
12  * ----- BEGIN LICENSE BLOCK -----
13  * Version: JCSL 1.0
14  *
15  * The contents of this file are subject to the Jahia Community Source License
16  * 1.0 or later (the "License"); you may not use this file except in
17  * compliance with the License. You may obtain a copy of the License at
18  * http://www.jahia.org/license
19  *
20  * Software distributed under the License is distributed on an "AS IS" basis,
21  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
22  * for the rights, obligations and limitations governing use of the contents
23  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
24  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
25  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
26  *
27  * The Shared Modifications are Jahia button taglibs.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution S�rl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * Sep 24 2002 Jahia Solutions S�rl: MAP Initial release.
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39 package org.jahia.deprecated.taglibs.button;
40
41 import java.io.IOException JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.jsp.JspException JavaDoc;
45 import javax.servlet.jsp.JspWriter JavaDoc;
46 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
47
48 import org.jahia.data.JahiaData;
49 import org.jahia.resourcebundle.JahiaResourceBundle;
50
51 /**
52  * <p>The Jahia Shared Modification is: Jahia Tag Libs</p>
53  *
54  * <p>Description:
55  * Display a country flag image (if possible) corresponding to a given language.
56  *
57  * Synopsis : <jahia:displayLanguageFlag code="<iso639>[_<iso3166>]"
58  * [href="<anchor href>"] [alt="<text>"]
59  * [resourceOn="<relative path to an image>"]
60  * [resourceOff="<relative path to an image>"]/>
61  *
62  * The language resource image is a composition of these both paramters :<br>
63  * <iso639> : is the iso 639 language code. If this code is incorrect then the
64  * code name is displayed as is.<br>
65  * <iso3166> : Optional parameter that represent the iso 3166 country code.<br>
66  * examples : code="en_UK"<br>
67  * code="en"<br>
68  * code="fr_CH"<br>
69  * href : This is the anchor tag < A> "href" parameter.<br>
70  * alt : Correspond to the < IMG> tag "alt" parameter.<br>
71  * resourceOn, resourceOff : Alternative resource defined by the relative
72  * path from the JSP source file to an image file.</p>
73  *
74  * <p>Copyright: MAP (Jahia Solutions S�rl 2002)</p>
75  * <p>Company: Jahia Solutions S�rl</p>
76  * @author MAP
77  * @version 1.0
78  */

79 public class DisplayLanguageFlagTag extends BodyTagSupport JavaDoc {
80
81     /**
82      * @param code The language code coresponding to the iso 639 language code
83      * list.
84      */

85     public void setCode(String JavaDoc code) {
86         _code = code;
87     }
88
89     /**
90      * @param href The anchor href parameter.
91      */

92     public void setHref(String JavaDoc href) {
93         _href = href;
94     }
95
96     /**
97      * @param resourceOn The path to a resource image file corresponding to the
98      * active position when mouse is on it.
99      */

100     public void setResourceOn(String JavaDoc resourceOn) {
101         _resourceOn = resourceOn;
102     }
103
104     /**
105      * @param resourceOff The path to a resource image file corresponding to the
106      * deactivate position when mouse doesn't point to it.
107      */

108     public void setResourceOff(String JavaDoc resourceOff) {
109         _resourceOff = resourceOff;
110     }
111
112     /**
113      * @param alt The alt parameter from the flag image.
114      */

115     public void setAlt(String JavaDoc alt) {
116         _alt = alt;
117     }
118
119     public int doStartTag() {
120
121         // Recover 'jData'
122
// @todo FIXME : This code is repeated in a lot of button taglibs
123
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
124         JahiaData jData = (JahiaData)request.getAttribute("org.jahia.data.JahiaData");
125
126         // Produce the HTML code
127
try {
128             JspWriter JavaDoc out = pageContext.getOut();
129             StringBuffer JavaDoc str = new StringBuffer JavaDoc("");
130             if (debug) {
131                 str.append("\n<!-- ======================================================================= -->\n");
132                 str.append("<!-- The following HTML code is generated by 'DisplayLanguageFlagTag' taglib -->\n");
133                 str.append("<!----------------------------------------------------------------------------->\n");
134             }
135             // Resolve file name
136
String JavaDoc flagName = _code;
137             String JavaDoc flagOnPath;
138             if ("".equals(_resourceOn)) {
139                 flagOnPath = JahiaResourceBundle.getUrlPathCommonResource(
140                     flagName + "FlagOn", jData.params());
141             } else {
142                 flagOnPath = _resourceOn;
143             }
144             // Define a unique ID that identify the rollover
145
String JavaDoc flagImgID = "flagImg" + String.valueOf(imgID++);
146             // Contruct image HTML tag
147
if (flagOnPath != null) {
148                 String JavaDoc flagOffPath = flagOnPath;
149                 // Should an anchor be written.
150
if (!"".equals(_href)) {
151                     if ("".equals(_resourceOff)) {
152                         flagOffPath = JahiaResourceBundle.getUrlPathCommonResource(
153                                 flagName + "FlagOff", jData.params());
154                     } else {
155                         flagOffPath = _resourceOff;
156                     }
157                     if (flagOffPath == null) {
158                         flagOffPath = "";
159                     }
160                     str.append("<a HREF=\"");
161                     str.append(_href);
162                     str.append("\"\n");
163                     str.append("\tonMouseOut=\"MM_swapImgRestore()\"\n");
164                     str.append("\tonMouseOver=\"MM_swapImage('");
165                     str.append(flagImgID);
166                     str.append("','','");
167                     str.append(flagOnPath);
168                     str.append("',1)\">\n");
169                 }
170                 // Write image HTML tag
171
str.append("<img name=\"");
172                 str.append(flagImgID);
173                 str.append("\" border=\"0\" alt=\"");
174                 str.append(_alt);
175                 str.append("\" SRC=\"");
176                 str.append("".equals(_href) ? flagOnPath : flagOffPath);
177                 str.append("\">");
178             } else { // No flag image resource found.
179
if (!"".equals(_href)) {
180                     str.append("<a HREF=\"");
181                     str.append(_href);
182                     str.append("\">");
183                 }
184                 str.append(flagName);
185             }
186             if (!"".equals(_href)) {
187                 str.append("</a>");
188             }
189             if (debug) {
190                 str.append("\n<!-- ======================================================================= -->\n");
191             }
192             out.print(str.toString());
193         } catch (IOException JavaDoc ioe) {
194             logger.debug(ioe.toString());
195         }
196         return SKIP_BODY;
197     }
198
199     public int doEndTag() throws JspException JavaDoc {
200         // let's reinitialize the tag variables to allow tag object reuse in
201
// pooling.
202
_code = "";
203         _href = "";
204         _resourceOn = "";
205         _resourceOff = "";
206         _alt = "";
207         return EVAL_PAGE;
208     }
209
210
211     // Taglib parameters
212
private String JavaDoc _code = "";
213     private String JavaDoc _href = "";
214     private String JavaDoc _resourceOn = "";
215     private String JavaDoc _resourceOff = "";
216     private String JavaDoc _alt = "";
217
218     // Display debug information
219
/** @todo FIXME : change harcoded debug information */
220     private boolean debug = false;
221     private static long imgID = 0;
222
223     private static org.apache.log4j.Logger logger =
224             org.apache.log4j.Logger.getLogger(DisplayLanguageFlagTag.class);
225
226 }
227
Popular Tags