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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataSelector.java,v 1.7 2005/10/10 17:00:58 rbair Exp $
  3.  *
  4.  * Copyright 2005 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.dataset;
  22. import java.beans.PropertyChangeListener;
  23. import java.beans.PropertyChangeSupport;
  24. import java.util.ArrayList;
  25. import java.util.BitSet;
  26. import java.util.Collections;
  27. import java.util.List;
  28. /**
  29.  *
  30.  * @author rbair
  31.  */
  32. public class DataSelector {
  33.     //protected for testing
  34.     /** Used as a prefix for auto-generated DataColumn names. */
  35.     protected static final String DEFAULT_NAME_PREFIX = "DataSelector";
  36.     /** The shared instance of the NameGenerator for DataSelectors not assigned a name. */
  37.     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
  38.     private DataTable table;
  39.     private String name;
  40. /**
  41.  * This variable tracks which records are selected. If the bit at position
  42.  * <i>n</i> is 1, then the <i>nth</i> record is selected.
  43.  */
  44.     private BitSet indices = new BitSet(0);
  45.     private int[] helper = new int[1];
  46.     private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  47.     
  48.     /** Creates a new instance of DataSelector */
  49.     public DataSelector(DataTable table) {
  50.         assert table != null;
  51.         this.table = table;
  52.         name = NAMEGEN.generateName(this);
  53.         if (table != null && table.getRowCount() > 0) {
  54.             setRowIndices(new int[]{0});
  55.         }
  56.     }
  57.     
  58.     /**
  59.      * @deprecated Use getTable() instead
  60.      */
  61.     public DataTable getDataTable() {
  62.         return table;
  63.     }
  64.     
  65.     public DataTable getTable() {
  66.         return table;
  67.     }
  68. /**
  69.  * @param name
  70.  */
  71. public void setName(String name) {
  72.         if (this.name != name) {
  73.             assert name != null && !name.trim().equals("");
  74.             String oldName = this.name;
  75.             this.name = name;
  76.             pcs.firePropertyChange("name", oldName, name);
  77.         }
  78. }
  79. public String getName() {
  80. return name;
  81. }
  82.     public List<Integer> getRowIndices() {
  83.         List<Integer> results = new ArrayList<Integer>(indices.cardinality());
  84.         int n=-1;
  85.         for(int i=0; i<indices.cardinality(); i++) {
  86.             n = indices.nextSetBit(n+1);
  87.             results.add(n);
  88.         }
  89.         return Collections.unmodifiableList(results);
  90.     }
  91.     
  92.     /**
  93.      * @return either the first selected index, or -1 if nothing is selected
  94.      */
  95.     public int getFirstRowIndex() {
  96.         return indices.cardinality() > 0 ? indices.nextSetBit(0) : -1;
  97.     }
  98.     
  99.     public void setRowIndices(int[] rowIndices) {
  100.         // JW: to honour the bean spec
  101.         List oldIndices = getRowIndices();
  102.      indices.clear();
  103.      for (int index : rowIndices) {
  104.      indices.set(index);
  105.      }
  106. //     fireSelectionModelChanged(new SelectionModelEvent(this, 0, selectedRecords.length()-1));
  107.         //TODO I'm abusing the property change listener here to get out the selection
  108.         //change. I need a real event, I think.
  109.         // JW: until then we can honour the bean spec with the following line:
  110.      pcs.firePropertyChange("rowIndices", oldIndices, getRowIndices());
  111.     }
  112.     /**
  113.      * Convenience method for setting a single row as the selected row.
  114.      * @param index must be less than the row count of the DataTable. Also,
  115.      * if it is greater than 0, then the row index will be set, however if
  116.      * it is less than zero, then the selection will be cleared.
  117.      */
  118.     public void setRowIndex(int index) {
  119.         assert index < table.getRowCount();
  120.         if (index >= 0) {
  121.             helper[0] = index;
  122.             setRowIndices(helper);
  123.         } else {
  124.             setRowIndices(new int[0]);
  125.         }
  126.     }
  127.     
  128.     public void setRowIndex(DataRow row) {
  129.         assert row != null;
  130. //        assert row.getTable() == table;
  131.         setRowIndices(new int[]{table.indexOfRow(row)});
  132.     }
  133.     
  134.     /**
  135.      * Adds a PropertyChangeListener to this class for any changes to bean 
  136.      * properties.
  137.      *
  138.      * @param listener The PropertyChangeListener to notify of changes to this 
  139.      * instance.
  140.      */
  141.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  142.         pcs.addPropertyChangeListener(listener);
  143.     }
  144.     
  145.    /**
  146.      * Adds a PropertyChangeListener to this class for specific property changes.
  147.      *
  148.      * @param property The name of the property to listen to changes for.
  149.      * @param listener The PropertyChangeListener to notify of changes to this 
  150.      * instance.
  151.      */
  152.     public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
  153.         pcs.addPropertyChangeListener(property,  listener);
  154.     }
  155.     
  156.     /**
  157.      * Stops notifying a specific listener of any changes to bean properties.
  158.      *
  159.      * @param listener The listener to stop receiving notifications.
  160.      */
  161.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  162.         pcs.removePropertyChangeListener(listener);
  163.     }
  164.     
  165.     /**
  166.      * Stops notifying a specific listener of changes to a specific property.
  167.      *
  168.      * @param propertyName The name of the property to ignore from now on.
  169.      * @param listener The listener to stop receiving notifications.
  170.      */
  171.     public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  172.         pcs.removePropertyChangeListener(propertyName,  listener);
  173.     }
  174.     
  175. }