KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > webservices > action > WebServiceClientActionGroup


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 package org.netbeans.modules.websvc.core.webservices.action;
20
21 import javax.swing.Action JavaDoc;
22 import javax.swing.JMenu JavaDoc;
23 import javax.swing.JMenuItem JavaDoc;
24 import javax.swing.JPopupMenu JavaDoc;
25 import org.openide.loaders.DataObject;
26 import org.openide.nodes.Node;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.Presenter;
30 import org.openide.util.actions.NodeAction;
31 import org.openide.util.actions.SystemAction;
32 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
33 import org.openide.util.Lookup;
34
35 /**
36  *
37  * @author Peter Williams
38  */

39 public class WebServiceClientActionGroup extends NodeAction implements Presenter.Popup {
40     
41     public String JavaDoc getName() {
42             return NbBundle.getMessage(WebServiceClientActionGroup.class, "LBL_WebServiceClientActionGroup"); // NOI18N
43
}
44
45     /** List of system actions to be displayed within this one's toolbar or submenu. */
46     private static final SystemAction[] grouped() {
47         return new SystemAction[] {
48             SystemAction.get(InvokeOperationAction.class),
49         };
50     }
51
52     public JMenuItem JavaDoc getPopupPresenter() {
53         Node[] activatedNodes = getActivatedNodes();
54         if(activatedNodes.length == 1 && hasWebServiceClient()) {
55             return new LazyMenu();
56         }
57         JMenuItem JavaDoc i = super.getPopupPresenter();
58         i.setVisible(false);
59         return i;
60     }
61
62     public HelpCtx getHelpCtx() {
63         // If you will provide context help then use:
64
// return new HelpCtx(PromoteBusinessMethodAction.class);
65
return HelpCtx.DEFAULT_HELP;
66     }
67
68     protected boolean enable(org.openide.nodes.Node[] activatedNodes) {
69         return true;
70     }
71
72     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
73         assert false : "Should never be called: ";
74     }
75
76     /**
77      * Returns true if this node is in a project that has any web service clients
78      * added to it.
79      */

80     private boolean hasWebServiceClient() {
81         Node[] activatedNodes = getActivatedNodes();
82         DataObject dobj = (DataObject)activatedNodes[0].getLookup().lookup(DataObject.class);
83         if(dobj != null) {
84             WebServicesClientSupport clientSupport = WebServicesClientSupport.getWebServicesClientSupport(dobj.getPrimaryFile());
85             if(clientSupport != null) {
86                 // !PW FIXME add code to confirm that the project actually has
87
// clients added to it.
88
return true;
89             }
90         }
91         return false;
92     }
93
94     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
95         boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().<Node>toArray(new Node[0]));
96         return enable ? this : null;
97     }
98     
99     /**
100      * Avoids constructing submenu until it will be needed.
101      */

102     private final class LazyMenu extends JMenu JavaDoc {
103         
104         public LazyMenu() {
105             super(WebServiceClientActionGroup.this.getName());
106         }
107         
108         public JPopupMenu JavaDoc getPopupMenu() {
109             if (getItemCount() == 0) {
110                 SystemAction[] grouped = grouped();
111                 for (int i = 0; i < grouped.length; i++) {
112                     SystemAction action = grouped[i];
113                     if (action == null) {
114                         addSeparator();
115                     } else if (action instanceof Presenter.Popup) {
116                         add(((Presenter.Popup)action).getPopupPresenter());
117                     } else {
118                         assert false : "Action had no popup presenter: " + action;
119                     }
120                 }
121             }
122             return super.getPopupMenu();
123         }
124  
125     }
126      
127 }
128
Popular Tags