KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > jsptag > PopupTag


1 /*
2  * PopupTag.java
3  *
4  * Version: $Revision: 1.9 $
5  *
6  * Date: $Date: 2005/08/25 17:20:24 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.jsptag;
41
42 import java.io.IOException JavaDoc;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
47 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
48
49 /**
50  * Tag for producing a popup window link. Takes advantage of Javascript to
51  * produce a small window that is brought to the front every time a popup link
52  * is pressed. If Javascript is not available, a simple HTML link is used as a
53  * fallback. The contents of the tag are used as the text of the link.
54  *
55  * Additionally, this will link to the "local" version of the URL, if a locally
56  * modified version exists.
57  *
58  * FIXME: Currently supports a single popup window at a hardcoded size; extra
59  * attributes could be added at a later date (e.g. name, width, height)
60  *
61  * @author Robert Tansley
62  * @version $Revision: 1.9 $
63  */

64 public class PopupTag extends BodyTagSupport JavaDoc
65 {
66     /** Path of default JSP version */
67     private String JavaDoc page;
68
69     public PopupTag()
70     {
71         super();
72     }
73
74     /**
75      * Get the JSP to display (default version)
76      *
77      * @return the page to display
78      */

79     public String JavaDoc getPage()
80     {
81         return page;
82     }
83
84     /**
85      * Set the JSP to display (default version)
86      *
87      * @param s
88      * the page to display
89      */

90     public void setPage(String JavaDoc s)
91     {
92         page = s;
93     }
94
95     public int doAfterBody() throws JspException JavaDoc
96     {
97         /*
98          * The output is the following, with PAGE and TEXT replaced
99          * appropriately:
100          *
101          * <script type="text/javascript"> <!-- document.write(' <a HREF="#"
102          * onClick="var popupwin =
103          * window.open(\'PAGE\',\'dspacepopup\',\'height=600,width=550,resizable,scrollbars\');popupwin.focus();return
104          * false;">TEXT <\/a>'); // --> </script> <noscript> <a HREF="PAGE"
105          * target="dspacepopup">TEXT </a>. </noscript>
106          *
107          * The script writes a Javascripted link which opens the popup window
108          * 600x550, or brings it to the front if it's already open. If
109          * Javascript is not available, plain HTML link in the NOSCRIPT element
110          * is used.
111          */

112         BodyContent JavaDoc bc = getBodyContent();
113         String JavaDoc linkText = bc.getString();
114         bc.clearBody();
115
116         HttpServletRequest JavaDoc hrq = (HttpServletRequest JavaDoc) pageContext.getRequest();
117         String JavaDoc actualPage = hrq.getContextPath() + page;
118
119         String JavaDoc html = "<script type=\"text/javascript\">\n"
120                 + "<!-- Javascript starts here\n"
121                 + "document.write('<a HREF=\"#\" onClick=\"var popupwin = window.open(\\'"
122                 + actualPage
123                 + "\\',\\'dspacepopup\\',\\'height=600,width=550,resizable,scrollbars\\');popupwin.focus();return false;\">"
124                 + linkText + "<\\/a>');\n" + "// -->\n"
125                 + "</script><noscript><a HREF=\"" + actualPage
126                 + "\" target=\"dspacepopup\">" + linkText + "</a></noscript>";
127
128         try
129         {
130             getPreviousOut().print(html);
131         }
132         catch (IOException JavaDoc ie)
133         {
134             throw new JspException JavaDoc(ie);
135         }
136
137         return SKIP_BODY;
138     }
139 }
140
Popular Tags