001: /*
002: * $Id: ComponentAdapter.java,v 1.8 2006/07/21 09:38:26 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.decorator;
023:
024: import javax.swing.JComponent;
025:
026: /**
027: * Abstract base class for all component data adapter classes.
028: * A <code>ComponentAdapter</code> allows a {@link Filter}, {@link Sorter},
029: * or {@link Highlighter} to interact with a {@link #target} component through a
030: * common API.
031: *
032: * It has two aspects:
033: * <ul>
034: * <li> interact with the data of the component. The methods for this are those
035: * taking row/column indices as parameters. The coordinates
036: * are in model coordinate system. Typical clients of are Filters.
037: * <li> interact with the view state for a given data element. The row/cloumn fields and the
038: * parameterless methods service this aspect. The coordinates are in view coordinate system.
039: * Typical clients are the highlighting part of Highlighters.
040: * </ul>
041: *
042: * The adapter is responsible for mapping column coordinates.
043: *
044: * All input column
045: * indices are in model coordinates with exactly two exceptions:
046: * <ul>
047: * <li> {@link #column} in column view coordinates
048: * <li> the mapping method {@link #viewToModel(int)} in view coordinates
049: * </ul>
050: *
051: * All input row indices are in model coordinates with exactly two exceptions:
052: * <ul>
053: * <li> {@link #row} in row view coordinates
054: * <li> the getter for the filtered value {@link #getFilteredValueAt(int, int)}
055: * takes the row in view coordinates.
056: * </ul>
057: *
058: *
059: * @author Ramesh Gupta
060: */
061: public abstract class ComponentAdapter {
062: /** current row in view coordinates. */
063: public int row = 0;
064: /** current column in view coordinates. */
065: public int column = 0;
066: protected final JComponent target;
067:
068: /**
069: * Constructs a ComponentAdapter, setting the specified component as the
070: * target component.
071: *
072: * @param component target component for this adapter
073: */
074: public ComponentAdapter(JComponent component) {
075: target = component;
076: }
077:
078: public JComponent getComponent() {
079: return target;
080: }
081:
082: //---------------------------- accessing the target's model
083:
084: /**
085: * returns the column's label (= headerValue).
086: *
087: * Used f.i. in SearchPanel to fill the field with the
088: * column name.
089: *
090: * Note: it's up to the implementation to decide for which
091: * columns it returns a name - most will do so for the
092: * subset with isTestable = true.
093: *
094: * @param columnIndex in model coordinates
095: * @return column name or null if not found/not testable.
096: */
097: public abstract String getColumnName(int columnIndex);
098:
099: /**
100: * returns the logical name (== identifier) of the column at
101: * columnIndex in model coordinates.
102: *
103: * Used f.i. JNTable to store and apply column properties by identifier.
104: *
105: * Note: it's up to the implementation to decide for which
106: * columns it returns a name - most will do so for the
107: * subset with isTestable = true.
108: *
109: * @param columnIndex in model coordinates
110: * @return the String value of the column identifier at columnIndex
111: * or null if no identifier set
112: */
113: public abstract String getColumnIdentifier(int columnIndex);
114:
115: /**
116: * Returns the number of columns in the target's data model.
117: *
118: * @return the number of columns in the target's data model.
119: */
120: public int getColumnCount() {
121: return 1; // default for combo-boxes, lists, and trees
122: }
123:
124: /**
125: * Returns the number of rows in the target's data model.
126: *
127: * @return the number of rows in the target's data model.
128: */
129: public int getRowCount() {
130: return 0;
131: }
132:
133: /**
134: * Returns the value of the target component's cell identified by the
135: * specified row and column in model coordinates.
136: *
137: * @param row in model coordinates
138: * @param column in model coordinates
139: * @return the value of the target component's cell identified by the
140: * specified row and column
141: */
142: public abstract Object getValueAt(int row, int column);
143:
144: public abstract void setValueAt(Object aValue, int row, int column);
145:
146: public abstract boolean isCellEditable(int row, int column);
147:
148: /**
149: * returns true if the column should be included in testing.
150: * Here: returns true if visible (that is modelToView gives a valid
151: * view column coordinate).
152: *
153: * @param column in model coordinates
154: * @return true if the column should be included in testing
155: */
156: public boolean isTestable(int column) {
157: return modelToView(column) >= 0;
158: }
159:
160: //----------------------- accessing the target's view state
161:
162: /**
163: * Returns the value of the cell identified by this adapter by invoking
164: * {@link #getValueAt(int, int)}, passing in the {@link #row} and
165: * {@link #column} values of this adapter. For target components that don't
166: * support multiple columns, the value of <code>column</code> is always zero.
167: *
168: * PENDING: needs clarification/cleanup - getValueAt(row, column) expects
169: * model coordinates!.
170: *
171: * @return the value of the cell identified by this adapter
172: */
173: public Object getValue() {
174: return getValueAt(row, column);
175: }
176:
177: /**
178: * returns the filtered value of the cell identified by the row
179: * in view coordinate and the column in model coordinates.
180: *
181: * Note: the asymetry of the coordinates is intentional - clients like
182: * Highlighters are interested in view values but might need to access
183: * non-visible columns for testing.
184: *
185: * @param row
186: * @param column
187: * @return the filtered value of the cell identified by the row
188: * in view coordinate and the column in model coordiantes
189: */
190: public abstract Object getFilteredValueAt(int row, int column);
191:
192: /**
193: * Returns true if the cell identified by this adapter currently has focus;
194: * Otherwise, it returns false.
195: *
196: * @return true if the cell identified by this adapter currently has focus;
197: * Otherwise, return false
198: */
199: public abstract boolean hasFocus();
200:
201: /**
202: * Returns true if the cell identified by this adapter is currently selected;
203: * Otherwise, it returns false.
204: *
205: * @return true if the cell identified by this adapter is currently selected;
206: * Otherwise, return false
207: */
208: public abstract boolean isSelected();
209:
210: /**
211: * Returns true if the cell identified by this adapter is currently expanded;
212: * Otherwise, it returns false. For components that do not support
213: * hierarchical data, this method always returns true because the cells in
214: * such components can never be collapsed.
215: *
216: * @return true if the cell identified by this adapter is currently expanded;
217: * Otherwise, return false
218: */
219: public boolean isExpanded() {
220: return true; // sensible default for JList and JTable
221: }
222:
223: /**
224: * Returns true if the cell identified by this adapter is a leaf node;
225: * Otherwise, it returns false. For components that do not support
226: * hierarchical data, this method always returns true because the cells in
227: * such components can never have children.
228: *
229: * @return true if the cell identified by this adapter is a leaf node;
230: * Otherwise, return false
231: */
232: public boolean isLeaf() {
233: return true; // sensible default for JList and JTable
234: }
235:
236: /**
237: * Returns true if the cell identified by this adapter displays the hierarchical node;
238: * Otherwise, it returns false. For components that do not support
239: * hierarchical data, this method always returns false because the cells in
240: * such components can never have children.
241: *
242: * @return true if the cell identified by this adapter displays the hierarchical node;
243: * Otherwise, return false
244: */
245: public boolean isHierarchical() {
246: return false; // sensible default for JList and JTable
247: }
248:
249: /**
250: * For target components that support multiple columns in their model,
251: * along with column reordering in the view, this method transforms the
252: * specified columnIndex from model coordinates to view coordinates. For all
253: * other types of target components, this method returns the columnIndex
254: * unchanged.
255: *
256: * @param columnIndex index of a column in model coordinates
257: * @return index of the specified column in view coordinates
258: */
259: public int modelToView(int columnIndex) {
260: return columnIndex; // sensible default for JList and JTree
261: }
262:
263: /**
264: * For target components that support multiple columns in their model,
265: * along with column reordering in the view, this method transforms the
266: * specified columnIndex from view coordinates to model coordinates. For all
267: * other types of target components, this method returns the columnIndex
268: * unchanged.
269: *
270: * @param columnIndex index of a column in view coordinates
271: * @return index of the specified column in model coordinates
272: */
273: public int viewToModel(int columnIndex) {
274: return columnIndex; // sensible default for JList and JTree
275: }
276:
277: public void refresh() {
278: target.revalidate();
279: target.repaint();
280: }
281: }
|