KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > PanelTag


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.jsp.JspException JavaDoc;
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 /**
17  * Render a panel element
18  * This tag can be used as is or subclassed to insert a specific content.
19  * The body/content of the panel must be the content of a table element (ie begin with a <tr> element)
20  *
21  * Method calls are:<br>
22  * <li>doStartPanel()</li>
23  * <li>doPrintTitle()</li>
24  * <li>doBeforeBody()</li>
25  * <li> - output of the tag content - </li>
26  * <li>doAfterBody()</li>
27  * @author: Jean-Noël Ribette
28  */

29 public class PanelTag extends LabelledTag implements LayoutEventListener {
30     /**
31      * Log object.
32      */

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 JavaDoc width;
40     protected String JavaDoc height;
41     protected String JavaDoc align = null; // "center";
42
public final static String JavaDoc CENTER = "center";
43     public final static String JavaDoc LEFT = "left";
44     public final static String JavaDoc RIGHT = "rigth";
45
46     // display modes
47
protected short editMode = 2;
48     protected short createMode = 2;
49     protected short inspectMode = 1;
50
51     protected short fieldDisplayMode;
52
53     // dynamic labels
54
protected String JavaDoc arg0Name;
55     protected String JavaDoc arg0Property;
56     
57     protected String JavaDoc model;
58     protected String JavaDoc 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     /**
76      * Process a StartLayoutEvent.
77      * @return the type of layout tag.
78      */

79     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
80         return in_event.consume(pageContext, "<tr>");
81     }
82     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
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 /**
101  * End the body of the panel.
102  */

103 protected void doAfterBody(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
104     panel.doAfterBody(buffer);
105 }
106 /**
107  * Prepare to print HTML content in the body of the panel.
108  */

109 protected void doBeforeBody(StringBuffer JavaDoc buffer) throws JspException JavaDoc{
110     doBeforeBody(buffer, align);
111 }
112 /**
113  * Prepare to print content in the body of the panel.
114  * @param align center/left/right
115  */

116 protected void doBeforeBody(StringBuffer JavaDoc buffer, String JavaDoc align) throws JspException JavaDoc {
117     panel.doBeforeBody(buffer, align);
118 }
119 /**
120  * Print the end of the panel in the buffer.
121  * Deleguate the call to the panel instance
122  */

123 protected void doEndPanel(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
124     panel.doEndPanel(buffer);
125 }
126 protected void doEndLayout() throws JspException JavaDoc {
127     new EndLayoutEvent(this, "</td>").send();
128 }
129 public int doEndLayoutTag() throws JspException JavaDoc {
130     if (fieldDisplayMode==AbstractModeFieldTag.MODE_NODISPLAY) return EVAL_PAGE;
131     
132     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
133     doAfterBody(buffer);
134     doEndPanel(buffer);
135     TagUtils.write(pageContext, buffer.toString());
136     doEndLayout();
137     return EVAL_PAGE;
138 }
139 /**
140  * Insert a blank line in the body of the panel
141  */

142 protected void doPrintBlankLine(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
143     panel.doPrintBlankLine(buffer, cols);
144 }
145 /**
146  * Display the title of the panel, using the key attribute.<br>
147  * A call to this method must be done after doStartPanel() and doBeforeBody()
148  * The call is delaguated to the panel instance.
149  */

