1 19 package org.netbeans.modules.websvc.core.webservices.action; 20 21 import javax.swing.Action ; 22 import javax.swing.JMenu ; 23 import javax.swing.JMenuItem ; 24 import javax.swing.JPopupMenu ; 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 39 public class WebServiceClientActionGroup extends NodeAction implements Presenter.Popup { 40 41 public String getName() { 42 return NbBundle.getMessage(WebServiceClientActionGroup.class, "LBL_WebServiceClientActionGroup"); } 44 45 46 private static final SystemAction[] grouped() { 47 return new SystemAction[] { 48 SystemAction.get(InvokeOperationAction.class), 49 }; 50 } 51 52 public JMenuItem getPopupPresenter() { 53 Node[] activatedNodes = getActivatedNodes(); 54 if(activatedNodes.length == 1 && hasWebServiceClient()) { 55 return new LazyMenu(); 56 } 57 JMenuItem i = super.getPopupPresenter(); 58 i.setVisible(false); 59 return i; 60 } 61 62 public HelpCtx getHelpCtx() { 63 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 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 return true; 89 } 90 } 91 return false; 92 } 93 94 public Action 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 102 private final class LazyMenu extends JMenu { 103 104 public LazyMenu() { 105 super(WebServiceClientActionGroup.this.getName()); 106 } 107 108 public JPopupMenu 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 |