KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ContextMenuItem;
19 import org.apache.struts.taglib.TagUtils;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringWriter JavaDoc;
27
28 /**
29  * <p>Wraps item, which will be placed in some ItemContainer. This represents
30  * something that can be rendered as an option (row) in context menu.</p>
31  * <p>
32  * Allowed attributes are:
33  * <ul>
34  * <li>
35  * <b>enabled</b> - whether this menu item is <em>enabled</em>. This specifies
36  * what child tag (<em>itemEnabled</em> or <em>itemDisabled</em>) will be
37  * rendered. Also this affects what style class will be used for context menu
38  * item and whether <em>itemLink</em> tag will generate a hyperlink while
39  * rendering to page (see {@link ContextMenuTag}). This can be "true" or
40  * "false", default is "true".
41  * </li>
42  * <li>
43  * <b>includeInContextMenu</b> - whether to render this item to context menu.
44  * If not, no context menu item corresponding to this one will be displayed in
45  * context menu. Useful when you don't want to include an item to menu if some
46  * condition is met. This can be "true" or "false", default is "true".
47  * </li>
48  * <li>
49  * <b>includeInPage</b> - whether to render this item to page. Useful when you
50  * don't want to include a corresponding link to a page and just want it to be
51  * in context menu. This can be "true" or "false", default is "true".
52  * </li>
53  * </ul>
54  * </p>
55  * <p><a HREF="ItemTag.java.htm"><i>View Source</i></a></p>
56  *
57  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
58  * @version $Revision: 1.6 $ $Date: 2005/09/21 13:46:05 $
59  * @jsp.tag name="item"
60  * body-content="scriptless"
61  * @see ContextMenuTag
62  * @see ItemSetTag
63  * @see ItemEnabledTag
64  * @see ItemDisabledTag
65  */

