KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > menu > Menu


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.menu;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.*;
24
25
26 /**
27  * @author Uddhab Pant
28  *
29  * This class represents menu. Menu is a place holder for
30  * sub menu and menu item.
31  *
32  */

33 public class Menu implements Serializable JavaDoc {
34     private String JavaDoc displayName;
35
36     /**
37      * Sub menu or menu item
38      * TODO: should this be of interface List? I think we may want to guarantee sort ordering?
39      */

40     private Collection childNodes;
41
42     /**
43      * Default contructor
44      *
45      */

46     public Menu() {
47         childNodes = new ArrayList();
48     }
49
50     /**
51      *
52      * @param displayName String
53      */

54     public Menu(String JavaDoc displayName) {
55         this.displayName = displayName;
56         childNodes = new ArrayList();
57     }
58
59     /**
60      * @return Returns the displayName.
61      */

62     public String JavaDoc getDisplayName() {
63         return displayName;
64     }
65
66     /**
67      * @param displayName The displayName to set.
68      */

69     public void setDisplayName(String JavaDoc displayName) {
70         this.displayName = displayName;
71     }
72
73     /**
74      * Adds menu item.
75      * @param menuItem MenuItem
76      */

77     public void addMenuItem(MenuItem menuItem) {
78         this.childNodes.add(menuItem);
79     }
80
81     /**
82      * Adds sub menu
83      * @param subMenu Menu
84      */

85     public void addSubMenu(Menu subMenu) {
86         this.childNodes.add(subMenu);
87     }
88
89     /**
90      * Returns sub menu and menu items
91      * @return Collection
92      */

93     public Collection getChildNodes() {
94         return this.childNodes;
95     }
96
97     /**
98      * Returns display name
99      * @return String
100      */

101     public String JavaDoc toString() {
102         return String.valueOf(displayName);
103     }
104     
105     public void accept(MenuVisitor visitor){
106         this.recurse(this.getChildNodes(), visitor);
107     }
108     
109     private void recurse(Collection nodes, MenuVisitor visitor){
110         Iterator children = nodes.iterator();
111         while(children.hasNext()){
112             Object JavaDoc child = children.next();
113             
114             if(child instanceof Menu){
115                 Menu childMenu = (Menu)child;
116                 visitor.visit(childMenu);
117                 recurse(childMenu.getChildNodes(), visitor);
118             }else if(child instanceof MenuItem){
119                 visitor.visit((MenuItem)child);
120             }
121         }
122     }
123
124     public Menu getSubMenu(String JavaDoc displayName) {
125         SubmenuVisitor visitor = new SubmenuVisitor(displayName);
126         this.recurse(this.getChildNodes(), visitor);
127         return visitor.getSubmenu();
128     }
129
130     public List getMenuDisplayNames() {
131         NameCollectionVisitor visitor = new NameCollectionVisitor();
132         this.recurse(this.getChildNodes(), visitor);
133         return visitor.getMenuNames();
134     }
135 }
136
137 class SubmenuVisitor implements MenuVisitor{
138     private Menu submenu;
139     private String JavaDoc displayName;
140     public SubmenuVisitor(String JavaDoc displayName){
141         this.displayName = displayName;
142     }
143     
144     public void visit(Menu menu) {
145         if(displayName.equals(menu.getDisplayName())){
146             submenu = menu;
147         }
148     }
149
150     public void visit(MenuItem menuItem) {
151         // nothing
152
}
153     
154     public Menu getSubmenu(){
155         return this.submenu;
156     }
157 }
158
159 class NameCollectionVisitor implements MenuVisitor{
160     private List menuNames;
161     
162     public NameCollectionVisitor(){
163         menuNames = new LinkedList();
164     }
165
166     public void visit(Menu menu) {
167         if(menu!=null && menu.getDisplayName()!=null){
168             menuNames.add(menu.getDisplayName());
169         }
170     }
171
172     public List getMenuNames() {
173         return this.menuNames;
174     }
175
176     public void visit(MenuItem menuItem) {
177         //nothing
178
}
179     
180 }
181
182
183
Popular Tags