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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: RowSizing.java,v 1.3 2005/10/13 08:59:53 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.SizeSequence;
  23. /**
  24.  * @author Jeanette Winzenburg
  25.  */
  26. public class RowSizing {
  27.     private SizeSequence viewSizes;
  28.     private SizeSequence modelSizes;
  29.     private FilterPipeline pipeline;
  30.     private PipelineListener pipelineListener;
  31.     private int defaultHeight;
  32.     public RowSizing() {
  33.     }
  34.     public RowSizing(FilterPipeline pipeline) {
  35.         this();
  36.         setFilters(pipeline);
  37.     }
  38.     /**
  39.      * 
  40.      * @param pipeline
  41.      * @param selection
  42.      */
  43.     public RowSizing(FilterPipeline pipeline, SizeSequence selection, int defaultHeight) {
  44.         this();
  45.         setViewSizeSequence(selection, defaultHeight);
  46.         setFilters(pipeline);
  47.     }
  48.     public void setViewSizeSequence(SizeSequence selection, int height) {
  49.         SizeSequence old = this.viewSizes;
  50.         if (old != null) {
  51.             clearModelSizes();
  52.         }
  53.         this.viewSizes = selection;
  54.         this.defaultHeight = height;
  55.         mapTowardsModel();
  56.     }
  57.     public SizeSequence getViewSizeSequence() {
  58.         return viewSizes;
  59.     }
  60.     public void setFilters(FilterPipeline pipeline) {
  61.         FilterPipeline old = this.pipeline;
  62.         if (old != null) {
  63.             old.removePipelineListener(pipelineListener);
  64.         }
  65.         this.pipeline = pipeline;
  66.         if (pipeline != null) {
  67.             pipeline.addPipelineListener(getPipelineListener());
  68.         }
  69.         restoreSelection();
  70.     }
  71.     public void clearModelSizes() {
  72.         modelSizes = null;
  73.     }
  74.     public void insertIndexInterval(int start, int length, int value) {
  75.         if (modelSizes == null) return;
  76.         modelSizes.insertEntries(start, length, value);
  77.     }
  78.     public void removeIndexInterval(int start, int length) {
  79.         if (modelSizes == null) return;
  80.         modelSizes.removeEntries(start, length);
  81.     }
  82.     public void restoreSelection() {
  83.         if (viewSizes == null) return;
  84.         viewSizes.setSizes(new int[0]);
  85.         viewSizes.insertEntries(0, getOutputSize(), defaultHeight);
  86.         int[] selected = modelSizes.getSizes();
  87.         for (int i = 0; i < selected.length; i++) {
  88.           int index = convertToView(i);
  89.           // index might be -1, but then addSelectionInterval ignores it. 
  90.           viewSizes.setSize(index, selected[i]);
  91.         }
  92.     }
  93.     private void mapTowardsModel() {
  94.         if (viewSizes == null) return;
  95.         modelSizes = new SizeSequence(getInputSize(), defaultHeight);
  96.         int[] selected = viewSizes.getSizes(); 
  97.         for (int i = 0; i < selected.length; i++) {
  98.             int modelIndex = convertToModel(i);
  99.             modelSizes.setSize(modelIndex, selected[i]); 
  100.         }
  101.     }
  102.     private int getInputSize() {
  103.         return pipeline != null ? pipeline.getInputSize() : 0;
  104.     }
  105.     private int getOutputSize() {
  106.         return pipeline != null ? pipeline.getOutputSize() : 0;
  107.     }
  108.     private int convertToModel(int index) {
  109.         return pipeline != null ? pipeline.convertRowIndexToModel(index) : index;
  110.     }
  111.     
  112.     private int convertToView(int index) {
  113.         return pipeline != null ? pipeline.convertRowIndexToView(index) : index;
  114.     }
  115.     
  116.     protected void updateFromPipelineChanged() {
  117.         restoreSelection();
  118.     }
  119.     private PipelineListener getPipelineListener() {
  120.         if (pipelineListener == null) {
  121.             pipelineListener = new PipelineListener() {
  122.                 public void contentsChanged(PipelineEvent e) {
  123.                     updateFromPipelineChanged();
  124.                     
  125.                 }
  126.                 
  127.             };
  128.         }
  129.         return pipelineListener;
  130.     }
  131. }