66 public class ItemTag extends ItemContainerSupport implements ItemContainer {
67
68     /**
69      * Whether or not this item is enabled
70      */

71     protected Boolean JavaDoc enabled = Boolean.TRUE;
72
73     /**
74      * Whether or not to add this item to the generated context menu
75      */

76     protected Boolean JavaDoc includeInContextMenu = Boolean.TRUE;
77
78     /**
79      * Whether or not to include this item to page
80      */

81     protected Boolean JavaDoc includeInPage = Boolean.TRUE;
82
83     /**
84      * Item we're currently creating
85      */

86     protected ContextMenuItem item = null;
87
88     /**
89      * Creates new instance of ItemTag
90      */

91     public ItemTag() {
92     }
93
94     /**
95      * Returns whether or not this item is enabled
96      *
97      * @return whether this item is enabled
98      * @jsp.attribute required="false"
99      * rtexprvalue="true"
100      * type="java.lang.Boolean"
101      * description="Whether or not this item is enabled on current row"
102      */

103     public Boolean JavaDoc getEnabled() {
104         return enabled;
105     }
106
107     /**
108      * Sets whether or not this item is enabled
109      *
110      * @param enabled whether this item is enabled
111      */

112     public void setEnabled(Boolean JavaDoc enabled) {
113         this.enabled = enabled;
114     }
115
116     /**
117      * Returns whether or not to add this item to the generated context menu
118      *
119      * @return whether to add this item to the generated context menu
120      * @jsp.attribute required="false"
121      * rtexprvalue="true"
122      * type="java.lang.Boolean"
123      * description="Whether or not to include this item in context menu"
124      */

125     public Boolean JavaDoc getIncludeInContextMenu() {
126         return includeInContextMenu;
127     }
128
129     /**
130      * Sets whether or not to add this item to the generated context menu
131      *
132      * @param includeInContextMenu whether to add this item to the generated context menu
133      */

134     public void setIncludeInContextMenu(Boolean JavaDoc includeInContextMenu) {
135         this.includeInContextMenu = includeInContextMenu;
136     }
137
138     /**
139      * Returns whether or not to include this item to page
140      *
141      * @return whether to include this item to page
142      * @jsp.attribute required="false"
143      * rtexprvalue="true"
144      * type="java.lang.Boolean"
145      * description="Whether or not to render this item to page"
146      */

147     public Boolean JavaDoc getIncludeInPage() {
148         return includeInPage;
149     }
150
151     /**
152      * Sets whether or not to include this item to page
153      *
154      * @param includeInPage whether to include this item to page
155      */

156     public void setIncludeInPage(Boolean JavaDoc includeInPage) {
157         this.includeInPage = includeInPage;
158     }
159
160
161     /**
162      * Returns item we're currently creating. Used by child tags to set, for example, enabled and disabled parts of item
163      *
164      * @return ContextMenuItem instance
165      */

166     public ContextMenuItem getItem() {
167         return item;
168     }
169
170     /**
171      * Performs tag's logic
172      *
173      * @throws JspException
174      * @throws IOException
175      */

176     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
177
178         // search for parent item container
179
ItemContainer container = (ItemContainer) findAncestorWithClass(this, ItemContainer.class);
180
181         if ( container == null ) {
182             throw new JspTagException JavaDoc("This tag is only valid when nested within some ItemContainer");
183         }
184
185         // ensure that all flags are set correclty
186

187         if ( enabled == null ) {
188             enabled = Boolean.FALSE;
189         }
190
191         if ( includeInContextMenu == null ) {
192             includeInContextMenu = Boolean.TRUE;
193         }
194
195         if ( includeInPage == null ) {
196             includeInPage = Boolean.TRUE;
197         }
198
199         // calculate index
200
Integer JavaDoc index = new Integer JavaDoc(0);
201
202         // search for parent ItemSet tag
203
ItemSetTag itemSetTag = (ItemSetTag) findAncestorWithClass(this, ItemSetTag.class);
204         if ( itemSetTag != null ) {
205             index = itemSetTag.getIndex();
206         }
207
208         // search for parent ItemTag if any
209
ItemTag parentItemTag = (container instanceof ItemTag) ? (ItemTag) container : null;
210
211         // obtain and invoke body content
212
// create new instance of ContextMenuItem
213
item = new ContextMenuItem();
214
215         if ( !includeInContextMenu.booleanValue() || (parentItemTag != null && !parentItemTag.getEnabled().booleanValue()) ) {
216             item.setHidden(Boolean.TRUE);
217         }
218
219         // Get body content to memory to decide whether we need to write it to
220
// page. We can't just skip body because it may contain tags that will
221
// form correct context menu structure.
222
StringWriter JavaDoc writer = new StringWriter JavaDoc();
223         JspFragment JavaDoc body = getJspBody();
224         if ( body != null ) {
225             body.invoke(writer);
226         }
227         if ( includeInPage.booleanValue() ) {
228             TagUtils.getInstance().write((PageContext JavaDoc) getJspContext(), writer.toString());
229         }
230
231         // items have been filled with nested item tags
232
if ( item != null ) {
233             item.setChildItems(menus);
234         }
235
236         // item has been filled with content, child items were also added by nested tags
237
// we can add this item to the enclosing item container
238

239         container.storeItem(index, item);
240
241     }
242
243     /**
244      * Overriden method from ItemContainerSupport. Ignores index, because ItemTag instance is re-created on each
245      * iteration and there is no way to save matrix, which would contain items for all rows. Matrix can be saved
246      * on top-level container only (in our case it is ContextMenuTag). ItemTag can hold vector only,
247      * and that's enough in all cases.
248      *
249      * @see ItemContainer#storeItem(Integer, com.blandware.atleap.webapp.taglib.core.util.ContextMenuItem)
250      */

251     public void storeItem(Integer JavaDoc index, ContextMenuItem item) {
252         menus.add(item);
253     }
254 }
255
Popular Tags