KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataSelector


1 /*
2  * $Id: DataSelector.java,v 1.2 2005/02/24 17:02:36 kleopatra Exp $
3  *
4  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.dataset;
9
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.beans.PropertyChangeSupport JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.BitSet JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.List JavaDoc;
16 import org.jdesktop.dataset.NameGenerator;
17
18 /**
19  *
20  * @author rbair
21  */

22 public class DataSelector {
23     //protected for testing
24
protected static final String JavaDoc DEFAULT_NAME_PREFIX = "DataSelector";
25     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
26
27     private DataTable table;
28     private String JavaDoc name;
29     /**
30      * This variable tracks which records are selected. If the bit at position
31      * <i>n</i> is 1, then the <i>nth</i> record is selected.
32      */

33     private BitSet JavaDoc indices = new BitSet JavaDoc(0);
34     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
35         
36     /** Creates a new instance of DataSelector */
37     public DataSelector(DataTable table) {
38         assert table != null;
39         this.table = table;
40         name = NAMEGEN.generateName(this);
41     }
42     
43     public DataTable getDataTable() {
44         return table;
45     }
46     
47     /**
48      * @param name
49      */

50     public void setName(String JavaDoc name) {
51         if (this.name != name) {
52             assert name != null && !name.trim().equals("");
53             String JavaDoc oldName = this.name;
54             this.name = name;
55             pcs.firePropertyChange("name", oldName, name);
56         }
57     }
58
59     public String JavaDoc getName() {
60         return name;
61     }
62
63     public List JavaDoc<Integer JavaDoc> getRowIndices() {
64         List JavaDoc<Integer JavaDoc> results = new ArrayList JavaDoc<Integer JavaDoc>(indices.cardinality());
65         int n=-1;
66         for(int i=0; i<indices.cardinality(); i++) {
67             n = indices.nextSetBit(n+1);
68             results.add(n);
69         }
70         return Collections.unmodifiableList(results);
71     }
72     
73     public void setRowIndices(int[] rowIndices) {
74         // JW: to honour the bean spec
75
List JavaDoc oldIndices = getRowIndices();
76         indices.clear();
77         for (int index : rowIndices) {
78             indices.set(index);
79         }
80 // fireSelectionModelChanged(new SelectionModelEvent(this, 0, selectedRecords.length()-1));
81
//TODO I'm abusing the property change listener here to get out the selection
82
//change. I need a real event, I think.
83
// JW: until then we can honour the bean spec with the following line:
84
pcs.firePropertyChange("rowIndices", oldIndices, getRowIndices());
85     }
86
87     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
88         pcs.addPropertyChangeListener(listener);
89     }
90     
91     public void addPropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
92         pcs.addPropertyChangeListener(property, listener);
93     }
94     
95     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
96         pcs.removePropertyChangeListener(listener);
97     }
98     
99     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
100         pcs.removePropertyChangeListener(propertyName, listener);
101     }
102     
103 }
104
Popular Tags