1 19 package org.netbeans.modules.j2ee.persistence.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.j2ee.persistence.api.PersistenceScope; 26 import org.openide.loaders.DataObject; 27 import org.openide.nodes.Node; 28 import org.openide.util.ContextAwareAction; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.NbBundle; 31 import org.openide.util.Utilities; 32 import org.openide.util.actions.NodeAction; 33 import org.openide.util.actions.Presenter; 34 import org.openide.util.actions.SystemAction; 35 36 43 public final class PersistenceActionGroup extends NodeAction { 44 45 protected void performAction(Node[] activatedNodes) { 46 } 48 49 public String getName() { 50 return NbBundle.getMessage(PersistenceActionGroup.class, "CTL_PersistenceActionGroup"); 51 } 52 53 @Override 54 protected void initialize() { 55 super.initialize(); 56 putValue("noIconInMenu", Boolean.TRUE); 58 } 59 60 public HelpCtx getHelpCtx() { 61 return HelpCtx.DEFAULT_HELP; 62 } 63 64 @Override 65 protected boolean asynchronous() { 66 return false; 67 } 68 69 70 private static SystemAction[] grouped() { 71 return new SystemAction[] { 72 SystemAction.get(UseEntityManagerAction.class), 73 }; 74 } 75 76 protected boolean enable(Node[] activatedNodes) { 77 if (activatedNodes.length == 0) { 78 return false; 79 } 80 return hasPersistenceContext(activatedNodes[0]); 81 } 82 83 @Override 84 public JMenuItem getPopupPresenter() { 85 Node[] activatedNodes = getActivatedNodes(); 86 boolean oneNodeSelected = activatedNodes.length == 1; 87 if (oneNodeSelected && hasPersistenceContext(activatedNodes[0])) { 88 return new LazyMenu(); 89 } 90 JMenuItem menuItem = super.getPopupPresenter(); 91 menuItem.setVisible(false); 92 return menuItem; 93 } 94 95 98 private final class LazyMenu extends JMenu { 99 100 public LazyMenu() { 101 super(PersistenceActionGroup.this.getName()); 102 } 103 104 @Override 105 public JPopupMenu getPopupMenu() { 106 if (getItemCount() == 0) { 107 Action [] grouped = grouped(); 108 for (int i = 0; i < grouped.length; i++) { 109 Action action = grouped[i]; 110 if (action == null && getItemCount() != 0) { 111 addSeparator(); 112 } else { 113 if (action instanceof ContextAwareAction) { 114 action = ((ContextAwareAction)action).createContextAwareInstance(Utilities.actionsGlobalContext()); 115 } 116 if (action instanceof Presenter.Popup) { 117 add(((Presenter.Popup)action).getPopupPresenter()); 118 } 119 } 120 } 121 } 122 return super.getPopupMenu(); 123 } 124 } 125 126 private static boolean hasPersistenceContext(Node node) { 127 DataObject dataObject = (DataObject) node.getCookie(DataObject.class); 128 if (dataObject != null) { 129 return PersistenceScope.getPersistenceScope(dataObject.getPrimaryFile()) != null; 130 } 131 return false; 132 } 133 134 } 135 136 | Popular Tags |