KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > detail > ListDetailPane


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.detail;
9
10 import org.gjt.jclasslib.browser.AbstractDetailPane;
11 import org.gjt.jclasslib.browser.BrowserServices;
12 import org.gjt.jclasslib.browser.detail.attributes.LinkRenderer;
13
14 import javax.swing.*;
15 import javax.swing.table.TableModel JavaDoc;
16 import javax.swing.tree.TreePath JavaDoc;
17 import java.awt.*;
18 import java.awt.event.*;
19
20 /**
21     Base class for all detail panes with a structure of
22     a variable number of row entries with the same number of columns.
23     
24     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
25     @version $Revision: 1.5 $ $Date: 2003/08/18 08:11:01 $
26 */

27 public abstract class ListDetailPane extends AbstractDetailPane {
28
29     // Visual components
30

31     private JTable table;
32     
33     /**
34         Constructor.
35         @param services the associated browser services.
36      */

37     protected ListDetailPane(BrowserServices services) {
38         super(services);
39     }
40     
41     protected void setupComponent() {
42
43         setLayout(new BorderLayout());
44         
45         table = new JTable();
46         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
47         table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
48         float rowHeightFactor = getRowHeightFactor();
49         if (rowHeightFactor != 1f) {
50             table.setRowHeight((int)(table.getRowHeight() * rowHeightFactor));
51         }
52
53         TableLinkListener linkListener = new TableLinkListener();
54         table.addMouseListener(linkListener);
55         table.addMouseMotionListener(linkListener);
56
57
58         JScrollPane scrollPane = new JScrollPane(table);
59
60         add(scrollPane, BorderLayout.CENTER);
61     }
62
63     public void show(TreePath JavaDoc treePath) {
64         TableModel JavaDoc tableModel = getTableModel(treePath);
65         table.setModel(tableModel);
66
67         createTableColumnModel(table, tableModel);
68         ((JLabel)table.getDefaultRenderer(Number JavaDoc.class)).setVerticalAlignment(JLabel.TOP);
69         ((JLabel)table.getDefaultRenderer(String JavaDoc.class)).setVerticalAlignment(JLabel.TOP);
70         table.setDefaultRenderer(Link.class, new LinkRenderer());
71
72     }
73
74     /**
75         Get the factor for calculating the row height as a multiple of the normal row
76         height of a single label.
77         @return the factor.
78      */

79     protected float getRowHeightFactor() {
80         return 1f;
81     }
82
83     /**
84         Create the table column model for the given table and table column model.
85         @param table the table
86         @param tableModel the table model
87      */

88     protected void createTableColumnModel(JTable table, TableModel JavaDoc tableModel) {
89         table.createDefaultColumnsFromModel();
90     }
91
92     /**
93         Get the table model for the selected tree node.
94         @param treePath the tree path selected in <tt>BrowserTreePane</tt>
95         @return the table model
96      */

97     protected abstract TableModel JavaDoc getTableModel(TreePath JavaDoc treePath);
98
99     /**
100         Create a link value object with a comment for use of a <tt>LinkRenderer</tt>.
101         @param index the constant pool index to link to.
102         @return the link value object.
103      */

104     protected Object JavaDoc createCommentLink(int index) {
105         return new LinkRenderer.LinkCommentValue(
106                         CPINFO_LINK_TEXT + String.valueOf(index),
107                         getConstantPoolEntryName(index)
108         );
109     }
110
111     /**
112         Link to the destination described by the target of the hyperlink
113         contained in a specific cell.
114         @param row the row number of the hyperlink
115         @param column the column number of the hyperlink
116      */

117     protected void link(int row, int column) {
118     }
119     
120     /**
121         Class for caching dynamically computed values in a read only table.
122      */

123     public static class ColumnCache {
124         
125         private Object JavaDoc[][] cache;
126
127         /**
128          * Constructor.
129          * @param rowNumber the row number.
130          * @param columnNumber the column number.
131          */

132         public ColumnCache(int rowNumber, int columnNumber) {
133             cache = new Object JavaDoc[rowNumber][columnNumber];
134         }
135         
136         /**
137             Get the cached value of a specific cell.
138             @param row the row number of the cell
139             @param column the column number of the cell
140             @return the value
141          */

142         public Object JavaDoc getValueAt(int row, int column) {
143             return cache[row][column];
144         }
145
146         /**
147             Set the cached value of a specific cell.
148             @param row the row number of the cell
149             @param column the column number of the cell
150             @param value the value
151          */

152         public void setValueAt(int row, int column, Object JavaDoc value) {
153             cache[row][column] = value;
154         }
155         
156     }
157
158     /**
159         Marker class returned by <tt>getColumnClass()</tt> to indicate a hyperlink
160         in a table.
161      */

162     public static class Link {
163     }
164     
165     private class TableLinkListener extends MouseAdapter
166                                     implements MouseMotionListener
167     {
168
169         private Cursor defaultCursor;
170         private int defaultCursorType;
171         private Cursor handCursor;
172         
173         private TableLinkListener() {
174
175             defaultCursor = Cursor.getDefaultCursor();
176             defaultCursorType = defaultCursor.getType();
177             handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
178         }
179         
180         public void mouseClicked(MouseEvent event) {
181             
182             Point point = event.getPoint();
183
184             if (isLink(point)) {
185                 link(table.rowAtPoint(point), table.columnAtPoint(point));
186             }
187         }
188
189         public void mouseDragged(MouseEvent event) {
190         }
191         
192         public void mouseMoved(MouseEvent event) {
193
194             Point point = event.getPoint();
195
196             if (table.getCursor().getType() == defaultCursorType && isLink(point)) {
197                 table.setCursor(handCursor);
198             } else if (!isLink(point)) {
199                 table.setCursor(defaultCursor);
200             }
201         }
202         
203         private boolean isLink(Point point) {
204
205             int column = table.columnAtPoint(point);
206             int row = table.rowAtPoint(point);
207
208             return row >= 0 && column >= 0 &&
209                    table.getColumnClass(column).equals(Link.class) &&
210                    !table.getModel().getValueAt(row, column).toString().equals(CPINFO_LINK_TEXT + "0");
211         }
212
213         
214     }
215     
216 }
217
218
Popular Tags