1 package fr.improve.struts.taglib.layout; 2 3 import javax.servlet.jsp.JspException ; 4 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 8 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 9 import fr.improve.struts.taglib.layout.event.LayoutEventListener; 10 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 11 import fr.improve.struts.taglib.layout.field.AbstractModeFieldTag; 12 import fr.improve.struts.taglib.layout.util.FormUtilsInterface; 13 import fr.improve.struts.taglib.layout.util.PanelInterface; 14 import fr.improve.struts.taglib.layout.util.TagUtils; 15 16 29 public class PanelTag extends LabelledTag implements LayoutEventListener { 30 33 private static Log LOG = LogFactory.getLog(PanelTag.class); 34 35 protected static PanelInterface defaultPanel = null; 36 protected PanelInterface panel = null; 37 38 protected int cols = 2; 39 protected String width; 40 protected String height; 41 protected String align = null; public final static String CENTER = "center"; 43 public final static String LEFT = "left"; 44 public final static String RIGHT = "rigth"; 45 46 protected short editMode = 2; 48 protected short createMode = 2; 49 protected short inspectMode = 1; 50 51 protected short fieldDisplayMode; 52 53 protected String arg0Name; 55 protected String arg0Property; 56 57 protected String model; 58 protected String jspStyleClass; 59 60 61 protected void initDynamicValues() { 62 super.initDynamicValues(); 63 jspStyleClass = styleClass; 64 if (styleClass==null){ 65 styleClass = getSkin().getProperty("styleclass.panel",null); 66 } 67 } 68 69 protected void reset() { 70 styleClass = jspStyleClass; 71 jspStyleClass = null; 72 super.reset(); 73 } 74 75 79 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 80 return in_event.consume(pageContext, "<tr>"); 81 } 82 public Object processEndLayoutEvent(EndLayoutEvent in_event) throws JspException { 83 return in_event.consume(pageContext, "</tr>"); 84 } 85 86 protected void computeDisplayMode() { 87 int formMode = getSkin().getFormUtils().getFormDisplayMode(pageContext); 88 switch (formMode) { 89 case FormUtilsInterface.CREATE_MODE : 90 fieldDisplayMode = createMode; 91 break; 92 case FormUtilsInterface.EDIT_MODE : 93 fieldDisplayMode = editMode; 94 break; 95 case FormUtilsInterface.INSPECT_MODE : 96 fieldDisplayMode = inspectMode; 97 break; 98 } 99 } 100 103 protected void doAfterBody(StringBuffer buffer) throws JspException { 104 panel.doAfterBody(buffer); 105 } 106 109 protected void doBeforeBody(StringBuffer buffer) throws JspException { 110 doBeforeBody(buffer, align); 111 } 112 116 protected void doBeforeBody(StringBuffer buffer, String align) throws JspException { 117 panel.doBeforeBody(buffer, align); 118 } 119 123 protected void doEndPanel(StringBuffer buffer) throws JspException { 124 panel.doEndPanel(buffer); 125 } 126 protected void doEndLayout() throws JspException { 127 new EndLayoutEvent(this, "</td>").send(); 128 } 129 public int doEndLayoutTag() throws JspException { 130 if (fieldDisplayMode==AbstractModeFieldTag.MODE_NODISPLAY) return EVAL_PAGE; 131 132 StringBuffer buffer = new StringBuffer (); 133 doAfterBody(buffer); 134 doEndPanel(buffer); 135 TagUtils.write(pageContext, buffer.toString()); 136 doEndLayout(); 137 return EVAL_PAGE; 138 } 139 142 protected void doPrintBlankLine(StringBuffer buffer) throws JspException { 143 panel.doPrintBlankLine(buffer, cols); 144 } 145 150 protected void doPrintTitle(StringBuffer buffer) throws JspException { 151 String l_title = null; 152 if (arg0Name!=null) { 153 Object l_bean = pageContext.findAttribute(arg0Name); 154 if (l_bean!=null) { 155 Object l_value = fr.improve.struts.taglib.layout.util.LayoutUtils.getProperty(l_bean, arg0Property); 156 if (l_value!=null) arg0 = l_value.toString(); 157 } 158 } 159 if (key==null && arg0!=null) l_title = arg0; 160 if (key!=null || name!=null) l_title = getLabel(); 161 panel.doPrintTitle(buffer, l_title); 162 } 163 166 protected void doStartPanel(StringBuffer buffer) throws JspException { 167 doStartPanel(buffer, align); 168 } 169 174 protected void doStartPanel(StringBuffer buffer, String align) throws JspException { 175 init(); 176 panel.init(pageContext, styleClass, this); 177 panel.doStartPanel(buffer, align, width); 178 } 179 protected void doStartLayout() throws JspException { 180 cols = getSkin().getFieldInterface().getColumnNumber(); 181 new StartLayoutEvent(this, "<td valign=\"top\" colspan=\"" + cols + "\">").send(); 182 } 183 public int doStartLayoutTag() throws JspException { 184 computeDisplayMode(); 185 if (fieldDisplayMode==AbstractModeFieldTag.MODE_NODISPLAY) return SKIP_BODY; 186 187 doStartLayout(); 188 StringBuffer buffer = new StringBuffer (); 189 doStartPanel(buffer); 190 doPrintTitle(buffer); 191 doBeforeBody(buffer); 192 TagUtils.write(pageContext,buffer.toString()); 193 return EVAL_BODY_INCLUDE; 194 } 195 200 public java.lang.String getArg0Name() { 201 return arg0Name; 202 } 203 208 public java.lang.String getArg0Property() { 209 return arg0Property; 210 } 211 public PanelInterface getPanelInterface() { 212 return panel; 213 } 214 public void init() { 215 try { 216 defaultPanel = (PanelInterface) getSkin().getPanelClass(model).newInstance(); 217 } catch (Exception e) { 218 LOG.error("Unable to instanciate model " + model,e); 219 defaultPanel = new fr.improve.struts.taglib.layout.util.BasicPanel(); 220 } 221 panel = defaultPanel; 222 } 223 public void release() { 224 super.release(); 225 cols = 2; 226 width= null; 227 height = null; 228 align ="center"; 229 panel = defaultPanel; 230 model = null; 231 232 editMode = 2; 233 createMode = 2; 234 inspectMode = 1; 235 } 236 239 public void setAlign(String align) { 240 this.align = align; 241 } 242 public String getAlign() { 243 return align; 244 } 245 250 public void setArg0Name(java.lang.String newArg0Name) { 251 arg0Name = newArg0Name; 252 } 253 258 public void setArg0Property(java.lang.String newArg0Property) { 259 arg0Property = newArg0Property; 260 } 261 265 public void setCols(int cols) { 266 this.cols = cols; 267 } 268 272 public void setMode(String mode) throws JspException { 273 if (mode==null) return; 275 if (mode.length()!=5) throw new JspException ("fieldTag mode " + mode + " is invalid."); 276 277 char c = mode.charAt(0); 279 createMode = convertMode(c); 280 281 c = mode.charAt(2); 283 editMode = convertMode(c); 284 285 c = mode.charAt(4); 287 inspectMode = convertMode(c); 288 } 289 290 295 private short convertMode(char c) throws JspException { 296 short lc_mode; 297 switch(c) { 298 case 'N': lc_mode=0; break; 299 case 'E': lc_mode=2; break; 300 case 'I': lc_mode=1; break; 301 default: throw new JspException ("panelTag mode " + c + " is invalid. Valid mode are N, E and I"); 302 } 303 return lc_mode; 304 } 305 306 310 public void setWidth(String width) { 311 if (!width.equals("0")) this.width = width; 312 else this.width=null; 313 } 314 public String getWidth() { 315 return width; 316 } 317 321 public String getHeight() { 322 return height; 323 } 324 325 329 public void setHeight(String height) { 330 this.height = height; 331 } 332 333 336 public String getModel() { 337 return model; 338 } 339 342 public void setModel(String model) { 343 this.model = model; 344 } 345 } 346 | Popular Tags |