KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > core > menu > lib > BasicMenuDescription


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.core.menu.lib;
27
28 import java.util.List JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.objectweb.util.explorer.ExplorerUtils;
32 import org.objectweb.util.explorer.core.common.api.Description;
33 import org.objectweb.util.explorer.core.menu.api.ItemDescription;
34 import org.objectweb.util.explorer.core.menu.api.MenuDescription;
35 import org.objectweb.util.explorer.core.menu.api.MenuElement;
36
37 /**
38  * Basic implementation of the <code>MenuDescription</code> interface.
39  *
40  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
41  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
42  *
43  * @version 0.1
44  */

45 public class BasicMenuDescription
46   implements MenuDescription
47 {
48
49     //==================================================================
50
//
51
// Internal States.
52
//
53
// ==================================================================
54

55     protected boolean inheritTreeMenu_ = true;
56     protected boolean inheritTypeMenu_ = true;
57     protected List JavaDoc menuElements_ = null;
58     protected List JavaDoc childMenuElements_ = null;
59     protected String JavaDoc descriptionType_ = Description.MENU_DESCRIPTION;
60     
61     // ==================================================================
62
//
63
// Constructors.
64
//
65
// ==================================================================
66

67     public BasicMenuDescription(){
68         menuElements_ = new Vector JavaDoc();
69         childMenuElements_ = new Vector JavaDoc();
70     }
71     
72     // ==================================================================
73
//
74
// Internal methods.
75
//
76
// ==================================================================
77

78     /**
79      * Two <code>BasicMenuDescription</code> are considered as equals
80      * if and only if they have the same items list.
81      */

82     protected boolean equals(BasicMenuDescription menuDesc){
83         if(menuDesc!=null){
84             return ExplorerUtils.compareObjects(menuElements_, menuDesc.menuElements_)
85                 && ExplorerUtils.compareObjects(childMenuElements_, menuDesc.childMenuElements_);
86         }
87         return false;
88     }
89     
90     // ==================================================================
91
//
92
// Public methods for Description interface.
93
//
94
// ==================================================================
95

96     /* (non-Javadoc)
97      * @see org.objectweb.util.explorer.core.common.api.Description#getDescriptionType()
98      */

99     public String JavaDoc getDescriptionType() {
100         return descriptionType_;
101     }
102
103     /* (non-Javadoc)
104      * @see boolean#isNull()
105      */

106     public boolean isEmpty() {
107         return menuElements_.isEmpty() && childMenuElements_.isEmpty();
108     }
109     
110     // ==================================================================
111
//
112
// Public methods for MenuDescription interface.
113
//
114
// ==================================================================
115

116     /* (non-Javadoc)
117      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#setDescriptionType(java.lang.String)
118      */

119     public void setDescriptionType(String JavaDoc descriptionType) {
120         descriptionType_ = descriptionType;
121     }
122     
123     /* (non-Javadoc)
124      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#setInheritTreeMenu(boolean)
125      */

126     public void setInheritTreeMenu(boolean inheritTreeMenu) {
127         inheritTreeMenu_ = inheritTreeMenu;
128     }
129
130     /* (non-Javadoc)
131      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#getInheritTreeMenu()
132      */

133     public boolean getInheritTreeMenu() {
134         return inheritTreeMenu_;
135     }
136
137     /* (non-Javadoc)
138      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#setInheritTypeMenu(boolean)
139      */

140     public void setInheritTypeMenu(boolean inheritTypeMenu) {
141         inheritTypeMenu_ = inheritTypeMenu;
142     }
143
144     /* (non-Javadoc)
145      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#getInheritTypeMenu()
146      */

147     public boolean getInheritTypeMenu() {
148         return inheritTypeMenu_;
149     }
150
151     /* (non-Javadoc)
152      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#addMenuElement(org.objectweb.util.explorer.core.menu.api.MenuElement)
153      */

154     /**
155      * Adds the given element into the elements list if
156      * <ul>
157      * <li>this one is not considered as null.</li>
158      * <li>this one is not yet present.</li>
159      * </ul>
160      * The menu elements are separated into two lists:
161      * The first one concerning the current menu itself
162      * and the second one for the children of this menu
163      * when it is used in a graphical context.
164      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#addMenuElement(org.objectweb.util.explorer.core.menu.api.MenuElement)
165      */

166     public void addMenuElement(MenuElement menuElement) {
167         if(menuElement!=null && !menuElement.isEmpty()){
168             if(menuElement instanceof ItemDescription){
169                 ItemDescription itemDesc = (ItemDescription)menuElement;
170                 List JavaDoc theList = (itemDesc.getTreeChildVisible())?childMenuElements_:menuElements_;
171                 if(!theList.contains(itemDesc)){
172                     theList.add(menuElement);
173                 }
174             } else {
175                 menuElements_.add(menuElement);
176             }
177         }
178     }
179
180     
181     /* (non-Javadoc)
182      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#addMenuElements(org.objectweb.util.explorer.core.menu.api.MenuElement[])
183      */

184     public void addMenuElements(MenuElement[] menuElements) {
185         if(menuElements!=null){
186             for (int i = 0; i < menuElements.length; i++) {
187                 addMenuElement(menuElements[i]);
188             }
189         }
190
191     }
192     
193     /* (non-Javadoc)
194      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#getMenuElements()
195      */

196     public MenuElement[] getMenuElements() {
197         return (MenuElement[])menuElements_.toArray(new MenuElement[menuElements_.size()]);
198     }
199     
200     /* (non-Javadoc)
201      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#getChildrenMenuElements()
202      */

203     public MenuElement[] getChildrenMenuElements() {
204         return (MenuElement[])childMenuElements_.toArray(new MenuElement[childMenuElements_.size()]);
205     }
206     
207     /* (non-Javadoc)
208      * @see org.objectweb.util.explorer.core.menu.api.MenuDescription#getAllMenuElements()
209      */

210     public MenuElement[] getAllMenuElements() {
211         MenuElement[] menuElement = getMenuElements();
212         MenuElement[] childrenMenuElement = getChildrenMenuElements();
213         boolean needSeparator = menuElement.length>0 && childrenMenuElement.length>0;
214         MenuElement[] mergeMenu = new MenuElement[menuElement.length + childrenMenuElement.length + (needSeparator?1:0)];
215         System.arraycopy(menuElement,0,mergeMenu,0,menuElement.length);
216         if(needSeparator){
217             mergeMenu[menuElement.length] = new BasicMenuSeparator();
218         }
219         System.arraycopy(childrenMenuElement,0,mergeMenu,menuElement.length + (needSeparator?1:0),childrenMenuElement.length);
220         return mergeMenu;
221     }
222     
223     // ==================================================================
224
//
225
// Public methods surcharging Object class.
226
//
227
// ==================================================================
228

229     /**
230      * Surcharging the Object.equals method.
231      * @see Object#equals(java.lang.Object)
232      */

233     public boolean equals(Object JavaDoc o){
234         if(o!=null && o instanceof BasicMenuDescription){
235             return equals((BasicMenuDescription)o);
236         }
237         return false;
238     }
239    
240     /**
241      * Return a String representation of a BasicAcceleratorDescription
242      * @see Object#toString()
243      */

244     public String JavaDoc toString(){
245         return "BasicMenuDescription["
246             + "inheritTreeMenu=" + inheritTreeMenu_ + ", "
247             + "inheritTypeMenu=" + inheritTypeMenu_ + ", "
248             + "menuElements=" + ExplorerUtils.arrayToString(menuElements_.toArray())
249             + "childMenuElements=" + ExplorerUtils.arrayToString(childMenuElements_.toArray())
250             + "]";
251     }
252         
253 }
254
255
Popular Tags