KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > Back


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.taglib;
66
67 import com.jcorporate.expresso.core.i18n.Messages;
68 import com.jcorporate.expresso.core.misc.ConfigManager;
69 import com.jcorporate.expresso.core.misc.ConfigurationException;
70 import com.jcorporate.expresso.core.misc.StringUtil;
71 import com.jcorporate.expresso.ext.struts.taglib.bean.ExMessageTag;
72 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
73
74 import javax.servlet.http.HttpServletRequest JavaDoc;
75 import javax.servlet.jsp.JspTagException JavaDoc;
76 import javax.servlet.jsp.JspWriter JavaDoc;
77 import javax.servlet.jsp.PageContext JavaDoc;
78 import java.io.IOException JavaDoc;
79 import java.util.Locale JavaDoc;
80
81
82 /**
83  * Tag to allow a "back arrow" for a series of pages. See DBMaint jsp's for
84  * an example of it's usage.
85  *
86  * @author Michael Nash
87  */

88 public class Back
89         extends ExpressoTagSupport {
90     private String JavaDoc page = null;
91
92     /**
93      * Tag parameter for defining what image is used for the 'back arrow'
94      */

95     private String JavaDoc image = "/expresso/expresso/images/FingerLeft.gif";
96
97     /**
98      * What is the 'tooltip' help that pops up for this page?
99      */

100     private String JavaDoc title = "Back to original page...";
101
102     /**
103      * What is the session key that stores the back arrow? (optional)
104      */

105     private String JavaDoc key = null;
106
107     /**
108      * Default session key where the back arrow's back URL is stored
109      */

110     public static String JavaDoc BACK_KEY = "com.jcorporate.expresso.ext.taglib.Back.";
111
112     public Back() {
113         super();
114     }
115
116     public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
117         return SKIP_BODY;
118     }
119
120
121     public int doEndTag()
122             throws JspTagException JavaDoc {
123         FastStringBuffer image = new FastStringBuffer(32);
124         try {
125
126             /**
127              * @todo this actually needs to get the appropriate dbcontext
128              * from the session context.
129              */

130             image.append(ConfigManager.getContext("default").getImages());
131             image.append("/FingerLeft.gif");
132         } catch (ConfigurationException ce) {
133             System.out.println(ce.getMessage());
134             System.out.println("Using default image value");
135         }
136
137         String JavaDoc backUrl = null;
138
139         if (page == null) {
140
141             /* means display the "back" link */
142             Object JavaDoc o = pageContext.getAttribute(BACK_KEY + ".URL",
143                     PageContext.SESSION_SCOPE);
144             Object JavaDoc ot = pageContext.getAttribute(BACK_KEY + ".TITLE",
145                     PageContext.SESSION_SCOPE);
146
147             if (o != null) {
148                 backUrl = (String JavaDoc) o;
149
150                 JspWriter JavaDoc writer = pageContext.getOut();
151                 title = (String JavaDoc) ot;
152
153                 if (backUrl.equals("")) {
154                     backUrl = "NONE";
155                 }
156                 if (!backUrl.equals("NONE")) {
157                     try {
158                         FastStringBuffer fsb = new FastStringBuffer(56);
159                         fsb.append("<a HREF=\"");
160                         fsb.append(backUrl);
161                         fsb.append("\" title=\"");
162                         fsb.append(StringUtil.notNull(title));
163                         fsb.append("\"><img SRC=\"");
164                         fsb.append(image.toString());
165                         fsb.append("\" border=\"0\"></a>");
166                         writer.print(fsb.toString());
167 // writer.print("<a HREF=\"" + backUrl + "\" title=\"" +
168
// StringUtil.notNull(title) +
169
// "\"><img SRC=\"" + image +
170
// "\" border=\"0\"></a>");
171
} catch (IOException JavaDoc ie) {
172                         throw new JspTagException JavaDoc("Cant write output");
173                     }
174                 }
175             }
176         } else {
177             if (page.equals("current")) {
178                 HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
179                 backUrl = req.getRequestURI();
180
181                 if (req.getQueryString() != null) {
182                     String JavaDoc queryString = req.getQueryString();
183 // FastStringBuffer fsb = FastStringBuffer.getInstance();
184

185                     FastStringBuffer fsb = new FastStringBuffer(queryString.length()
186                             + backUrl.length()
187                             + 4);
188                     fsb.append(backUrl);
189                     fsb.append("?");
190                     fsb.append(req.getQueryString());
191                     backUrl = fsb.toString();
192 // fsb.release();
193
}
194             } else {
195                 backUrl = page;
196             }
197
198             pageContext.setAttribute(BACK_KEY + ".URL", backUrl,
199                     PageContext.SESSION_SCOPE);
200
201             if (title != null) {
202                 pageContext.setAttribute(BACK_KEY + ".TITLE", title,
203                         PageContext.SESSION_SCOPE);
204             }
205             if (key != null) {
206                 pageContext.setAttribute(BACK_KEY + ".TITLE", getString(key),
207                         PageContext.SESSION_SCOPE);
208             }
209         }
210
211         return EVAL_PAGE;
212     }
213
214     public String JavaDoc getKey() {
215         return key;
216     }
217
218     public void setKey(String JavaDoc newKey) {
219         key = newKey;
220     }
221
222     public String JavaDoc getPage() {
223         return page;
224     }
225
226     /**
227      * @return java.lang.String
228      */

229     public String JavaDoc getTitle() {
230         return title;
231     }
232
233     public String JavaDoc getImage() {
234         return image;
235     }
236
237     public void setPage(String JavaDoc newName) {
238         page = newName;
239     }
240
241     public void setImage(String JavaDoc newName) {
242         image = newName;
243     }
244
245     public void setTitle(String JavaDoc newName) {
246         title = newName;
247     }
248
249     /**
250      * i18n Method Returns the given string as defined in the message bundles
251      *
252      * @param key the name of the key string
253      * @return String as retrieved from the Messages properties.
254      */

255     private String JavaDoc getString(String JavaDoc key) {
256         StringUtil.assertNotBlank(key,
257                 "You must specify a key for the string.");
258
259         Locale JavaDoc l = (Locale JavaDoc) pageContext.getAttribute(Messages.LOCALE_KEY,
260                 PageContext.SESSION_SCOPE);
261         String JavaDoc schemaClass = (String JavaDoc) pageContext.getAttribute(ExMessageTag.SCHEMA_KEY,
262                 PageContext.PAGE_SCOPE);
263
264         if (schemaClass != null) {
265             Object JavaDoc[] args = {};
266
267             return Messages.getString(schemaClass, l, key, args);
268         } else {
269             return Messages.getString(l, key);
270         }
271     }
272 } /* Back */
273
274
Popular Tags