Lines 1-193
Link Here
|
1 |
/* |
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action; |
46 |
|
47 |
|
48 |
import java.io.IOException; |
49 |
import java.util.logging.Level; |
50 |
import java.util.logging.Logger; |
51 |
import javax.lang.model.element.TypeElement; |
52 |
import org.netbeans.api.java.source.ElementHandle; |
53 |
import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; |
54 |
import org.netbeans.modules.j2ee.common.Util; |
55 |
import org.netbeans.modules.j2ee.common.method.FieldCustomizer; |
56 |
import org.netbeans.modules.j2ee.common.method.MethodModel; |
57 |
import org.netbeans.modules.j2ee.dd.api.ejb.EjbJarMetadata; |
58 |
import org.netbeans.modules.j2ee.dd.api.ejb.Entity; |
59 |
import org.netbeans.modules.j2ee.ejbcore.Utils; |
60 |
import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EjbMethodController; |
61 |
import org.netbeans.modules.j2ee.ejbcore.util._RetoucheUtil; |
62 |
import org.netbeans.modules.j2ee.ejbcore.action.CmFieldGenerator; |
63 |
import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController; |
64 |
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel; |
65 |
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction; |
66 |
import org.omg.CORBA.Request; |
67 |
import org.openide.filesystems.FileObject; |
68 |
import org.openide.nodes.Node; |
69 |
import org.openide.util.Exceptions; |
70 |
import org.openide.util.HelpCtx; |
71 |
import org.openide.util.Lookup; |
72 |
import org.openide.util.NbBundle; |
73 |
import org.openide.util.RequestProcessor; |
74 |
import org.openide.util.actions.NodeAction; |
75 |
import sun.print.RasterPrinterJob; |
76 |
|
77 |
/** |
78 |
* Action that can always be invoked and work procedurally. |
79 |
* @author Chris Webster |
80 |
*/ |
81 |
public class AddCmpFieldAction extends NodeAction { |
82 |
|
83 |
private final String NAME = NbBundle.getMessage(AddCmpFieldAction.class, "LBL_AddCmpFieldAction"); |
84 |
|
85 |
public String getName() { |
86 |
return NAME; |
87 |
} |
88 |
|
89 |
public HelpCtx getHelpCtx() { |
90 |
return HelpCtx.DEFAULT_HELP; |
91 |
} |
92 |
|
93 |
protected boolean asynchronous() { |
94 |
return false; |
95 |
} |
96 |
|
97 |
protected boolean enable(org.openide.nodes.Node[] activatedNodes) { |
98 |
if (activatedNodes == null || activatedNodes.length < 1) { |
99 |
return false; |
100 |
} |
101 |
EjbMethodController ejbMethodController; |
102 |
ElementHandle<TypeElement> elementHandle = null; |
103 |
try { |
104 |
elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]); |
105 |
if (elementHandle == null) { |
106 |
return false; |
107 |
} |
108 |
} catch (IOException ioe) { |
109 |
Exceptions.printStackTrace(ioe); |
110 |
return false; |
111 |
} |
112 |
FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class); |
113 |
if (fileObject == null) { |
114 |
return false; |
115 |
} |
116 |
return activatedNodes.length == 1 && |
117 |
(ejbMethodController = EjbMethodController.createFromClass(fileObject, elementHandle.getQualifiedName())) != null && |
118 |
ejbMethodController instanceof EntityMethodController && |
119 |
((EntityMethodController) ejbMethodController).isCMP(); |
120 |
} |
121 |
|
122 |
protected void performAction(org.openide.nodes.Node[] activatedNodes) { |
123 |
if (activatedNodes == null || activatedNodes.length < 1) { |
124 |
return; |
125 |
} |
126 |
try { |
127 |
ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]); |
128 |
if (elementHandle != null) { |
129 |
FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class); |
130 |
FileObject ddFile = EjbJar.getEjbJar(fileObject).getDeploymentDescriptor(); |
131 |
EntityMethodController emc = (EntityMethodController) EjbMethodController.createFromClass(fileObject, elementHandle.getQualifiedName()); |
132 |
MethodModel.Variable field = MethodModel.Variable.create("java.lang.String", "cmpField"); |
133 |
addCmpField(emc, ddFile, field); |
134 |
} |
135 |
} catch (IOException ioe) { |
136 |
Exceptions.printStackTrace(ioe); |
137 |
} |
138 |
} |
139 |
|
140 |
public static boolean addCmpField(FileObject fileObject, String className, FileObject ddFile) { |
141 |
MethodModel.Variable field = MethodModel.Variable.create("java.lang.String", "cmpField"); |
142 |
EntityMethodController emc = (EntityMethodController) EntityMethodController.createFromClass(fileObject, className); |
143 |
try { |
144 |
return addCmpField(emc, ddFile, field); |
145 |
} catch (IOException ioe) { |
146 |
Exceptions.printStackTrace(ioe); |
147 |
} |
148 |
return false; |
149 |
} |
150 |
|
151 |
private static boolean addCmpField(EntityMethodController emc, FileObject ddFile, MethodModel.Variable field) throws IOException { |
152 |
|
153 |
final String ejbClass = emc.getBeanClass(); |
154 |
final Entity[] entity = new Entity[1]; |
155 |
final FileObject[] ejbClassFO = new FileObject[1]; |
156 |
|
157 |
MetadataModel<EjbJarMetadata> metadataModel = EjbJar.getEjbJar(ddFile).getMetadataModel(); |
158 |
metadataModel.runReadAction(new MetadataModelAction<EjbJarMetadata, Void>() { |
159 |
public Void run(EjbJarMetadata metadata) { |
160 |
entity[0] = (Entity) metadata.findByEjbClass(ejbClass); |
161 |
ejbClassFO[0] = metadata.findResource(Utils.toResourceName(ejbClass)); |
162 |
return null; |
163 |
} |
164 |
}); |
165 |
|
166 |
final FieldCustomizer customizer = new FieldCustomizer(entity[0], field, "", |
167 |
emc.getLocal() != null, emc.getRemote() != null, true, true, false, false); |
168 |
if (customizer.customizeField()) { |
169 |
final MethodModel.Variable customizedField = customizer.getField(); |
170 |
final CmFieldGenerator generator = CmFieldGenerator.create(emc.getBeanClass(), ejbClassFO[0]); |
171 |
|
172 |
RequestProcessor.getDefault().post(new Runnable() { |
173 |
public void run() { |
174 |
try { |
175 |
generator.addCmpField(customizedField, customizer.isLocalGetter(), customizer.isLocalSetter(), |
176 |
customizer.isRemoteGetter(), customizer.isRemoteSetter(), customizer.getDescription()); |
177 |
} catch (IOException ioe) { |
178 |
Logger.getLogger(AddCmpFieldAction.class.getName()).log(Level.WARNING, null, ioe); |
179 |
} |
180 |
} |
181 |
}); |
182 |
|
183 |
return true; |
184 |
} |
185 |
return false; |
186 |
} |
187 |
|
188 |
public javax.swing.Action createContextAwareInstance(org.openide.util.Lookup actionContext) { |
189 |
boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0])); |
190 |
return enable ? super.createContextAwareInstance(actionContext) : null; |
191 |
} |
192 |
|
193 |
} |