1 6 7 package org.jdesktop.swing.table; 8 9 import java.beans.PropertyChangeEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import javax.swing.event.TableColumnModelEvent ; 18 import javax.swing.table.DefaultTableColumnModel ; 19 import javax.swing.table.TableColumn ; 20 21 22 32 public class DefaultTableColumnModelExt extends DefaultTableColumnModel implements TableColumnModelExt { 33 private static final String IGNORE_EVENT = "TableColumnModelExt.ignoreEvent"; 34 38 private List allColumns = new ArrayList (); 39 40 43 private Set invisibleColumns = new HashSet (); 44 45 private Map oldIndexes = new HashMap (); 46 47 51 private VisibilityListener visibilityListener = new VisibilityListener(); 52 53 56 public DefaultTableColumnModelExt() { 57 super(); 58 } 59 60 public List getAllColumns() { 61 return new ArrayList (allColumns); 63 } 64 65 public Set getInvisibleColumns() { 66 return new HashSet (invisibleColumns); 67 } 68 69 78 public boolean isRemovedToInvisibleEvent(int oldIndex) { 79 Integer index = new Integer (oldIndex); 80 return oldIndexes.containsValue(index); 81 } 82 83 92 public boolean isAddedFromInvisibleEvent(int newIndex) { 93 if (!(getColumn(newIndex) instanceof TableColumnExt)) return false; 94 return Boolean.TRUE.equals(((TableColumnExt) getColumn(newIndex)).getClientProperty(IGNORE_EVENT)); 95 } 96 97 98 public void removeColumn(TableColumn column) { 99 if (column instanceof TableColumnExt) { 101 ((TableColumnExt)column).removePropertyChangeListener(visibilityListener); 102 } 103 invisibleColumns.remove(column); 105 allColumns.remove(column); 106 super.removeColumn(column); 108 } 109 110 public void addColumn(TableColumn aColumn) { 111 boolean oldVisible = true; 114 if (aColumn instanceof TableColumnExt) { 116 TableColumnExt xColumn = (TableColumnExt) aColumn; 117 oldVisible = xColumn.isVisible(); 118 xColumn.setVisible(true); 119 xColumn.addPropertyChangeListener(visibilityListener); 120 } 121 allColumns.add(aColumn); 130 super.addColumn(aColumn); 131 if (aColumn instanceof TableColumnExt) { 132 ((TableColumnExt) aColumn).setVisible(oldVisible); 133 } 134 135 } 146 147 148 protected void moveToInvisible(TableColumnExt col) { 149 int oldIndex = tableColumns.indexOf(col); 151 invisibleColumns.add(col); 152 oldIndexes.put(col, new Integer (oldIndex)); 153 super.removeColumn(col); 154 } 160 161 protected void moveToVisible(TableColumnExt col) { 162 invisibleColumns.remove(col); 164 Integer oldIndexInteger = (Integer )oldIndexes.get(col); 165 int oldIndex = oldIndexInteger == null ? getColumnCount() : oldIndexInteger.intValue(); 166 oldIndexes.remove(col); 167 col.putClientProperty(IGNORE_EVENT, Boolean.TRUE); 168 super.addColumn(col); 169 moveColumn(getColumnCount() - 1, oldIndex); 170 col.putClientProperty(IGNORE_EVENT, null); 171 } 177 178 private final class VisibilityListener implements PropertyChangeListener { 179 public void propertyChange(PropertyChangeEvent evt) { 180 if (evt.getPropertyName().equals("visible")) { 181 boolean oldValue = ((Boolean )evt.getOldValue()).booleanValue(); 182 boolean newValue = ((Boolean )evt.getNewValue()).booleanValue(); 183 TableColumnExt col = (TableColumnExt)evt.getSource(); 184 185 if (oldValue && !newValue) { 186 moveToInvisible(col); 187 } else if (!oldValue && newValue) { 188 moveToVisible(col); 189 } 190 } 191 } 192 } 193 194 } 195 | Popular Tags |