KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
51  * Provide action for using a data source.
52  * @author Chris Webster
53  * @author Martin Adamek
54  */

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 JavaDoc ioe) {
66             ErrorManager.getDefault().notify(ioe);
67         }
68     }
69     
70     private boolean generate(FileObject fileObject, ElementHandle<TypeElement> elementHandle) throws IOException JavaDoc {
71         Project project = FileOwnerQuery.getOwner(fileObject);
72         //make sure configuration is ready
73
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()); //NOI18N
77
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         //#73163: disable OK button when no db connections are available
88
dialogDescriptor.setValid(checkConnections());
89         selectDatabasePanel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
90             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
91                 if (evt.getPropertyName().equals(SelectDatabasePanel.IS_VALID)) {
92                     Object JavaDoc newvalue = evt.getNewValue();
93                     if ((newvalue != null) && (newvalue instanceof Boolean JavaDoc)) {
94                         dialogDescriptor.setValid(((Boolean JavaDoc)newvalue).booleanValue() && checkConnections());
95                     }
96                 }
97             }
98         });
99         selectDatabasePanel.checkDatasource();
100         Object JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc 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