KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import javax.swing.Action JavaDoc;
25 import javax.swing.JMenu JavaDoc;
26 import javax.swing.JMenuItem JavaDoc;
27 import javax.swing.JSeparator JavaDoc;
28
29 import org.columba.core.gui.base.CMenu;
30
31
32 public class ExtendableMenu extends CMenu {
33
34     private MenuModel model;
35
36     private Hashtable JavaDoc<String JavaDoc, ExtendableMenu> map = new Hashtable JavaDoc<String JavaDoc, ExtendableMenu>();
37
38     private String JavaDoc id;
39
40     public ExtendableMenu(String JavaDoc id, String JavaDoc label) {
41         super(label, id);
42
43         this.id = id;
44
45         model = new MenuModel(id);
46
47         map.put(id, this);
48     }
49
50     public MenuModel getMenuModel() {
51         return model;
52     }
53
54     /**
55      * @see javax.swing.JMenu#add(javax.swing.Action)
56      */

57     public JMenuItem JavaDoc add(Action JavaDoc action) {
58         model.add(action);
59
60         return super.add(action);
61
62     }
63
64     /**
65      * @see javax.swing.JMenu#add(javax.swing.JMenuItem)
66      */

67     public JMenuItem JavaDoc add(JMenuItem JavaDoc menuItem) {
68         model.add(menuItem);
69
70         return super.add(menuItem);
71     }
72
73     /**
74      * @see javax.swing.JMenu#addSeparator()
75      */

76     public void addSeparator() {
77         int index = getComponentCount();
78
79         // make sure that we don't end up with two separators
80
if (index > 0 && (index - 1 < getComponentCount())) {
81             Component JavaDoc prev = getComponent(index - 1);
82             if (prev instanceof JSeparator JavaDoc)
83                 return;
84         }
85
86         model.addSeparator();
87
88         super.addSeparator();
89     }
90
91     /**
92      * @see javax.swing.JMenu#insert(javax.swing.Action, int)
93      */

94     public JMenuItem JavaDoc insert(Action JavaDoc action, int pos) {
95         model.insert(action, pos);
96
97         return super.insert(action, pos);
98     }
99
100     /**
101      * @see javax.swing.JMenu#insert(javax.swing.JMenuItem, int)
102      */

103     public JMenuItem JavaDoc insert(JMenuItem JavaDoc menuItem, int pos) {
104         model.insert(menuItem, pos);
105
106         return super.insert(menuItem, pos);
107     }
108
109     /**
110      * @see javax.swing.JMenu#insertSeparator(int)
111      */

112     public void insertSeparator(int index) {
113         model.insertSeparator(index);
114
115         super.insertSeparator(index);
116     }
117
118     public void addPlaceholder(String JavaDoc placeholderId) {
119         model.appendPlaceholder(placeholderId);
120     }
121
122     public void insertPlaceholder(String JavaDoc placeholderId, int pos) {
123         model.insertPlaceholder(placeholderId, pos);
124     }
125
126     public void insert(Action JavaDoc action, String JavaDoc placeholderId) {
127         int index = model.insert(action, placeholderId);
128         if (index != -1)
129             super.insert(action, index);
130     }
131
132     public void insert(Component JavaDoc component, String JavaDoc placeholderId) {
133         int index = model.insert(component, placeholderId);
134         if (index != -1)
135             super.add(component, index);
136     }
137
138     public void insert(JMenuItem JavaDoc menuItem, String JavaDoc placeholderId) {
139         if (menuItem == null)
140             throw new IllegalArgumentException JavaDoc("menuItem == null");
141         if (placeholderId == null)
142             throw new IllegalArgumentException JavaDoc("placeholderId == null");
143
144         int index = model.insert(menuItem, placeholderId);
145         if (index != -1)
146             super.insert(menuItem, index);
147     }
148
149     public void insertSeparator(String JavaDoc placeholderId) {
150         int index = model.insertSeparator(placeholderId);
151         // make sure that we don't end up with two separators
152
if (index > 0 && (index - 1 < getComponentCount())) {
153             Component JavaDoc prev = getComponent(index - 1);
154             if (prev instanceof JSeparator JavaDoc)
155                 return;
156         }
157
158         if (index != -1)
159             super.insertSeparator(index);
160     }
161
162     public void add(ExtendableMenu submenu) {
163
164         map.put(submenu.getId(), submenu);
165
166         model.addSubmenu(submenu.getMenuModel());
167
168         super.add((JMenu JavaDoc) submenu);
169     }
170
171     /**
172      * @return Returns the id.
173      */

174     public String JavaDoc getId() {
175         return id;
176     }
177
178     public Enumeration JavaDoc<ExtendableMenu> getSubmenuEnumeration() {
179         return map.elements();
180     }
181
182     /**
183      * @see javax.swing.JMenu#add(java.awt.Component, int)
184      */

185     public Component JavaDoc add(Component JavaDoc component, int index) {
186         model.insert(component, index);
187
188         return super.add(component, index);
189     }
190
191     /**
192      * @see javax.swing.JMenu#add(java.awt.Component)
193      */

194     public Component JavaDoc add(Component JavaDoc component) {
195         model.add(component);
196
197         return super.add(component);
198     }
199
200     public void insertPlaceholder(String JavaDoc placeholderId,
201             String JavaDoc insertionPlaceholder) {
202         model.insertPlaceholder(placeholderId, insertionPlaceholder);
203     }
204 }
205
Popular Tags