KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > menu > context > ItemLinkTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.menu.context;
17
18 import com.blandware.atleap.webapp.taglib.core.ParamParent;
19 import com.blandware.atleap.webapp.taglib.core.html.LinkTag;
20 import com.blandware.atleap.webapp.util.core.WebappConstants;
21 import org.apache.commons.validator.GenericValidator;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * <p>Placed inside tag which renders part of context menu item, this tag
29  * renders HTML element, which will act as hyperlink.
30  * <br />
31  * This tag is only valid when nested within <em>itemEnabled</em> or
32  * <em>itemDisabled</em> tag (which must be nested within <em>item</em> tag).
33  * </p>
34  * <p>When content is rendered to page, this tag suppresses rendering of A tag,
35  * IF AND ONLY IF it's a child of <em>itemDisabled</em> tag, else it just calls
36  * super methods (so, usual A tag is rendered). Otherwise, it renders DIV
37  * element that is a part of context menu structure.</p>
38  * <p>
39  * Allowed attributes are:
40  * <ul>
41  * <li>
42  * <b>confirmExpression</b> - javascript expression used to determine, whether
43  * or not to follow link on click. This expression should produce boolean value.
44  * Will be added to 'onclick' event handler. You should not specify 'onclick'
45  * event handler manually for this tag, because its value will be replaced with
46  * our JavaScript code.<br />
47  * Note: this should be expression, not operator (like 'return').
48  * </li>
49  * <li>
50  * <b>highlightedStyleClass</b> - class of highlighted context menu item that
51  * will be generated by this tag
52  * </li>
53  * <li>
54  * <b>disabledStyleClass</b> - class of disabled context menu item that will be
55  * generated by this tag
56  * </li>
57  * <li>
58  * <b>highlightedDisabledStyleClass</b> - class of highlighted disabled context
59  * menu item that will be generated by this tag
60  * </li>
61  * </ul>
62  * Some attributes are inherited from
63  * {@link com.blandware.atleap.webapp.taglib.core.BaseHandlerTag}.
64  * </p>
65  * <p><a HREF="ItemLinkTag.java.htm"><i>View Source</i></a></p>
66  *
67  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
68  * @version $Revision: 1.11 $ $Date: 2005/09/21 13:46:05 $
69  * @see ItemTag
70  * @see ItemLabelTag
71  * @jsp.tag name="itemLink"
72  * body-content="scriptless"
73  */

