~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/ejbcore/src/org/netbeans/modules/j2ee/ejbcore/ui/logicalview/ejb/entity/MethodChildren.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.entity;
 
43
 
 
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;
 
73
 
 
74
/**
 
75
 *
 
76
 * @author Chris Webster
 
77
 * @author Martin Adamek
 
78
 */
 
79
 
 
80
public class MethodChildren extends ComponentMethodModel {
 
81
 
 
82
    private ComponentMethodViewStrategy mvs;
 
83
    private final EntityMethodController controller;
 
84
    private final boolean local;
 
85
    private final FileObject ddFile;
 
86
    private final Entity entity;
 
87
    
 
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());
 
90
        controller = smc;
 
91
        this.local = local;
 
92
        this.ddFile = ddFile;
 
93
        this.entity = model;
 
94
        mvs = new EntityStrategy();
 
95
    }
 
96
 
 
97
    protected Collection<String> getInterfaces() {
 
98
        if (local) {
 
99
            return controller.getLocalInterfaces();
 
100
        } else {
 
101
            return controller.getRemoteInterfaces();
 
102
        }
 
103
    }
 
104
 
 
105
    public ComponentMethodViewStrategy createViewStrategy() {
 
106
        return mvs;
 
107
    }
 
108
 
 
109
    private class EntityStrategy implements ComponentMethodViewStrategy {
 
110
        
 
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);
 
116
            }
 
117
            controller.delete(me,local);
 
118
        }
 
119
 
 
120
        public Image getBadge(MethodModel me, Collection interfaces) {
 
121
            return null;
 
122
        }
 
123
 
 
124
        public Image getIcon(MethodModel me, Collection interfaces) {
 
125
            IconVisitor iv = new IconVisitor();
 
126
            return Utilities.loadImage(iv.getIconUrl(controller.getMethodTypeFromInterface(me)));
 
127
        }
 
128
 
 
129
        public void openMethod(final MethodModel me, final String implClass, FileObject implClassFO, Collection interfaces) {
 
130
            if (controller.getMethodTypeFromInterface(me).getKind() == MethodType.Kind.FINDER) {
 
131
                try {
 
132
                    DataObject ddFileDO = DataObject.find(ddFile);
 
133
                    Object c = ddFileDO.getCookie(DDEditorNavigator.class);
 
134
                    if (c != null) {
 
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]);
 
140
                            }
 
141
                        }
 
142
                    }
 
143
                } catch (DataObjectNotFoundException donf) {
 
144
                    Exceptions.printStackTrace(donf);
 
145
                }
 
146
            }
 
147
            final List<ElementHandle<ExecutableElement>> methodHandle = new ArrayList<ElementHandle<ExecutableElement>>();
 
148
            try {
 
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));
 
157
                            }
 
158
                        }
 
159
                    }
 
160
                }, true);
 
161
            } catch (IOException ioe) {
 
162
                Exceptions.printStackTrace(ioe);
 
163
            }
 
164
            if (methodHandle.size() > 0) {
 
165
                ElementOpen.open(implClassFO, methodHandle.get(0));
 
166
            }
 
167
        }
 
168
 
 
169
    }
 
170
 
 
171
}