2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.entity;
44
import java.awt.Image;
45
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.Collection;
48
import java.util.List;
49
import javax.lang.model.element.ExecutableElement;
50
import javax.lang.model.element.TypeElement;
51
import javax.lang.model.util.ElementFilter;
52
import org.netbeans.api.java.source.ClasspathInfo;
53
import org.netbeans.api.java.source.CompilationController;
54
import org.netbeans.api.java.source.ElementHandle;
55
import org.netbeans.api.java.source.JavaSource;
56
import org.netbeans.api.java.source.ui.ElementOpen;
57
import org.netbeans.modules.j2ee.common.DDEditorNavigator;
58
import org.netbeans.modules.j2ee.common.method.MethodModel;
59
import org.netbeans.modules.j2ee.common.method.MethodModelSupport;
60
import org.netbeans.modules.j2ee.common.source.AbstractTask;
61
import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
62
import org.netbeans.modules.j2ee.dd.api.ejb.Query;
63
import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
64
import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType;
65
import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.shared.ComponentMethodModel;
66
import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.shared.ComponentMethodViewStrategy;
67
import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.shared.IconVisitor;
68
import org.openide.filesystems.FileObject;
69
import org.openide.loaders.DataObject;
70
import org.openide.loaders.DataObjectNotFoundException;
71
import org.openide.util.Exceptions;
72
import org.openide.util.Utilities;
76
* @author Chris Webster
77
* @author Martin Adamek
80
public class MethodChildren extends ComponentMethodModel {
82
private ComponentMethodViewStrategy mvs;
83
private final EntityMethodController controller;
84
private final boolean local;
85
private final FileObject ddFile;
86
private final Entity entity;
88
public MethodChildren(ClasspathInfo cpInfo, EntityMethodController smc, Entity model, Collection interfaces, boolean local, FileObject ddFile) {
89
super(cpInfo, smc.getBeanClass(), interfaces, local ? smc.getLocalHome() : smc.getHome());
94
mvs = new EntityStrategy();
97
protected Collection<String> getInterfaces() {
99
return controller.getLocalInterfaces();
101
return controller.getRemoteInterfaces();
105
public ComponentMethodViewStrategy createViewStrategy() {
109
private class EntityStrategy implements ComponentMethodViewStrategy {
111
public void deleteImplMethod(MethodModel me, String implClass, FileObject implClassFO, Collection interfaces) throws IOException {
112
String methodName = me.getName();
113
if (methodName.startsWith("find") || //NOI18N
114
methodName.startsWith("ejbSelect")) { //NOI18N
115
controller.deleteQueryMapping(me, ddFile);
117
controller.delete(me,local);
120
public Image getBadge(MethodModel me, Collection interfaces) {
124
public Image getIcon(MethodModel me, Collection interfaces) {
125
IconVisitor iv = new IconVisitor();
126
return Utilities.loadImage(iv.getIconUrl(controller.getMethodTypeFromInterface(me)));
129
public void openMethod(final MethodModel me, final String implClass, FileObject implClassFO, Collection interfaces) {
130
if (controller.getMethodTypeFromInterface(me).getKind() == MethodType.Kind.FINDER) {
132
DataObject ddFileDO = DataObject.find(ddFile);
133
Object c = ddFileDO.getCookie(DDEditorNavigator.class);
135
Query[] queries = entity.getQuery();
136
for (int i = 0; i < queries.length; i++) {
137
String methodName = queries[i].getQueryMethod().getMethodName();
138
if (methodName.equals(me.getName())) {
139
((DDEditorNavigator) c).showElement(queries[i]);
143
} catch (DataObjectNotFoundException donf) {
144
Exceptions.printStackTrace(donf);
147
final List<ElementHandle<ExecutableElement>> methodHandle = new ArrayList<ElementHandle<ExecutableElement>>();
149
JavaSource javaSource = JavaSource.forFileObject(implClassFO);
150
javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
151
public void run(CompilationController controller) throws IOException {
152
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
153
TypeElement typeElement = controller.getElements().getTypeElement(implClass);
154
for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
155
if (MethodModelSupport.isSameMethod(controller, executableElement, me)) {
156
methodHandle.add(ElementHandle.create(executableElement));
161
} catch (IOException ioe) {
162
Exceptions.printStackTrace(ioe);
164
if (methodHandle.size() > 0) {
165
ElementOpen.open(implClassFO, methodHandle.get(0));