ComponentAdapter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:10k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ComponentAdapter.java,v 1.5 2005/10/10 18:02:28 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.decorator;
  22. import javax.swing.JComponent;
  23. /**
  24.  * Abstract base class for all component data adapter classes.
  25.  * A <code>ComponentAdapter</code> allows a {@link Filter}, {@link Sorter},
  26.  * or {@link Highlighter} to interact with a {@link #target} component through a
  27.  * common API.
  28.  * 
  29.  * It has two aspects:
  30.  * <ul>
  31.  * <li> interact with the data of the component. The methods for this are those 
  32.  * taking row/column indices as parameters. The coordinates
  33.  * are in model coordinate system. Typical clients of are Filters.
  34.  * <li> interact with the view state for a given data element. The row/cloumn fields and the
  35.  * parameterless methods service this aspect. The coordinates are in view coordinate system.
  36.  * Typical clients are the highlighting part of Highlighters.
  37.  * </ul>
  38.  * 
  39.  * The adapter is responsible for mapping column coordinates. 
  40.  * 
  41.  * All input column 
  42.  * indices are in model coordinates with exactly two exceptions:
  43.  * <ul>
  44.  * <li> {@link #column} in column view coordinates
  45.  * <li> the mapping method viewToModel(columnIndex) in view coordinates
  46.  * </ul>
  47.  * 
  48.  * All input row indices are in model coordinates with exactly two exceptions:
  49.  * <ul>
  50.  * <li> {@link #row} in row view coordinates
  51.  * <li> the access method for the filtered value takes the row in view coordinates.
  52.  * </ul>
  53.  *  
  54.  * 
  55.  * @author Ramesh Gupta
  56.  */
  57. public abstract class ComponentAdapter {
  58.     /** current row in view coordinates. */
  59.     public int row = 0;
  60.     /** current column in view coordinates. */
  61.     public int column = 0;
  62.     protected final JComponent target;
  63.     /**
  64.      * Constructs a ComponentAdapter, setting the specified component as the
  65.      * target component.
  66.      *
  67.      * @param component target component for this adapter
  68.      */
  69.     public ComponentAdapter(JComponent component) {
  70.         target = component;
  71.     }
  72.     public JComponent getComponent() {
  73.         return target;
  74.     }
  75. //---------------------------- accessing the target's model
  76.     
  77.     /**
  78.      * returns the column's label (= headerValue).
  79.      * 
  80.      * Used f.i. in SearchPanel to fill the field with the 
  81.      * column name.
  82.      * 
  83.      * Note: it's up to the implementation to decide for which
  84.      * columns it returns a name - most will do so for the
  85.      * subset with isTestable = true.
  86.      * 
  87.      * @param columnIndex in model coordinates
  88.      * @return column name or null if not found/not testable.
  89.      */
  90.     public abstract String getColumnName(int columnIndex);
  91.     /**
  92.      * returns the logical name (== identifier) of the column at 
  93.      * columnIndex in model coordinates.
  94.      * 
  95.      * Used f.i. JNTable to store and apply column properties by identifier.
  96.      * 
  97.      * Note: it's up to the implementation to decide for which
  98.      * columns it returns a name - most will do so for the
  99.      * subset with isTestable = true.
  100.      * 
  101.      * @param columnIndex in model coordinates
  102.      * @return the String value of the column identifier at columnIndex
  103.      *   or null if no identifier set
  104.      */
  105.     public abstract String getColumnIdentifier(int columnIndex);
  106.     /**
  107.      * Returns the number of columns in the target's data model.
  108.      *
  109.      * @return the number of columns in the target's data model.
  110.      */
  111.     public int getColumnCount() {
  112.         return 1; // default for combo-boxes, lists, and trees
  113.     }
  114.     /**
  115.      * Returns the number of rows in the target's data model.
  116.      *
  117.      * @return the number of rows in the target's data model.
  118.      */
  119.     public int getRowCount() {
  120.         return 0;
  121.     }
  122.     /**
  123.      * Returns the value of the target component's cell identified by the
  124.      * specified row and column in model coordinates.
  125.      *
  126.      * @param row in model coordinates
  127.      * @param column in model coordinates
  128.      * @return the value of the target component's cell identified by the
  129.      * specified row and column
  130.      */
  131.     public abstract Object getValueAt(int row, int column);
  132.     public abstract void setValueAt(Object aValue, int row, int column);
  133.     public abstract boolean isCellEditable(int row, int column);
  134.     /**
  135.      * returns true if the column should be included in testing.
  136.      * Here: returns true if visible (that is modelToView gives a valid
  137.      * view column coordinate).
  138.      * 
  139.      * @param column in model coordinates
  140.      * @return
  141.      */
  142.     public  boolean isTestable(int column) {
  143.         return modelToView(column) >= 0;
  144.     }
  145.     
  146. //----------------------- accessing the target's view state
  147.     
  148.     /**
  149.      * Returns the value of the cell identified by this adapter by invoking
  150.      * {@link #getValueAt(int, int)}, passing in the {@link #row} and
  151.      * {@link #column} values of this adapter. For target components that don't
  152.      * support multiple columns, the value of <code>column</code> is always zero.
  153.      *
  154.      * PENDING: needs clarification/cleanup - getValueAt(row, column) expects 
  155.      * model coordinates!.
  156.      * 
  157.      * @return the value of the cell identified by this adapter
  158.      */
  159.     public Object getValue() {
  160.         return getValueAt(row, column);
  161.     }
  162.     /**
  163.      * returns the filtered value of the cell identified by the row
  164.      * in view coordinate and the column in model coordinates.
  165.      * 
  166.      * Note: the asymetry of the coordinates is intentional - clients like
  167.      * Highlighters are interested in view values but might need to access
  168.      * non-visible columns for testing.
  169.      * 
  170.      * @param row
  171.      * @param column
  172.      * @return
  173.      */
  174.     public abstract Object getFilteredValueAt(int row, int column);
  175.     /**
  176.      * Returns true if the cell identified by this adapter currently has focus;
  177.      * Otherwise, it returns false.
  178.      *
  179.      * @return true if the cell identified by this adapter currently has focus;
  180.      *  Otherwise, return false
  181.      */
  182.     public abstract boolean hasFocus();
  183.     /**
  184.      * Returns true if the cell identified by this adapter is currently selected;
  185.      * Otherwise, it returns false.
  186.      *
  187.      * @return true if the cell identified by this adapter is currently selected;
  188.      *  Otherwise, return false
  189.      */
  190.     public abstract boolean isSelected();
  191.     /**
  192.      * Returns true if the cell identified by this adapter is currently expanded;
  193.      * Otherwise, it returns false. For components that do not support
  194.      * hierarchical data, this method always returns true because the cells in
  195.      * such components can never be collapsed.
  196.      *
  197.      * @return true if the cell identified by this adapter is currently expanded;
  198.      *  Otherwise, return false
  199.      */
  200.     public boolean isExpanded() {
  201.         return true; // sensible default for JList and JTable
  202.     }
  203.     /**
  204.      * Returns true if the cell identified by this adapter is a leaf node;
  205.      * Otherwise, it returns false. For components that do not support
  206.      * hierarchical data, this method always returns true because the cells in
  207.      * such components can never have children.
  208.      *
  209.      * @return true if the cell identified by this adapter is a leaf node;
  210.      *  Otherwise, return false
  211.      */
  212.     public boolean isLeaf() {
  213.         return true; // sensible default for JList and JTable
  214.     }
  215.     /**
  216.      * Returns true if the cell identified by this adapter displays the hierarchical node;
  217.      * Otherwise, it returns false. For components that do not support
  218.      * hierarchical data, this method always returns false because the cells in
  219.      * such components can never have children.
  220.      *
  221.      * @return true if the cell identified by this adapter displays the hierarchical node;
  222.      *  Otherwise, return false
  223.      */
  224.     public boolean isHierarchical() {
  225.         return false; // sensible default for JList and JTable
  226.     }
  227.     /**
  228.      * For target components that support multiple columns in their model,
  229.      * along with column reordering in the view, this method transforms the
  230.      * specified columnIndex from model coordinates to view coordinates. For all
  231.      * other types of target components, this method returns the columnIndex
  232.      * unchanged.
  233.      *
  234.      * @param columnIndex index of a column in model coordinates
  235.      * @return index of the specified column in view coordinates
  236.      */
  237.     public int modelToView(int columnIndex) {
  238.         return columnIndex; // sensible default for JList and JTree
  239.     }
  240.    /**
  241.      * For target components that support multiple columns in their model,
  242.      * along with column reordering in the view, this method transforms the
  243.      * specified columnIndex from view coordinates to model coordinates. For all
  244.      * other types of target components, this method returns the columnIndex
  245.      * unchanged.
  246.      *
  247.      * @param columnIndex index of a column in view coordinates
  248.      * @return index of the specified column in model coordinates
  249.      */
  250.     public int viewToModel(int columnIndex) {
  251.         return columnIndex; // sensible default for JList and JTree
  252.     }
  253.     public void refresh() {
  254.         target.revalidate();
  255.         target.repaint();
  256.     }
  257. }