1 7 8 package org.gjt.jclasslib.browser; 9 10 import org.gjt.jclasslib.browser.config.classpath.FindResult; 11 import org.gjt.jclasslib.browser.config.window.BrowserPath; 12 import org.gjt.jclasslib.browser.config.window.WindowState; 13 import org.gjt.jclasslib.io.ClassFileReader; 14 import org.gjt.jclasslib.mdi.BasicDesktopManager; 15 import org.gjt.jclasslib.mdi.BasicInternalFrame; 16 import org.gjt.jclasslib.structures.ClassFile; 17 import org.gjt.jclasslib.structures.InvalidByteCodeException; 18 import org.gjt.jclasslib.util.GUIHelper; 19 20 import javax.swing.*; 21 import java.awt.*; 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyVetoException ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.jar.JarEntry ; 27 import java.util.jar.JarFile ; 28 29 35 public class BrowserInternalFrame extends BasicInternalFrame 36 implements BrowserServices { 37 38 39 43 public static final Class [] CONSTRUCTOR_ARGUMENTS = 44 new Class []{BasicDesktopManager.class, WindowState.class}; 45 46 private String fileName; 47 private ClassFile classFile; 48 49 51 private BrowserComponent browserComponent; 52 53 60 public BrowserInternalFrame(BasicDesktopManager desktopManager, WindowState windowState) { 61 super(desktopManager, windowState.getFileName()); 62 this.fileName = windowState.getFileName(); 63 64 setFrameIcon(BrowserMDIFrame.ICON_APPLICATION); 65 readClassFile(); 66 setupInternalFrame(windowState.getBrowserPath()); 67 } 68 69 public Object getInitParam() { 70 WindowState windowState = new WindowState(fileName, browserComponent.getBrowserPath()); 71 return windowState; 72 } 73 74 76 public ClassFile getClassFile() { 77 return classFile; 78 } 79 80 public void activate() { 81 82 desktopManager.getDesktopPane().setSelectedFrame(this); 84 } 85 86 public BrowserComponent getBrowserComponent() { 87 return browserComponent; 88 } 89 90 public Action getActionBackward() { 91 return getParentFrame().getActionBackward(); 92 } 93 94 public Action getActionForward() { 95 return getParentFrame().getActionForward(); 96 } 97 98 public void openClassFile(String className, BrowserPath browserPath) { 99 100 FindResult findResult = getParentFrame().getConfig().findClass(className); 101 while (findResult == null) { 102 int result = GUIHelper.showOptionDialog(getParentFrame(), 103 "The class " + className + " could not be found.\n" + 104 "You can check your classpath configuration and try again.", 105 new String []{"Setup classpath", "Cancel"}, 106 JOptionPane.WARNING_MESSAGE); 107 if (result == 0) { 108 getParentFrame().getActionSetupClasspath().actionPerformed(new ActionEvent (this, 0, null)); 109 findResult = getParentFrame().getConfig().findClass(className); 110 } else { 111 return; 112 } 113 } 114 115 BrowserInternalFrame frame = (BrowserInternalFrame)desktopManager.getOpenFrame(new WindowState(findResult.getFileName())); 116 if (frame != null) { 117 try { 118 frame.setSelected(true); 119 frame.browserComponent.setBrowserPath(browserPath); 120 desktopManager.scrollToVisible(frame); 121 } catch (PropertyVetoException e) { 122 } 123 } else { 124 WindowState windowState = new WindowState(findResult.getFileName(), browserPath); 125 frame = new BrowserInternalFrame(desktopManager, windowState); 126 if (frame != null) { 127 if (isMaximum()) { 128 try { 129 frame.setMaximum(true); 130 } catch (PropertyVetoException ex) { 131 } 132 } else { 133 desktopManager.scrollToVisible(frame); 134 } 135 } 136 } 137 } 138 139 public boolean canOpenClassFiles() { 140 return true; 141 } 142 143 146 public void reload() { 147 readClassFile(); 148 browserComponent.rebuild(); 149 } 150 151 156 public String getFileName() { 157 return fileName; 158 } 159 160 private void setupInternalFrame(BrowserPath browserPath) { 161 162 setTitle(fileName); 163 164 Container contentPane = getContentPane(); 165 contentPane.setLayout(new BorderLayout()); 166 167 browserComponent = new BrowserComponent(this); 168 contentPane.add(browserComponent, BorderLayout.CENTER); 169 170 setupInternalFrame(); 171 browserComponent.setBrowserPath(browserPath); 172 173 } 174 175 private BrowserMDIFrame getParentFrame() { 176 return (BrowserMDIFrame)desktopManager.getParentFrame(); 177 } 178 179 private void readClassFile() { 180 try { 181 int index = fileName.indexOf('!'); 182 if (index > -1) { 183 String jarFileName = fileName.substring(0, index); 184 String classFileName = fileName.substring(index + 1); 185 JarFile jarFile = new JarFile (jarFileName); 186 JarEntry jarEntry = jarFile.getJarEntry(classFileName); 187 if (jarEntry != null) { 188 classFile = ClassFileReader.readFromInputStream(jarFile.getInputStream(jarEntry)); 189 } 190 } else { 191 classFile = ClassFileReader.readFromFile(new File(fileName)); 192 } 193 } catch (InvalidByteCodeException ex) { 194 ex.printStackTrace(); 195 } catch (IOException ex) { 196 ex.printStackTrace(); 197 } 198 } 199 200 } 201 | Popular Tags |