KickJava   Java API By Example, From Geeks To Geeks.

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


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.window.*;
11 import org.gjt.jclasslib.structures.*;
12
13 import javax.swing.*;
14 import javax.swing.event.TreeSelectionEvent JavaDoc;
15 import javax.swing.event.TreeSelectionListener JavaDoc;
16 import javax.swing.tree.TreePath JavaDoc;
17 import java.awt.*;
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedList JavaDoc;
20
21 /**
22  * Visual component displaying a class file.
23  *
24  * @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
25  * @version $Revision: 1.9 $ $Date: 2004/12/28 13:04:31 $
26  */

27 public class BrowserComponent extends JComponent
28         implements TreeSelectionListener JavaDoc {
29
30     private BrowserHistory history;
31     private BrowserServices services;
32
33     // Visual Components
34

35     private JSplitPane splitPane;
36     private BrowserTreePane treePane;
37     private BrowserDetailPane detailPane;
38
39     /**
40      * Constructor.
41      *
42      * @param services the associated browser services
43      */

44     public BrowserComponent(BrowserServices services) {
45
46         this.services = services;
47         setupComponent();
48     }
49
50     /**
51      * Get the pane containing the tree structure for the shown class file.
52      *
53      * @return the pane
54      */

55     public BrowserTreePane getTreePane() {
56         return treePane;
57     }
58
59     /**
60      * Get the pane containing the detail area for the specific tree node selected
61      * in the <tt>BrowserTreePane</tt>.
62      *
63      * @return the pane
64      */

65     public BrowserDetailPane getDetailPane() {
66         return detailPane;
67     }
68
69     /**
70      * Get the navigation history of this child window.
71      *
72      * @return the history
73      */

74     public BrowserHistory getHistory() {
75         return history;
76     }
77
78     /**
79      * Construct a <tt>BrowserPath</tt> object for the curently selected path in the tree.
80      *
81      * @return the browser path
82      */

83     public BrowserPath getBrowserPath() {
84
85         TreePath JavaDoc selectionPath = treePane.getTree().getSelectionPath();
86         if (selectionPath == null || selectionPath.getPathCount() < 3) {
87             return null;
88         }
89
90         BrowserTreeNode categoryNode = (BrowserTreeNode)selectionPath.getPathComponent(2);
91         String JavaDoc category = categoryNode.getType();
92         if (category.equals(BrowserTreeNode.NODE_NO_CONTENT)) {
93             return null;
94         }
95
96         BrowserPath browserPath = new BrowserPath();
97         browserPath.addPathComponent(new CategoryHolder(category));
98         int categoryNodeIndex = categoryNode.getIndex();
99         if (category.equals(BrowserTreeNode.NODE_CONSTANT_POOL)) {
100             --categoryNodeIndex;
101         }
102         if (category.equals(BrowserTreeNode.NODE_METHOD)) {
103             MethodInfo methodInfo = services.getClassFile().getMethods()[categoryNodeIndex];
104             addClassMemberPathComponent(methodInfo, browserPath, selectionPath);
105         } else if (category.equals(BrowserTreeNode.NODE_FIELD)) {
106             FieldInfo fieldInfo = services.getClassFile().getFields()[categoryNodeIndex];
107             addClassMemberPathComponent(fieldInfo, browserPath, selectionPath);
108         } else {
109             browserPath.addPathComponent(new IndexHolder(categoryNodeIndex));
110         }
111
112         return browserPath;
113     }
114
115     /**
116      * Set the currently selected path in the tree by analyzing a <tt>BrowserPath</tt> object.
117      *
118      * @param browserPath the browser path
119      */

120     public void setBrowserPath(BrowserPath browserPath) {
121
122         if (browserPath == null) {
123             return;
124         }
125         LinkedList JavaDoc pathComponents = browserPath.getPathComponents();
126         Iterator JavaDoc it = pathComponents.iterator();
127         if (!it.hasNext()) {
128             return;
129         }
130         CategoryHolder categoryComponent = (CategoryHolder)it.next();
131         String JavaDoc category = categoryComponent.getCategory();
132         TreePath JavaDoc path = treePane.getPathForCategory(category);
133         if (path == null) {
134             return;
135         }
136         while (it.hasNext()) {
137             PathComponent pathComponent = (PathComponent)it.next();
138             int childIndex;
139             if (pathComponent instanceof ReferenceHolder) {
140                 ReferenceHolder referenceHolder = (ReferenceHolder)pathComponent;
141                 try {
142                     if (category.equals(BrowserTreeNode.NODE_METHOD)) {
143                         childIndex = services.getClassFile().getMethodIndex(referenceHolder.getName(), referenceHolder.getType());
144                     } else if (category.equals(BrowserTreeNode.NODE_FIELD)) {
145                         childIndex = services.getClassFile().getFieldIndex(referenceHolder.getName(), referenceHolder.getType());
146                     } else {
147                         break;
148                     }
149                 } catch (InvalidByteCodeException ex) {
150                     break;
151                 }
152             } else if (pathComponent instanceof IndexHolder) {
153                 childIndex = ((IndexHolder)pathComponent).getIndex();
154             } else {
155                 break;
156             }
157             BrowserTreeNode lastNode = (BrowserTreeNode)path.getLastPathComponent();
158             if (childIndex >= lastNode.getChildCount()) {
159                 break;
160             }
161             path = path.pathByAddingChild(lastNode.getChildAt(childIndex));
162         }
163
164         JTree tree = treePane.getTree();
165         tree.expandPath(path);
166         tree.setSelectionPath(path);
167         Object JavaDoc[] pathObjects = path.getPath();
168         if (pathObjects.length > 2) {
169             TreePath JavaDoc categoryPath = new TreePath JavaDoc(new Object JavaDoc[]{pathObjects[0], pathObjects[1], pathObjects[2]});
170             tree.scrollPathToVisible(categoryPath);
171         }
172
173     }
174
175     /**
176      * Rebuild tree view, clear history and try to set the same path in the browser as before.
177      */

178     public void rebuild() {
179
180
181         BrowserPath browserPath = getBrowserPath();
182         reset();
183         if (browserPath != null) {
184             setBrowserPath(browserPath);
185         }
186     }
187
188     /**
189      * Rebuild tree view and clear history.
190      */

191     public void reset() {
192
193         JTree tree = treePane.getTree();
194         tree.removeTreeSelectionListener(this);
195         treePane.rebuild();
196         history.clear();
197         tree.addTreeSelectionListener(this);
198         checkSelection();
199     }
200
201
202     /**
203      * Check whether anything is selected. If not select the first node.
204      */

205     public void checkSelection() {
206
207         JTree tree = treePane.getTree();
208         if (services.getClassFile() == null) {
209             ((CardLayout)detailPane.getLayout()).show(detailPane, BrowserTreeNode.NODE_NO_CONTENT);
210         } else {
211             if (tree.getSelectionPath() == null) {
212                 BrowserTreeNode rootNode = (BrowserTreeNode)tree.getModel().getRoot();
213                 tree.setSelectionPath(new TreePath JavaDoc(new Object JavaDoc[]{rootNode, rootNode.getFirstChild()}));
214             }
215         }
216     }
217
218     public void valueChanged(TreeSelectionEvent JavaDoc selectionEvent) {
219
220         services.activate();
221
222         TreePath JavaDoc selectedPath = selectionEvent.getPath();
223
224         history.updateHistory(selectedPath);
225         showDetailPaneForPath(selectedPath);
226
227     }
228
229     private void addClassMemberPathComponent(ClassMember classMember, BrowserPath browserPath, TreePath JavaDoc selectionPath) {
230
231         try {
232             browserPath.addPathComponent(new ReferenceHolder(classMember.getName(), classMember.getDescriptor()));
233             if (selectionPath.getPathCount() > 3) {
234                 for (int i = 3; i < selectionPath.getPathCount(); i++) {
235                     BrowserTreeNode attributeNode = (BrowserTreeNode)selectionPath.getPathComponent(i);
236                     browserPath.addPathComponent(new IndexHolder(attributeNode.getIndex()));
237                 }
238             }
239         } catch (InvalidByteCodeException ex) {
240         }
241     }
242
243     private void showDetailPaneForPath(TreePath JavaDoc path) {
244
245         BrowserTreeNode node = (BrowserTreeNode)path.getLastPathComponent();
246         String JavaDoc nodeType = node.getType();
247         detailPane.showPane(nodeType, path);
248     }
249
250
251     private void setupComponent() {
252
253         setLayout(new BorderLayout());
254
255         detailPane = new BrowserDetailPane(services);
256
257         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
258                 buildTreePane(),
259                 detailPane);
260
261         add(splitPane, BorderLayout.CENTER);
262
263     }
264
265     private BrowserTreePane buildTreePane() {
266
267         treePane = new BrowserTreePane(services);
268
269         JTree tree = treePane.getTree();
270         tree.addTreeSelectionListener(this);
271         history = new BrowserHistory(services);
272
273         return treePane;
274     }
275
276 }
277
Popular Tags