KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 9 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 import java.util.Map JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13
14 import org.apache.struts.util.ResponseUtils;
15
16 import fr.improve.struts.taglib.layout.collection.CollectionTag;
17 import fr.improve.struts.taglib.layout.collection.ItemContext;
18 import fr.improve.struts.taglib.layout.util.IDatagridRenderer;
19 import fr.improve.struts.taglib.layout.util.LayoutUtils;
20 import fr.improve.struts.taglib.layout.util.TagUtils;
21
22 /**
23  * The datagrid tag is a special collection tag.
24  *
25  * @author jnribette
26  */

27 public class DatagridTag extends CollectionTag {
28     public static final String JavaDoc LOADED = "fr.improve.struts.taglib.layout.datagrid.DatagridTag.LOADED";
29     
30     protected String JavaDoc jspOnRowClick;
31     protected String JavaDoc jspStyleId;
32     protected DatagridImpl datagrid;
33     protected StringBuffer JavaDoc initCode = new StringBuffer JavaDoc(1000);
34         
35     protected boolean allowSelection = true;
36     protected boolean allowMultipleSelection = true;
37     
38     public int doStartLayoutTag() throws JspException JavaDoc {
39         loadScript();
40         
41         // Set the onRowClick attribute.
42
jspOnRowClick = onRowClick;
43         if (allowSelection) {
44             onRowClick = "strutsLayoutDatagridData['" + property + "'].selectDatagridLine(this)";
45         }
46         
47         // Set the styleId attribute if not set.
48
jspStyleId = styleId;
49         if (styleId==null) {
50             styleId = property + "JsId";
51         }
52         
53         // Get the datagrid object.
54
datagrid = LayoutUtils.getDatagrid(pageContext, name, property);
55         
56         // Start the tag.
57
int ret = super.doStartLayoutTag();
58         
59         // Define the JS datagrid object.
60
checkRenderer();
61         IDatagridRenderer lc_renderer = (IDatagridRenderer) panel;
62         TagUtils.write(pageContext, "<script>strutsLayoutDatagridData['");
63         TagUtils.write(pageContext, property);
64         TagUtils.write(pageContext, "'] = new StrutsLayout.Datagrid('");
65         TagUtils.write(pageContext, property);
66         TagUtils.write(pageContext, "', '");
67         TagUtils.write(pageContext, styleId);
68         TagUtils.write(pageContext, "', '");
69         TagUtils.write(pageContext, lc_renderer.getRowStyleClass());
70         TagUtils.write(pageContext, "', '");
71         TagUtils.write(pageContext, lc_renderer.getRowStyleClass2());
72         TagUtils.write(pageContext, "', ");
73         TagUtils.write(pageContext, String.valueOf(allowSelection));
74         TagUtils.write(pageContext, ", ");
75         TagUtils.write(pageContext, String.valueOf(allowMultipleSelection));
76         TagUtils.write(pageContext, ");\n");
77         
78         // Define the styleClass.
79
Iterator JavaDoc lc_it = lc_renderer.getRowStyleClassMap().entrySet().iterator();
80         while (lc_it.hasNext()) {
81             Map.Entry JavaDoc lc_entry = (Map.Entry JavaDoc) lc_it.next();
82             TagUtils.write(pageContext, "strutsLayoutDatagridData['");
83             TagUtils.write(pageContext, property);
84             TagUtils.write(pageContext, "'].addStyleClass('");
85             TagUtils.write(pageContext, lc_entry.getKey().toString());
86             TagUtils.write(pageContext, "', '");
87             TagUtils.write(pageContext, lc_entry.getValue().toString());
88             TagUtils.write(pageContext,"');");
89             TagUtils.write(pageContext, "\n");
90         }
91         TagUtils.write(pageContext, "</script>\n");
92         
93         return ret;
94     }
95
96     /**
97      * Check the renderer is an instaneof IDatagridRenderer.
98      */

99     private void checkRenderer() throws JspException JavaDoc {
100         if (!(panel instanceof IDatagridRenderer)) {
101             throw new JspException JavaDoc("Bad use of <layout:datagrid> : the renderer " + panel.getClass().getName() + " does not implement IDatagridRenderer. Please use the model attribute and/or configure Struts-Layout properly");
102         }
103         
104     }
105
106     /**
107      * Load the required Javascript code.
108      */

109     protected void loadScript() throws JspException JavaDoc {
110         if (pageContext.getRequest().getAttribute(LOADED)==null) {
111             TagUtils.write(pageContext, "<script SRC=\"");
112             TagUtils.write(pageContext, LayoutUtils.getSkin(pageContext.getSession()).getConfigDirectory(pageContext.getRequest()));
113             TagUtils.write(pageContext, "/datagrid.js\"></script>");
114             pageContext.getRequest().setAttribute(LOADED, "");
115         }
116     }
117
118     public void reset() {
119         super.reset();
120         onRowClick = jspOnRowClick;
121         styleId = jspStyleId;
122         datagrid = null;
123     }
124     
125     /**
126      * Override addItem in BaseCollectionTag to also define the columns in Javascript.
127      */

128     public void addItem(StringBuffer JavaDoc in_buffer, ItemContext in_context) throws JspException JavaDoc {
129         
130         if (isFirst()) {
131             defineColumn(in_context.getProperty(), ((DatagridItemContext)in_context).getColumnType());
132         } else if (getColumn()==0) {
133             renderLineState();
134         }
135         super.addItem(in_buffer, in_context);
136     }
137
138     /**
139      * Define a column in Javascript to make it possible to add columns in DHTML.
140      */

141     protected void defineColumn(String JavaDoc in_property, ColumnType in_type) throws JspException JavaDoc {
142         IDatagridRenderer lc_renderer = (IDatagridRenderer) panel;
143         TagUtils.write(pageContext, "<script>");
144         TagUtils.write(pageContext, "strutsLayoutDatagridData['");
145         TagUtils.write(pageContext, property);
146         TagUtils.write(pageContext, "'].addColumn('");
147         TagUtils.write(pageContext, in_property);
148         TagUtils.write(pageContext, "', '");
149         TagUtils.write(pageContext, lc_renderer.getColumnStyleClass(getColumn()));
150         TagUtils.write(pageContext, "', '");
151         TagUtils.write(pageContext, in_type.getType());
152         TagUtils.write(pageContext, "'");
153         if (ColumnType.SELECT.equals(in_type.getType())) {
154             TagUtils.write(pageContext,", [");
155             Iterator JavaDoc it = in_type.getValues().iterator();
156             while (it.hasNext()) {
157                 DatagridSelectTag.DatagridChoice choice = (DatagridSelectTag.DatagridChoice) it.next();
158                 TagUtils.write(pageContext, "new StrutsLayout.Option('");
159                 TagUtils.write(pageContext, ResponseUtils.filter(choice.label));
160                 TagUtils.write(pageContext, "','");
161                 TagUtils.write(pageContext, choice.value);
162                 TagUtils.write(pageContext, "')");
163                 if (it.hasNext()) {
164                     TagUtils.write(pageContext, ",");
165                 }
166             }
167             TagUtils.write(pageContext, "]");
168         } else if (ColumnType.EMPTY.equals(in_type.getType())) {
169             TagUtils.write(pageContext,", '");
170             TagUtils.write(pageContext,in_type.getJavascript());
171             TagUtils.write(pageContext,"'");
172         }
173         TagUtils.write(pageContext,");");
174         TagUtils.write(pageContext, "</script>\n");
175         
176         if (ColumnType.CHECKBOX.equals(in_type)) {
177             // Need to reset all the checkbox.
178
datagrid.addBooleanProperty(in_property);
179         }
180     }
181     
182     public int doEndLayoutTag() throws JspException JavaDoc {
183         int lc_ret = super.doEndLayoutTag();
184         TagUtils.write(pageContext, "<script>\n");
185         TagUtils.write(pageContext, initCode.toString());
186         TagUtils.write(pageContext, "</script>\n");
187         initCode.setLength(0);
188         return lc_ret;
189     }
190     
191     protected void renderLineState() {
192         String JavaDoc lc_state = datagrid.getObjectState(getIndex());
193         initCode.append("strutsLayoutDatagridData['");
194         initCode.append(property);
195         initCode.append("'].initState(");
196         initCode.append(getIndex());
197         initCode.append(",'");
198         initCode.append(lc_state);
199         initCode.append("');\n");
200     }
201     
202     public void release() {
203         super.release();
204         allowSelection = true;
205         allowMultipleSelection = true;
206     }
207     
208     public void setSelectionAllowed(boolean in_allowSelection) {
209         allowSelection = in_allowSelection;
210     }
211     public boolean isSelectionAllowed() {
212         return allowSelection;
213     }
214     public void setMultipleSelectionAllowed(boolean in_multipleSelectionAllowed) {
215         allowMultipleSelection = in_multipleSelectionAllowed;
216     }
217     public boolean isMultipleSelectionAllowed() {
218         return allowMultipleSelection;
219     }
220 }
221
Popular Tags