001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.menu;
019:
020: import java.awt.Component;
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023:
024: import javax.swing.Action;
025: import javax.swing.JMenu;
026: import javax.swing.JMenuItem;
027: import javax.swing.JSeparator;
028:
029: import org.columba.core.gui.base.CMenu;
030:
031: public class ExtendableMenu extends CMenu {
032:
033: private MenuModel model;
034:
035: private Hashtable<String, ExtendableMenu> map = new Hashtable<String, ExtendableMenu>();
036:
037: private String id;
038:
039: public ExtendableMenu(String id, String label) {
040: super (label, id);
041:
042: this .id = id;
043:
044: model = new MenuModel(id);
045:
046: map.put(id, this );
047: }
048:
049: public MenuModel getMenuModel() {
050: return model;
051: }
052:
053: /**
054: * @see javax.swing.JMenu#add(javax.swing.Action)
055: */
056: public JMenuItem add(Action action) {
057: model.add(action);
058:
059: return super .add(action);
060:
061: }
062:
063: /**
064: * @see javax.swing.JMenu#add(javax.swing.JMenuItem)
065: */
066: public JMenuItem add(JMenuItem menuItem) {
067: model.add(menuItem);
068:
069: return super .add(menuItem);
070: }
071:
072: /**
073: * @see javax.swing.JMenu#addSeparator()
074: */
075: public void addSeparator() {
076: int index = getComponentCount();
077:
078: // make sure that we don't end up with two separators
079: if (index > 0 && (index - 1 < getComponentCount())) {
080: Component prev = getComponent(index - 1);
081: if (prev instanceof JSeparator)
082: return;
083: }
084:
085: model.addSeparator();
086:
087: super .addSeparator();
088: }
089:
090: /**
091: * @see javax.swing.JMenu#insert(javax.swing.Action, int)
092: */
093: public JMenuItem insert(Action action, int pos) {
094: model.insert(action, pos);
095:
096: return super .insert(action, pos);
097: }
098:
099: /**
100: * @see javax.swing.JMenu#insert(javax.swing.JMenuItem, int)
101: */
102: public JMenuItem insert(JMenuItem menuItem, int pos) {
103: model.insert(menuItem, pos);
104:
105: return super .insert(menuItem, pos);
106: }
107:
108: /**
109: * @see javax.swing.JMenu#insertSeparator(int)
110: */
111: public void insertSeparator(int index) {
112: model.insertSeparator(index);
113:
114: super .insertSeparator(index);
115: }
116:
117: public void addPlaceholder(String placeholderId) {
118: model.appendPlaceholder(placeholderId);
119: }
120:
121: public void insertPlaceholder(String placeholderId, int pos) {
122: model.insertPlaceholder(placeholderId, pos);
123: }
124:
125: public void insert(Action action, String placeholderId) {
126: int index = model.insert(action, placeholderId);
127: if (index != -1)
128: super .insert(action, index);
129: }
130:
131: public void insert(Component component, String placeholderId) {
132: int index = model.insert(component, placeholderId);
133: if (index != -1)
134: super .add(component, index);
135: }
136:
137: public void insert(JMenuItem menuItem, String placeholderId) {
138: if (menuItem == null)
139: throw new IllegalArgumentException("menuItem == null");
140: if (placeholderId == null)
141: throw new IllegalArgumentException("placeholderId == null");
142:
143: int index = model.insert(menuItem, placeholderId);
144: if (index != -1)
145: super .insert(menuItem, index);
146: }
147:
148: public void insertSeparator(String placeholderId) {
149: int index = model.insertSeparator(placeholderId);
150: // make sure that we don't end up with two separators
151: if (index > 0 && (index - 1 < getComponentCount())) {
152: Component prev = getComponent(index - 1);
153: if (prev instanceof JSeparator)
154: return;
155: }
156:
157: if (index != -1)
158: super .insertSeparator(index);
159: }
160:
161: public void add(ExtendableMenu submenu) {
162:
163: map.put(submenu.getId(), submenu);
164:
165: model.addSubmenu(submenu.getMenuModel());
166:
167: super .add((JMenu) submenu);
168: }
169:
170: /**
171: * @return Returns the id.
172: */
173: public String getId() {
174: return id;
175: }
176:
177: public Enumeration<ExtendableMenu> getSubmenuEnumeration() {
178: return map.elements();
179: }
180:
181: /**
182: * @see javax.swing.JMenu#add(java.awt.Component, int)
183: */
184: public Component add(Component component, int index) {
185: model.insert(component, index);
186:
187: return super .add(component, index);
188: }
189:
190: /**
191: * @see javax.swing.JMenu#add(java.awt.Component)
192: */
193: public Component add(Component component) {
194: model.add(component);
195:
196: return super .add(component);
197: }
198:
199: public void insertPlaceholder(String placeholderId,
200: String insertionPlaceholder) {
201: model.insertPlaceholder(placeholderId, insertionPlaceholder);
202: }
203: }
|