~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/action/GoToSourceActionGroup.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.action;
 
43
 
 
44
import java.io.IOException;
 
45
import java.util.ArrayList;
 
46
import java.util.List;
 
47
import javax.swing.Action;
 
48
import org.netbeans.api.java.source.CompilationController;
 
49
import org.netbeans.api.java.source.JavaSource;
 
50
import org.netbeans.modules.j2ee.common.source.AbstractTask;
 
51
import org.netbeans.modules.j2ee.common.source.SourceUtils;
 
52
import org.netbeans.modules.j2ee.dd.api.ejb.EjbJarMetadata;
 
53
import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
 
54
import org.netbeans.modules.j2ee.ejbcore.Utils;
 
55
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
 
56
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
 
57
import org.openide.filesystems.FileObject;
 
58
import org.openide.nodes.Node;
 
59
import org.openide.util.Exceptions;
 
60
import org.openide.util.HelpCtx;
 
61
import org.openide.util.NbBundle;
 
62
 
 
63
/**
 
64
 * Group holding source navigation actions for Session and Entity EJBs
 
65
 *
 
66
 * @author Martin Adamek
 
67
 */
 
68
public class GoToSourceActionGroup extends EJBActionGroup {
 
69
 
 
70
    private final static int EJB_CLASS = 0;
 
71
    private final static int REMOTE = 1;
 
72
    private final static int LOCAL = 2;
 
73
    private final static int HOME = 3;
 
74
    private final static int LOCAL_HOME = 4;
 
75
    
 
76
    public String getName() {
 
77
        return NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoToSourceGroup");
 
78
    }
 
79
 
 
80
    protected Action[] grouped() {
 
81
 
 
82
        final FileObject[] results = new FileObject[5];
 
83
        
 
84
        Node node = getActivatedNodes()[0];
 
85
        if (node == null) {
 
86
            return new Action[0];
 
87
        }
 
88
        
 
89
        FileObject fileObject = node.getLookup().lookup(FileObject.class);
 
90
        final String[] ejbClass = new String[1];
 
91
        
 
92
        JavaSource javaSource = JavaSource.forFileObject(fileObject);
 
93
        MetadataModel<EjbJarMetadata> model = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject).getMetadataModel();
 
94
 
 
95
        try {
 
96
            javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
 
97
                public void run(CompilationController controller) throws IOException {
 
98
                    controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
 
99
                    ejbClass[0] = SourceUtils.newInstance(controller).getTypeElement().getQualifiedName().toString();
 
100
                }
 
101
            }, true);
 
102
 
 
103
            model.runReadAction(new MetadataModelAction<EjbJarMetadata, Void>() {
 
104
                public Void run(EjbJarMetadata metadata) {
 
105
                    EntityAndSession ejb = (EntityAndSession) metadata.findByEjbClass(ejbClass[0]);
 
106
                    results[EJB_CLASS] = ejb.getEjbClass() ==null ? null : metadata.findResource(Utils.toResourceName(ejb.getEjbClass()));
 
107
                    results[REMOTE] = ejb.getRemote() ==null ? null : metadata.findResource(Utils.toResourceName(ejb.getRemote()));
 
108
                    results[LOCAL] = ejb.getLocal() ==null ? null : metadata.findResource(Utils.toResourceName(ejb.getLocal()));
 
109
                    results[HOME] = ejb.getHome() ==null ? null : metadata.findResource(Utils.toResourceName(ejb.getHome()));
 
110
                    results[LOCAL_HOME] = ejb.getLocalHome() ==null ? null : metadata.findResource(Utils.toResourceName(ejb.getLocalHome()));
 
111
                    return null;
 
112
                }
 
113
            });
 
114
        } catch (IOException ioe) {
 
115
            Exceptions.printStackTrace(ioe);
 
116
        }
 
117
 
 
118
        List<Action> actions = new ArrayList<Action>();
 
119
        actions.add(new GoToSourceAction(results[EJB_CLASS], NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoTo_BeanImplementation")));
 
120
        if (results[REMOTE] != null) {
 
121
            actions.add(new GoToSourceAction(results[REMOTE], NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoTo_RemoteInterface")));
 
122
        }
 
123
        if (results[LOCAL] != null) {
 
124
            actions.add(new GoToSourceAction(results[LOCAL], NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoTo_LocalInterface")));
 
125
        }
 
126
        if (results[HOME] != null) {
 
127
            actions.add(new GoToSourceAction(results[HOME], NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoTo_RemoteHomeInterface")));
 
128
        }
 
129
        if (results[LOCAL_HOME] != null) {
 
130
            actions.add(new GoToSourceAction(results[LOCAL_HOME], NbBundle.getMessage(GoToSourceActionGroup.class, "LBL_GoTo_LocalHomeInterface")));
 
131
        }
 
132
        
 
133
        return actions.toArray(new Action[actions.size()]);
 
134
    }
 
135
 
 
136
    public HelpCtx getHelpCtx() {
 
137
        return HelpCtx.DEFAULT_HELP;
 
138
    }
 
139
    
 
140
}