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.ButtonGroup;
026: import javax.swing.JRadioButtonMenuItem;
027:
028: import org.columba.api.gui.frame.IContainer;
029: import org.columba.api.gui.frame.IFrameMediator;
030: import org.columba.api.plugin.IExtension;
031: import org.columba.api.plugin.IExtensionHandler;
032: import org.columba.api.plugin.IExtensionHandlerKeys;
033: import org.columba.api.plugin.PluginHandlerNotFoundException;
034: import org.columba.api.plugin.PluginLoadingFailedException;
035: import org.columba.core.gui.frame.DefaultFrameController;
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: /**
041: * @author fdietz
042: *
043: */
044: public class SwitchPerspectiveSubmenu extends IMenu implements
045: ActionListener {
046:
047: private ButtonGroup group;
048:
049: private IExtensionHandler handler;
050:
051: /**
052: * @param controller
053: * @param caption
054: */
055: public SwitchPerspectiveSubmenu(IFrameMediator controller) {
056: super (controller, "Window",
057: ((DefaultFrameController) controller).getViewItem()
058: .get("id"));
059:
060: String id = ((DefaultFrameController) getFrameMediator())
061: .getViewItem().get("id");
062:
063: // check if this is a management frame instance
064: // -> if so create submenu to switch perspectives
065: // -> otherwise, don't create submenu
066: boolean isManagedFrame = false;
067:
068: try {
069: handler = PluginManager.getInstance().getExtensionHandler(
070: IExtensionHandlerKeys.ORG_COLUMBA_CORE_FRAME);
071: } catch (PluginHandlerNotFoundException e) {
072: e.printStackTrace();
073: }
074:
075: String[] managedFrames = null;
076: if (id != null) {
077: managedFrames = getManagedFrames(handler);
078: for (int i = 0; i < managedFrames.length; i++) {
079: if (id.equals(managedFrames[i]))
080: isManagedFrame = true;
081: }
082: }
083:
084: if (!isManagedFrame) {
085: setEnabled(false);
086: return;
087: }
088:
089: group = new ButtonGroup();
090:
091: for (int i = 0; i < managedFrames.length; i++) {
092: JRadioButtonMenuItem menu = createMenu(managedFrames[i],
093: managedFrames[i]);
094: if (id.equals(managedFrames[i]))
095: menu.setSelected(true);
096:
097: add(menu);
098: }
099:
100: }
101:
102: public String[] getManagedFrames(IExtensionHandler handler) {
103:
104: Vector result = new Vector();
105: Enumeration _enum = handler.getExtensionEnumeration();
106: while (_enum.hasMoreElements()) {
107: IExtension extension = (IExtension) _enum.nextElement();
108: String managed = extension.getMetadata().getAttribute(
109: "managed");
110: if (managed == null)
111: managed = "false";
112:
113: if (managed.equals("true"))
114: result.add(extension.getMetadata().getId());
115:
116: }
117:
118: return (String[]) result.toArray(new String[0]);
119:
120: }
121:
122: /**
123: * @return
124: */
125: private JRadioButtonMenuItem createMenu(String name,
126: String actionCommand) {
127: JRadioButtonMenuItem menu = new JRadioButtonMenuItem(name);
128: group.add(menu);
129: menu.setActionCommand(actionCommand);
130: menu.addActionListener(this );
131: return menu;
132: }
133:
134: /**
135: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
136: */
137: public void actionPerformed(ActionEvent arg0) {
138: final String action = arg0.getActionCommand();
139:
140: // awt-event-thread
141: javax.swing.SwingUtilities.invokeLater(new Runnable() {
142: public void run() {
143:
144: IFrameMediator mediator = getFrameMediator();
145:
146: IContainer container = mediator.getContainer();
147:
148: try {
149: FrameManager.getInstance().switchView(container,
150: action);
151: } catch (PluginLoadingFailedException e) {
152: e.printStackTrace();
153: }
154: }
155: });
156:
157: }
158:
159: }
|