KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > SendJMSMessageAction


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.entres;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.lang.model.element.ElementKind;
26 import javax.lang.model.element.TypeElement;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import org.netbeans.api.java.source.CompilationController;
30 import org.netbeans.api.java.source.ElementHandle;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer;
35 import org.netbeans.modules.j2ee.common.Util;
36 import org.netbeans.modules.j2ee.common.source.AbstractTask;
37 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
38 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
41 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
42 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
43 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
44 import org.netbeans.modules.j2ee.ejbcore.action.SendJMSGenerator;
45 import org.openide.DialogDescriptor;
46 import org.openide.DialogDisplayer;
47 import org.openide.ErrorManager;
48 import org.openide.NotifyDescriptor;
49 import org.openide.filesystems.FileObject;
50 import org.openide.nodes.Node;
51 import org.openide.util.HelpCtx;
52 import org.openide.util.NbBundle;
53 import org.openide.util.Lookup;
54 import org.openide.util.actions.NodeAction;
55
56
57 /**
58  * Provide action for sending a JMS Message
59  * @author Chris Webster
60  * @author Martin Adamek
61  */

62 public class SendJMSMessageAction extends NodeAction {
63     
64     protected void performAction(Node[] nodes) {
65        try {
66             JButton JavaDoc okButton = new JButton JavaDoc();
67             okButton.setText(NbBundle.getMessage(SendJMSMessageAction.class, "LBL_Ok"));
68             okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SendJMSMessageAction.class, "ACSD_Ok"));
69             JButton JavaDoc cancelButton = new JButton JavaDoc();
70             cancelButton.setText(NbBundle.getMessage(SendJMSMessageAction.class, "LBL_Cancel"));
71             cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SendJMSMessageAction.class, "ACSD_Cancel"));
72             ElementHandle<TypeElement> beanClass = _RetoucheUtil.getJavaClassFromNode(nodes[0]);
73             FileObject srcFile = nodes[0].getLookup().lookup(FileObject.class);
74             Project enterpriseProject = FileOwnerQuery.getOwner(srcFile);
75             EnterpriseReferenceContainer erc = enterpriseProject.getLookup().lookup(EnterpriseReferenceContainer.class);
76         
77             MessageDestinationPanel panel = new MessageDestinationPanel(okButton, erc.getServiceLocatorName());
78             final DialogDescriptor dialogDescriptor = new DialogDescriptor(
79                     panel,
80                     NbBundle.getMessage(SendJMSMessageAction.class,"LBL_SelectMessageDestination"),
81                     true,
82                     DialogDescriptor.OK_CANCEL_OPTION,
83                     DialogDescriptor.OK_OPTION,
84                     DialogDescriptor.DEFAULT_ALIGN,
85                     new HelpCtx(MessageDestinationPanel.class),
86                     null
87                     );
88             
89             panel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
90                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
91                     if (evt.getPropertyName().equals(MessageDestinationPanel.IS_VALID)) {
92                         Object JavaDoc newvalue = evt.getNewValue();
93                         if ((newvalue != null) && (newvalue instanceof Boolean JavaDoc)) {
94                             dialogDescriptor.setValid(((Boolean JavaDoc)newvalue).booleanValue());
95                         }
96                     }
97                 }
98             });
99             panel.checkDestination();
100
101             Object JavaDoc button = DialogDisplayer.getDefault().notify(dialogDescriptor);
102             if (button != DialogDescriptor.OK_OPTION) {
103                 return;
104             }
105             
106             JMSDestination destination = panel.getSelectedDestination();
107             String JavaDoc serviceLocator = panel.getServiceLocator();
108             ServiceLocatorStrategy serviceLocatorStrategy = null;
109             if (serviceLocator != null) {
110                 serviceLocatorStrategy =
111                         ServiceLocatorStrategy.create(enterpriseProject, srcFile,
112                                                       serviceLocator);
113             }
114             SendJMSGenerator generator = new SendJMSGenerator(destination);
115             generator.genMethods(erc, beanClass.getQualifiedName().toString(), srcFile, serviceLocatorStrategy);
116             if (serviceLocator != null) {
117                 erc.setServiceLocatorName(serviceLocator);
118             }
119         } catch (IOException JavaDoc ioe) {
120             NotifyDescriptor notifyDescriptor = new NotifyDescriptor.Message(ioe.getMessage(),
121             NotifyDescriptor.ERROR_MESSAGE);
122             DialogDisplayer.getDefault().notify(notifyDescriptor);
123         }
124     }
125     
126     protected boolean enable(Node[] nodes) {
127         if (nodes == null || nodes.length != 1) {
128             return false;
129         }
130         FileObject fileObject = nodes[0].getLookup().lookup(FileObject.class);
131         if (fileObject == null) {
132             return false;
133         }
134         Project project = FileOwnerQuery.getOwner(fileObject);
135         J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
136         String JavaDoc serverInstanceId = j2eeModuleProvider.getServerInstanceID();
137         if (serverInstanceId == null) {
138             return true;
139         }
140         J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceId);
141         if (platform == null) {
142             return true;
143         }
144         if (!platform.getSupportedModuleTypes().contains(J2eeModule.EJB)) {
145             return false;
146         }
147         String JavaDoc j2eeVersion = j2eeModuleProvider.getJ2eeModule().getModuleVersion();
148         Object JavaDoc moduleType = j2eeModuleProvider.getJ2eeModule().getModuleType();
149         if (Util.isJavaEE5orHigher(project) ||
150                 (J2eeModule.WAR.equals(moduleType) && WebApp.VERSION_2_4.equals(j2eeVersion)) ||
151                 (J2eeModule.EJB.equals(moduleType) && EjbJar.VERSION_2_1.equals(j2eeVersion))) {
152             JavaSource javaSource = JavaSource.forFileObject(fileObject);
153             final boolean[] isInterface = new boolean[1];
154             try {
155                 final ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(nodes[0]);
156                 if (javaSource == null || elementHandle == null) {
157                     return false;
158                 }
159                 javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
160                     public void run(CompilationController controller) throws IOException JavaDoc {
161                         controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
162                         TypeElement typeElement = elementHandle.resolve(controller);
163                         isInterface[0] = ElementKind.INTERFACE == typeElement.getKind();
164                     }
165                 }, true);
166                 return elementHandle == null ? false : !isInterface[0];
167             } catch (IOException JavaDoc ioe) {
168                 ErrorManager.getDefault().notify(ioe);
169             }
170         }
171         return false;
172     }
173     
174     public String JavaDoc getName() {
175         return NbBundle.getMessage(SendJMSMessageAction.class, "LBL_SendJMSMessageAction");
176     }
177     
178     public HelpCtx getHelpCtx() {
179         return HelpCtx.DEFAULT_HELP;
180     }
181     
182     protected boolean asynchronous() {
183         return false;
184     }
185     
186     /** Perform extra initialization of this action's singleton.
187      * PLEASE do not use constructors for this purpose!
188      * protected void initialize() {
189      * super.initialize();
190      * putProperty(Action.SHORT_DESCRIPTION, NbBundle.getMessage(CallEjbAction.class, "HINT_Action"));
191      * }
192      */

193
194     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
195         boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0]));
196         return enable ? this : null;
197     }
198     
199 }
200
Popular Tags