KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > action > EJBActionGroup


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.ejb.action;
21
22 import java.io.IOException JavaDoc;
23 import javax.lang.model.element.TypeElement;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.JMenu JavaDoc;
26 import javax.swing.JMenuItem JavaDoc;
27 import javax.swing.JPopupMenu JavaDoc;
28 import org.netbeans.api.java.source.ElementHandle;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
31 import org.openide.nodes.Node;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
35 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EjbMethodController;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.util.ContextAwareAction;
39 import org.openide.util.HelpCtx;
40 import org.openide.util.Lookup;
41 import org.openide.util.NbBundle;
42 import org.openide.util.actions.Presenter;
43 import org.openide.util.actions.NodeAction;
44 import org.openide.util.actions.SystemAction;
45
46 /**
47  * Action which just holds a few other SystemAction's for grouping purposes.
48  * @author cwebster
49  */

50 public class EJBActionGroup extends NodeAction implements Presenter.Popup {
51     
52     Lookup actionContext;
53     
54     public String JavaDoc getName() {
55         return NbBundle.getMessage(EJBActionGroup.class, "LBL_EJBActionGroup");
56     }
57     
58     /** List of system actions to be displayed within this one's toolbar or submenu. */
59     protected Action JavaDoc[] grouped() {
60         return new Action JavaDoc[] {
61             SystemAction.get(ExposeInLocalAction.class),
62             SystemAction.get(ExposeInRemoteAction.class),
63             null,
64             new AddBusinessMethodAction(),
65             new AddCreateMethodAction(),
66             new AddFinderMethodAction(),
67             new AddHomeMethodAction(),
68             new AddSelectMethodAction(),
69             SystemAction.get(AddCmpFieldAction.class)
70         };
71     }
72     
73     public JMenuItem JavaDoc getPopupPresenter() {
74         if (isEnabled() && isEjbProject(getActivatedNodes())) {
75             return getMenu();
76         }
77         JMenuItem JavaDoc jMenuItem = super.getPopupPresenter();
78         jMenuItem.setVisible(false);
79         return jMenuItem;
80     }
81     
82     protected JMenu JavaDoc getMenu() {
83         return new LazyMenu(actionContext);
84     }
85     
86     public HelpCtx getHelpCtx() {
87         return HelpCtx.DEFAULT_HELP;
88         // If you will provide context help then use:
89
// return new HelpCtx(PromoteBusinessMethodAction.class);
90
}
91     
92     protected boolean enable(org.openide.nodes.Node[] activatedNodes) {
93         if (activatedNodes.length != 1) {
94             return false;
95         }
96         FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class);
97         if (fileObject == null) {
98             return false;
99         }
100         JavaSource javaSource = JavaSource.forFileObject(fileObject);
101         String JavaDoc className = null;
102         try {
103             if (javaSource == null) {
104                 return false;
105             }
106             ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]);
107             if (elementHandle != null) {
108                 className = elementHandle.getQualifiedName();
109             }
110         } catch (IOException JavaDoc ex) {
111             ErrorManager.getDefault().notify(ex);
112         }
113         EjbMethodController ejbMethodController = null;
114         if (className != null) {
115              ejbMethodController = EjbMethodController.createFromClass(fileObject, className);
116         }
117         return ejbMethodController != null;
118     }
119     
120     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
121         // do nothing -- should never be called
122
}
123     
124     public boolean isEjbProject(Node[] activatedNodes) {
125         return activatedNodes.length == 1 &&
126                isContainingProjectEjb(activatedNodes[0].getLookup().lookup(FileObject.class));
127     }
128     
129     private static boolean isContainingProjectEjb(FileObject fileObject) {
130         if (fileObject == null) {
131             return false;
132         }
133         Project project = FileOwnerQuery.getOwner(fileObject);
134         if (project == null) {
135             return false;
136         }
137         return EjbJar.getEjbJars(project).length > 0;
138     }
139     
140     /** Implements <code>ContextAwareAction</code> interface method. */
141     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
142         this.actionContext = actionContext;
143         return super.createContextAwareInstance(actionContext);
144     }
145
146     /**
147      * Avoids constructing submenu until it will be needed.
148      */

149     private final class LazyMenu extends JMenu JavaDoc {
150         
151         private final Lookup lookup;
152         
153         public LazyMenu(Lookup lookup) {
154             super(EJBActionGroup.this.getName());
155             this.lookup = lookup;
156         }
157         
158         public JPopupMenu JavaDoc getPopupMenu() {
159             if (getItemCount() == 0) {
160                 Action JavaDoc[] grouped = grouped();
161                 for (int i = 0; i < grouped.length; i++) {
162                     Action JavaDoc action = grouped[i];
163                     if (action == null && getItemCount() != 0) {
164                         addSeparator();
165                     } else {
166                         if (action instanceof ContextAwareAction) {
167                             action = ((ContextAwareAction)action).createContextAwareInstance(lookup);
168                         }
169                         if (action instanceof Presenter.Popup) {
170                             add(((Presenter.Popup)action).getPopupPresenter());
171                         }
172                     }
173                 }
174             }
175             return super.getPopupMenu();
176         }
177     }
178     
179 }
180
Popular Tags