01: /*
02: The contents of this file are subject to the Mozilla Public License Version 1.1
03: (the "License"); you may not use this file except in compliance with the
04: License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
05:
06: Software distributed under the License is distributed on an "AS IS" basis,
07: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
08: for the specific language governing rights and
09: 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 Stich.
14: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15:
16: All Rights Reserved.
17: */
18: package org.columba.core.scripting.extensions;
19:
20: import java.util.logging.Logger;
21:
22: import javax.swing.JFrame;
23:
24: import org.columba.api.gui.frame.IContainer;
25: import org.columba.core.gui.action.AbstractColumbaAction;
26: import org.columba.core.gui.frame.FrameManager;
27: import org.columba.core.gui.menu.ExtendableMenuBar;
28:
29: /**
30: * Insert action in menu.
31: * <p>
32: * Lookup <code>menuId</code> and <code>placeholderId</code> in
33: * <code>org.columba.core.action.menu.xml</code>.
34: * <p>
35: * TODO (@author fdietz): we should consider adding public constants here or to
36: * ExtendableMenuBar to make it easier for users to find insertion
37: * points.
38: *
39: * @author Celso Pinto (cpinto@yimports.com)
40: */
41: public class MenuExtensionPoint extends AbstractExtensionPoint {
42:
43: public static final String EXTENSION_POINT_ID = "main_menu";
44:
45: private static final Logger LOG = Logger
46: .getLogger(MenuExtensionPoint.class.getName());
47:
48: public MenuExtensionPoint() {
49: super (EXTENSION_POINT_ID);
50: }
51:
52: /**
53: * Add an action at the end or beginning of the menu.
54: *
55: * @param action
56: * Your action
57: * @param menuId
58: * A menu identifier that will be used to get the menu where the
59: * action will be added.
60: * @param position
61: * Use AbstractExtensionPoint.POSITION_BEGINNING or POSITION_END
62: * values
63: */
64: public void addAction(AbstractColumbaAction action, String menuId,
65: String placeholderId) {
66:
67: ExtendableMenuBar menu = getDefaultMenubar();
68: if (menu == null) {
69: /* TODO create exception for this */
70: throw new RuntimeException(
71: "Could not retrieve default menu bar");
72: }
73:
74: menu.insertAction(menuId, placeholderId, action);
75:
76: }
77:
78: private IContainer getFirstContainer() {
79: IContainer[] frames = FrameManager.getInstance()
80: .getOpenFrames();
81: if (frames.length == 0) {
82: LOG.warning(getClass().getName()
83: + ": Not enough open frames!");
84: return null;
85: }
86: return frames[0];
87: }
88:
89: private ExtendableMenuBar getDefaultMenubar() {
90: JFrame frame = (JFrame) getFirstContainer();
91: return (ExtendableMenuBar) frame.getJMenuBar();
92: }
93: }
|