KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > util > ContextMenuItem


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.util;
17
18 import com.blandware.atleap.common.util.StringUtil;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * <p>Describes item in context menu</p>
26  * <p><a HREF="ContextMenuItem.java.htm"><i>View Source</i></a></p>
27  *
28  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
29  * @version $Revision: 1.6 $ $Date: 2005/08/11 14:34:45 $
30  */

31 public class ContextMenuItem implements Serializable JavaDoc {
32
33     /**
34      * ID of this menu item to use on JSP
35      */

36     protected String JavaDoc id = null;
37
38     /**
39      * Content of item which will be rendered on JSP page
40      */

41     protected String JavaDoc content;
42
43     /**
44      * Whether this menu item is hidden
45      */

46     protected Boolean JavaDoc hidden = Boolean.FALSE;
47
48     /**
49      * Reference to parent item
50      */

51     protected ContextMenuItem parentItem = null;
52
53     /**
54      * List of child items
55      */

56     protected List JavaDoc childItems = new ArrayList JavaDoc();
57
58     /**
59      * Creates new instance of MenuItem
60      */

61     public ContextMenuItem() {
62         hidden = Boolean.FALSE;
63     }
64
65     /**
66      * Creates new instance of ContextMenuItem and sets its content
67      *
68      * @param content Content to set
69      */

70     public ContextMenuItem(String JavaDoc content) {
71         this.content = content;
72     }
73
74     /**
75      * Returns context menu item content
76      *
77      * @return content
78      */

79     public String JavaDoc getContent() {
80         return content;
81     }
82
83     /**
84      * Sets context menu item content
85      *
86      * @param content content to set
87      */

88     public void setContent(String JavaDoc content) {
89         this.content = content;
90     }
91
92     /**
93      * Returns whether this menu item is hidden
94      *
95      * @return whether this menu item is hidden
96      */

97     public Boolean JavaDoc getHidden() {
98         return hidden;
99     }
100
101     /**
102      * Sets whether this menu item is hidden
103      *
104      * @param hidden whether this menu item is hidden
105      */

106     public void setHidden(Boolean JavaDoc hidden) {
107         this.hidden = hidden;
108     }
109
110     /**
111      * Returns parent item of this one
112      *
113      * @return Parent item
114      */

115     public ContextMenuItem getParentItem() {
116         return parentItem;
117     }
118
119     /**
120      * Sets parent item for this one
121      *
122      * @param parentItem Parent item to set
123      */

124     public void setParentItem(ContextMenuItem parentItem) {
125         this.parentItem = parentItem;
126     }
127
128     /**
129      * Returns list of child items
130      *
131      * @return List of child items
132      */

133     public List JavaDoc getChildItems() {
134         return childItems;
135     }
136
137     public void setChildItems(List JavaDoc childItems) {
138         for ( int i = 0; i < childItems.size(); i++ ) {
139             ContextMenuItem item = (ContextMenuItem) childItems.get(i);
140             addChildItem(item);
141         }
142     }
143
144     /**
145      * Adds child item
146      *
147      * @param childItem Item to add as a child
148      */

149     public void addChildItem(ContextMenuItem childItem) {
150         if ( childItem != null ) {
151             if ( childItem.getParentItem() != null ) {
152                 childItem.getParentItem().removeChildItem(childItem);
153             }
154             childItem.setParentItem(this);
155         }
156         childItems.add(childItem);
157     }
158
159     /**
160      * Removes child item
161      *
162      * @param childItem Child item to remove
163      */

164     public void removeChildItem(ContextMenuItem childItem) {
165         childItem.setParentItem(null);
166         childItems.remove(childItem);
167     }
168
169     /**
170      * Returns ID of this item. If ID has not already been initialized, generates it according to parent item's ID and
171      * position of this item in the list of parent's children. Can return <code>null</code> if there is no parent item of this one,
172      * or parent item's ID is <code>null</code>
173      *
174      * @return ID of this item
175      */

176     public String JavaDoc getId() {
177         if ( id == null && parentItem != null ) {
178             String JavaDoc parentId = parentItem.getId();
179             if ( parentId != null ) {
180                 int pos = parentItem.getChildItems().indexOf(this);
181                 id = parentId + "__" + pos;
182             }
183         }
184         return id;
185     }
186
187     /**
188      * Sets ID of this item
189      *
190      * @param id ID to set
191      */

192     public void setId(String JavaDoc id) {
193         this.id = id;
194     }
195
196     /**
197      * Generates JavaScript code, which will create client-side representation of this server-side object
198      *
199      * @return JavaScript code, which will create client-side representation of this server-side object
200      */

201     public String JavaDoc getJavascriptCode() {
202         StringBuffer JavaDoc code = new StringBuffer JavaDoc("new ContextMenuItem(");
203         code.append("\"").append(getId()).append("\", ");
204         code.append("\"").append(StringUtil.escape(content)).append("\", ");
205         code.append(hidden.toString());
206
207         // append children one-by-one
208
for ( int i = 0; i < childItems.size(); i++ ) {
209             ContextMenuItem item = (ContextMenuItem) childItems.get(i);
210             code.append(", ");
211             if ( item != null ) {
212                 code.append(item.getJavascriptCode());
213             } else {
214                 code.append(item);
215             }
216         }
217         code.append(")");
218         return code.toString();
219     }
220
221     /**
222      * Returns list of all context menu items in following order: root, his
223      * first child descendants, his second child descendants, ... his last child
224      * descendants
225      *
226      * @return list of items in preorder
227      */

228     public List JavaDoc getAsPlainList() {
229         List JavaDoc result = new ArrayList JavaDoc();
230         result.add(this);
231
232         for (int i = 0; i < childItems.size(); i++) {
233             ContextMenuItem child = (ContextMenuItem) childItems.get(i);
234             result.addAll(child.getAsPlainList());
235         }
236
237         return result;
238     }
239 }
240
Popular Tags