KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > taglib > FormatCustomFieldTag


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.taglib;
20
21 import java.net.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.*;
30 import org.apache.struts.util.*;
31
32 import cowsultants.itracker.ejb.client.models.*;
33 import cowsultants.itracker.ejb.client.resources.*;
34 import cowsultants.itracker.ejb.client.util.*;
35 import cowsultants.itracker.web.util.*;
36
37 public final class FormatCustomFieldTag extends TagSupport JavaDoc {
38     public static final String JavaDoc DISPLAY_TYPE_EDIT = "edit";
39     public static final String JavaDoc DISPLAY_TYPE_VIEW = "view";
40
41     private CustomFieldModel field;
42     private String JavaDoc currentValue;
43     private String JavaDoc displayType;
44     private String JavaDoc formName;
45
46     public CustomFieldModel getField() {
47         return field;
48     }
49
50     public void setField(CustomFieldModel value) {
51         field = value;
52     }
53
54     public String JavaDoc getCurrentValue() {
55         return currentValue;
56     }
57
58     public void setCurrentValue(String JavaDoc value) {
59         currentValue = value;
60     }
61
62     public String JavaDoc getDisplayType() {
63         return displayType;
64     }
65
66     public void setDisplayType(String JavaDoc value) {
67         displayType = value;
68     }
69
70     public String JavaDoc getFormName() {
71         return formName;
72     }
73
74     public void setFormName(String JavaDoc value) {
75         formName = value;
76     }
77
78     public int doStartTag() throws JspException JavaDoc {
79         return SKIP_BODY;
80     }
81
82     public int doEndTag() throws JspException JavaDoc {
83         Locale currLocale = null;
84
85         if(field != null) {
86             HttpSession session = pageContext.getSession();
87             if(session != null) {
88                 currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
89             }
90
91             field.setLabels(currLocale);
92
93             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
94             buf.append("<td class=\"" + (DISPLAY_TYPE_VIEW.equalsIgnoreCase(displayType) ? "editColumnTitle" : "editColumnText") +"\">");
95             buf.append(field.getName() + ": ");
96             buf.append("</td>\n");
97             buf.append("<td align=\"left\" class=\"editColumnText\">");
98             if(DISPLAY_TYPE_VIEW.equalsIgnoreCase(displayType)) {
99                 if(currentValue != null) {
100                     buf.append((field.getFieldType() == CustomFieldUtilities.TYPE_LIST ? field.getOptionNameByValue(currentValue) : currentValue));
101                 }
102             } else {
103                 Object JavaDoc requestValue = RequestUtils.lookup(pageContext, org.apache.struts.taglib.html.Constants.BEAN_KEY, "customFields(" + field.getId() + ")", null);
104                 if(currentValue == null && requestValue != null) {
105                     currentValue = requestValue.toString();
106                 }
107
108                 if(field.getFieldType() == CustomFieldUtilities.TYPE_LIST) {
109                     buf.append("<select name=\"customFields(" + field.getId() + ")\">\n");
110                     CustomFieldValueModel[] options = field.getOptions();
111                     for(int i = 0; i < options.length; i++) {
112                         buf.append("<option value=\"" + options[i].getValue() + "\"");
113                         buf.append((currentValue != null && currentValue.equals(options[i].getValue()) ? " selected=\"selected\"" : ""));
114                         buf.append(">");
115                         buf.append(options[i].getName());
116                         buf.append("</option>\n");
117                     }
118                     buf.append("</select>\n");
119                 } else if(field.getFieldType() == CustomFieldUtilities.TYPE_DATE) {
120                     buf.append("<input type=\"text\" name=\"customFields(" + field.getId() +")\"");
121                     buf.append((currentValue != null && ! currentValue.equals("") ? " value=\"" + currentValue + "\"" : ""));
122                     buf.append(">&nbsp;");
123                     buf.append("<img onmouseup=\"toggleDatePicker('cf" + field.getId() + "','" + formName + ".customFields(" + field.getId() + ")')\" ");
124                     buf.append("id=cf" + field.getId() + "Pos name=cf" + field.getId() + "Pos width=19 height=19 SRC=\"");
125                     try {
126                         buf.append(RequestUtils.computeURL(pageContext, null, null, "/images/calendar.gif", null, null, null, false));
127                     } catch(MalformedURLException murle) {
128                         buf.append("images/calendar.gif");
129                     }
130                     buf.append("\" align=\"top\" border=\"0\">");
131                     buf.append("<div id=\"cf" + field.getId() + "\" style=\"position:absolute;\"></div>");
132                 } else {
133                     buf.append("<input type=\"text\" name=\"customFields(" + field.getId() +")\"");
134                     buf.append((currentValue != null && ! currentValue.equals("") ? " value=\"" + currentValue + "\"" : ""));
135                     buf.append(">");
136                 }
137             }
138             buf.append("</td>\n");
139
140             ResponseUtils.write(pageContext, buf.toString());
141         }
142
143         clearState();
144         return (EVAL_PAGE);
145     }
146
147     public void release() {
148         super.release();
149         clearState();
150     }
151
152     private void clearState() {
153         field = null;
154         currentValue = null;
155     }
156 }
157
Popular Tags