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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: Selection.java,v 1.11 2005/10/13 08:59:54 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 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.swingx.decorator;
  22. import javax.swing.DefaultListSelectionModel;
  23. import javax.swing.ListSelectionModel;
  24. import javax.swing.event.ListSelectionEvent;
  25. import javax.swing.event.ListSelectionListener;
  26. /**
  27.  * Responsible for keeping track of selection in model coordinates.<p>
  28.  * 
  29.  * updates view selection on pipeline change.
  30.  * updates model selection on view selection change.
  31.  * 
  32.  * @author Jeanette Winzenburg
  33.  */
  34. public class Selection {
  35.     /** selection in view coordinates. */
  36.     private ListSelectionModel viewSelection;
  37.     
  38.     /** selection in model coordinates. */
  39.     private DefaultListSelectionModel modelSelection;
  40.     /** mapping pipeline. */
  41.     private FilterPipeline pipeline;
  42.     /** listener to view selection. */
  43.     private ListSelectionListener viewSelectionListener;
  44.     /** state flag for locking non-listening phases. */
  45.     private boolean isListening;
  46.     /** listener to mapping pipeline. */
  47.     private PipelineListener pipelineListener;
  48.     /**
  49.      * PRE: selection != null;
  50.      * 
  51.      * @param pipeline
  52.      * @param selection
  53.      */
  54.     public Selection(FilterPipeline pipeline, ListSelectionModel selection) {
  55.         modelSelection = new DefaultListSelectionModel();
  56.         setViewSelectionModel(selection);
  57.         setFilters(pipeline);
  58.     }
  59.     /**
  60.      * sets the view selection model. Must not be null.
  61.      * 
  62.      * @param selection holding selected indices in view coordinates
  63.      */
  64.     public void setViewSelectionModel(ListSelectionModel selection) {
  65.         ListSelectionModel old = this.viewSelection;
  66.         if (old != null) {
  67.             old.removeListSelectionListener(viewSelectionListener);
  68.             modelSelection.clearSelection();
  69.         }
  70.         this.viewSelection = selection;
  71.         mapTowardsModel();
  72.         viewSelection.addListSelectionListener(getViewSelectionListener());
  73.         isListening = true;
  74.     }
  75.     public void setFilters(FilterPipeline pipeline) {
  76.         FilterPipeline old = this.pipeline;
  77.         if (old != null) {
  78.             old.removePipelineListener(pipelineListener);
  79.         }
  80.         this.pipeline = pipeline;
  81.         if (pipeline != null) {
  82.             pipeline.addPipelineListener(getPipelineListener());
  83.         }
  84.         restoreSelection();
  85.     }
  86.     public void restoreSelection() {
  87.         lock();
  88.         // JW - hmm... clearSelection doesn't reset the lead/anchor. Why not?
  89.         viewSelection.clearSelection();
  90.         int[] selected = getSelectedRows(modelSelection);
  91.         for (int i = 0; i < selected.length; i++) {
  92.           int index = convertToView(selected[i]);
  93.           // index might be -1, but then addSelectionInterval ignores it. 
  94.           viewSelection.addSelectionInterval(index, index);
  95.         }
  96.         int lead = modelSelection.getLeadSelectionIndex();
  97.         // PENDING: JW - this is a quick hack for spurious AIOB - need to enquire why 
  98.         // they happen in the first place
  99.         if (lead >= 0) {
  100.             lead = convertToView(lead);
  101.         }
  102.         if (viewSelection instanceof DefaultListSelectionModel) {
  103.             ((DefaultListSelectionModel) viewSelection).moveLeadSelectionIndex(lead);
  104.         } else {
  105.             // PENDING: not tested, don't have a non-DefaultXX handy
  106.             viewSelection.removeSelectionInterval(lead, lead);
  107.             viewSelection.addSelectionInterval(lead, lead);
  108.         }
  109.         unlock();
  110.     }
  111.     public void unlock() {
  112.         if (!isListening) {
  113.             viewSelection.setValueIsAdjusting(false);
  114.             viewSelection.addListSelectionListener(getViewSelectionListener());
  115.             isListening = true;
  116.         }
  117.     }
  118.     public void lock() {
  119.         if (isListening) {
  120.             viewSelection.removeListSelectionListener(viewSelectionListener);
  121.             viewSelection.setValueIsAdjusting(true);
  122.             isListening = false;
  123.         }
  124.     }
  125.     public void clearModelSelection() {
  126.         // JW: need to reset anchor/lead?
  127.         modelSelection.clearSelection();
  128.     }
  129.     public void insertIndexInterval(int start, int length, boolean before) {
  130.         modelSelection.insertIndexInterval(start, length, before);
  131.     }
  132.     public void removeIndexInterval(int start, int end) {
  133.         modelSelection.removeIndexInterval(start, end);
  134.     }
  135.     private void mapTowardsModel() {
  136.         modelSelection.clearSelection();
  137.         int[] selected = getSelectedRows(viewSelection); 
  138.         for (int i = 0; i < selected.length; i++) {
  139.             int modelIndex = convertToModel(selected[i]);
  140.             modelSelection.addSelectionInterval(modelIndex, modelIndex); 
  141.         }
  142.         if (selected.length > 0) {
  143.             // convert lead selection index to model coordinates
  144.             modelSelection.moveLeadSelectionIndex(convertToModel(viewSelection.getLeadSelectionIndex()));
  145.         }
  146.     }
  147.     private int convertToModel(int index) {
  148.         // JW: check for valid index? must be < pipeline.getOutputSize()
  149.         return (pipeline != null) && pipeline.isAssigned() ? pipeline.convertRowIndexToModel(index) : index;
  150.     }
  151.     
  152.     private int convertToView(int index) {
  153.         // JW: check for valid index? must be < pipeline.getInputSize()
  154.         return (pipeline != null) && pipeline.isAssigned() ? pipeline.convertRowIndexToView(index) : index;
  155.     }
  156.     
  157.     protected void updateFromViewSelectionChanged(int firstIndex, int lastIndex) {
  158.         for (int i = firstIndex; i <= lastIndex; i++) {
  159.             int modelIndex = convertToModel(i);
  160.             if (viewSelection.isSelectedIndex(i)) {
  161.                 modelSelection.addSelectionInterval(modelIndex, modelIndex);
  162.             } else {
  163.                 modelSelection.removeSelectionInterval(modelIndex, modelIndex);
  164.             }
  165.         }
  166.     }
  167.     protected void updateFromPipelineChanged() {
  168.         restoreSelection();
  169.     }
  170.     private int[] getSelectedRows(ListSelectionModel selection) {
  171.         int iMin = selection.getMinSelectionIndex();
  172.         int iMax = selection.getMaxSelectionIndex();
  173.         if ((iMin == -1) || (iMax == -1)) {
  174.             return new int[0];
  175.         }
  176.         int[] rvTmp = new int[1 + (iMax - iMin)];
  177.         int n = 0;
  178.         for (int i = iMin; i <= iMax; i++) {
  179.             if (selection.isSelectedIndex(i)) {
  180.                 rvTmp[n++] = i;
  181.             }
  182.         }
  183.         int[] rv = new int[n];
  184.         System.arraycopy(rvTmp, 0, rv, 0, n);
  185.         return rv;
  186.     }
  187.     
  188.     private PipelineListener getPipelineListener() {
  189.         if (pipelineListener == null) {
  190.             pipelineListener = new PipelineListener() {
  191.                 public void contentsChanged(PipelineEvent e) {
  192.                     updateFromPipelineChanged();
  193.                 }
  194.                 
  195.             };
  196.         }
  197.         return pipelineListener;
  198.     }
  199.     private ListSelectionListener getViewSelectionListener() {
  200.         if (viewSelectionListener == null) {
  201.             viewSelectionListener = new ListSelectionListener() {
  202.                 public void valueChanged(ListSelectionEvent e) {
  203.                     if (e.getValueIsAdjusting()) return;
  204.                     updateFromViewSelectionChanged(e.getFirstIndex(), e.getLastIndex());
  205.                 }
  206.                 
  207.             };
  208.         }
  209.         return viewSelectionListener;
  210.     }
  211. }