KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > adapter > SelectionModelAdapter


1 /*
2  * SelectionModelAdapter.java
3  *
4  * Created on February 24, 2005, 6:56 AM
5  */

6
7 package org.jdesktop.dataset.adapter;
8
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import org.jdesktop.dataset.DataSelector;
14 import org.jdesktop.swing.data.SelectionModel;
15 import org.jdesktop.swing.data.SelectionModelEvent;
16 import org.jdesktop.swing.data.SelectionModelListener;
17
18 /**
19  *
20  * @author rbair
21  */

22 public class SelectionModelAdapter implements SelectionModel {
23     private DataSelector selector;
24     private List JavaDoc<SelectionModelListener> listeners = new ArrayList JavaDoc<SelectionModelListener>();
25     private boolean changing = false;
26     
27     /** Creates a new instance of SelectionModelAdapter */
28     public SelectionModelAdapter(DataSelector ds) {
29         assert ds != null;
30         this.selector = ds;
31         this.selector.addPropertyChangeListener("rowIndices", new PropertyChangeListener JavaDoc() {
32             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
33                 if (!changing) {
34                     changing = true;
35                     SelectionModelEvent e = new SelectionModelEvent(
36                             SelectionModelAdapter.this, 0,
37                             selector.getDataTable().getRows().size() - 1);
38                     for (SelectionModelListener listener : listeners) {
39                         listener.selectionChanged(e);
40                     }
41                     changing = false;
42                 }
43             }
44         });
45         
46     }
47
48     public void addSelectionModelListener(SelectionModelListener listener) {
49         if (!listeners.contains(listener)) {
50             listeners.add(listener);
51         }
52     }
53
54     public void removeSelectionModelListener(SelectionModelListener listener) {
55         listeners.remove(listener);
56     }
57
58     public void setSelectionIndices(int[] indices) {
59         changing = true;
60         selector.setRowIndices(indices);
61         changing = false;
62     }
63
64     public int[] getSelectionIndices() {
65         List JavaDoc<Integer JavaDoc> indices = selector.getRowIndices();
66         int[] results = new int[indices.size()];
67         for (int i=0; i<results.length; i++) {
68             results[i] = indices.get(i);
69         }
70         return results;
71     }
72 }
Popular Tags