View Javadoc

1   /*
2    * MenuComponent.java
3    *
4    * Created on January 28, 2001, 8:10 PM
5    */
6   package net.sf.navigator.menu;
7   
8   import org.apache.commons.lang.StringUtils;
9   
10  import java.io.Serializable;
11  import java.util.ArrayList;
12  import java.util.Collections;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  
17  /**
18   * This class extends {@link MenuBase} and basically contains helper methods
19   * for adding and fetching children and parents.
20   *
21   * @author Scott Sayles, Matt Raible
22   * @version $Revision: 1.19 $ $Date: 2006/10/17 11:27:09 $
23   */
24  public class MenuComponent extends MenuBase implements Serializable, Component {
25      //~ Static fields/initializers =============================================
26      protected static MenuComponent[] _menuComponent = new MenuComponent[0];
27  
28      //~ Instance fields ========================================================
29  
30      protected List menuComponents = Collections.synchronizedList(new ArrayList());
31      protected MenuComponent parentMenu;
32      private boolean last;
33      private String breadCrumb;
34  
35      //~ Methods ================================================================
36  
37      public void addMenuComponent(MenuComponent menuComponent) {
38          if ((menuComponent.getName() == null) || (menuComponent.getName().equals(""))) {
39              menuComponent.setName(this.name + menuComponents.size());
40          }
41  
42          if (!menuComponents.contains(menuComponent)) {
43              menuComponents.add(menuComponent);
44              menuComponent.setParent(this);
45          }
46      }
47  
48      public MenuComponent[] getMenuComponents() {
49          return (MenuComponent[]) menuComponents.toArray(_menuComponent);
50      }
51  
52      public void setMenuComponents(MenuComponent[] menuComponents) {
53          for (int i = 0; i < menuComponents.length; i++) {
54              MenuComponent component = menuComponents[i];
55              this.menuComponents.add(component);
56          }
57      }
58  
59      public void setParent(MenuComponent parentMenu) {
60          if (parentMenu != null) {
61              // look up the parent and make sure that it has this menu as a child
62              if (!parentMenu.getComponents().contains(this)) {
63                  parentMenu.addMenuComponent(this);
64              }
65          }
66          this.parentMenu = parentMenu;
67      }
68  
69      public MenuComponent getParent() {
70          return parentMenu;
71      }
72  
73      /**
74       * Convenience method for Velocity templates
75       * @return menuComponents as a java.util.List
76       */
77      public List getComponents() {
78          return menuComponents;
79      }
80  
81      /**
82       * This method compares all attributes, except for parent and children
83       *
84       * @param o the object to compare to
85       */
86      public boolean equals(Object o) {
87          if (!(o instanceof MenuComponent)) {
88              return false;
89          }
90          MenuComponent m = (MenuComponent) o;
91          // Compare using StringUtils to avoid NullPointerExceptions
92          return StringUtils.equals(m.getAction(), this.action) &&
93                  StringUtils.equals(m.getAlign(), this.align) &&
94                  StringUtils.equals(m.getAltImage(), this.altImage) &&
95                  StringUtils.equals(m.getDescription(), this.description) &&
96                  StringUtils.equals(m.getForward(), this.forward) &&
97                  StringUtils.equals(m.getHeight(), this.height) &&
98                  StringUtils.equals(m.getImage(), this.image) &&
99                  StringUtils.equals(m.getLocation(), this.location) &&
100                 StringUtils.equals(m.getName(), this.name) &&
101                 StringUtils.equals(m.getOnclick(), this.onclick) &&
102                 StringUtils.equals(m.getOndblclick(), this.ondblclick) &&
103                 StringUtils.equals(m.getOnmouseout(), this.onmouseout) &&
104                 StringUtils.equals(m.getOnmouseover(), this.onmouseover) &&
105                 StringUtils.equals(m.getOnContextMenu(), this.onContextMenu) &&
106                 StringUtils.equals(m.getPage(), this.page) &&
107                 StringUtils.equals(m.getRoles(), this.roles) &&
108                 StringUtils.equals(m.getTarget(), this.target) &&
109                 StringUtils.equals(m.getTitle(), this.title) &&
110                 StringUtils.equals(m.getToolTip(), this.toolTip) &&
111                 StringUtils.equals(m.getWidth(), this.width) &&
112                 StringUtils.equals(m.getModule(), this.module);
113     }
114 
115     /**
116      * Get the depth of the menu
117      *
118      * @return Depth of menu
119      */
120     public int getMenuDepth() {
121         return getMenuDepth(this, 0);
122     }
123 
124     private int getMenuDepth(MenuComponent menu, int currentDepth) {
125         int depth = currentDepth + 1;
126 
127         MenuComponent[] subMenus = menu.getMenuComponents();
128         if (subMenus != null) {
129             for (int a = 0; a < subMenus.length; a++) {
130                 int depthx = getMenuDepth(subMenus[a], currentDepth + 1);
131                 if (depth < depthx)
132                     depth = depthx;
133             }
134         }
135 
136         return depth;
137     }
138 
139     /**
140      * Returns the last.
141      *
142      * @return boolean
143      */
144     public boolean isLast() {
145         return last;
146     }
147 
148     /**
149      * Sets the last.
150      *
151      * @param last The last to set
152      */
153     public void setLast(boolean last) {
154         this.last = last;
155     }
156 
157     /**
158      * Remove all children from a parent menu item
159      */
160     public void removeChildren() {
161         for (Iterator iterator = this.getComponents().iterator(); iterator.hasNext();) {
162             MenuComponent child = (MenuComponent) iterator.next();
163             child.setParent(null);
164             iterator.remove();
165         }
166     }
167 
168     public String getBreadCrumb() {
169         return breadCrumb;
170     }
171 
172     /**
173      * Build the breadcrumb trail leading to this menuComponent
174      *
175      * @param delimiter type of separator
176      */
177     protected void setBreadCrumb(String delimiter) {
178         if (getParent() == null) {
179             breadCrumb = name;
180             setChildBreadCrumb(delimiter);
181         } else {
182             MenuComponent parent = getParent();
183             breadCrumb = parent.getBreadCrumb() + delimiter + name;
184             setChildBreadCrumb(delimiter);
185         }
186     }
187 
188     private void setChildBreadCrumb(String delimiter) {
189         List children = this.getComponents();
190         for (Iterator iterator = children.iterator(); iterator.hasNext();) {
191             MenuComponent child = (MenuComponent) iterator.next();
192             child.setBreadCrumb(delimiter);
193         }
194     }
195 
196     public String toString() {
197         return "name: " + this.name;
198     }
199 }