KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import fr.improve.struts.taglib.layout.event.ComputeLayoutSpanEvent;
6 import fr.improve.struts.taglib.layout.event.ComputeLayoutSpanEventListener;
7 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
8 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
9 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
10 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent;
11 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeListener;
12 import fr.improve.struts.taglib.layout.util.LayoutUtils;
13 import fr.improve.struts.taglib.layout.util.TagUtils;
14
15 /**
16  * @author jnribette
17  */

18 public class GridLayoutTag extends LayoutTagSupport implements LayoutEventListener, ComputeLayoutSpanEventListener, StaticCodeIncludeListener {
19     protected String JavaDoc cols = "2";
20     
21     protected boolean space = true;
22     
23     protected String JavaDoc styleClass;
24     protected String JavaDoc width;
25     protected String JavaDoc align;
26     
27     /**
28      * IE does not allows to set this property with css :(
29      */

30     protected int borderSpacing = 0;
31         
32     protected String JavaDoc id;
33     protected String JavaDoc height;
34     
35     protected int currentCols = 1;
36     protected int currentSpan = 1;
37     protected String JavaDoc jspStyleClass;
38     
39     protected StringBuffer JavaDoc staticCode = new StringBuffer JavaDoc();
40     
41     protected void initDynamicValues() {
42         jspStyleClass = styleClass;
43         if (styleClass==null){
44             styleClass = LayoutUtils.getSkin(pageContext.getSession()).getProperty("styleclass.grid",null);
45         }
46     }
47     protected void reset() {
48         styleClass = jspStyleClass;
49         jspStyleClass = null;
50         staticCode = new StringBuffer JavaDoc();
51     }
52     
53     public int doStartLayoutTag() throws JspException JavaDoc {
54         int lc_fieldColumnNumber = LayoutUtils.getSkin(pageContext.getSession()).getFieldInterface().getColumnNumber();
55         new StartLayoutEvent(this, "<td colspan=\"" + lc_fieldColumnNumber + "\">").send();
56         
57         if (id!=null || height!=null) {
58             TagUtils.write(pageContext, "<div style=\"");
59             if (height!=null) {
60                 TagUtils.write(pageContext, "height:");
61                 TagUtils.write(pageContext, height);
62                 TagUtils.write(pageContext, ";");
63             }
64             TagUtils.write(pageContext,"overflow-y:scroll;overflow:-moz-scrollbars-vertical;");
65             if (id!=null) {
66                 TagUtils.write(pageContext, "\" id=\"");
67                 TagUtils.write(pageContext, id);
68             }
69             TagUtils.write(pageContext, "\">");
70         }
71         
72         TagUtils.write(pageContext, "<table border=\"0\" cellspacing=\"");
73         TagUtils.write(pageContext, String.valueOf(borderSpacing));
74         TagUtils.write(pageContext,"\" cellpadding=\"0\"");
75         if (styleClass!=null) {
76             TagUtils.write(pageContext, " class=\"");
77             TagUtils.write(pageContext, styleClass);
78             TagUtils.write(pageContext, "\"");
79         }
80         if (width!=null) {
81             TagUtils.write(pageContext, " width=\"");
82             TagUtils.write(pageContext, width);
83             TagUtils.write(pageContext, "\"");
84         }
85         if (align!=null) {
86             TagUtils.write(pageContext, " align=\"");
87             TagUtils.write(pageContext, align);
88             TagUtils.write(pageContext, "\"");
89         }
90         TagUtils.write(pageContext,"><tr>");
91         return EVAL_BODY_INCLUDE;
92     }
93     public int doEndLayoutTag() throws JspException JavaDoc {
94         currentCols = 1;
95         TagUtils.write(pageContext, "</tr></table>");
96         
97         if (id!=null || height!=null) {
98             TagUtils.write(pageContext, "</div>");
99         }
100         if (staticCode.length()!=0) {
101             TagUtils.write(pageContext, staticCode.toString());
102         }
103         
104         new EndLayoutEvent(this, "</td>").send();
105         return EVAL_PAGE;
106     }
107     public void release() {
108         super.release();
109         cols = "2";
110         space = true;
111         styleClass = null;
112         height = null;
113         id = null;
114         width = null;
115         align = null;
116         borderSpacing = 0;
117     }
118     
119     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
120         if (currentCols > Integer.parseInt(cols)) {
121             TagUtils.write(pageContext, "</tr><tr>");
122             currentCols = 1;
123         }
124         currentCols += currentSpan;
125         currentSpan = 1;
126         return in_event.consume(pageContext, "");
127     }
128     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
129         return in_event.consume(pageContext, space ? "<td>&nbsp;</td>" : "");
130     }
131     
132     public Integer JavaDoc computeColspan(ComputeLayoutSpanEvent in_event) throws JspException JavaDoc {
133         currentSpan = in_event.getColspan();
134         if (space) {
135             // Get default colspan.
136
int lc_value = ((Integer JavaDoc)in_event.consume(this)).intValue();
137             
138             // Add an extra cell for the space after each component.
139
lc_value += in_event.getColspan();
140             
141             // But we add the last one ourself.
142
lc_value -= 1;
143             
144             // Return.
145
return new Integer JavaDoc(lc_value);
146         } else {
147             // We don't alter the number of td.
148
return (Integer JavaDoc) in_event.consume(this);
149         }
150         
151     }
152     
153     /**
154      * Sets the cols.
155      * @param cols The cols to set
156      */

157     public void setCols(String JavaDoc cols) {
158         this.cols = cols;
159     }
160
161     /**
162      * Returns the space.
163      * @return boolean
164      */

165     public boolean isSpace() {
166         return space;
167     }
168
169     /**
170      * Sets the space.
171      * @param space The space to set
172      */

173     public void setSpace(boolean space) {
174         this.space = space;
175     }
176
177     /**
178      * Sets the styleClass.
179      * @param styleClass The styleClass to set
180      */

181     public void setStyleClass(String JavaDoc styleClass) {
182         this.styleClass = styleClass;
183     }
184
185     /**
186      * Sets the height.
187      * @param height The height to set
188      */

189     public void setHeight(String JavaDoc height) {
190         this.height = height;
191     }
192
193     /**
194      * Sets the id.
195      * @param id The id to set
196      */

197     public void setId(String JavaDoc id) {
198         this.id = id;
199     }
200
201     public void setWidth(String JavaDoc in_string) {
202         width = in_string;
203     }
204     
205     public void setBorderSpacing(int in_spacing) {
206         borderSpacing = in_spacing;
207     }
208
209     public String JavaDoc getAlign() {
210         return align;
211     }
212     
213     public void setAlign(String JavaDoc align) {
214         this.align = align;
215     }
216     
217     /**
218      * @see fr.improve.struts.taglib.layout.event.StaticCodeIncludeListener#processStaticCodeIncludeEvent(fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent)
219      */

220     public Object JavaDoc processStaticCodeIncludeEvent(StaticCodeIncludeLayoutEvent in_event) throws JspException JavaDoc {
221         String JavaDoc lc_value = (String JavaDoc) in_event.sendToParent(this);
222         if (height==null && id==null) {
223             return lc_value;
224         } else {
225             staticCode.append(lc_value);
226             return "";
227         }
228     }
229 }
230
Popular Tags