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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SelectionModelAdapter.java,v 1.5 2005/10/10 17:01:13 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.adapter;
  22. import java.beans.PropertyChangeEvent;
  23. import java.beans.PropertyChangeListener;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.jdesktop.binding.SelectionModel;
  27. import org.jdesktop.binding.SelectionModelEvent;
  28. import org.jdesktop.binding.SelectionModelListener;
  29. import org.jdesktop.dataset.DataSelector;
  30. /**
  31.  * 
  32.  * @author rbair
  33.  * 
  34.  */
  35. public class SelectionModelAdapter implements SelectionModel {
  36.     private DataSelector selector;
  37.     private List<SelectionModelListener> listeners = new ArrayList<SelectionModelListener>();
  38. //    private boolean changing = false;
  39.     
  40.     /** Creates a new instance of SelectionModelAdapter */
  41.     public SelectionModelAdapter(DataSelector ds) {
  42.         assert ds != null;
  43.         this.selector = ds;
  44.         this.selector.addPropertyChangeListener("rowIndices",  new PropertyChangeListener() {
  45.             public void propertyChange(PropertyChangeEvent evt) {
  46.                 // JW: the flag prevents firing an SelectionEvent if the 
  47.                 // DataSelector's selection is set from here.
  48.                 // TODO: formally test
  49.                 // TODO: minimize the change interval
  50. //                if (!changing) {
  51. //                    changing = true;
  52.                     SelectionModelEvent e = new SelectionModelEvent(
  53.                             SelectionModelAdapter.this, 0, 
  54.                             selector.getDataTable().getRowCount() - 1);
  55.                     for (SelectionModelListener listener : listeners) {
  56.                         listener.selectionChanged(e);
  57.                     }
  58. //                    changing = false;
  59. //                }
  60.             }
  61.         });
  62.         
  63.     }
  64.     public void addSelectionModelListener(SelectionModelListener listener) {
  65.         if (!listeners.contains(listener)) {
  66.             listeners.add(listener);
  67.         }
  68.     }
  69.     public void removeSelectionModelListener(SelectionModelListener listener) {
  70.         listeners.remove(listener);
  71.     }
  72.     public void setSelectionIndices(int[] indices) {
  73.         // JW: must fire selectionEvent!
  74. //        changing = true;
  75.         selector.setRowIndices(indices);
  76. //       changing = false;
  77.     }
  78.     public int[] getSelectionIndices() {
  79.         List<Integer> indices = selector.getRowIndices();
  80.         int[] results = new int[indices.size()];
  81.         for (int i=0; i<results.length; i++) {
  82.             results[i] = indices.get(i);
  83.         }
  84.         return results;
  85.     }
  86. }