KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > popup > AdvancedJPopupMenu


1 /*===========================================================================
2
3 ObjectWeb Browser Framework
4 Copyright (C) 2004 USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jérôme Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.popup;
28
29 /** The namingContext's imports*/
30 import org.objectweb.util.browser.api.IconProvider;
31 import org.objectweb.util.browser.api.MenuItem;
32 import org.objectweb.util.browser.api.MenuItemTreeView;
33 import org.objectweb.util.browser.api.TreeView;
34 import org.objectweb.util.browser.core.common.ClassResolver;
35 import org.objectweb.util.browser.core.common.DynamicTree;
36 import org.objectweb.util.browser.core.icon.EmptyIconProvider;
37
38 /** The java API's imports*/
39 import java.awt.Component JavaDoc;
40 import javax.swing.Action JavaDoc;
41 import javax.swing.Icon JavaDoc;
42 import javax.swing.JPopupMenu JavaDoc;
43 import javax.swing.JMenuItem JavaDoc;
44 import javax.swing.KeyStroke JavaDoc;
45
46 public class AdvancedJPopupMenu extends JPopupMenu JavaDoc {
47
48     /** The associated tree */
49     protected DynamicTree tree_;
50
51     /** Indicates if the menu has to inherit of his parent tree menu */
52     protected boolean inheritTreeMenu_;
53
54     /** A treeview to obtain the object on which the menu item will act*/
55     protected MenuItemTreeView treeView_;
56
57     public AdvancedJPopupMenu(DynamicTree tree, MenuItemTreeView treeView, boolean inheritTreeMenu) {
58         tree_ = tree;
59         treeView_ = treeView;
60         inheritTreeMenu_ = inheritTreeMenu;
61     }
62
63     /**
64      * Default constructor
65      * @param tree The associated tree
66      * @param
67      */

68     public AdvancedJPopupMenu(DynamicTree tree, boolean inheritTreeMenu) {
69         this(tree, new DefaultMenuItemTreeView(tree), inheritTreeMenu);
70     }
71
72     /**
73      * Default constructor
74      * @param tree The associated tree
75      */

76     public AdvancedJPopupMenu(DynamicTree tree) {
77         this(tree, false);
78     }
79
80     public AdvancedJPopupMenu(DynamicTree tree, MenuItemTreeView treeView) {
81         this(tree, treeView, false);
82     }
83
84     /**
85      * Creates a JMenuItem with the given params
86      * @param label The label of the item
87      * @param action The corresponding action
88      * @param itemState Indicates the item states ()
89      * @return The created JMenuItem
90      */

91     protected JMenuItem JavaDoc makeMenuItem(String JavaDoc label, MenuItem action, int itemState, ItemProperty itemProperty) {
92         
93         IconProvider iconProvider = itemProperty.getIconProvider();
94         boolean isUserIcon = iconProvider!=null;
95         if(!isUserIcon)
96             iconProvider = new EmptyIconProvider();
97         Icon JavaDoc icon = iconProvider.newIcon(treeView_.getSelectedObject());
98            
99         KeyStroke JavaDoc keyStroke = itemProperty.getKeyStroke();
100         Action JavaDoc theAction = new DefaultAction(tree_, action, treeView_, label, icon, itemProperty.getKeyStroke(), itemProperty.getMnemonic(), isUserIcon, itemState);
101         MyJMenuItem item = new MyJMenuItem(theAction, action.getClass().getName());
102         //if(keyStroke!=null){
103
// item.setAccelerator(keyStroke);
104
//}
105

106         //MyJMenuItem item = null;
107
//if(iconProvider!=null)
108
// item = new MyJMenuItem(tree_, label, treeView_, iconProvider.newIcon(treeView_.getSelectedObject()));
109
//else
110
// item = new MyJMenuItem(tree_, label, treeView_, null);
111
//item.addActionListener(action);
112
if (itemState == MenuItem.DISABLED_STATUS)
113             item.setEnabled(false);
114         return item;
115     }
116
117     /**
118      * Indicates if the given menuItem already exists into the current menu.
119      * @param menuItem The item to test
120      * @return true if the given menuItem already exists
121      */

122     protected boolean contains(MyJMenuItem menuItem) {
123         Component JavaDoc myComponent[] = getComponents();
124         for (int i = 0; i < myComponent.length; i++)
125             if (myComponent[i] instanceof MyJMenuItem
126                 && ((MyJMenuItem) myComponent[i]).getText().equals(menuItem.getText())
127                 /*&& ((MyJMenuItem) myComponent[i]).getActionClassName().equals(menuItem.getActionClassName())*/)
128                 return true;
129         return false;
130     }
131
132     /**
133      * Returns the inheritTreeMenu value.
134      * @return true if the menu has to inherit tree menu
135      */

136     public boolean hasToInheritTreeMenu() {
137         return inheritTreeMenu_;
138     }
139
140     /**
141      * Overwritten of the container method
142      * Avoid to add two separators successively.
143      * @param comp The component to add
144      * @return The added component
145      */

146     public Component JavaDoc add(Component JavaDoc comp) {
147         if (getComponentCount() > 0) {
148             Component JavaDoc c = getComponent(getComponentCount() - 1);
149             if (!(c instanceof javax.swing.JPopupMenu.Separator && comp instanceof javax.swing.JPopupMenu.Separator))
150                 return super.add(comp);
151         } else if (!(getComponentCount() == 0 && comp instanceof javax.swing.JPopupMenu.Separator))
152             return super.add(comp);
153         return comp;
154     }
155
156     /**
157      * Add all the menu's items into the current menu.
158      * If an item already exists, this one is not included
159      * @param popupMenu The menu to add
160      */

161     public void add(JPopupMenu JavaDoc popupMenu) {
162         if (popupMenu != null) {
163             Component JavaDoc toAdd[] = popupMenu.getComponents();
164             if (toAdd.length > 0) {
165                 addSeparator();
166                 for (int i = 0; i < toAdd.length; i++) {
167                     if (((toAdd[i] instanceof MyJMenuItem) && !contains((MyJMenuItem) toAdd[i])) || !(toAdd[i] instanceof MyJMenuItem))
168                         add(toAdd[i]);
169                 }
170                 // Remove the last component if it's a Separator instance
171
Component JavaDoc c = getComponent(getComponentCount() - 1);
172                 if (c instanceof javax.swing.JPopupMenu.Separator)
173                     remove(c);
174             }
175         }
176     }
177
178     /**
179      * Adds an item into the current menu
180      * @param label Label of the item
181      * @param className The name of the class to instanciate
182      * @param tree The associated tree
183      */

184     public void addMenuItem(String JavaDoc label, ItemProperty itemProperty, DynamicTree tree) {
185         if(label!=null && itemProperty!=null){
186             String JavaDoc className = itemProperty.getClassName();
187             if(label.startsWith("-separator-")){
188                 addSeparator();
189             } else if (className != null && !className.equals("")) {
190                 MenuItem action = null;
191                 try {
192                     action = (MenuItem) ClassResolver.resolve(className).newInstance();
193                 } catch (java.lang.ClassNotFoundException JavaDoc e) {
194                     System.out.println("Error : " + className + " : Class Not Found !");
195                 } catch (java.lang.InstantiationException JavaDoc e) {
196                     e.printStackTrace();
197                 } catch (java.lang.IllegalAccessException JavaDoc e) {
198                     e.printStackTrace();
199                 } catch (java.lang.ClassCastException JavaDoc e) {
200                     System.out.println("Error : " + className + " : Not a org.objectweb.util.browser.api.MenuItem implementation");
201                 }
202                 if (action != null){
203                     int itemState = action.getStatus(treeView_);
204                     if(itemState==MenuItem.DISABLED_STATUS || itemState==MenuItem.ENABLED_STATUS)
205                         super.add(makeMenuItem(label, action, itemState, itemProperty));
206                     else if (itemState==MenuItem.NOT_VISIBLE_STATUS){
207                         // Nothing to do
208
}
209                 }
210             }
211         }
212     }
213     
214     /**
215      * Fixes the treeView to use depending on the context. It can be a treeView provided by a JTree or by a JTable.
216      * @param treeView The TreeView to use.
217      */

218     public void setTreeView(TreeView treeView){
219         
220     }
221 }
222
Popular Tags