KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
11 import org.gjt.jclasslib.browser.config.window.BrowserPath;
12 import org.gjt.jclasslib.io.ClassFileReader;
13 import org.gjt.jclasslib.structures.ClassFile;
14 import org.openide.*;
15 import org.openide.filesystems.*;
16 import org.openide.filesystems.FileSystem;
17 import org.openide.nodes.Node;
18 import org.openide.text.CloneableEditorSupport;
19 import org.openide.windows.*;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.io.*;
26 import java.util.HashMap JavaDoc;
27
28 /**
29     Parent component for a class file browser in Netbeans.
30
31     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
32     @version $Revision: 1.9 $ $Date: 2004/02/10 16:06:56 $
33 */

34 public class ClassFileViewer extends TopComponent
35                              implements BrowserServices
36 {
37
38     private static HashMap JavaDoc fileObjectToClassFileViewer = new HashMap JavaDoc();
39
40     private static final String JavaDoc VERSION = "1.2";
41
42     private FileObject fo;
43     private ClassFileNode node;
44     private boolean initialized = false;
45     private ClassFile classFile;
46
47     private Action actionBackward;
48     private Action actionForward;
49     private Action actionReload;
50
51     private BrowserComponent browserComponent;
52
53     /**
54         Retrieve an already opened <tt>ClassFileViewer</tt> or create
55         a new one if necessary.
56         @param fo the <tt>FileObject</tt> for which to create a
57                   <tt>ClassFileViewer</tt>
58         @return the <tt>ClassFileViewer</tt>
59      */

60     public static ClassFileViewer getCachedClassFileViewer(FileObject fo) {
61
62         if (fileObjectToClassFileViewer.containsKey(fo)) {
63             return (ClassFileViewer)fileObjectToClassFileViewer.get(fo);
64         } else {
65             ClassFileViewer viewer = new ClassFileViewer(fo);
66             fileObjectToClassFileViewer.put(fo, viewer);
67             return viewer;
68         }
69     }
70
71     /**
72      * Constructor.
73      */

74     public ClassFileViewer() {
75         setCloseOperation(CLOSE_EACH);
76     }
77
78     private ClassFileViewer(FileObject fo) {
79
80         this();
81         this.fo = fo;
82         node = new ClassFileNode(fo);
83         setActivatedNodes(new Node[] {node});
84
85     }
86
87     public boolean canClose (Workspace workspace, boolean last) {
88
89         fileObjectToClassFileViewer.remove(fo);
90         return true;
91     }
92
93     public Image getIcon() {
94
95         if (node != null) {
96             return node.getIcon(BeanInfo.ICON_COLOR_16x16);
97         } else {
98             return null;
99         }
100
101     }
102
103     public void open(Workspace ws) {
104
105         init();
106         if (ws == null) {
107             ws = WindowManager.getDefault().getCurrentWorkspace();
108         }
109         Mode mode = ws.findMode(CloneableEditorSupport.EDITOR_MODE);
110         if (mode != null) {
111             mode.dockInto(this);
112         }
113         super.open(ws);
114     }
115
116     public void writeExternal (ObjectOutput out)
117         throws IOException
118     {
119         if (out == null) {
120             return;
121         }
122         if (node == null) {
123             out.writeBoolean(false);
124             return;
125         }
126         out.writeBoolean(true);
127         out.writeUTF(VERSION);
128         out.writeObject(node.getHandle());
129
130         super.writeExternal(out);
131     }
132
133     public void readExternal (ObjectInput in)
134         throws IOException, ClassNotFoundException JavaDoc
135     {
136         if (in == null) {
137             return;
138         }
139         boolean valid = in.readBoolean();
140         if (!valid) {
141             return;
142         }
143         in.readUTF(); // version string
144
Node.Handle handle = (Node.Handle)in.readObject();
145         super.readExternal(in);
146
147         node = (ClassFileNode)handle.getNode();
148         fo = node.getFileObject();
149         setActivatedNodes(new Node[] {node});
150     }
151
152     // Browser services
153

154     public ClassFile getClassFile() {
155         return classFile;
156     }
157
158     public BrowserComponent getBrowserComponent() {
159         return browserComponent;
160     }
161
162     public Action getActionBackward() {
163         return actionBackward;
164     }
165
166     public Action getActionForward() {
167         return actionForward;
168     }
169
170     public void openClassFile(String JavaDoc className, BrowserPath browserPath) {
171
172         FileSystem targetFs = OpenAction.getTargetFileSystem(fo);
173         String JavaDoc classFileName = className.replace('.', '/') + ".class";
174         FileObject targetFo = targetFs.findResource(classFileName);
175         if (targetFo == null) {
176             targetFo = Repository.getDefault().findResource(classFileName);
177         }
178         if (targetFo != null) {
179             OpenAction.openFileObject(targetFo, browserPath);
180         } else {
181             NotifyDescriptor desc = new NotifyDescriptor.Message("The class " + className + " could not be found.", NotifyDescriptor.INFORMATION_MESSAGE);
182             TopManager.getDefault().notify(desc);
183         }
184     }
185
186     public boolean canOpenClassFiles() {
187         return true;
188     }
189
190     public void activate() {
191         // not applicable
192
}
193
194     private synchronized void init () {
195
196         if (initialized) {
197             return;
198         }
199
200         if (!SwingUtilities.isEventDispatchThread()) {
201             SwingUtilities.invokeLater(new Runnable JavaDoc () {
202                                             public void run () {
203                                                 init();
204                                             }
205                                        });
206             return;
207         }
208
209         if (fo != null) {
210             setName(fo.getName());
211         }
212
213         if (fo != null && !readClassFile()) {
214             this.close();
215             return;
216         }
217
218         setupActions();
219         setupComponent();
220
221         initialized = true;
222     }
223
224     private boolean readClassFile() {
225         try {
226             classFile =
227                 ClassFileReader.readFromInputStream(fo.getInputStream());
228         } catch (Exception JavaDoc ex) {
229             ErrorManager.getDefault().notify(
230                 ErrorManager.EXCEPTION,
231                 ex);
232             return false;
233         }
234         return true;
235     }
236
237     private void setupActions() {
238
239         actionBackward = new DefaultAction("Backward", BrowserMDIFrame.loadIcon("browser_backward_small.png"));
240         actionBackward.putValue(Action.SHORT_DESCRIPTION, "Move backward in the navigation history");
241         actionBackward.setEnabled(false);
242
243         actionForward = new DefaultAction("Forward", BrowserMDIFrame.loadIcon("browser_forward_small.png"));
244         actionForward.putValue(Action.SHORT_DESCRIPTION, "Move forward in the navigation history");
245         actionForward.setEnabled(false);
246
247         actionReload = new DefaultAction("Reload", BrowserMDIFrame.loadIcon("reload_small.png"));
248         actionReload.putValue(Action.SHORT_DESCRIPTION, "Reload class file");
249         actionReload.setEnabled(true);
250
251     }
252
253     private void setupComponent() {
254
255         setLayout(new BorderLayout());
256         browserComponent = new BrowserComponent(this);
257         add(buildToolbar(), BorderLayout.NORTH);
258         add(browserComponent, BorderLayout.CENTER);
259     }
260
261     private JToolBar buildToolbar() {
262
263         JToolBar toolBar = new JToolBar();
264         toolBar.add(actionBackward);
265         toolBar.add(actionForward);
266         toolBar.addSeparator();
267         toolBar.add(actionReload);
268
269         toolBar.setFloatable(false);
270
271         return toolBar;
272     }
273
274     private class DefaultAction extends AbstractAction {
275
276         private DefaultAction(String JavaDoc name, Icon icon) {
277             super(name, icon);
278         }
279
280         public void actionPerformed(ActionEvent JavaDoc ev) {
281
282             if (this == actionBackward) {
283                 browserComponent.getHistory().historyBackward();
284             } else if (this == actionForward) {
285                 browserComponent.getHistory().historyForward();
286             } else if (this == actionReload) {
287                 readClassFile();
288                 browserComponent.rebuild();
289             }
290         }
291     }
292
293 }
294
Popular Tags