74 public class ItemLinkTag extends LinkTag implements ParamParent {
75
76     /**
77      * Javascript expression used to determine, whether or not to follow link on click. This expression should produce boolean value.
78      * Will be added to 'onclick' event handler. You should not specify 'onclick' event handler manually for this tag,
79      * because its value will be replaced with our JavaScript code
80      */

81     protected String JavaDoc confirmExpression;
82
83     /**
84      * This is class of highlighted context menu item that will be generated
85      * by this tag
86      */

87     protected String JavaDoc highlightedStyleClass = null;
88
89     /**
90      * This is class of disabled context menu item that will be generated
91      * by this tag
92      */

93     protected String JavaDoc disabledStyleClass = null;
94
95     /**
96      * This is class of highlighted disabled context menu item that will be generated
97      * by this tag
98      */

99     protected String JavaDoc highlightedDisabledStyleClass = null;
100
101     /**
102      * Internal flag, used in overridden methods from LinkTag to determine action to perform
103      */

104     protected int renderingFlag = ItemPart.FLAG_RENDER_TO_PAGE;
105
106     /**
107      * Internal flag, used to store info whether item tag to which this tag
108      * belongs is enabled. For disabled one no links will be generated.
109      */

110     protected boolean isItemEnabled;
111
112     /**
113      * Returns confirm JS expression
114      *
115      * @return confirm expression
116      * @see #confirmExpression
117      * @jsp.attribute required="false"
118      * rtexprvalue="true"
119      * type="java.lang.String"
120      * description="Javascript expression used to determine, whether or not follow link on click"
121      */

122     public String JavaDoc getConfirmExpression() {
123         return confirmExpression;
124     }
125
126     /**
127      * Sets confirm JS expression
128      *
129      * @param confirmExpression confirm expression to set
130      * @see #confirmExpression
131      */

132     public void setConfirmExpression(String JavaDoc confirmExpression) {
133         this.confirmExpression = confirmExpression;
134     }
135
136     /**
137      * Returns class of highlighted context menu item
138      *
139      * @return class of highlighted context menu item
140      * @see #highlightedStyleClass
141      * @jsp.attribute required="false"
142      * rtexprvalue="true"
143      * type="java.lang.String"
144      * description="Class of highlighted context menu item"
145      */

146     public String JavaDoc getHighlightedStyleClass() {
147         return highlightedStyleClass;
148     }
149
150     /**
151      * Sets class of highlighted context menu item
152      *
153      * @param highlightedStyleClass class of highlighted context menu item to set
154      * @see #highlightedStyleClass
155      */

156     public void setHighlightedStyleClass(String JavaDoc highlightedStyleClass) {
157         this.highlightedStyleClass = highlightedStyleClass;
158     }
159
160     /**
161      * Returns class of disabled context menu item
162      *
163      * @return class of disabled context menu item
164      * @see #disabledStyleClass
165      * @jsp.attribute required="false"
166      * rtexprvalue="true"
167      * type="java.lang.String"
168      * description="Class of disabled context menu item"
169      */

170     public String JavaDoc getDisabledStyleClass() {
171         return disabledStyleClass;
172     }
173
174     /**
175      * Sets class of disabled context menu item
176      *
177      * @param disabledStyleClass class to set
178      */

179     public void setDisabledStyleClass(String JavaDoc disabledStyleClass) {
180         this.disabledStyleClass = disabledStyleClass;
181     }
182
183     /**
184      * Returns class of highlighted disabled context menu item
185      *
186      * @return class of highlighted disabled context menu item
187      * @see #highlightedDisabledStyleClass
188      * @jsp.attribute required="false"
189      * rtexprvalue="true"
190      * type="java.lang.String"
191      * description="Class of highlighted disabled context menu item"
192      */

193     public String JavaDoc getHighlightedDisabledStyleClass() {
194         return highlightedDisabledStyleClass;
195     }
196
197     /**
198      * Sets class of highlighted disabled context menu item
199      *
200      * @param highlightedDisabledStyleClass class to set
201      */

202     public void setHighlightedDisabledStyleClass(String JavaDoc highlightedDisabledStyleClass) {
203         this.highlightedDisabledStyleClass = highlightedDisabledStyleClass;
204     }
205
206     /**
207      * Performs tag logic
208      *
209      * @throws JspException
210      */

211     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
212
213         // search for parent ItemPart
214
ItemPart part = (ItemPart) findAncestorWithClass(this, ItemPart.class);
215         if ( part == null ) {
216             throw new JspTagException JavaDoc("This tag is only valid when nested within 'itemEnabled' or 'itemDisabled' tag");
217         }
218
219         ItemTag parentItemTag = (ItemTag) findAncestorWithClass(this, ItemTag.class);
220         if (parentItemTag == null) {
221             throw new JspTagException JavaDoc("This tag is only valid when nested within 'item' tag");
222         }
223         // to prevent appearance of pressable link
224
isItemEnabled = parentItemTag.getEnabled().booleanValue();
225
226         // save flag from parent ItemPart, disable parameters validation and call super method
227
renderingFlag = part.getRenderingFlag();
228         suppressValidation = true;
229         super.doTag();
230     }
231
232     /**
233      * If we're rendering to page, appends opening tag of HTML element, which
234      * renders a hyperlink, to the specified StringBuffer. Otherwise, creates
235      * a div.
236      *
237      * @param element where to write
238      */

