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.netbeans.modules.websvc.core.WebServiceActionProvider; 26 import org.openide.loaders.DataObject; 27 import org.openide.nodes.Node; 28 import org.openide.util.HelpCtx; 29 import org.openide.util.Lookup; 30 import org.openide.util.NbBundle; 31 import org.openide.util.actions.Presenter; 32 import org.openide.util.actions.NodeAction; 33 import org.openide.util.actions.SystemAction; 34 35 36 public class WebServiceActionGroup extends NodeAction implements Presenter.Popup 37 { 38 public String getName() { 39 return NbBundle.getMessage(WebServiceActionGroup.class, "LBL_WebServiceActionGroup"); 40 } 41 42 43 private static final SystemAction[] grouped() { 44 return new SystemAction[] { 45 SystemAction.get(AddOperationEditorAction.class), 46 }; 47 } 48 49 public JMenuItem getPopupPresenter() { 50 Node[] activatedNodes = getActivatedNodes(); 51 if (activatedNodes.length == 1 && 52 hasWebService()){ 53 return new LazyMenu(); 54 } 55 JMenuItem i = super.getPopupPresenter(); 56 i.setVisible(false); 57 return i; 58 } 59 60 public HelpCtx getHelpCtx() { 61 return HelpCtx.DEFAULT_HELP; 62 } 65 66 protected boolean enable(org.openide.nodes.Node[] activatedNodes) { 67 return true; 68 } 69 70 protected void performAction(org.openide.nodes.Node[] activatedNodes) { 71 assert false : "Should never be called: "; 72 } 73 74 78 private boolean hasWebService() { 79 Node[] activatedNodes = getActivatedNodes(); 80 DataObject dobj = (DataObject)activatedNodes[0].getLookup().lookup(DataObject.class); 81 if(dobj != null) { 82 86 return (WebServiceActionProvider.getAddOperationAction(dobj.getPrimaryFile()) != null); 87 92 } 93 return false; 94 } 95 96 public Action createContextAwareInstance(Lookup actionContext) { 97 boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().<Node>toArray(new Node[0])); 98 return enable ? this : null; 99 } 100 101 104 private final class LazyMenu extends JMenu { 105 106 public LazyMenu() { 107 super(WebServiceActionGroup.this.getName()); 108 } 109 110 public JPopupMenu getPopupMenu() { 111 if (getItemCount() == 0) { 112 SystemAction[] grouped = grouped(); 113 for (int i = 0; i < grouped.length; i++) { 114 SystemAction action = grouped[i]; 115 if (action == null) { 116 addSeparator(); 117 } else if (action instanceof Presenter.Popup) { 118 add(((Presenter.Popup)action).getPopupPresenter()); 119 } else { 120 assert false : "Action had no popup presenter: " + action; 121 } 122 } 123 } 124 return super.getPopupMenu(); 125 } 126 127 } 128 } 129 | Popular Tags |