KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > BrowserInternalFrame


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.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 JavaDoc;
23 import java.beans.PropertyVetoException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.jar.JarEntry JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28
29 /**
30  * A child window of the class file browser application.
31  *
32  * @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
33  * @version $Revision: 1.9 $ $Date: 2004/12/28 13:04:31 $
34  */

35 public class BrowserInternalFrame extends BasicInternalFrame
36         implements BrowserServices {
37
38
39     /**
40      * Constructor for creating a derived <tt>BasicInternalFrame</tt> with
41      * an initialization parameter.
42      */

43     public static final Class JavaDoc[] CONSTRUCTOR_ARGUMENTS =
44             new Class JavaDoc[]{BasicDesktopManager.class, WindowState.class};
45
46     private String JavaDoc fileName;
47     private ClassFile classFile;
48
49     // Visual Components
50

51     private BrowserComponent browserComponent;
52
53     /**
54      * Constructor.
55      *
56      * @param desktopManager the associated desktop manager
57      * @param windowState the window state object. The frame will load the class file from
58      * information present within this object.
59      */

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 JavaDoc getInitParam() {
70         WindowState windowState = new WindowState(fileName, browserComponent.getBrowserPath());
71         return windowState;
72     }
73
74     // Browser services
75

76     public ClassFile getClassFile() {
77         return classFile;
78     }
79
80     public void activate() {
81
82         // force sync of toolbar state with this frame
83
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 JavaDoc 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 JavaDoc[]{"Setup classpath", "Cancel"},
106                     JOptionPane.WARNING_MESSAGE);
107             if (result == 0) {
108                 getParentFrame().getActionSetupClasspath().actionPerformed(new ActionEvent JavaDoc(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 JavaDoc 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 JavaDoc ex) {
131                     }
132                 } else {
133                     desktopManager.scrollToVisible(frame);
134                 }
135             }
136         }
137     }
138
139     public boolean canOpenClassFiles() {
140         return true;
141     }
142
143     /**
144      * Reload class file.
145      */

146     public void reload() {
147         readClassFile();
148         browserComponent.rebuild();
149     }
150
151     /**
152      * Get the file name for the displayed class file.
153      *
154      * @return the file name
155      */

156     public String JavaDoc 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 JavaDoc jarFileName = fileName.substring(0, index);
184                 String JavaDoc classFileName = fileName.substring(index + 1);
185                 JarFile JavaDoc jarFile = new JarFile JavaDoc(jarFileName);
186                 JarEntry JavaDoc 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 JavaDoc ex) {
196             ex.printStackTrace();
197         }
198     }
199
200 }
201
Popular Tags