1 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 ; 24 import java.beans.BeanInfo ; 25 import java.io.*; 26 import java.util.HashMap ; 27 28 34 public class ClassFileViewer extends TopComponent 35 implements BrowserServices 36 { 37 38 private static HashMap fileObjectToClassFileViewer = new HashMap (); 39 40 private static final String 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 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 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 135 { 136 if (in == null) { 137 return; 138 } 139 boolean valid = in.readBoolean(); 140 if (!valid) { 141 return; 142 } 143 in.readUTF(); 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 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 className, BrowserPath browserPath) { 171 172 FileSystem targetFs = OpenAction.getTargetFileSystem(fo); 173 String 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 } 193 194 private synchronized void init () { 195 196 if (initialized) { 197 return; 198 } 199 200 if (!SwingUtilities.isEventDispatchThread()) { 201 SwingUtilities.invokeLater(new Runnable () { 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 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 name, Icon icon) { 277 super(name, icon); 278 } 279 280 public void actionPerformed(ActionEvent 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 |