KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > extensions > MenuExtensionPoint


1 /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1
3   (the "License"); you may not use this file except in compliance with the
4   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5   
6   Software distributed under the License is distributed on an "AS IS" basis,
7   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8   for the specific language governing rights and
9   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 JavaDoc;
21
22 import javax.swing.JFrame JavaDoc;
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 JavaDoc EXTENSION_POINT_ID = "main_menu";
44
45     private static final Logger JavaDoc LOG = Logger.getLogger(MenuExtensionPoint.class
46             .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 JavaDoc menuId,
65             String JavaDoc placeholderId) {
66
67         ExtendableMenuBar menu = getDefaultMenubar();
68         if (menu == null) {
69             /* TODO create exception for this */
70             throw new RuntimeException JavaDoc("Could not retrieve default menu bar");
71         }
72
73         menu.insertAction(menuId, placeholderId, action);
74
75     }
76
77     private IContainer getFirstContainer() {
78         IContainer[] frames = FrameManager.getInstance().getOpenFrames();
79         if (frames.length == 0) {
80             LOG.warning(getClass().getName() + ": Not enough open frames!");
81             return null;
82         }
83         return frames[0];
84     }
85
86     private ExtendableMenuBar getDefaultMenubar() {
87         JFrame JavaDoc frame = (JFrame JavaDoc) getFirstContainer();
88         return (ExtendableMenuBar) frame.getJMenuBar();
89     }
90 }
91
Popular Tags