KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > decorator > ComponentAdapter


1 /*
2  * $Id: ComponentAdapter.java,v 1.2 2004/08/07 06:38:29 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.decorator;
9
10 import javax.swing.JComponent JavaDoc;
11
12 /**
13  * Abstract base class for all component data adapter classes.
14  * A <code>ComponentAdapter</code> allows a {@link Filter}, {@link Sorter},
15  * or {@link Highlighter} to interact with a {@link #target} component through a
16  * common API.
17  *
18  * @author Ramesh Gupta
19  */

20 public abstract class ComponentAdapter {
21     public int row = 0;
22     public int column = 0;
23     protected final JComponent JavaDoc target;
24
25     /**
26      * Constructs a ComponentAdapter, setting the specified component as the
27      * target component.
28      *
29      * @param component target component for this adapter
30      */

31     public ComponentAdapter(JComponent JavaDoc component) {
32         target = component;
33     }
34
35     public JComponent JavaDoc getComponent() {
36         return target;
37     }
38
39     /**
40      *
41      * @param columnIndex in view coordinates
42      * @return column name
43      */

44     public String JavaDoc getColumnName(int columnIndex) {
45         throw new RuntimeException JavaDoc("getColumnName() must be overridden by subclass");
46     }
47
48     /**
49      * Returns the number of columns in the target component's view.
50      *
51      * @return the number of columns in the target component's view
52      */

53     public int getColumnCount() {
54         return 1; // default for combo-boxes, lists, and trees
55
}
56
57     /**
58      * Returns the number of rows in the target component's view.
59      *
60      * @return the number of rows in the target component's view
61      */

62     public int getRowCount() {
63         return 0;
64     }
65
66     /**
67      * Returns the value of the cell identified by this adapter by invoking
68      * {@link #getValueAt(int, int)}, passing in the {@link #row} and
69      * {@link #column} values of this adapter. For target components that don't
70      * support multiple columns, the value of <code>column</code> is always zero.
71      *
72      * @return the value of the cell identified by this adapter
73      */

74     public Object JavaDoc getValue() {
75         return getValueAt(row, column);
76     }
77
78     /**
79      * Returns the value of the target component's cell identified by the
80      * specified row and column.
81      *
82      * @param row
83      * @param column
84      * @return the value of the target component's cell identified by the
85      * specified row and column
86      */

87     public abstract Object JavaDoc getValueAt(int row, int column);
88     public abstract Object JavaDoc getFilteredValueAt(int row, int column);
89     public abstract void setValueAt(Object JavaDoc aValue, int row, int column);
90
91     public abstract boolean isCellEditable(int row, int column);
92
93     /**
94      * Returns true if the cell identified by this adapter currently has focus;
95      * Otherwise, it returns false.
96      *
97      * @return true if the cell identified by this adapter currently has focus;
98      * Otherwise, return false
99      */

100     public abstract boolean hasFocus();
101
102     /**
103      * Returns true if the cell identified by this adapter is currently selected;
104      * Otherwise, it returns false.
105      *
106      * @return true if the cell identified by this adapter is currently selected;
107      * Otherwise, return false
108      */

109     public abstract boolean isSelected();
110
111     /**
112      * Returns true if the cell identified by this adapter is currently expanded;
113      * Otherwise, it returns false. For components that do not support
114      * hierarchical data, this method always returns true because the cells in
115      * such components can never be collapsed.
116      *
117      * @return true if the cell identified by this adapter is currently expanded;
118      * Otherwise, return false
119      */

120     public boolean isExpanded() {
121         return true; // sensible default for JList and JTable
122
}
123
124     /**
125      * Returns true if the cell identified by this adapter is a leaf node;
126      * Otherwise, it returns false. For components that do not support
127      * hierarchical data, this method always returns true because the cells in
128      * such components can never have children.
129      *
130      * @return true if the cell identified by this adapter is a leaf node;
131      * Otherwise, return false
132      */

133     public boolean isLeaf() {
134         return true; // sensible default for JList and JTable
135
}
136
137     /**
138      * Returns true if the cell identified by this adapter displays the hierarchical node;
139      * Otherwise, it returns false. For components that do not support
140      * hierarchical data, this method always returns false because the cells in
141      * such components can never have children.
142      *
143      * @return true if the cell identified by this adapter displays the hierarchical node;
144      * Otherwise, return false
145      */

146     public boolean isHierarchical() {
147         return false; // sensible default for JList and JTable
148
}
149
150     /**
151      * For target components that support multiple columns in their model,
152      * along with column reordering in the view, this method transforms the
153      * specified columnIndex from model coordinates to view coordinates. For all
154      * other types of target components, this method returns the columnIndex
155      * unchanged.
156      *
157      * @param columnIndex index of a column in model coordinates
158      * @return index of the specified column in view coordinates
159      */

160     public int modelToView(int columnIndex) {
161         return columnIndex; // sensible default for JList and JTree
162
}
163
164     /**
165      * For target components that support multiple columns in their model,
166      * along with column reordering in the view, this method transforms the
167      * specified columnIndex from view coordinates to model coordinates. For all
168      * other types of target components, this method returns the columnIndex
169      * unchanged.
170      *
171      * @param columnIndex index of a column in view coordinates
172      * @return index of the specified column in model coordinates
173      */

174     public int viewToModel(int columnIndex) {
175         return columnIndex; // sensible default for JList and JTree
176
}
177
178     public void refresh() {
179         target.revalidate();
180         target.repaint();
181     }
182 }
Popular Tags