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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DefaultTableColumnModelExt.java,v 1.9 2005/10/13 08:59:55 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.table;
  22. import java.beans.PropertyChangeEvent;
  23. import java.beans.PropertyChangeListener;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.HashSet;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import javax.swing.table.DefaultTableColumnModel;
  33. import javax.swing.table.TableColumn;
  34. /**
  35.  * A default implementation supporting invisible columns.
  36.  *
  37.  * Note (JW): hot fixed issues #156, #157. To really do it
  38.  * need enhanced TableColumnModelEvent and -Listeners that are
  39.  * aware of the event.
  40.  * 
  41.  *  
  42.  * @author Richard Bair
  43.  */
  44. public class DefaultTableColumnModelExt extends DefaultTableColumnModel implements TableColumnModelExt {
  45.     private static final String IGNORE_EVENT = "TableColumnModelExt.ignoreEvent";
  46.     /**
  47.      * contains a list of all of the columns, in the order in which they would
  48.      * appear if all of the columns were visible.
  49.      */
  50.     private List allColumns = new ArrayList();
  51.     
  52.     /**
  53.      * Set of invisible columns. These must be of type TableColumnExt.
  54.      */
  55.     private Set invisibleColumns = new HashSet();
  56.     
  57.     private Map oldIndexes = new HashMap();
  58.     
  59.     /**
  60.      * Listener attached to TableColumnExt instances to listen for changes
  61.      * to their visibility status, and to hide/show the column as oppropriate
  62.      */
  63.     private VisibilityListener visibilityListener = new VisibilityListener();
  64.     
  65.     /** 
  66.      * Creates a new instance of DefaultTableColumnModelExt 
  67.      */
  68.     public DefaultTableColumnModelExt() {
  69.         super();
  70.     }
  71.     /**
  72.      * 
  73.      */
  74.     public List getAllColumns() {
  75.         //defensive copy
  76.         return getColumns(true);
  77.     }
  78.     /**
  79.      * 
  80.      */
  81.     public List getColumns(boolean includeHidden) {
  82.         if (includeHidden) {
  83.             return new ArrayList(allColumns);
  84.         } 
  85.         return Collections.list(getColumns());
  86.     }
  87.     public int getColumnCount(boolean includeHidden) {
  88.         if (includeHidden) {
  89.             return allColumns.size();
  90.         }
  91.         return getColumnCount();
  92.     }
  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     public TableColumnExt getColumnExt(Object identifier) {
  97.         for (Iterator iter = allColumns.iterator(); iter.hasNext();) {
  98.             TableColumn column = (TableColumn) iter.next();
  99.             if ((column instanceof TableColumnExt) && (identifier.equals(column.getIdentifier()))) {
  100.                 return (TableColumnExt) column;
  101.             }
  102.         }
  103.         return null;
  104.     }
  105.     public Set getInvisibleColumns() {
  106.         return new HashSet(invisibleColumns);
  107.     }
  108.     /**
  109.      * hot fix for #157: listeners that are aware of
  110.      * the possible existence of invisible columns
  111.      * should check if the received columnRemoved originated
  112.      * from moving a column from visible to invisible.
  113.      * 
  114.      * @param oldIndex the fromIndex of the columnEvent
  115.      * @return true if the column was moved to invisible
  116.      */
  117.     public boolean isRemovedToInvisibleEvent(int oldIndex) {
  118.         Integer index = new Integer(oldIndex);
  119.         return oldIndexes.containsValue(index);
  120.     }
  121.     /**
  122.      * hot fix for #157: listeners that are aware of
  123.      * the possible existence of invisible columns
  124.      * should check if the received columnAdded originated
  125.      * from moving a column from invisible to visible.
  126.      * 
  127.      * @param newIndex the toIndex of the columnEvent
  128.      * @return true if the column was moved to visible
  129.      */
  130.     public boolean isAddedFromInvisibleEvent(int newIndex) {
  131.         if (!(getColumn(newIndex) instanceof TableColumnExt)) return false;
  132.         return Boolean.TRUE.equals(((TableColumnExt) getColumn(newIndex)).getClientProperty(IGNORE_EVENT));
  133.     }
  134.     
  135.     public void removeColumn(TableColumn column) {
  136.         //remove the visibility listener if appropriate
  137.         if (column instanceof TableColumnExt) {
  138.             ((TableColumnExt)column).removePropertyChangeListener(visibilityListener);
  139.         }
  140.         //remove from the invisible columns set and the allColumns list first
  141.         invisibleColumns.remove(column);
  142.         allColumns.remove(column);
  143.         //let the superclass handle notification etc
  144.         super.removeColumn(column);
  145.     }
  146.     public void addColumn(TableColumn aColumn) {
  147.         // hacking to guarantee correct events
  148.         // two step: add as visible, setVisible
  149.         boolean oldVisible = true;
  150.         //add the visibility listener if appropriate
  151.         if (aColumn instanceof TableColumnExt) {
  152.             TableColumnExt xColumn = (TableColumnExt) aColumn;
  153.             oldVisible = xColumn.isVisible();
  154.             xColumn.setVisible(true);
  155.             xColumn.addPropertyChangeListener(visibilityListener);
  156.         }
  157.         //append the column to the end of "allColumns". If the column is
  158.         //visible, let super add it to the list of columns. If not, then
  159.         //add it to the set of invisible columns and return.
  160.         //In the case of an invisible column being added to the model,
  161.         //I still do event notification for the model having
  162.         //been changed so that the ColumnControlButton or other similar
  163.         //code interacting with invisible columns knows that a new invisible
  164.         //column has been added
  165.         allColumns.add(aColumn);
  166.         super.addColumn(aColumn);
  167.         if (aColumn instanceof TableColumnExt) {
  168.             ((TableColumnExt) aColumn).setVisible(oldVisible);
  169.         }
  170.         
  171.     }
  172.         
  173.     protected void moveToInvisible(TableColumnExt col) {
  174.         //make invisible
  175.         int oldIndex = tableColumns.indexOf(col);
  176.         invisibleColumns.add(col);
  177.         oldIndexes.put(col, new Integer(oldIndex));
  178.         super.removeColumn(col);
  179.     }
  180.     protected void moveToVisible(TableColumnExt col) {
  181.         //make visible
  182.         invisibleColumns.remove(col);
  183.         Integer oldIndexInteger = (Integer)oldIndexes.get(col);
  184.         int oldIndex = oldIndexInteger == null ? getColumnCount() : oldIndexInteger.intValue();
  185.         oldIndexes.remove(col);
  186.         col.putClientProperty(IGNORE_EVENT, Boolean.TRUE);
  187.         super.addColumn(col);
  188.         moveColumn(getColumnCount() - 1, Math.min(getColumnCount() - 1, oldIndex));
  189.         col.putClientProperty(IGNORE_EVENT, null);
  190.     }
  191.     private final class VisibilityListener implements PropertyChangeListener {        
  192.         public void propertyChange(PropertyChangeEvent evt) {
  193.             if (evt.getPropertyName().equals("visible")) {
  194.                 boolean oldValue = ((Boolean)evt.getOldValue()).booleanValue();
  195.                 boolean newValue = ((Boolean)evt.getNewValue()).booleanValue();
  196.                 TableColumnExt col = (TableColumnExt)evt.getSource();
  197.                 if (oldValue && !newValue) {
  198.                     moveToInvisible(col);
  199.                 } else if (!oldValue && newValue) {
  200.                     moveToVisible(col);
  201.                 }
  202.             }
  203.         }
  204.     }
  205.     
  206. }