KickJava   Java API By Example, From Geeks To Geeks.

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


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.structures.*;
11 import org.gjt.jclasslib.util.ExtendedJLabel;
12
13 import javax.swing.*;
14 import javax.swing.tree.TreePath JavaDoc;
15 import java.awt.*;
16 import java.awt.event.MouseListener JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 /**
20     Base class for all detail panes showing specific information for
21     a specific tree node selected in <tt>BrowserTreePane</tt>.
22     
23     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
24     @version $Revision: 1.5 $ $Date: 2003/08/18 08:06:31 $
25 */

26 public abstract class AbstractDetailPane extends JPanel {
27     
28     /** Text prepended to constant pool hyperlinks. */
29     public static final String JavaDoc CPINFO_LINK_TEXT = "cp_info #";
30     /** Color for hyperlinks. */
31     public static final Color COLOR_LINK = new Color(0, 128, 0);
32
33     /** Color for highlighted text (values in key-value pairs). */
34     protected static final Color COLOR_HIGHLIGHT = new Color(128, 0, 0);
35
36     /** Services for this detail pane. */
37     protected BrowserServices services;
38
39     private HashMap JavaDoc labelToMouseListener = new HashMap JavaDoc();
40  
41     /**
42         Constructs a detail pane with a specified parent frame.
43         @param services browser services
44      */

45     protected AbstractDetailPane(BrowserServices services) {
46         this.services = services;
47         setupComponent();
48     }
49
50     /**
51         Get the associated <tt>BrowserServices</tt> object.
52         @return the browser services
53      */

54     public BrowserServices getBrowserServices() {
55         return services;
56     }
57
58     /**
59         Show the detail pane for a specific tree node
60         selected in <tt>BrowserTreePane</tt>.
61         @param treePath the <tt>TreePath</tt> for the selection
62                         in <tt>BrowserTreePane</tt>
63      */

64     public abstract void show(TreePath JavaDoc treePath);
65
66     /**
67         Setup the detail pane at the beginning of its life cycle.
68      */

69     protected abstract void setupComponent();
70
71     /**
72         Create a normal label (keys in key-value pairs).
73         @return the label
74      */

75     protected ExtendedJLabel normalLabel() {
76         return normalLabel("");
77     }
78
79     /**
80         Create a normal label (keys in key-value pairs).
81         @param text the text for the label
82         @return the label
83      */

84     protected ExtendedJLabel normalLabel(String JavaDoc text) {
85         ExtendedJLabel label = new ExtendedJLabel(text);
86         return label;
87     }
88
89     /**
90         Create a highlighted label (values in key-value pairs).
91         @return the label
92      */

93     protected ExtendedJLabel highlightLabel() {
94         ExtendedJLabel label = normalLabel();
95         label.setForeground(COLOR_HIGHLIGHT);
96         return label;
97     }
98     
99     /**
100         Create a label with the appearance of a hyperlink.
101         @return the label
102      */

103     protected ExtendedJLabel linkLabel() {
104         ExtendedJLabel label = normalLabel();
105         label.setForeground(COLOR_LINK);
106         label.setRequestFocusEnabled(true);
107         label.setUnderlined(true);
108         return label;
109     }
110     
111     /**
112         Determine the index of the tree node selected in <tt>BrowserTreePane</tt>
113         among its siblings.
114         @param treePath the tree path
115         @return the index
116      */

117     protected int getIndex(TreePath JavaDoc treePath) {
118         return ((BrowserTreeNode)treePath.getLastPathComponent()).getIndex();
119     }
120     
121     /**
122         Find the attribute pertaining to a specific tree path.
123         @param path the tree path
124         @return the attribute
125      */

126     protected AttributeInfo findAttribute(TreePath JavaDoc path) {
127         
128         TreePath JavaDoc parentPath = path.getParentPath();
129         BrowserTreeNode parentNode = (BrowserTreeNode)parentPath.getLastPathComponent();
130         String JavaDoc parentNodeType = parentNode.getType();
131         
132         ClassFile classFile = services.getClassFile();
133         int parentIndex = getIndex(parentPath);
134         int index = getIndex(path);
135         
136         if (parentNodeType.equals(BrowserTreeNode.NODE_NO_CONTENT)) {
137             return classFile.getAttributes()[index];
138
139         } else if (parentNodeType.equals(BrowserTreeNode.NODE_FIELD)) {
140             return classFile.getFields()[parentIndex].getAttributes()[index];
141
142         } else if (parentNodeType.equals(BrowserTreeNode.NODE_METHOD)) {
143             return classFile.getMethods()[parentIndex].getAttributes()[index];
144
145         } else {
146             return findAttribute(parentPath).getAttributes()[index];
147         }
148     }
149     
150     /**
151         Get the name of a constant pool entry.
152         @param constantPoolIndex the index of the constant pool entry
153         @return the name
154      */

155     protected String JavaDoc getConstantPoolEntryName(int constantPoolIndex) {
156
157         try {
158             return services.getClassFile().getConstantPoolEntryName(constantPoolIndex);
159         } catch (InvalidByteCodeException ex) {
160             return "invalid constant pool reference";
161         }
162     }
163     
164     /**
165         Construct a hyperlink into the constant pool.
166         @param value the label for the hyperlink source
167         @param comment an oprional label whose text is automatically set to
168                        the name of the constant pool entry
169         @param constantPoolIndex the index of the constant pool entry for the
170                                  target of the hyperlink
171      */

172     protected void constantPoolHyperlink(ExtendedJLabel value,
173                                          ExtendedJLabel comment,
174                                          int constantPoolIndex) {
175                                          
176         value.setText(CPINFO_LINK_TEXT + constantPoolIndex);
177         setupMouseListener(value, constantPoolIndex);
178         value.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
179
180         if (comment != null) {
181             comment.setToolTipText(comment.getText());
182             comment.setText("<" + getConstantPoolEntryName(constantPoolIndex) + ">");
183         }
184
185     }
186     
187     private void setupMouseListener(ExtendedJLabel value, int constantPoolIndex) {
188
189         MouseListener JavaDoc oldListener = (MouseListener JavaDoc)labelToMouseListener.get(value);
190         if (oldListener != null) {
191             value.removeMouseListener(oldListener);
192         }
193         MouseListener JavaDoc newListener = new ConstantPoolHyperlinkListener(
194                                         services,
195                                         constantPoolIndex);
196
197         value.addMouseListener(newListener);
198         labelToMouseListener.put(value, newListener);
199     }
200 }
201
202
Popular Tags