239     protected void openElement(StringBuffer JavaDoc element) {
240         if ( renderingFlag == ItemPart.FLAG_RENDER_TO_MENU ) {
241             element.append("<div ");
242         } else {
243             if (isItemEnabled) {
244                 super.openElement(element);
245             }
246         }
247     }
248
249     /**
250      * @see com.blandware.atleap.webapp.taglib.core.html.LinkTag#appendLocation(StringBuffer, String)
251      */

252     protected void appendLocation(StringBuffer JavaDoc element, String JavaDoc location) {
253         if ( renderingFlag == ItemPart.FLAG_RENDER_TO_MENU ) {
254             if (GenericValidator.isBlankOrNull(location)) {
255                 location = "";
256             }
257             element.append("url=\"").append(location).append("\"");
258         } else {
259             if (isItemEnabled) {
260                 if (name == null && action == null && forward == null && href == null) {
261                     href = "#";
262                 }
263                 super.appendLocation(element, location);
264             }
265         }
266     }
267
268     /**
269      * @see com.blandware.atleap.webapp.taglib.core.html.LinkTag#appendAttributes(StringBuffer)
270      */

271     protected void appendAttributes(StringBuffer JavaDoc element) {
272         if ( !GenericValidator.isBlankOrNull(confirmExpression) ) {
273             confirmExpression = "eval('" + confirmExpression + "')";
274         }
275         if ( renderingFlag == ItemPart.FLAG_RENDER_TO_MENU ) {
276             // override JS attributes
277
onclick = "onMenuItemClick(this);";
278             if ( !GenericValidator.isBlankOrNull(confirmExpression) ) {
279                 onclick = "if ( " + confirmExpression + " ) { " + onclick + "} else { return false;}";
280             }
281             if (GenericValidator.isBlankOrNull(styleClass)) {
282                 styleClass = WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS;
283             }
284             if (GenericValidator.isBlankOrNull(highlightedStyleClass)) {
285                 highlightedStyleClass = WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS;
286             }
287             if (GenericValidator.isBlankOrNull(disabledStyleClass)) {
288                 disabledStyleClass = WebappConstants.DISABLED_CONTEXT_MENU_ITEM_STYLE_CLASS;
289             }
290             if (GenericValidator.isBlankOrNull(highlightedDisabledStyleClass)) {
291                 highlightedDisabledStyleClass = WebappConstants.HIGHLIGHTED_DISABLED_CONTEXT_MENU_ITEM_STYLE_CLASS;
292             }
293             if (!isItemEnabled) {
294                 styleClass = disabledStyleClass;
295                 highlightedStyleClass = highlightedDisabledStyleClass;
296             }
297             onmouseover = "activateContextMenuItem(this, '" + highlightedStyleClass + "');";
298             onmouseout = "deactivateContextMenuItem(this, '" + styleClass + "');";
299         } else {
300             onclick = "return " + confirmExpression + ";";
301         }
302         if (renderingFlag == ItemPart.FLAG_RENDER_TO_MENU || isItemEnabled) {
303             super.appendAttributes(element);
304         }
305     }
306
307     /**
308      * @see com.blandware.atleap.webapp.taglib.core.html.LinkTag#closeOpeningTag(StringBuffer)
309      */

310     protected void closeOpeningTag(StringBuffer JavaDoc element) {
311         if ( renderingFlag == ItemPart.FLAG_RENDER_TO_MENU ) {
312             element.append(">");
313         } else {
314             if (isItemEnabled) {
315                 super.closeOpeningTag(element);
316             }
317         }
318     }
319
320     /**
321      * @see com.blandware.atleap.webapp.taglib.core.html.LinkTag#closeElement(StringBuffer)
322      */

323     protected void closeElement(StringBuffer JavaDoc element) {
324         if ( renderingFlag == ItemPart.FLAG_RENDER_TO_MENU ) {
325             element.append("</div>");
326         } else {
327             if (isItemEnabled) {
328                 super.closeElement(element);
329             }
330         }
331     }
332
333 }
334
Popular Tags