KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SelectEPersonTag.java
3  *
4  * Version: $Revision: 1.8 $
5  *
6  * Date: $Date: 2005/10/13 05:48:03 $
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.JspWriter JavaDoc;
47 import javax.servlet.jsp.jstl.fmt.LocaleSupport;
48 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
49
50 import org.dspace.eperson.EPerson;
51
52 /**
53  * <p>
54  * Tag for producing an e-person select widget in a form. Somewhat analogous to
55  * the HTML SELECT element. An input field is produced with a button which pops
56  * up a window from which e-people can be selected. Selected e-epeople are added
57  * to the field in the form. If the selector is for multiple e-people, a 'remove
58  * selected from list' button is also added.
59  * </p>
60  *
61  * <p>
62  * On any form that has a selecteperson tag (only one allowed per page), you
63  * need to include the following Javascript code on all of the submit buttons,
64  * to ensure that the e-people IDs are posted and that the popup window is
65  * closed:
66  * </p>
67  *
68  * <p>
69  * <code>onclick="javascript:finishEPerson();"</code>
70  * </p>
71  *
72  * @author Robert Tansley
73  * @version $Revision: 1.8 $
74  */

75 public class SelectEPersonTag extends TagSupport JavaDoc
76 {
77     /** Multiple e-people? */
78     private boolean multiple;
79
80     /** Which eperson/epeople are initially in the list? */
81     private EPerson[] epeople;
82
83     public SelectEPersonTag()
84     {
85         super();
86     }
87
88     /**
89      * Setter for multiple attribute
90      *
91      * @param s
92      * attribute from JSP
93      */

94     public void setMultiple(String JavaDoc s)
95     {
96         if ((s != null)
97                 && (s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("true")))
98         {
99             multiple = true;
100         }
101         else
102         {
103             multiple = false;
104         }
105     }
106
107     /**
108      * Setter for e-people in list
109      *
110      * @param e
111      * attribute from JSP
112      */

113     public void setSelected(Object JavaDoc e)
114     {
115         if (e instanceof EPerson)
116         {
117             epeople = new EPerson[1];
118             epeople[0] = (EPerson) e;
119         }
120         else if (e instanceof EPerson[])
121         {
122             epeople = (EPerson[]) e;
123         }
124     }
125
126     public void release()
127     {
128         multiple = false;
129         epeople = null;
130     }
131
132     public int doStartTag() throws JspException JavaDoc
133     {
134         try
135         {
136             JspWriter JavaDoc out = pageContext.getOut();
137             HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext
138                     .getRequest();
139
140             out.print("<table><tr><td colspan=\"2\" align=\"center\"><select multiple=\"multiple\" name=\"eperson_id\" size=\"");
141             out.print(multiple ? "10" : "1");
142             out.println("\">");
143             // ensure that if no eperson is selected that a blank option is displayed - xhtml compliance
144
if (epeople == null || epeople.length == 0)
145             {
146               out.print("<option value=\"\">&nbsp;</option>");
147             }
148              
149             if (epeople != null)
150             {
151                 for (int i = 0; i < epeople.length; i++)
152                 {
153                     out.print("<option value=\"" + epeople[i].getID() + "\">");
154                     out.print(epeople[i].getFullName() + " ("
155                             + epeople[i].getEmail() + ")");
156                     out.println("</option>");
157                 }
158             }
159             // add blank option value if no person selected to ensure that code is xhtml compliant
160
//out.print("<option/>");
161
out.print("</select></td>");
162
163             if (multiple)
164             {
165                 out.print("</tr><tr><td width=\"50%\" align=\"center\">");
166             }
167             else
168             {
169                 out.print("<td>");
170             }
171
172             String JavaDoc p = (multiple ?
173                             LocaleSupport.getLocalizedMessage(pageContext,
174                                     "org.dspace.app.webui.jsptag.SelectEPersonTag.selectPeople")
175                             : LocaleSupport.getLocalizedMessage(pageContext,
176                                     "org.dspace.app.webui.jsptag.SelectEPersonTag.selectPerson") );
177             out.print("<input type=\"button\" value=\"" + p
178                     + "\" onclick=\"javascript:popup_window('"
179                     + req.getContextPath() + "/tools/eperson-list?multiple="
180                     + multiple + "', 'eperson_popup');\" />");
181
182             if (multiple)
183             {
184                 out.print("</td><td width=\"50%\" align=\"center\">");
185                 out.print("<input type=\"button\" value=\""
186                                         + LocaleSupport.getLocalizedMessage(pageContext,
187                                                 "org.dspace.app.webui.jsptag.SelectEPersonTag.removeSelected")
188                                         + "\" onclick=\"javascript:removeSelected(window.document.forms[0].eperson_id);\"/>");
189             }
190
191             out.println("</td></tr></table>");
192         }
193         catch (IOException JavaDoc ie)
194         {
195             throw new JspException JavaDoc(ie);
196         }
197
198         return SKIP_BODY;
199     }
200 }
201
Popular Tags