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.globalactions;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.util.Enumeration;
023: import java.util.Vector;
024:
025: import javax.swing.JMenuItem;
026:
027: import org.columba.api.gui.frame.IFrameMediator;
028: import org.columba.api.plugin.IExtension;
029: import org.columba.api.plugin.IExtensionHandler;
030: import org.columba.api.plugin.IExtensionHandlerKeys;
031: import org.columba.api.plugin.PluginException;
032: import org.columba.api.plugin.PluginHandlerNotFoundException;
033: import org.columba.api.plugin.PluginLoadingFailedException;
034: import org.columba.core.gui.action.AbstractColumbaAction;
035: import org.columba.core.gui.base.CMenuItem;
036: import org.columba.core.gui.frame.FrameManager;
037: import org.columba.core.gui.menu.IMenu;
038: import org.columba.core.plugin.PluginManager;
039:
040: public class NewSubmenu extends IMenu implements ActionListener {
041:
042: public NewSubmenu(IFrameMediator controller) {
043: super (controller, controller.getString("menu", "mainframe",
044: "menu_file_new"), "NewSubMenu");
045:
046: IExtensionHandler frameHandler = null;
047: try {
048: frameHandler = PluginManager
049: .getInstance()
050: .getExtensionHandler(
051: IExtensionHandlerKeys.ORG_COLUMBA_CORE_FRAME);
052: } catch (PluginHandlerNotFoundException e) {
053: e.printStackTrace();
054: return;
055: }
056:
057: IExtensionHandler newItemHandler = null;
058: try {
059: newItemHandler = PluginManager
060: .getInstance()
061: .getExtensionHandler(
062: IExtensionHandlerKeys.ORG_COLUMBA_CORE_NEWITEM);
063: } catch (PluginHandlerNotFoundException e) {
064: e.printStackTrace();
065: return;
066: }
067:
068: // create menuitems for all registered frame extensions
069: String[] managedFrames = getManagedFrames(frameHandler);
070: for (int i = 0; i < managedFrames.length; i++) {
071: JMenuItem menu = createMenu(managedFrames[i],
072: managedFrames[i]);
073: add(menu);
074: }
075:
076: addSeparator();
077:
078: // create menuitems for all registered new items
079: String[] ids = getAllItems(newItemHandler);
080: for (int i = 0; i < ids.length; i++) {
081: JMenuItem item = createNewItemMenuItem(newItemHandler,
082: ids[i]);
083: if (item == null)
084: continue;
085: add(item);
086: }
087:
088: }
089:
090: private JMenuItem createNewItemMenuItem(
091: IExtensionHandler newItemHandler, String id) {
092: AbstractColumbaAction action = null;
093: IExtension extension = newItemHandler.getExtension(id);
094: if (extension == null)
095: return null;
096:
097: try {
098: action = (AbstractColumbaAction) extension
099: .instanciateExtension(new Object[] { getFrameMediator() });
100: return new CMenuItem(action);
101: } catch (PluginException e) {
102: e.printStackTrace();
103: return null;
104: }
105: }
106:
107: private JMenuItem createMenu(String name, String actionCommand) {
108: JMenuItem menu = new CMenuItem(name);
109: menu.setActionCommand(actionCommand);
110: menu.addActionListener(this );
111: return menu;
112: }
113:
114: public String[] getManagedFrames(IExtensionHandler handler) {
115: Vector<String> result = new Vector<String>();
116: Enumeration _enum = handler.getExtensionEnumeration();
117: while (_enum.hasMoreElements()) {
118: IExtension extension = (IExtension) _enum.nextElement();
119: String managed = extension.getMetadata().getAttribute(
120: "managed");
121: if (managed == null)
122: managed = "false";
123:
124: if (managed.equals("true"))
125: result.add(extension.getMetadata().getId());
126: }
127: return (String[]) result.toArray(new String[0]);
128: }
129:
130: public String[] getAllItems(IExtensionHandler handler) {
131: Vector<String> result = new Vector<String>();
132: Enumeration _enum = handler.getExtensionEnumeration();
133: while (_enum.hasMoreElements()) {
134: IExtension extension = (IExtension) _enum.nextElement();
135: result.add(extension.getMetadata().getId());
136: }
137: return (String[]) result.toArray(new String[0]);
138: }
139:
140: public void actionPerformed(ActionEvent event) {
141: final String action = event.getActionCommand();
142:
143: try {
144: FrameManager.getInstance().openView(action);
145: } catch (PluginLoadingFailedException e) {
146: e.printStackTrace();
147: }
148:
149: }
150:
151: }
|