KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > action > GoToSourceAction


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.ejb.action;
21
22 import java.awt.Toolkit JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.openide.ErrorManager;
28 import org.openide.cookies.EditCookie;
29 import org.openide.cookies.OpenCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.DataObjectNotFoundException;
33 import org.openide.util.actions.Presenter;
34
35 /**
36  *
37  * @author Martin Adamek
38  */

39 public class GoToSourceAction extends AbstractAction JavaDoc implements Presenter.Popup {
40
41     private final String JavaDoc className;
42     private final ClassPath classPath;
43     private final String JavaDoc actionName;
44     
45     public GoToSourceAction(ClassPath classPath, String JavaDoc className, String JavaDoc actionName) {
46         this.classPath = classPath;
47         this.className = className;
48         this.actionName = actionName;
49     }
50
51     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
52         openSourceFO(getFileObject());
53     }
54
55     public Object JavaDoc getValue(String JavaDoc key) {
56         if (NAME.equals(key)) {
57             return actionName;
58         }
59         else {
60             return super.getValue(key);
61         }
62     }
63
64     public JMenuItem JavaDoc getPopupPresenter() {
65         return new JMenuItem JavaDoc (this);
66     }
67
68     // private helpers =========================================================
69

70     private FileObject getFileObject() {
71         assert className != null: "cannot find null className"; //NOI18N
72
return classPath.findResource(className.replace('.', '/') + ".java"); //NOI18N
73
}
74     
75     /*
76      * from NbJavaFastOpen
77      */

78     private void openSourceFO(FileObject fileObject){
79         if (fileObject == null) {
80             return;
81         }
82         DataObject dob;
83         try {
84             dob = DataObject.find(fileObject);
85         } catch (DataObjectNotFoundException e) {
86             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
87             dob = null;
88         }
89         
90         if (dob != null) {
91             EditCookie editCookie = (EditCookie)dob.getCookie(EditCookie.class);
92             if (editCookie != null) {
93                 editCookie.edit();
94             } else {
95                 OpenCookie openCookie = (OpenCookie)dob.getCookie(OpenCookie.class);
96                 if (openCookie != null) {
97                     openCookie.open();
98                 } else {
99                     Toolkit.getDefaultToolkit().beep();
100                 }
101             }
102         }
103     }
104
105 }
Popular Tags