KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > menu > MenuModel


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.menu;
19
20 import java.awt.Component JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25 import javax.swing.Action JavaDoc;
26 import javax.swing.JMenuItem JavaDoc;
27
28 import org.columba.core.logging.Logging;
29
30 public class MenuModel {
31
32     private static final Logger JavaDoc LOG = Logger
33             .getLogger("org.columba.core.gui.menu"); //$NON-NLS-1$
34

35     private IMenuElement rootElement;
36
37     private Hashtable JavaDoc placeholders = new Hashtable JavaDoc();
38
39     private Hashtable JavaDoc submenues = new Hashtable JavaDoc();
40
41     private String JavaDoc id;
42
43     public MenuModel(String JavaDoc id) {
44         this.id = id;
45
46         rootElement = MenuElementFactory.createMenuElement(id, "rootLabel");
47         submenues.put(id, this);
48     }
49
50     public void insert(Action JavaDoc action, int position) {
51         insert(position, MenuElementFactory.createActionElement(action));
52     }
53
54     public void insert(Component JavaDoc component, int position) {
55         insert(position, MenuElementFactory.createComponentElement(component));
56     }
57
58     public void insert(JMenuItem JavaDoc menuItem, int position) {
59         insert(position, MenuElementFactory.createMenuItemElement(menuItem));
60     }
61
62     public void insertSeparator(int position) {
63         insert(position, MenuElementFactory.createSeparatorElement());
64     }
65
66     public void insertPlaceholder(String JavaDoc placeholderId, int position) {
67         insert(position, MenuElementFactory
68                 .createPlaceholderElement(placeholderId));
69     }
70
71     protected void insert(int position, IMenuElement element) {
72         rootElement.insert(element, position);
73
74         if (element.isPlaceholder()) {
75             String JavaDoc id = ((MenuElement) element).getPlaceholderId();
76             if (placeholders.containsKey(id))
77                 throw new IllegalArgumentException JavaDoc("placeholder id <" + id
78                         + "> already used.");
79
80             placeholders.put(id, element);
81         }
82     }
83
84     protected void append(IMenuElement element) {
85
86         rootElement.add(element);
87
88         if (element.isPlaceholder()) {
89             String JavaDoc id = ((MenuElement) element).getPlaceholderId();
90             if (placeholders.containsKey(id))
91                 throw new IllegalArgumentException JavaDoc("placeholder id <" + id
92                         + "> already used.");
93
94             placeholders.put(id, element);
95         }
96     }
97
98     protected int insert(IMenuElement element, String JavaDoc placeholderId) {
99         if (element == null)
100             throw new IllegalArgumentException JavaDoc("element == null");
101         if (placeholderId == null)
102             throw new IllegalArgumentException JavaDoc(
103                     "placeholderId == null, for element " + element.toString());
104
105         if (placeholders.containsKey(placeholderId) == false) {
106             if (Logging.DEBUG)
107                 printDebugPlaceholders();
108
109             LOG.severe("no matching placeholder with id <" + placeholderId
110                     + "> in menu <" + getId() + "> found.");
111
112         }
113
114         IMenuElement placeholderElement = (IMenuElement) placeholders
115                 .get(placeholderId);
116
117         int index = rootElement.indexOf(placeholderElement);
118
119         if (index != -1)
120             insert(index, element);
121
122         return index;
123     }
124
125     /**
126      *
127      */

128     private void printDebugPlaceholders() {
129         Enumeration JavaDoc e = placeholders.elements();
130         while (e.hasMoreElements()) {
131             LOG.info(((IMenuElement) e.nextElement()).toString());
132         }
133     }
134
135     /**
136      * @return Returns the id.
137      */

138     public String JavaDoc getId() {
139         return id;
140     }
141
142     /** ****************** public helper methods ******************* */
143
144     public void add(Component JavaDoc component) {
145         append(MenuElementFactory.createComponentElement(component));
146     }
147
148     public void add(Action JavaDoc action) {
149         append(MenuElementFactory.createActionElement(action));
150     }
151
152     public void add(JMenuItem JavaDoc menuItem) {
153         append(MenuElementFactory.createMenuItemElement(menuItem));
154     }
155
156     public void addSeparator() {
157         append(MenuElementFactory.createSeparatorElement());
158     }
159
160     public int insertSeparator(String JavaDoc placeholderId) {
161         int index = insert(MenuElementFactory.createSeparatorElement(),
162                 placeholderId);
163         return index;
164     }
165
166     public void appendPlaceholder(String JavaDoc placeholderId) {
167         append(MenuElementFactory.createPlaceholderElement(placeholderId));
168     }
169
170     public MenuModel getSubmenu(String JavaDoc submenuId) {
171         if (submenues.containsKey(submenuId) == false)
172             throw new IllegalArgumentException JavaDoc("no matching menu with id "
173                     + submenuId + " found.");
174         return (MenuModel) submenues.get(submenuId);
175     }
176
177     public void addSubmenu(MenuModel submenu) {
178         rootElement.add(submenu.getRootElement());
179         submenues.put(submenu.getId(), submenu);
180
181     }
182
183     protected IMenuElement getRootElement() {
184         return rootElement;
185     }
186
187     public int insert(Action JavaDoc action, String JavaDoc placeholderId) {
188         int index = insert(MenuElementFactory.createActionElement(action),
189                 placeholderId);
190
191         return index;
192     }
193
194     public int insert(JMenuItem JavaDoc menuItem, String JavaDoc placeholderId) {
195         int index = insert(MenuElementFactory.createMenuItemElement(menuItem),
196                 placeholderId);
197         return index;
198     }
199
200     public Enumeration JavaDoc getSubmenuEnumeration() {
201         return submenues.elements();
202     }
203
204     public void insertPlaceholder(String JavaDoc placeholderId,
205             String JavaDoc insertionPlaceholder) {
206         insert(MenuElementFactory.createPlaceholderElement(placeholderId),
207                 insertionPlaceholder);
208     }
209
210     public int insert(Component JavaDoc component, String JavaDoc placeholderId) {
211         int index = insert(
212                 MenuElementFactory.createComponentElement(component),
213                 placeholderId);
214         return index;
215     }
216 }
217
Popular Tags