KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > nbmodule > OpenAction


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.nbmodule;
9
10 import org.gjt.jclasslib.browser.config.window.BrowserPath;
11 import org.netbeans.modules.java.JavaCompilerType;
12 import org.netbeans.modules.java.settings.JavaSettings;
13 import org.openide.compiler.CompilerType;
14 import org.openide.cookies.OpenCookie;
15 import org.openide.filesystems.*;
16 import org.openide.loaders.DataObject;
17 import org.openide.nodes.Node;
18 import org.openide.util.HelpCtx;
19 import org.openide.util.Lookup;
20 import org.openide.util.actions.CookieAction;
21
22 import javax.swing.*;
23
24 /**
25     Action to open a class file.
26  
27     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
28     @version $Revision: 1.6 $ $Date: 2003/08/18 07:56:58 $
29 */

30 public class OpenAction extends CookieAction {
31
32     static FileSystem getTargetFileSystem(FileObject foSource) {
33
34         JavaSettings javaSettings = (JavaSettings)Lookup.getDefault().lookup(JavaSettings.class);
35         CompilerType compiler = javaSettings.getCompiler();
36
37         if (!(compiler instanceof JavaCompilerType)) {
38             return null;
39         }
40         FileSystem targetFs = ((JavaCompilerType)compiler).getTargetFileSystem();
41         if (targetFs == null) {
42             try {
43                 targetFs = foSource.getFileSystem();
44             } catch (FileStateInvalidException ex) {
45                 return null;
46             }
47         }
48         return targetFs;
49
50     }
51
52     static void openFileObject(FileObject fo, final BrowserPath browserPath) {
53
54         final ClassFileViewer viewer = ClassFileViewer.getCachedClassFileViewer(fo);
55         if (!viewer.isOpened()) {
56             viewer.open();
57         }
58         SwingUtilities.invokeLater(
59                 new Runnable JavaDoc () {
60                      public void run () {
61                          viewer.getBrowserComponent().setBrowserPath(browserPath);
62                          viewer.requestFocus();
63                      }
64                 }
65         );
66     }
67
68     public String JavaDoc getName () {
69         return "View class file";
70     }
71     
72     protected String JavaDoc iconResource () {
73         return "/org/gjt/jclasslib/nbmodule/nbmodule.gif";
74     }
75     
76     protected boolean enable(Node[] nodes) {
77         return getClassFileObject(nodes) != null;
78     }
79
80     protected Class JavaDoc[] cookieClasses() {
81         return new Class JavaDoc[] { OpenCookie.class };
82     }
83
84     protected int mode () {
85         return MODE_EXACTLY_ONE;
86     }
87    
88     protected void performAction (Node[] nodes) {
89
90         FileObject fo = getClassFileObject(nodes);
91         if (fo != null) {
92             openFileObject(fo, null);
93         }
94     }
95
96     public HelpCtx getHelpCtx () {
97         return HelpCtx.DEFAULT_HELP;
98     }
99     
100     private FileObject getClassFileObject(Node[] nodes) {
101
102         if (nodes == null || nodes.length == 0) {
103             return null;
104         }
105         
106         DataObject dataObject = (DataObject)nodes[0].getCookie(DataObject.class);
107         if (dataObject == null) {
108             return null;
109         } else {
110             return lookupFileObject(dataObject);
111         }
112     }
113     
114     private FileObject lookupFileObject(DataObject dataObject) {
115
116         FileObject foSource = dataObject.getPrimaryFile();
117         if (foSource.hasExt("class")) {
118             return foSource;
119
120         } else if (foSource.hasExt("java")) {
121             FileSystem targetFs = getTargetFileSystem(foSource);
122
123             String JavaDoc className = foSource.getPackageName('/');
124             return targetFs.findResource(className + ".class");
125         } else {
126             return null;
127         }
128     }
129         
130 }
131
132
Popular Tags