KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > action > PersistenceActionGroup


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.j2ee.persistence.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.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 /**
37  * Action group holding Java Persistence related actions.
38  * It is visible only if persistence is provided by owning project
39  * (if there is at least one persistence unit defined in persistence.xml)
40  *
41  * @author Martin Adamek
42  */

43 public final class PersistenceActionGroup extends NodeAction {
44     
45     protected void performAction(Node[] activatedNodes) {
46         // this is just action group, this should not be called
47
}
48     
49     public String JavaDoc getName() {
50         return NbBundle.getMessage(PersistenceActionGroup.class, "CTL_PersistenceActionGroup");
51     }
52     
53     @Override JavaDoc
54     protected void initialize() {
55         super.initialize();
56         // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
57
putValue("noIconInMenu", Boolean.TRUE);
58     }
59     
60     public HelpCtx getHelpCtx() {
61         return HelpCtx.DEFAULT_HELP;
62     }
63     
64     @Override JavaDoc
65     protected boolean asynchronous() {
66         return false;
67     }
68     
69     /** List of system actions to be displayed within this one's toolbar or submenu. */
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 JavaDoc
84     public JMenuItem JavaDoc getPopupPresenter() {
85         Node[] activatedNodes = getActivatedNodes();
86         boolean oneNodeSelected = activatedNodes.length == 1;
87         if (oneNodeSelected && hasPersistenceContext(activatedNodes[0])) {
88             return new LazyMenu();
89         }
90         JMenuItem JavaDoc menuItem = super.getPopupPresenter();
91         menuItem.setVisible(false);
92         return menuItem;
93     }
94
95     /**
96      * Avoids constructing submenu until it will be needed.
97      */

98     private final class LazyMenu extends JMenu JavaDoc {
99         
100         public LazyMenu() {
101             super(PersistenceActionGroup.this.getName());
102         }
103         
104         @Override JavaDoc
105         public JPopupMenu JavaDoc getPopupMenu() {
106             if (getItemCount() == 0) {
107                 Action JavaDoc[] grouped = grouped();
108                 for (int i = 0; i < grouped.length; i++) {
109                     Action JavaDoc 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