KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > menu > MenuComponent


1 package com.blandware.atleap.webapp.menu;
2
3 import com.blandware.atleap.common.util.ConvertUtil;
4 import org.apache.commons.lang.StringUtils;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10
11 /**
12  * <p>This class extends {@link MenuBase} and basically contains helper methods
13  * for adding and fetching children and parents.</p>
14  * <p>This class was copied from Struts Menu.</p>
15  * <p><a HREF="MenuBase.java.htm"><i>View Source</i></a></p>
16  *
17  * @author Scott Sayles
18  * @author Matt Raible
19  * @version $Revision: 1.8 $ $Date: 2006/03/16 11:09:43 $
20  */

21 public class MenuComponent extends MenuBase implements Serializable JavaDoc {
22     //~ Static fields/initializers =============================================
23
protected static MenuComponent[] _menuComponent = new MenuComponent[0];
24
25     //~ Instance fields ========================================================
26

27     protected List JavaDoc menuComponents = new ArrayList JavaDoc();
28     protected MenuComponent parentMenu = null;
29
30     /**
31      * Contains ID of this menu conponent
32      */

33     protected transient String JavaDoc ident = null;
34
35     //~ Constructors ===========================================================
36

37     /**
38      * Creates new MenuComponent
39      */

40     public MenuComponent() {
41         super();
42     }
43
44     //~ Methods ================================================================
45

46     /**
47      * Adds a menu component to list of children
48      *
49      * @param menuComponent Menu component to add
50      */

51     public void addMenuComponent(MenuComponent menuComponent) {
52         if ( (menuComponent.getName() == null) ||
53                 (menuComponent.getName().equals("")) ) {
54             menuComponent.setName(this.name + menuComponents.size());
55         }
56
57         if ( !menuComponents.contains(menuComponent) ) {
58             menuComponents.add(menuComponent);
59             menuComponent.setParent(this);
60         }
61     }
62
63     /**
64      * Returns a list of child menu components
65      *
66      * @return List of children
67      */

68     public MenuComponent[] getMenuComponents() {
69         MenuComponent[] menus =
70                 (MenuComponent[]) menuComponents.toArray(_menuComponent);
71
72         return menus;
73     }
74
75     /**
76      * Sets a parent of this menu component
77      *
78      * @param parentMenu Menu component to set as parent
79      */

80     public void setParent(MenuComponent parentMenu) {
81         if ( parentMenu != null ) {
82             // look up the parent and make sure that it has this menu as a child
83
if ( !parentMenu.getComponents().contains(this) ) {
84                 parentMenu.addMenuComponent(this);
85             }
86         }
87         this.parentMenu = parentMenu;
88     }
89
90     /**
91      * Returns a parent menu component
92      *
93      * @return Parent
94      */

95     public MenuComponent getParent() {
96         return parentMenu;
97     }
98
99     /**
100      * Convenience method for Velocity templates
101      *
102      * @return list of child components
103      */

104     public List JavaDoc getComponents() {
105         return menuComponents;
106     }
107
108     /**
109      * Returns ID of this component. If ID has not already been initialized, generates it according to parent component's ID and
110      * position of this component in the list of parent's children. Can return <code>null</code> if there is no parent component of this one,
111      * or parent component's ID is <code>null</code>
112      *
113      * @return ID of this item
114      */

115     public String JavaDoc getIdent() {
116         if ( ident == null ) {
117             if (parentMenu == null) {
118                 ident = "DropdownMenu__" + this.hashCode();
119             } else {
120                 String JavaDoc parentId = parentMenu.getIdent();
121                 if ( parentId != null ) {
122                     int pos = parentMenu.getComponents().indexOf(this);
123                     ident = parentId + "__" + pos;
124                 }
125             }
126         }
127         return ident;
128     }
129
130     /**
131      * Returns list of all menu components in following order: root, his
132      * first child descendants, his second child descendants, ... his last child
133      * descendants
134      *
135      * @return list of items in preorder
136      */

137     public List JavaDoc getComponentsAsPlainList() {
138         List JavaDoc result = new ArrayList JavaDoc();
139         result.add(this);
140
141         for (int i = 0; i < menuComponents.size(); i++) {
142             MenuComponent child = (MenuComponent) menuComponents.get(i);
143             result.addAll(child.getComponentsAsPlainList());
144         }
145
146         return result;
147     }
148
149     /**
150      * Generates JavaScript code, which will create client-side representation of this server-side object
151      *
152      * @return JavaScript code, which will create client-side representation of this server-side object
153      */

154     public String JavaDoc getJavascriptCode() {
155         StringBuffer JavaDoc code = new StringBuffer JavaDoc("new DropdownMenuItem(");
156         code.append("\"").append(getIdent()).append("\"");
157
158         // append children one-by-one
159
for ( int i = 0; i < menuComponents.size(); i++ ) {
160             MenuComponent item = (MenuComponent) menuComponents.get(i);
161             code.append(", ");
162             if ( item != null ) {
163                 code.append(item.getJavascriptCode());
164             } else {
165                 code.append(item);
166             }
167         }
168         code.append(")");
169         return code.toString();
170     }
171
172     /**
173      * Returns comma-separated list of roles that are assigned to this component
174      * or any of its ancestors
175      *
176      * @return list of roles
177      */

178     public String JavaDoc getRolesWithAncestors() {
179         MenuComponent parent = getParent();
180         if (parent == null) {
181             return getRoles();
182         } else {
183             List JavaDoc thisRoles = ConvertUtil.convertStringToList(getRoles(), ",", true);
184             List JavaDoc parentRoles = ConvertUtil.convertStringToList(parent.getRolesWithAncestors(), ",", true);
185             thisRoles.addAll(parentRoles);
186             return ConvertUtil.convertListToString(thisRoles, ",");
187         }
188     }
189
190     /**
191      * This method compares all attributes, except for parent and children
192      *
193      * @param o The object to compare to
194      * @return <code>true</code> if given object is equal to this
195      */

196     public boolean equals(Object JavaDoc o) {
197         if ( !(o instanceof MenuComponent) ) {
198             return false;
199         }
200         MenuComponent m = (MenuComponent) o;
201         // Compare using StringUtils to avoid NullPointerExceptions
202
return StringUtils.equals(m.getAction(), this.action) &&
203                 StringUtils.equals(m.getAlign(), this.align) &&
204                 StringUtils.equals(m.getAltImage(), this.altImage) &&
205                 StringUtils.equals(m.getDescription(), this.description) &&
206                 StringUtils.equals(m.getForward(), this.forward) &&
207                 StringUtils.equals(m.getStyle(), this.style) &&
208                 StringUtils.equals(m.getImage(), this.image) &&
209                 StringUtils.equals(m.getLocation(), this.location) &&
210                 StringUtils.equals(m.getName(), this.name) &&
211                 StringUtils.equals(m.getOnclick(), this.onclick) &&
212                 StringUtils.equals(m.getOnmouseout(), this.onmouseout) &&
213                 StringUtils.equals(m.getOnmouseover(), this.onmouseover) &&
214                 StringUtils.equals(m.getAnchor(), this.anchor) &&
215                 StringUtils.equals(m.getRoles(), this.roles) &&
216                 StringUtils.equals(m.getTarget(), this.target) &&
217                 StringUtils.equals(m.getTitle(), this.title) &&
218                 StringUtils.equals(m.getToolTip(), this.toolTip) &&
219                 StringUtils.equals(m.getStyleClass(), this.styleClass);
220     }
221 }
222
Popular Tags