1 7 package fr.improve.struts.taglib.layout.datagrid; 8 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import javax.servlet.jsp.JspException ; 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 27 public class DatagridTag extends CollectionTag { 28 public static final String LOADED = "fr.improve.struts.taglib.layout.datagrid.DatagridTag.LOADED"; 29 30 protected String jspOnRowClick; 31 protected String jspStyleId; 32 protected DatagridImpl datagrid; 33 protected StringBuffer initCode = new StringBuffer (1000); 34 35 protected boolean allowSelection = true; 36 protected boolean allowMultipleSelection = true; 37 38 public int doStartLayoutTag() throws JspException { 39 loadScript(); 40 41 jspOnRowClick = onRowClick; 43 if (allowSelection) { 44 onRowClick = "strutsLayoutDatagridData['" + property + "'].selectDatagridLine(this)"; 45 } 46 47 jspStyleId = styleId; 49 if (styleId==null) { 50 styleId = property + "JsId"; 51 } 52 53 datagrid = LayoutUtils.getDatagrid(pageContext, name, property); 55 56 int ret = super.doStartLayoutTag(); 58 59 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 Iterator lc_it = lc_renderer.getRowStyleClassMap().entrySet().iterator(); 80 while (lc_it.hasNext()) { 81 Map.Entry lc_entry = (Map.Entry ) 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 99 private void checkRenderer() throws JspException { 100 if (!(panel instanceof IDatagridRenderer)) { 101 throw new JspException ("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 109 protected void loadScript() throws JspException { 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 128 public void addItem(StringBuffer in_buffer, ItemContext in_context) throws JspException { 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 141 protected void defineColumn(String in_property, ColumnType in_type) throws JspException { 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 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 datagrid.addBooleanProperty(in_property); 179 } 180 } 181 182 public int doEndLayoutTag() throws JspException { 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 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 |