150 protected void doPrintTitle(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
151     String JavaDoc l_title = null;
152     if (arg0Name!=null) {
153         Object JavaDoc l_bean = pageContext.findAttribute(arg0Name);
154         if (l_bean!=null) {
155             Object JavaDoc 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 /**
164  * Start to display a panel with the default alignment.
165  */

166 protected void doStartPanel(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
167     doStartPanel(buffer, align);
168 }
169 /**
170  * Start to display a panel with the specified alignment.
171  * Deleguate the call to the panel instance.
172  * @param align left/center/right
173  */

174 protected void doStartPanel(StringBuffer JavaDoc buffer, String JavaDoc align) throws JspException JavaDoc {
175     init();
176     panel.init(pageContext, styleClass, this);
177     panel.doStartPanel(buffer, align, width);
178 }
179 protected void doStartLayout() throws JspException JavaDoc {
180     cols = getSkin().getFieldInterface().getColumnNumber();
181     new StartLayoutEvent(this, "<td valign=\"top\" colspan=\"" + cols + "\">").send();
182 }
183 public int doStartLayoutTag() throws JspException JavaDoc {
184     computeDisplayMode();
185     if (fieldDisplayMode==AbstractModeFieldTag.MODE_NODISPLAY) return SKIP_BODY;
186     
187     doStartLayout();
188     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
189     doStartPanel(buffer);
190     doPrintTitle(buffer);
191     doBeforeBody(buffer);
192     TagUtils.write(pageContext,buffer.toString());
193     return EVAL_BODY_INCLUDE;
194 }
195 /**
196  * Insert the method's description here.
197  * Creation date: (26/09/2001 11:33:21)
198  * @return java.lang.String
199  */

200 public java.lang.String JavaDoc getArg0Name() {
201     return arg0Name;
202 }
203 /**
204  * Insert the method's description here.
205  * Creation date: (26/09/2001 11:33:21)
206  * @return java.lang.String
207  */

208 public java.lang.String JavaDoc 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 JavaDoc 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 /**
237  * Set the alignment of the panel (left, center or right)
238  */

239 public void setAlign(String JavaDoc align) {
240     this.align = align;
241 }
242 public String JavaDoc getAlign() {
243     return align;
244 }
245 /**
246  * Insert the method's description here.
247  * Creation date: (26/09/2001 11:33:21)
248  * @param newArg0Name java.lang.String
249  */

250 public void setArg0Name(java.lang.String JavaDoc newArg0Name) {
251     arg0Name = newArg0Name;
252 }
253 /**
254  * Insert the method's description here.
255  * Creation date: (26/09/2001 11:33:21)
256  * @param newArg0Property java.lang.String
257  */

258 public void setArg0Property(java.lang.String JavaDoc newArg0Property) {
259     arg0Property = newArg0Property;
260 }
261 /**
262  * Set the numbers of columns<br>
263  * Use when displaying the title and blank lines.
264  */

265 public void setCols(int cols) {
266     this.cols = cols;
267 }
268     /**
269      * Set the display mode
270      * format is XX,XX,XX where XX can be N (not displayed), E (editable), I (inspectable). Order is create mode, edit mode, inspect mode
271      */

272     public void setMode(String JavaDoc mode) throws JspException JavaDoc {
273         // Do some basic tests.
274
if (mode==null) return;
275         if (mode.length()!=5) throw new JspException JavaDoc("fieldTag mode " + mode + " is invalid.");
276         
277         // Compute the create mode.
278
char c = mode.charAt(0);
279         createMode = convertMode(c);
280         
281         //Compute the edit mode.
282
c = mode.charAt(2);
283         editMode = convertMode(c);
284         
285         // Compute the inspect mode.
286
c = mode.charAt(4);
287         inspectMode = convertMode(c);
288     }
289
290     /**
291      * Convert display mode from char to short.
292      * @param c
293      * @throws JspException
294      */

295     private short convertMode(char c) throws JspException JavaDoc {
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 JavaDoc("panelTag mode " + c + " is invalid. Valid mode are N, E and I");
302         }
303         return lc_mode;
304     }
305     
306 /**
307  * with of the panel tables
308  * default: 100%
309  */

310 public void setWidth(String JavaDoc width) {
311     if (!width.equals("0")) this.width = width;
312     else this.width=null;
313 }
314 public String JavaDoc getWidth() {
315     return width;
316 }
317     /**
318      * Returns the height.
319      * @return String
320      */

321     public String JavaDoc getHeight() {
322         return height;
323     }
324
325     /**
326      * Sets the height.
327      * @param height The height to set
328      */

329     public void setHeight(String JavaDoc height) {
330         this.height = height;
331     }
332
333     /**
334      * @return Returns the model.
335      */

336     public String JavaDoc getModel() {
337         return model;
338     }
339     /**
340      * @param model The model to set.
341      */

342     public void setModel(String JavaDoc model) {
343         this.model = model;
344     }
345 }
346
Popular Tags