1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import javax.lang.model.element.ElementKind; 26 import javax.lang.model.element.TypeElement; 27 import javax.swing.Action ; 28 import javax.swing.JButton ; 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 62 public class SendJMSMessageAction extends NodeAction { 63 64 protected void performAction(Node[] nodes) { 65 try { 66 JButton okButton = new JButton (); 67 okButton.setText(NbBundle.getMessage(SendJMSMessageAction.class, "LBL_Ok")); 68 okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SendJMSMessageAction.class, "ACSD_Ok")); 69 JButton cancelButton = new JButton (); 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 () { 90 public void propertyChange(PropertyChangeEvent evt) { 91 if (evt.getPropertyName().equals(MessageDestinationPanel.IS_VALID)) { 92 Object newvalue = evt.getNewValue(); 93 if ((newvalue != null) && (newvalue instanceof Boolean )) { 94 dialogDescriptor.setValid(((Boolean )newvalue).booleanValue()); 95 } 96 } 97 } 98 }); 99 panel.checkDestination(); 100 101 Object button = DialogDisplayer.getDefault().notify(dialogDescriptor); 102 if (button != DialogDescriptor.OK_OPTION) { 103 return; 104 } 105 106 JMSDestination destination = panel.getSelectedDestination(); 107 String 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 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 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 j2eeVersion = j2eeModuleProvider.getJ2eeModule().getModuleVersion(); 148 Object 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 { 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 ioe) { 168 ErrorManager.getDefault().notify(ioe); 169 } 170 } 171 return false; 172 } 173 174 public String 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 193 194 public Action 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 |