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 org.netbeans.api.db.explorer.ConnectionManager; 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.source.AbstractTask; 36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 37 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil; 38 import org.netbeans.modules.j2ee.ejbcore.action.UseDatabaseGenerator; 39 import org.openide.DialogDescriptor; 40 import org.openide.DialogDisplayer; 41 import org.openide.ErrorManager; 42 import org.openide.NotifyDescriptor; 43 import org.openide.filesystems.FileObject; 44 import org.openide.nodes.Node; 45 import org.openide.util.HelpCtx; 46 import org.openide.util.NbBundle; 47 import org.openide.util.actions.NodeAction; 48 49 50 55 public class UseDatabaseAction extends NodeAction { 56 57 protected void performAction(Node[] nodes) { 58 if (nodes == null || nodes.length != 1) { 59 return; 60 } 61 FileObject fileObject = nodes[0].getLookup().lookup(FileObject.class); 62 try { 63 ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(nodes[0]); 64 generate(fileObject, elementHandle); 65 } catch (IOException ioe) { 66 ErrorManager.getDefault().notify(ioe); 67 } 68 } 69 70 private boolean generate(FileObject fileObject, ElementHandle<TypeElement> elementHandle) throws IOException { 71 Project project = FileOwnerQuery.getOwner(fileObject); 72 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 74 j2eeModuleProvider.getConfigSupport().ensureConfigurationReady(); 75 EnterpriseReferenceContainer enterpriseReferenceContainer = project.getLookup().lookup(EnterpriseReferenceContainer.class); 76 SelectDatabasePanel selectDatabasePanel = new SelectDatabasePanel(j2eeModuleProvider, enterpriseReferenceContainer.getServiceLocatorName()); final DialogDescriptor dialogDescriptor = new DialogDescriptor( 78 selectDatabasePanel, 79 NbBundle.getMessage(UseDatabaseAction.class, "LBL_ChooseDatabase"), 80 true, 81 DialogDescriptor.OK_CANCEL_OPTION, 82 DialogDescriptor.OK_OPTION, 83 DialogDescriptor.DEFAULT_ALIGN, 84 new HelpCtx(SelectDatabasePanel.class), 85 null 86 ); 87 dialogDescriptor.setValid(checkConnections()); 89 selectDatabasePanel.addPropertyChangeListener(new PropertyChangeListener () { 90 public void propertyChange(PropertyChangeEvent evt) { 91 if (evt.getPropertyName().equals(SelectDatabasePanel.IS_VALID)) { 92 Object newvalue = evt.getNewValue(); 93 if ((newvalue != null) && (newvalue instanceof Boolean )) { 94 dialogDescriptor.setValid(((Boolean )newvalue).booleanValue() && checkConnections()); 95 } 96 } 97 } 98 }); 99 selectDatabasePanel.checkDatasource(); 100 Object option = DialogDisplayer.getDefault().notify(dialogDescriptor); 101 if (option == NotifyDescriptor.OK_OPTION) { 102 UseDatabaseGenerator generator = new UseDatabaseGenerator(); 103 generator.generate( 104 fileObject, 105 elementHandle, 106 selectDatabasePanel.getDatasource(), 107 selectDatabasePanel.createServerResources(), 108 selectDatabasePanel.getServiceLocator() 109 ); 110 } 111 return false; 112 } 113 114 protected boolean enable(Node[] nodes) { 115 if (nodes == null || nodes.length != 1) { 116 return false; 117 } 118 FileObject fileObject = nodes[0].getLookup().lookup(FileObject.class); 119 if (fileObject == null) { 120 return false; 121 } 122 JavaSource javaSource = JavaSource.forFileObject(fileObject); 123 final boolean[] isInterface = new boolean[1]; 124 try { 125 final ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(nodes[0]); 126 if (elementHandle == null || javaSource == null) { 127 return false; 128 } 129 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 130 public void run(CompilationController controller) throws IOException { 131 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 132 TypeElement typeElement = elementHandle.resolve(controller); 133 isInterface[0] = ElementKind.INTERFACE == typeElement.getKind(); 134 } 135 }, true); 136 return elementHandle == null ? false : !isInterface[0]; 137 } catch (IOException ioe) { 138 ErrorManager.getDefault().notify(ioe); 139 } 140 return false; 141 } 142 143 private boolean checkConnections() { 144 return ConnectionManager.getDefault().getConnections().length > 0; 145 } 146 147 public String getName() { 148 return NbBundle.getMessage(UseDatabaseAction.class, "LBL_UseDbAction"); 149 } 150 151 public HelpCtx getHelpCtx() { 152 return HelpCtx.DEFAULT_HELP; 153 } 154 155 protected boolean asynchronous() { 156 return false; 157 } 158 159 protected void initialize() { 160 super.initialize(); 161 putProperty(Action.SHORT_DESCRIPTION, NbBundle.getMessage(UseDatabaseAction.class, "HINT_UseDbAction")); 162 } 163 } 164 | Popular Tags |