KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > table > DefaultTableColumnModelExt


1 /*
2  * DefaultTableColumnModelExt.java
3  *
4  * Created on January 14, 2005, 12:41 PM
5  */

6
7 package org.jdesktop.swing.table;
8
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import javax.swing.event.TableColumnModelEvent JavaDoc;
18 import javax.swing.table.DefaultTableColumnModel JavaDoc;
19 import javax.swing.table.TableColumn JavaDoc;
20
21
22 /**
23  * A default implementation supporting invisible columns.
24  *
25  * Note (JW): hot fixed issues #156, #157. To really do it
26  * need enhanced TableColumnModelEvent and -Listeners that are
27  * aware of the event.
28  *
29  *
30  * @author Richard Bair
31  */

32 public class DefaultTableColumnModelExt extends DefaultTableColumnModel JavaDoc implements TableColumnModelExt {
33     private static final String JavaDoc IGNORE_EVENT = "TableColumnModelExt.ignoreEvent";
34     /**
35      * contains a list of all of the columns, in the order in which they would
36      * appear if all of the columns were visible.
37      */

38     private List JavaDoc allColumns = new ArrayList JavaDoc();
39     
40     /**
41      * Set of invisible columns. These must be of type TableColumnExt.
42      */

43     private Set JavaDoc invisibleColumns = new HashSet JavaDoc();
44     
45     private Map JavaDoc oldIndexes = new HashMap JavaDoc();
46     
47     /**
48      * Listener attached to TableColumnExt instances to listen for changes
49      * to their visibility status, and to hide/show the column as oppropriate
50      */

51     private VisibilityListener visibilityListener = new VisibilityListener();
52     
53     /**
54      * Creates a new instance of DefaultTableColumnModelExt
55      */

56     public DefaultTableColumnModelExt() {
57         super();
58     }
59
60     public List JavaDoc getAllColumns() {
61         //defensive copy
62
return new ArrayList JavaDoc(allColumns);
63     }
64
65     public Set JavaDoc getInvisibleColumns() {
66         return new HashSet JavaDoc(invisibleColumns);
67     }
68
69     /**
70      * hot fix for #157: listeners that are aware of
71      * the possible existence of invisible columns
72      * should check if the received columnRemoved originated
73      * from moving a column from visible to invisible.
74      *
75      * @param oldIndex the fromIndex of the columnEvent
76      * @return true if the column was moved to invisible
77      */

78     public boolean isRemovedToInvisibleEvent(int oldIndex) {
79         Integer JavaDoc index = new Integer JavaDoc(oldIndex);
80         return oldIndexes.containsValue(index);
81     }
82
83     /**
84      * hot fix for #157: listeners that are aware of
85      * the possible existence of invisible columns
86      * should check if the received columnAdded originated
87      * from moving a column from invisible to visible.
88      *
89      * @param oldIndex the toIndex of the columnEvent
90      * @return true if the column was moved to visible
91      */

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 JavaDoc column) {
99         //remove the visibility listener if appropriate
100
if (column instanceof TableColumnExt) {
101             ((TableColumnExt)column).removePropertyChangeListener(visibilityListener);
102         }
103         //remove from the invisible columns set and the allColumns list first
104
invisibleColumns.remove(column);
105         allColumns.remove(column);
106         //let the superclass handle notification etc
107
super.removeColumn(column);
108     }
109
110     public void addColumn(TableColumn JavaDoc aColumn) {
111         // hacking to guarantee correct events
112
// two step: add as visible, setVisible
113
boolean oldVisible = true;
114         //add the visibility listener if appropriate
115
if (aColumn instanceof TableColumnExt) {
116             TableColumnExt xColumn = (TableColumnExt) aColumn;
117             oldVisible = xColumn.isVisible();
118             xColumn.setVisible(true);
119             xColumn.addPropertyChangeListener(visibilityListener);
120         }
121         //append the column to the end of "allColumns". If the column is
122
//visible, let super add it to the list of columns. If not, then
123
//add it to the set of invisible columns and return.
124
//In the case of an invisible column being added to the model,
125
//I still do event notification for the model having
126
//been changed so that the ColumnControlButton or other similar
127
//code interacting with invisible columns knows that a new invisible
128
//column has been added
129
allColumns.add(aColumn);
130         super.addColumn(aColumn);
131         if (aColumn instanceof TableColumnExt) {
132             ((TableColumnExt) aColumn).setVisible(oldVisible);
133         }
134         
135 // if (aColumn instanceof TableColumnExt && !((TableColumnExt)aColumn).isVisible()) {
136
// invisibleColumns.add(aColumn);
137
// //The indexes are -1 because no visible difference has occurred. The
138
// //javadoc doesn't indicate that -1 is unacceptable, but errors may
139
// //occur. There must be a better way...
140
// TableColumnModelEvent evt = new TableColumnModelEvent(this, -1, -1);
141
// fireColumnAdded(evt);
142
// } else {
143
// super.addColumn(aColumn);
144
// }
145
}
146
147         
148     protected void moveToInvisible(TableColumnExt col) {
149         //make invisible
150
int oldIndex = tableColumns.indexOf(col);
151         invisibleColumns.add(col);
152         oldIndexes.put(col, new Integer JavaDoc(oldIndex));
153         super.removeColumn(col);
154         //need to fire some listener that will cause the table
155
//associated with this model to be refreshed...
156
// tableColumns.remove(col);
157
// TableColumnModelEvent e = new TableColumnModelEvent(DefaultTableColumnModelExt.this, -1, -1);
158
// fireColumnMoved(e);
159
}
160
161     protected void moveToVisible(TableColumnExt col) {
162         //make visible
163
invisibleColumns.remove(col);
164         Integer JavaDoc oldIndexInteger = (Integer JavaDoc)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 // tableColumns.add(oldIndex, col);
172
// //need to fire some listener that will cause the table
173
// //associated with this model to be refreshed...
174
// TableColumnModelEvent e = new TableColumnModelEvent(DefaultTableColumnModelExt.this, -1, oldIndex);
175
// fireColumnMoved(e);
176
}
177
178     private final class VisibilityListener implements PropertyChangeListener JavaDoc {
179         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
180             if (evt.getPropertyName().equals("visible")) {
181                 boolean oldValue = ((Boolean JavaDoc)evt.getOldValue()).booleanValue();
182                 boolean newValue = ((Boolean JavaDoc)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