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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id $Exp
  3.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  4.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  * 
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19.  */
  20. package org.jdesktop.binding.swingx.adapter;
  21. import javax.swing.ListSelectionModel;
  22. import javax.swing.event.ListSelectionEvent;
  23. import javax.swing.event.ListSelectionListener;
  24. import org.jdesktop.binding.IndexMapper;
  25. import org.jdesktop.binding.SelectionBinding;
  26. import org.jdesktop.binding.SelectionModel;
  27. import org.jdesktop.binding.SelectionModelEvent;
  28. import org.jdesktop.binding.SelectionModelListener;
  29. /**
  30.  * Note (JW): ListSelectionModel indices are in view coordinates,
  31.  * SelectionModel indices are in model coordinates. Need to convert.
  32.  * 
  33.  * PENDING: Done here via IndexMapper - but need to think if it's 
  34.  * the correct place.
  35.  * 
  36.  *
  37.  * @author Richard Bair
  38.  * @author Jeanette Winzenburg
  39.  */
  40. public class ListSelectionBinding extends SelectionBinding {
  41.     private boolean indexIsChanging = false;
  42.     private IndexMapper indexMapper;
  43.     private ListSelectionModel listSelectionModel;
  44.     
  45.     /** Creates a new instance of ListSelectionBinding */
  46.     public ListSelectionBinding(final SelectionModel dataSelectionModel, final ListSelectionModel viewSelectionModel) {
  47.         this(dataSelectionModel, viewSelectionModel, null);
  48.     }
  49.     
  50.     public ListSelectionBinding(final SelectionModel dataSelectionModel, 
  51.             final ListSelectionModel listSelectionModel, IndexMapper mapper) { 
  52.         super(dataSelectionModel);
  53.         this.indexMapper = mapper;
  54.         this.listSelectionModel = listSelectionModel;
  55. installViewSelectionListener();
  56. installDataSelectionListener();
  57.         updateViewSelectionModelFromData();
  58.     }
  59.     private void installDataSelectionListener() {
  60.         //add a property change listener to listen for selection change events
  61. //in the selection model. Set the currently selected row(s) to be the
  62. //same as the current row(s) in the selection model
  63. selectionModel.addSelectionModelListener(new SelectionModelListener() {
  64. public void selectionChanged(SelectionModelEvent evt) {
  65. updateViewSelectionModelFromData();
  66. }
  67. });
  68.     }
  69.     private void installViewSelectionListener() {
  70.         //listen for changes in the list selection and maintain the
  71. //synchronicity with the selection model
  72. listSelectionModel.addListSelectionListener(new ListSelectionListener() {
  73. public void valueChanged(ListSelectionEvent e) {
  74. if (!e.getValueIsAdjusting() && !indexIsChanging) {
  75.                 //    invokeSetDataSelectionIndices(getSelectedIndices(listSelectionModel));
  76.                     selectionModel.setSelectionIndices(getSelectedIndices(listSelectionModel));
  77. }
  78. }
  79. });
  80.     }
  81.     
  82.     private int[] getSelectedIndices(ListSelectionModel sm) {
  83.         int iMin = sm.getMinSelectionIndex();
  84.         int iMax = sm.getMaxSelectionIndex();
  85.         if ((iMin < 0) || (iMax < 0)) {
  86.             return new int[0];
  87.         }
  88.         int[] rvTmp = new int[1+ (iMax - iMin)];
  89.         int n = 0;
  90.         // selected indices in view coordinates
  91.         for(int i = iMin; i <= iMax; i++) {
  92.             if (sm.isSelectedIndex(i)) {
  93.                 rvTmp[n++] = i;
  94.             }
  95.         }
  96.         int[] rv = new int[n];
  97.         System.arraycopy(rvTmp, 0, rv, 0, n);
  98.         rv = convertIndicesToModel(rv);
  99.         return rv;
  100.     }
  101.     private void setSelectedIndices(int[] indices, ListSelectionModel sm) {
  102.         sm.clearSelection();
  103.         // indices are in model coordinates
  104.         // must be converted to view coordinates
  105.         indices = convertIndicesToView(indices);
  106.         for(int i = 0; i < indices.length; i++) {
  107.             sm.addSelectionInterval(indices[i], indices[i]);
  108.         }
  109.     }
  110.     private int[] convertIndicesToView(int[] indices) {
  111.         if (indexMapper == null) return indices;
  112.         for (int i = 0; i < indices.length; i++) {
  113.             indices[i] = indexMapper.modelToView(indices[i]);
  114.         }
  115.         return indices;
  116.     }
  117.     private int[] convertIndicesToModel(int[] rv) {
  118.         if (indexMapper == null) return rv;
  119.         for (int i = 0; i < rv.length; i++) {
  120.             rv[i] = indexMapper.viewToModel(rv[i]);
  121.         }
  122.         return rv;
  123.     }
  124.     private void updateViewSelectionModelFromData() {
  125.         indexIsChanging = true;
  126.         setSelectedIndices(selectionModel.getSelectionIndices(), listSelectionModel);
  127.         indexIsChanging = false;
  128.     }
  129. //    private void invokeSetDataSelectionIndices(final int[] is) {
  130. //        SwingUtilities.invokeLater(new Runnable() {
  131. //            public void run() {
  132. //                // must be set in model coordinates
  133. //                selectionModel.setSelectionIndices(is);
  134. //
  135. //            }
  136. //        });
  137. //    }
  138. }