KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > datagrid > DatagridSelectTag


1 /*
2  * Created on 13 avr. 2004
3  *
4  * Copyright Improve SA 2004.
5  * All rights reserved.
6  */

7 package fr.improve.struts.taglib.layout.datagrid;
8
9 import java.util.Iterator JavaDoc;
10
11 import javax.servlet.jsp.JspException JavaDoc;
12
13 import org.apache.struts.util.ResponseUtils;
14
15 import fr.improve.struts.taglib.layout.collection.SimpleItemContext;
16 import fr.improve.struts.taglib.layout.field.AbstractFieldTag;
17 import fr.improve.struts.taglib.layout.field.Choice;
18 import fr.improve.struts.taglib.layout.field.ChoiceTag;
19
20 /**
21  * Simplified collectionInput tag for the datagrid : only the property and key attributes are needed.
22  * @author jnribette
23  */

24 public class DatagridSelectTag extends AbstractDatagridColumnTag implements ChoiceTag {
25     /**
26      * Simple bean to hold a select option.
27      */

28     static class DatagridChoice {
29         private DatagridChoice(Choice in_choice) {
30             label = in_choice.getChoiceLabel();
31             value= in_choice.getChoiceValue();
32         }
33         String JavaDoc label;
34         String JavaDoc value;
35     }
36     
37     /**
38      * Return the item context associated with the datagrod column
39      * Here, a DatagridItemContext of type select.
40      */

41     protected SimpleItemContext createItemContext() {
42         DatagridItemContext lc_context = new DatagridItemContext();
43         lc_context.setColumnType(ColumnType.select());
44         return lc_context;
45     }
46
47     /**
48      * Deprecated method from the ChoiceTag interface.
49      * @deprecated
50      */

51     public void addChoice(StringBuffer JavaDoc sb, String JavaDoc value, String JavaDoc label) throws JspException JavaDoc {
52         // Deprecated method from the ChoiceTag interface.
53

54     }
55
56     /**
57      * Add an option value (method fro mthe ChoiceTag interface.
58      */

59     public void addChoice(StringBuffer JavaDoc in_buffer, Choice in_choice) throws JspException JavaDoc {
60         ((DatagridItemContext)context).getColumnType().getValues().add(new DatagridChoice(in_choice));
61     }
62
63     /**
64      * Add the option tags after the select tag.
65      */

66     protected String JavaDoc doAfterValue() throws JspException JavaDoc {
67         // Create StringBuffer.
68
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
69         
70         // Iterate in each option.
71
Iterator JavaDoc it = ((DatagridItemContext)context).getColumnType().getValues().iterator();
72         Object JavaDoc fieldValue = buildInputFieldValue(parentCollectionTag, false);
73         while (it.hasNext()) {
74             DatagridChoice choice = (DatagridChoice) it.next();
75             if (fieldDisplayMode!=AbstractFieldTag.MODE_INSPECT) {
76                 // Editable mode.
77
appendEditableOption(buffer, fieldValue, choice);
78             } else {
79                 // Inspect mode.
80
appendInspectOption(buffer, fieldValue, choice);
81             }
82         }
83         
84         // Close select tag.
85
if (fieldDisplayMode!=AbstractFieldTag.MODE_INSPECT) {
86             buffer.append("</select>\n");
87         }
88         return buffer.toString();
89     }
90     
91     /**
92      * Add an option in edit mode.
93      */

94     protected void appendEditableOption(StringBuffer JavaDoc in_buffer, Object JavaDoc fieldValue, DatagridChoice choice) {
95         in_buffer.append("<option value=\"");
96         in_buffer.append(choice.value);
97         in_buffer.append("\"");
98         if (fieldValue!=null && fieldValue.toString().equals(choice.value)) {
99             in_buffer.append(" selected");
100         }
101         in_buffer.append(">");
102         in_buffer.append(ResponseUtils.filter(choice.label));
103         in_buffer.append("</option>\n");
104     }
105     
106     /**
107      * Add an option in non edit mode.
108      */

109     protected void appendInspectOption(StringBuffer JavaDoc in_buffer, Object JavaDoc fieldValue, DatagridChoice choice) {
110         if (fieldValue!=null && fieldValue.toString().equals(choice.value)) {
111             in_buffer.append(ResponseUtils.filter(choice.label));
112         }
113     }
114
115     /**
116      * The tag should start with &lt;select and not &lt;input
117      */

118     protected void appendFieldStart(StringBuffer JavaDoc in_buffer) {
119         in_buffer.append("<select");
120     }
121
122     /**
123      * Do not append the field value in the select tag.
124      */

125     protected void appendFieldValue(StringBuffer JavaDoc in_buffer, Object JavaDoc in_value) {
126         // Do nothing.
127
}
128     
129     /**
130      * Do not append the field value in inspect mode.
131      * (the value is the HTML option value,
132      * not the one that is displayed to the user.
133      */

134     protected void appendInspectFieldValue(StringBuffer JavaDoc in_buffer, Object JavaDoc in_value) {
135         // Do nothing.
136
}
137
138     protected int evaluateFirstBody() {
139         return EVAL_BODY_INCLUDE;
140     }
141
142     protected int evaluateNextBody() {
143         return EVAL_BODY_INCLUDE;
144     }
145
146     protected void reset() {
147         ((DatagridItemContext)context).getColumnType().getValues().clear();
148         super.reset();
149     }
150 }
151
Popular Tags