KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > menu > MenuComponent


1 /*
2  * MenuComponent.java
3  *
4  * Created on January 28, 2001, 8:10 PM
5  */

6
7 package fr.improve.struts.taglib.layout.menu;
8
9
10 import java.io.Serializable JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 /**
17  * MenuComponent, base on Struts-Menu.
18  *
19  * @author Scott Sayles
20  * @author jnribette
21  */

22 public class MenuComponent extends MenuBase implements Serializable JavaDoc {
23
24     protected ArrayList JavaDoc menuComponents = new ArrayList JavaDoc();
25     protected Map JavaDoc children = new HashMap JavaDoc();
26     
27     protected static MenuComponent[] _menuComponent = new MenuComponent[0];
28     protected static MenuAction[] _menuAction = new MenuAction[0];
29     
30     protected MenuComponent parentMenu = null;
31     
32     /**
33      * A menu can be shared in the ServletContext
34      * so it is not possible to store the value
35      * in the menu itself.
36      *
37      * This class is serializable,
38      * so open is transient.
39      */

40     private transient ThreadLocal JavaDoc open = new ThreadLocal JavaDoc();
41     
42     protected ArrayList JavaDoc menuActions = new ArrayList JavaDoc();
43     
44   
45     /** Creates new MenuComponent */
46     public MenuComponent() {
47         super();
48     }
49     
50     public MenuComponent(String JavaDoc in_id) {
51         super(in_id);
52         if (in_id==null || in_id.indexOf('*')!=-1 || in_id.indexOf('_')!=-1) {
53             throw new IllegalArgumentException JavaDoc("MenuComponent id cannot be null, nor contain '*' or '_'");
54         }
55     }
56     
57     /**
58      * Adds a MenuComponent
59      * @param menuComponent
60      */

61     public void addMenuComponent(MenuComponent menuComponent) {
62         menuComponents.add(menuComponent);
63         menuComponent.setParent(this);
64         if((menuComponent.getName() == null) || (menuComponent.getName().equals(""))) {
65             menuComponent.setName(this.name + menuComponents.size());
66         }
67         String JavaDoc lc_id = menuComponent.getId();
68         if (lc_id!=null) {
69             children.put(lc_id, menuComponent);
70         }
71     }
72     
73     public MenuComponent[] getMenuComponents() {
74         MenuComponent[] menus = (MenuComponent[])menuComponents.toArray(_menuComponent);
75         return menus;
76     }
77     
78     /**
79      * Return the child with the specified id if id are being used.
80      * However, return the element at the specified index.
81      *
82      * @param in_id the item id, or index.
83      * @return
84      */

85     public MenuComponent getChild(String JavaDoc in_id) {
86         if (children.isEmpty() && hasMenuComponents()) {
87             // Pas d'id.
88
try {
89                 int lc_pos = Integer.parseInt(in_id);
90                 if (lc_pos<menuComponents.size()) {
91                     return (MenuComponent) menuComponents.get(lc_pos);
92                 }
93                 return null;
94             } catch (NumberFormatException JavaDoc e) {
95                 return null;
96 }
97         } else {
98             return (MenuComponent) children.get(in_id);
99         }
100     }
101
102     public void setParent(MenuComponent parentMenu) {
103         this.parentMenu = parentMenu;
104     }
105     
106     public MenuComponent getParent() {
107         return parentMenu;
108     }
109     
110     /**
111      * Returns an array of the actions.
112      */

113     public MenuAction[] getMenuAction() {
114         return (MenuAction[]) menuActions.toArray(_menuAction);
115     }
116     
117     /**
118      * Adds an action.
119      */

120     public void addMenuAction(MenuAction in_action) {
121         menuActions.add(in_action);
122     }
123
124     /**
125      * Return true if the menu has submenus.
126      */

127     public boolean hasMenuComponents() {
128         return !menuComponents.isEmpty();
129     }
130     
131     /**
132      * Add a child menu at the specfied index.
133      */

134     public void addMenuComponent(int in_index, MenuComponent in_menuComponent) {
135         menuComponents.add(in_index, in_menuComponent);
136         String JavaDoc lc_id = in_menuComponent.getId();
137         if (lc_id!=null) {
138             children.put(lc_id, in_menuComponent);
139         }
140     }
141     
142     public void removeMenuComponent(int in_index) {
143         MenuComponent lc_element = (MenuComponent)menuComponents.get(in_index);
144         String JavaDoc lc_id = lc_element.getId();
145         if (lc_id!=null) {
146             children.remove(lc_id);
147         }
148         menuComponents.remove(in_index);
149
150     }
151     
152     public void removeMenuComponent(MenuComponent in_menuComponent) {
153         String JavaDoc lc_id = in_menuComponent.getId();
154         if (lc_id!=null) {
155             children.remove(lc_id);
156         }
157         menuComponents.remove(in_menuComponent);
158     }
159     
160     public void setOpen(boolean in_open) {
161         checkOpen();
162         open.set(in_open ? Boolean.TRUE : Boolean.FALSE);
163     }
164     
165     /**
166      * Reinitialize the "open" ThreadLocal
167      * if a serialization/deserialization has occur.
168      *
169      */

170     private void checkOpen() {
171         if (open==null) {
172             open = new ThreadLocal JavaDoc();
173         }
174     }
175     
176     public boolean isOpen() {
177         checkOpen();
178         Boolean JavaDoc lc_open = (Boolean JavaDoc) open.get();
179         if (lc_open==null) {
180             return false;
181         } else {
182             return lc_open.booleanValue();
183         }
184     }
185     
186     public void setOpen(boolean in_open, int in_level) {
187         setOpen(in_open);
188         int lc_level = in_level - 1;
189         if (lc_level>0) {
190             MenuComponent[] lc_menus = (MenuComponent[]) getMenuComponents();
191             for (int i=0; i < lc_menus.length; i++) {
192                 lc_menus[i].setOpen(in_open, lc_level);
193             }
194         }
195     }
196     
197     public int indexOf(MenuComponent in_component) {
198         return menuComponents.indexOf(in_component);
199     }
200     
201     public void closeAll() {
202         if (isOpen() || getParent()==null) {
203             MenuComponent[] lc_menus = (MenuComponent[]) getMenuComponents();
204             for (int i=0; i < lc_menus.length; i++) {
205                 lc_menus[i].closeAll();
206             }
207         }
208         setOpen(false);
209     }
210 }
Popular Tags