KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > detail > attributes > AbstractAttributeListDetailPane


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.attributes;
9
10 import org.gjt.jclasslib.browser.BrowserServices;
11 import org.gjt.jclasslib.browser.detail.ListDetailPane;
12 import org.gjt.jclasslib.structures.AttributeInfo;
13
14 import javax.swing.*;
15 import javax.swing.table.*;
16 import javax.swing.tree.TreePath JavaDoc;
17 import java.util.WeakHashMap JavaDoc;
18
19 /**
20     Base class for all detail panes showing specific information for
21     a specific attribute tree node selected in <tt>BrowserTreePane</tt>
22     which can be displayed as a list of row entries with the same number of
23     columns.
24     
25     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
26     @version $Revision: 1.5 $ $Date: 2003/08/18 08:18:35 $
27 */

28 public abstract class AbstractAttributeListDetailPane extends ListDetailPane {
29
30     /** Default width in pixels for a column displaying a number. */
31     protected static final int NUMBER_COLUMN_WIDTH = 60;
32     /** Default width in pixels for a column displaying a hyperlink. */
33     protected static final int LINK_COLUMN_WIDTH = 80;
34     /** Default width in pixels for a column displaying a verbose entry. */
35     protected static final int VERBOSE_COLUMN_WIDTH = 250;
36
37     private static final int COLUMN_MIN_WIDTH = 20;
38     private static final int ROW_NUMBER_COLUMN_WIDTH = 35;
39
40     private static WeakHashMap JavaDoc attributeToTableModel = new WeakHashMap JavaDoc();
41
42     private AbstractAttributeTableModel tableModel;
43     
44     /**
45         Constructor.
46         @param services the associated browser services.
47      */

48     protected AbstractAttributeListDetailPane(BrowserServices services) {
49         super(services);
50     }
51     
52     protected TableModel getTableModel(TreePath JavaDoc treePath) {
53         AttributeInfo attribute = findAttribute(treePath);
54         
55         tableModel = getCachedTableModel(attribute);
56         return tableModel;
57     }
58
59     protected void link(int row, int column) {
60         tableModel.link(row, column);
61     }
62
63     /**
64         Create the table model for a specific attribute. This method is called
65         by <tt>getTableModel()</tt>.
66         @param attribute the attribute
67         @return the table model
68      */

69     protected abstract AbstractAttributeTableModel createTableModel(AttributeInfo attribute);
70     
71     /**
72         Get the width in pixels for a specific column.
73         @param column the index of the column in the model
74         @return the width
75      */

76     protected int getColumnWidth(int column) {
77         return tableModel.getColumnWidth(column);
78     }
79
80     protected void createTableColumnModel(JTable table, TableModel tableModel) {
81         AbstractAttributeTableModel attributeTableModel =
82                 (AbstractAttributeTableModel)tableModel;
83         
84         TableColumnModel tableColumnModel = attributeTableModel.getTableColumnModel();
85         if (tableColumnModel == null) {
86             table.createDefaultColumnsFromModel();
87             tableColumnModel = table.getColumnModel();
88             attributeTableModel.setTableColumnModel(tableColumnModel);
89
90         } else {
91             table.setColumnModel(tableColumnModel);
92         }
93         adjustColumns(table, tableColumnModel);
94     }
95     
96     
97     private void adjustColumns(JTable table, TableColumnModel tableColumnModel) {
98         
99         TableColumn tableColumn;
100         for (int i = 0; i < tableColumnModel.getColumnCount(); i++) {
101             tableColumn = tableColumnModel.getColumn(i);
102             tableColumn.setMinWidth(COLUMN_MIN_WIDTH);
103             
104             int width = (i == 0) ? ROW_NUMBER_COLUMN_WIDTH : getColumnWidth(i);
105             
106             tableColumn.setWidth(width);
107             tableColumn.setPreferredWidth(width);
108             
109         }
110
111     }
112     
113     private AbstractAttributeTableModel getCachedTableModel(AttributeInfo attribute) {
114         
115         AbstractAttributeTableModel tableModel =
116             (AbstractAttributeTableModel)attributeToTableModel.get(attribute);
117
118         if (tableModel == null) {
119             tableModel = createTableModel(attribute);
120                                 
121             attributeToTableModel.put(attribute, tableModel);
122             
123         }
124         
125         return tableModel;
126     }
127
128 }
129
130
Popular Tags