KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > EnterpriseRefActionGroup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
21 import javax.swing.*;
22 import org.openide.loaders.DataObject;
23 import org.openide.nodes.Node;
24
25 import org.openide.util.*;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer;
29 import org.openide.util.actions.Presenter;
30 import org.openide.util.actions.NodeAction;
31 import org.openide.util.actions.SystemAction;
32
33 /**
34  * Action which just holds a few other SystemAction's for grouping purposes.
35  * @author cwebster
36  */

37 public class EnterpriseRefActionGroup extends NodeAction implements Presenter.Popup {
38     
39     public String JavaDoc getName() {
40         return NbBundle.getMessage(EnterpriseRefActionGroup.class, "LBL_EnterpriseActionGroup");
41     }
42     
43     /** List of system actions to be displayed within this one's toolbar or submenu. */
44     private static final SystemAction[] grouped() {
45         return new SystemAction[] {
46             SystemAction.get(CallEjbAction.class),
47             SystemAction.get(UseDatabaseAction.class),
48             SystemAction.get(SendJMSMessageAction.class),
49             SystemAction.get(SendEmailAction.class)
50         };
51     }
52     
53     public JMenuItem getPopupPresenter() {
54         boolean oneNodeSelected = getActivatedNodes().length == 1;
55         if (oneNodeSelected && hasEnterpriseRefStrategy()) {
56             return new LazyMenu();
57         }
58         JMenuItem jMenuItem = super.getPopupPresenter();
59         jMenuItem.setVisible(false);
60         return jMenuItem;
61     }
62     
63     public HelpCtx getHelpCtx() {
64         return HelpCtx.DEFAULT_HELP;
65         // If you will provide context help then use:
66
// return new HelpCtx(PromoteBusinessMethodAction.class);
67
}
68     
69     protected boolean enable(org.openide.nodes.Node[] activatedNodes) {
70         return true;
71     }
72     
73     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
74          assert false : "Should never be called: ";
75     }
76     
77     private Project getSelectedProject() {
78         Node[] activatedNodes = getActivatedNodes();
79         DataObject dobj = (DataObject)
80             activatedNodes[0].getLookup().lookup(DataObject.class);
81         
82         return dobj == null ? null:FileOwnerQuery.getOwner(dobj.getPrimaryFile());
83     }
84     
85     private boolean hasEnterpriseRefStrategy() {
86         Project project = getSelectedProject();
87         return project != null &&
88             project.getLookup().lookup(EnterpriseReferenceContainer.class) != null;
89     }
90
91     /**
92      * Avoids constructing submenu until it will be needed.
93      */

94     private final class LazyMenu extends JMenu {
95         
96         public LazyMenu() {
97             super(EnterpriseRefActionGroup.this.getName());
98         }
99         
100         public JPopupMenu getPopupMenu() {
101             if (getItemCount() == 0) {
102                 Action[] grouped = grouped();
103                 for (int i = 0; i < grouped.length; i++) {
104                     Action action = grouped[i];
105                     if (action == null && getItemCount() != 0) {
106                         addSeparator();
107                     } else {
108                         if (action instanceof ContextAwareAction) {
109                             action = ((ContextAwareAction)action).createContextAwareInstance(Utilities.actionsGlobalContext());
110                         }
111                         if (action instanceof Presenter.Popup) {
112                             add(((Presenter.Popup)action).getPopupPresenter());
113                         }
114                     }
115                 }
116             }
117             return super.getPopupMenu();
118         }
119     }
120     
121 }
122
Popular Tags