1 6 7 package org.jdesktop.dataset.adapter; 8 9 import java.beans.PropertyChangeEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.util.ArrayList ; 12 import java.util.List ; 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 22 public class SelectionModelAdapter implements SelectionModel { 23 private DataSelector selector; 24 private List <SelectionModelListener> listeners = new ArrayList <SelectionModelListener>(); 25 private boolean changing = false; 26 27 28 public SelectionModelAdapter(DataSelector ds) { 29 assert ds != null; 30 this.selector = ds; 31 this.selector.addPropertyChangeListener("rowIndices", new PropertyChangeListener () { 32 public void propertyChange(PropertyChangeEvent 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 <Integer > 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 |