KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > action > AbstractAddMethodAction


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
20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.lang.model.element.TypeElement;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import org.netbeans.api.java.source.ElementHandle;
29 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.nodes.Node;
33 import org.openide.util.ContextAwareAction;
34 import org.openide.util.Lookup;
35 import org.openide.util.actions.Presenter;
36
37 /**
38  * Action that can always be invoked and work procedurally.
39  *
40  * @author Chris Webster
41  * @author Martin Adamek
42  */

43 public abstract class AbstractAddMethodAction extends AbstractAction JavaDoc implements Presenter.Popup, ContextAwareAction {
44     
45     /** Action context. */
46     private Lookup context;
47     private final AbstractAddMethodStrategy strategy;
48
49     public AbstractAddMethodAction(AbstractAddMethodStrategy strategy) {
50         super(/*strategy.getTitle()*/);
51         this.strategy = strategy;
52     }
53
54     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
55         this.context = actionContext;
56         boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0]));
57         return enable ? this : null;
58     }
59     
60     public String JavaDoc getName(){
61         return strategy.getTitle();
62     }
63     
64     protected boolean enable(org.openide.nodes.Node[] activatedNodes) {
65         if (activatedNodes.length != 1) {
66             return false;
67         }
68         FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class);
69         try {
70             if (fileObject != null) {
71                 ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]);
72                 if (elementHandle != null) {
73                     if (strategy.supportsEjb(fileObject, elementHandle.getQualifiedName())) {
74                         return true;
75                     }
76                 }
77             }
78         } catch (IOException JavaDoc ex) {
79             ErrorManager.getDefault().notify(ex);
80         }
81         return false;
82     }
83     
84     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
85         performAction(context.lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0]));
86     }
87     
88     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
89         if (activatedNodes.length != 1) {
90             return;
91         }
92         FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class);
93         try {
94             if (fileObject != null) {
95                 ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]);
96                 if (elementHandle != null) {
97                     if (strategy.supportsEjb(fileObject, elementHandle.getQualifiedName())) {
98                         strategy.addMethod(fileObject, elementHandle.getQualifiedName());
99                     }
100                 }
101             }
102         } catch (IOException JavaDoc ex) {
103             ErrorManager.getDefault().notify(ex);
104         }
105     }
106
107     public Object JavaDoc getValue(String JavaDoc key) {
108         if (NAME.equals(key)) {
109             return getName();
110         } else {
111             return super.getValue(key);
112         }
113     }
114
115     public JMenuItem JavaDoc getPopupPresenter() {
116         return new JMenuItem JavaDoc (this);
117     }
118
119 }
120
Popular Tags