KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import fr.improve.struts.taglib.layout.el.Expression;
6 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
7 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
8 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
9 import fr.improve.struts.taglib.layout.field.AbstractModeFieldTag;
10 import fr.improve.struts.taglib.layout.policy.AbstractPolicy;
11 import fr.improve.struts.taglib.layout.skin.Skin;
12 import fr.improve.struts.taglib.layout.util.FormUtils;
13 import fr.improve.struts.taglib.layout.util.LayoutUtils;
14 import fr.improve.struts.taglib.layout.util.ParentFinder;
15 import fr.improve.struts.taglib.layout.util.TagUtils;
16
17 /**
18  * Base class for the submit, cancel and image action tags.
19  *
20  * The action is display or not in function of the display mode, which can be N (not display) or D (display)
21  * The action can be nested in a <layout:formActions> tag or a <layout:line> tag.
22  *
23  * @author: Jean-NoŽl Ribette, F. BELLINGARD
24  */

25 public abstract class ActionTag extends BaseHandlerTag implements LayoutEventListener {
26     
27     protected String JavaDoc property;
28     protected boolean display = true;
29     protected String JavaDoc valign;
30     private String JavaDoc mode;
31     protected String JavaDoc policy = null;
32     protected boolean cell = false;
33     
34     protected String JavaDoc jspMode;
35     protected boolean jspDisabled;
36     protected String JavaDoc jspOnclick;
37   
38   public final int doStartTag() throws JspException JavaDoc {
39     ParentFinder.registerTag(pageContext, this);
40     initDynamicValues();
41     return doStartLayoutTag();
42   }
43   
44   public final int doEndTag() throws JspException JavaDoc {
45     try {
46       return doEndLayoutTag();
47     } finally {
48       reset();
49       ParentFinder.deregisterTag(pageContext);
50     }
51   }
52
53     /**
54      * End the tag.
55      */

56     public int doEndLayoutTag() throws JspException JavaDoc {
57         // do nothing if the tag is no displayed
58
if (!display) {
59             display = true;
60             return EVAL_PAGE;
61         }
62         
63         if (cell) {
64             cell = false;
65             return EVAL_PAGE;
66         }
67
68         // end the Struts tag.
69
int ret = tag.doEndTag();
70
71         // end the layout
72
StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc("");
73         endActionLayout(lc_buffer);
74         new EndLayoutEvent(this, lc_buffer.toString()).send();
75         return ret;
76     }
77
78     /**
79      * Start the tag
80      */

81     public int doStartLayoutTag() throws JspException JavaDoc {
82         // if the action is about to be displayed, check the authorization first
83
if (policy != null) {
84             Skin lc_currentSkin = LayoutUtils.getSkin(getPageContext().getSession());
85             AbstractPolicy lc_policy = lc_currentSkin.getPolicy();
86             switch (lc_policy.getAuthorizedDisplayMode(getPolicy(), getReqCode(), getProperty(), getPageContext())) {
87             case AbstractPolicy.MODE_EDIT:
88                 break;
89             case AbstractPolicy.MODE_NODISPLAY:
90                 display = false;
91                 break;
92             case AbstractPolicy.MODE_DISABLED:
93                 display = true;
94                 setDisabled(true);
95                 break;
96             case AbstractPolicy.MODE_CELL:
97                 cell = true;
98                break;
99             default:
100                 throw new IllegalStateException JavaDoc(lc_policy.getClass().getName() + " returns an illegal value");
101             }
102         }
103         
104         // do nothing if the action is not displayed in this mode.
105
if (!display) {
106             return SKIP_BODY;
107         }
108         
109         // just display an empty column if MODE_CELL
110
if (cell) {
111             return doCellMode();
112         }
113         
114         // start the layout.
115
StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc("");
116         beginActionLayout(lc_buffer);
117         new StartLayoutEvent(this, lc_buffer.toString()).send();
118
119         // start the Struts tag.
120
copyProperties();
121         String JavaDoc onclick = null;
122         if (reqCode != null) {
123             onclick = getRequestCode();
124         }
125         if (onclick!=null) {
126         String JavaDoc previousOnclick = getOnclick();
127         if (previousOnclick != null)
128             onclick += ";" + previousOnclick;
129
130         tag.setOnclick(onclick);
131         }
132         return tag.doStartTag();
133     }
134     
135     /**
136      * Display an empty column in cell mode.
137      */

138     protected int doCellMode() throws JspException JavaDoc {
139         new StartLayoutEvent(this, null).send();
140         TagUtils.write(pageContext, "<th colspan=\"");
141         TagUtils.write(pageContext, String.valueOf(LayoutUtils.getSkin(pageContext.getSession()).getFieldInterface().getColumnNumber()));
142         if (getStyleClass()!=null) {
143             TagUtils.write(pageContext, "\" class=\"");
144             TagUtils.write(pageContext, getStyleClass());
145         }
146         TagUtils.write(pageContext, "\">&nbsp;</th>");
147         new EndLayoutEvent(this, null).send();
148         return SKIP_BODY;
149     }
150     
151     
152     /**
153      * Set in wich form modes the action should be displayed or not.
154      * The format form in_mode is X,Y,Z where allowed values are D (Displayed) and N (not displayed) in the same order as the input field tags.
155      */

156     public void setMode(String JavaDoc in_mode) {
157         mode = in_mode;
158     }
159
160     public String JavaDoc getProperty() {
161         return property;
162     }
163     
164     /**
165      * Initialize dynamic values.
166      */

167     protected void initDynamicValues() {
168         // Evaluate mode as an EL.
169
jspMode = mode;
170         mode = Expression.evaluate(mode, pageContext);
171         
172         // Determinate if the action should be displaued or not.
173
jspDisabled = getDisabled();
174         if (mode!=null) {
175             int lc_visible = FormUtils.computeVisibilityMode(pageContext, mode);
176             switch(lc_visible) {
177                 case AbstractModeFieldTag.MODE_EDIT:
178                     display = true;
179                     break;
180                 case AbstractModeFieldTag.MODE_NODISPLAY:
181                     display = false;
182                     break;
183                 case AbstractModeFieldTag.MODE_DISABLED:
184                     display = true;
185                     setDisabled(true);
186                     break;
187                 case AbstractModeFieldTag.MODE_CELL:
188                     cell = true;
189                     break;
190             }
191         }
192         
193         // Evaluate onlick as an EL
194
jspOnclick = getOnclick();
195         setOnclick(Expression.evaluate(jspOnclick, pageContext));
196     }
197     
198     /**
199      * Reset dynamic values.
200      */

201     protected void reset() {
202         mode = jspMode;
203         jspMode = null;
204         setDisabled(jspDisabled);
205         display = true;
206         cell = false;
207         
208         setOnclick(jspOnclick);
209         jspOnclick = null;
210     }
211     
212     public void release() {
213         super.release();
214         tag.release();
215         policy = null;
216         property = null;
217         valign = null;
218         mode = null;
219     }
220
221     /**
222      * Prepare to display the action.
223      */

224     protected void beginActionLayout(StringBuffer JavaDoc in_buffer) throws JspException JavaDoc {
225         in_buffer.append("<td>&nbsp;</td><td");
226         if (valign!=null) {
227             in_buffer.append(" valign=\"");
228             in_buffer.append(valign);
229             in_buffer.append("\"");
230         }
231         in_buffer.append(">");
232     }
233
234     /**
235      * End the display of the action.
236      */

237     protected void endActionLayout(StringBuffer JavaDoc in_buffer) {
238         in_buffer.append("</td>");
239     }
240
241     public void setProperty(String JavaDoc property) {
242         this.property = property;
243     }
244     /**
245      * Sets the valign.
246      * @param valign The valign to set
247      */

248     public void setValign(String JavaDoc valign) {
249         this.valign = valign;
250     }
251
252     /**
253      * @see fr.improve.struts.taglib.layout.event.LayoutEventListener#processEndLayoutEvent(fr.improve.struts.taglib.layout.event.EndLayoutEvent)
254      */

255     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
256         return Boolean.FALSE;
257     }
258
259     /**
260      * @see fr.improve.struts.taglib.layout.event.LayoutEventListener#processStartLayoutEvent(fr.improve.struts.taglib.layout.event.StartLayoutEvent)
261      */

262     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
263         return Boolean.FALSE;
264     }
265     
266     /**
267      * @return Returns the policy.
268      */

269     public String JavaDoc getPolicy()
270     {
271         return policy;
272     }
273     /**
274      * @param policy The policy to set.
275      */

276     public void setPolicy(String JavaDoc policy)
277     {
278         this.policy = policy;
279     }
280     
281     protected void copyProperties() throws JspException JavaDoc {
282         LayoutUtils.copyProperties(tag, this);
283     }
284
285 }
Popular Tags