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: import java.util.logging.Logger;
024:
025: import javax.swing.Action;
026: import javax.swing.JMenuItem;
027:
028: import org.columba.core.logging.Logging;
029:
030: public class MenuModel {
031:
032: private static final Logger LOG = Logger
033: .getLogger("org.columba.core.gui.menu"); //$NON-NLS-1$
034:
035: private IMenuElement rootElement;
036:
037: private Hashtable placeholders = new Hashtable();
038:
039: private Hashtable submenues = new Hashtable();
040:
041: private String id;
042:
043: public MenuModel(String id) {
044: this .id = id;
045:
046: rootElement = MenuElementFactory.createMenuElement(id,
047: "rootLabel");
048: submenues.put(id, this );
049: }
050:
051: public void insert(Action action, int position) {
052: insert(position, MenuElementFactory.createActionElement(action));
053: }
054:
055: public void insert(Component component, int position) {
056: insert(position, MenuElementFactory
057: .createComponentElement(component));
058: }
059:
060: public void insert(JMenuItem menuItem, int position) {
061: insert(position, MenuElementFactory
062: .createMenuItemElement(menuItem));
063: }
064:
065: public void insertSeparator(int position) {
066: insert(position, MenuElementFactory.createSeparatorElement());
067: }
068:
069: public void insertPlaceholder(String placeholderId, int position) {
070: insert(position, MenuElementFactory
071: .createPlaceholderElement(placeholderId));
072: }
073:
074: protected void insert(int position, IMenuElement element) {
075: rootElement.insert(element, position);
076:
077: if (element.isPlaceholder()) {
078: String id = ((MenuElement) element).getPlaceholderId();
079: if (placeholders.containsKey(id))
080: throw new IllegalArgumentException("placeholder id <"
081: + id + "> already used.");
082:
083: placeholders.put(id, element);
084: }
085: }
086:
087: protected void append(IMenuElement element) {
088:
089: rootElement.add(element);
090:
091: if (element.isPlaceholder()) {
092: String id = ((MenuElement) element).getPlaceholderId();
093: if (placeholders.containsKey(id))
094: throw new IllegalArgumentException("placeholder id <"
095: + id + "> already used.");
096:
097: placeholders.put(id, element);
098: }
099: }
100:
101: protected int insert(IMenuElement element, String placeholderId) {
102: if (element == null)
103: throw new IllegalArgumentException("element == null");
104: if (placeholderId == null)
105: throw new IllegalArgumentException(
106: "placeholderId == null, for element "
107: + element.toString());
108:
109: if (placeholders.containsKey(placeholderId) == false) {
110: if (Logging.DEBUG)
111: printDebugPlaceholders();
112:
113: LOG.severe("no matching placeholder with id <"
114: + placeholderId + "> in menu <" + getId()
115: + "> found.");
116:
117: }
118:
119: IMenuElement placeholderElement = (IMenuElement) placeholders
120: .get(placeholderId);
121:
122: int index = rootElement.indexOf(placeholderElement);
123:
124: if (index != -1)
125: insert(index, element);
126:
127: return index;
128: }
129:
130: /**
131: *
132: */
133: private void printDebugPlaceholders() {
134: Enumeration e = placeholders.elements();
135: while (e.hasMoreElements()) {
136: LOG.info(((IMenuElement) e.nextElement()).toString());
137: }
138: }
139:
140: /**
141: * @return Returns the id.
142: */
143: public String getId() {
144: return id;
145: }
146:
147: /** ****************** public helper methods ******************* */
148:
149: public void add(Component component) {
150: append(MenuElementFactory.createComponentElement(component));
151: }
152:
153: public void add(Action action) {
154: append(MenuElementFactory.createActionElement(action));
155: }
156:
157: public void add(JMenuItem menuItem) {
158: append(MenuElementFactory.createMenuItemElement(menuItem));
159: }
160:
161: public void addSeparator() {
162: append(MenuElementFactory.createSeparatorElement());
163: }
164:
165: public int insertSeparator(String placeholderId) {
166: int index = insert(MenuElementFactory.createSeparatorElement(),
167: placeholderId);
168: return index;
169: }
170:
171: public void appendPlaceholder(String placeholderId) {
172: append(MenuElementFactory
173: .createPlaceholderElement(placeholderId));
174: }
175:
176: public MenuModel getSubmenu(String submenuId) {
177: if (submenues.containsKey(submenuId) == false)
178: throw new IllegalArgumentException(
179: "no matching menu with id " + submenuId + " found.");
180: return (MenuModel) submenues.get(submenuId);
181: }
182:
183: public void addSubmenu(MenuModel submenu) {
184: rootElement.add(submenu.getRootElement());
185: submenues.put(submenu.getId(), submenu);
186:
187: }
188:
189: protected IMenuElement getRootElement() {
190: return rootElement;
191: }
192:
193: public int insert(Action action, String placeholderId) {
194: int index = insert(MenuElementFactory
195: .createActionElement(action), placeholderId);
196:
197: return index;
198: }
199:
200: public int insert(JMenuItem menuItem, String placeholderId) {
201: int index = insert(MenuElementFactory
202: .createMenuItemElement(menuItem), placeholderId);
203: return index;
204: }
205:
206: public Enumeration getSubmenuEnumeration() {
207: return submenues.elements();
208: }
209:
210: public void insertPlaceholder(String placeholderId,
211: String insertionPlaceholder) {
212: insert(MenuElementFactory
213: .createPlaceholderElement(placeholderId),
214: insertionPlaceholder);
215: }
216:
217: public int insert(Component component, String placeholderId) {
218: int index = insert(MenuElementFactory
219: .createComponentElement(component), placeholderId);
220: return index;
221: }
222: }
|