1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action; 21 22 import java.io.IOException ; 23 import javax.lang.model.element.TypeElement; 24 import javax.swing.Action ; 25 import javax.swing.JMenu ; 26 import javax.swing.JMenuItem ; 27 import javax.swing.JPopupMenu ; 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 50 public class EJBActionGroup extends NodeAction implements Presenter.Popup { 51 52 Lookup actionContext; 53 54 public String getName() { 55 return NbBundle.getMessage(EJBActionGroup.class, "LBL_EJBActionGroup"); 56 } 57 58 59 protected Action [] grouped() { 60 return new Action [] { 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 getPopupPresenter() { 74 if (isEnabled() && isEjbProject(getActivatedNodes())) { 75 return getMenu(); 76 } 77 JMenuItem jMenuItem = super.getPopupPresenter(); 78 jMenuItem.setVisible(false); 79 return jMenuItem; 80 } 81 82 protected JMenu getMenu() { 83 return new LazyMenu(actionContext); 84 } 85 86 public HelpCtx getHelpCtx() { 87 return HelpCtx.DEFAULT_HELP; 88 } 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 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 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 } 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 141 public Action createContextAwareInstance(Lookup actionContext) { 142 this.actionContext = actionContext; 143 return super.createContextAwareInstance(actionContext); 144 } 145 146 149 private final class LazyMenu extends JMenu { 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 getPopupMenu() { 159 if (getItemCount() == 0) { 160 Action [] grouped = grouped(); 161 for (int i = 0; i < grouped.length; i++) { 162 Action 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 |