1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.swing;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.TransformedList;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import ca.odell.glazedlists.impl.IteratorAsEnumeration;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.util.Enumeration;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
/**
* A {@link TableColumnModel} that holds an {@link EventList}. Each element of
* the list corresponds to a {@link TableColumn} in the model.
*
* <p>The EventTableColumnModel class is <strong>not thread-safe</strong>.
* Unless otherwise noted, all methods are only safe to be called from the
* event dispatch thread. To do this programmatically, use
* {@link SwingUtilities#invokeAndWait(Runnable)}.
*
* @author James Lemieux
*/
public class EventTableColumnModel<T extends TableColumn> implements TableColumnModel, PropertyChangeListener, ListSelectionListener, ListEventListener<T> {
/** the proxy moves events to the Swing Event Dispatch thread */
protected TransformedList<T, T> swingThreadSource;
/** <tt>true</tt> indicates that disposing this TableColumnModel should dispose of the swingThreadSource as well */
private final boolean disposeSwingThreadSource;
/** list of TableColumnModelListeners */
private final EventListenerList listenerList = new EventListenerList();
/** change event (only one needed) */
private final transient ChangeEvent changeEvent = new ChangeEvent(this);
/** model for keeping track of column selections */
private ListSelectionModel selectionModel;
/** column selection allowed in this column model */
private boolean columnSelectionAllowed;
/** width of the margin between each column */
private int columnMargin;
/** a local cache of the combined width of all columns */
private int totalColumnWidth;
/**
* Creates a new model that contains the {@link TableColumn} objects from
* the given <code>source</code>. Changes to the <code>source</code> are
* reflected in this model.
*/
public EventTableColumnModel(EventList<T> source) {
setSelectionModel(createSelectionModel());
setColumnMargin(1);
invalidateWidthCache();
setColumnSelectionAllowed(false);
// lock the source list for reading since we want to prevent writes
// from occurring until we fully initialize this EventTableColumnModel
source.getReadWriteLock().readLock().lock();
try {
// ensure all of the TableColumns are non-null
for (int i = 0, n = source.size(); i < n; i++) {
if (source.get(i) == null)
throw new IllegalStateException("null TableColumn objects are not allowed in EventTableColumnModel");
}
// start listening to each of the TableColumns for property changes that may resize the table header
for (int i = 0, n = source.size(); i < n; i++)
source.get(i).addPropertyChangeListener(this);
disposeSwingThreadSource = !GlazedListsSwing.isSwingThreadProxyList(source);
swingThreadSource = disposeSwingThreadSource ? GlazedListsSwing.swingThreadProxyList(source) : (TransformedList<T, T>) source;
swingThreadSource.addListEventListener(this);
} finally {
source.getReadWriteLock().readLock().unlock();
}
}
/** @inheritDoc */
public void addColumn(TableColumn column) {
swingThreadSource.getReadWriteLock().writeLock().lock();
try {
swingThreadSource.add((T) column);
} finally {
swingThreadSource.getReadWriteLock().writeLock().unlock();
}
}
/** @inheritDoc */
public void removeColumn(TableColumn column) {
swingThreadSource.getReadWriteLock().writeLock().lock();
try {
swingThreadSource.remove(column);
} finally {
swingThreadSource.getReadWriteLock().writeLock().unlock();
}
}
/** @inheritDoc */
public void moveColumn(int columnIndex, int newIndex) {
if (columnIndex < 0 || columnIndex >= getColumnCount())
throw new IllegalArgumentException("columnIndex out of range");
if (newIndex < 0 || newIndex >= getColumnCount())
throw new IllegalArgumentException("newIndex out of range");
// If the column has not yet moved far enough to change positions
// post the event anyway, the "draggedDistance" property of the
// tableHeader will say how far the column has been dragged.
// Here we are really trying to get the best out of an
// API that could do with some rethinking. We preserve backward
// compatibility by slightly bending the meaning of these methods.
if (columnIndex == newIndex) {
fireColumnMoved(new TableColumnModelEvent(this, columnIndex, newIndex));
return;
}
swingThreadSource.getReadWriteLock().writeLock().lock();
try {
final boolean selected = selectionModel.isSelectedIndex(columnIndex);
swingThreadSource.add(newIndex, swingThreadSource.remove(columnIndex));
// preserve the selection after the move if one existed
if (selected)
selectionModel.addSelectionInterval(newIndex, newIndex);
} finally {
swingThreadSource.getReadWriteLock().writeLock().unlock();
}
}
/** @inheritDoc */
public void setColumnMargin(int newMargin) {
if (newMargin != columnMargin) {
columnMargin = newMargin;
fireColumnMarginChanged();
}
}
/** @inheritDoc */
public int getColumnMargin() {
return columnMargin;
}
/** @inheritDoc */
public int getColumnCount() {
swingThreadSource.getReadWriteLock().readLock().lock();
try {
return swingThreadSource.size();
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
}
/** @inheritDoc */
public Enumeration<TableColumn> getColumns() {
return new IteratorAsEnumeration<TableColumn>(swingThreadSource.iterator());
}
/** @inheritDoc */
public int getColumnIndex(Object identifier) {
if (identifier == null)
throw new IllegalArgumentException("identifier is null");
swingThreadSource.getReadWriteLock().readLock().lock();
try {
for (int i = 0, n = swingThreadSource.size(); i < n; i++) {
if (identifier.equals(swingThreadSource.get(i).getIdentifier()))
return i;
}
throw new IllegalArgumentException("Identifier not found");
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
}
/** @inheritDoc */
public TableColumn getColumn(int columnIndex) {
swingThreadSource.getReadWriteLock().readLock().lock();
try {
return swingThreadSource.get(columnIndex);
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
}
/** @inheritDoc */
public int getColumnIndexAtX(int x) {
if (x < 0) return -1;
swingThreadSource.getReadWriteLock().readLock().lock();
try {
for (int i = 0, n = swingThreadSource.size(); i < n; i++) {
TableColumn column = swingThreadSource.get(i);
x = x - column.getWidth();
if (x < 0)
return i;
}
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
return -1;
}
/** @inheritDoc */
public int getTotalColumnWidth() {
if (totalColumnWidth == -1)
recalcWidthCache();
return totalColumnWidth;
}
/**
* Recalculates the total combined width of all columns.
*/
private void recalcWidthCache() {
swingThreadSource.getReadWriteLock().readLock().lock();
try {
totalColumnWidth = 0;
for (int i = 0, n = swingThreadSource.size(); i < n; i++)
totalColumnWidth += swingThreadSource.get(i).getWidth();
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
}
/**
* Mark the cached value of the total width of all columns as dirty and in
* need of being recalculated.
*/
private void invalidateWidthCache() {
totalColumnWidth = -1;
}
/** @inheritDoc */
public void setColumnSelectionAllowed(boolean flag) {
columnSelectionAllowed = flag;
}
/** @inheritDoc */
public boolean getColumnSelectionAllowed() {
return columnSelectionAllowed;
}
/** @inheritDoc */
public int[] getSelectedColumns() {
if (selectionModel != null) {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
if (iMin == -1 || iMax == -1)
return new int[0];
int[] rvTmp = new int[1 + (iMax - iMin)];
int n = 0;
for (int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i)) {
rvTmp[n++] = i;
}
}
int[] rv = new int[n];
System.arraycopy(rvTmp, 0, rv, 0, n);
return rv;
}
return new int[0];
}
/** @inheritDoc */
public int getSelectedColumnCount() {
if (selectionModel != null) {
int iMin = selectionModel.getMinSelectionIndex();
int iMax = selectionModel.getMaxSelectionIndex();
int count = 0;
for (int i = iMin; i <= iMax; i++) {
if (selectionModel.isSelectedIndex(i))
count++;
}
return count;
}
return 0;
}
/** @inheritDoc */
public void setSelectionModel(ListSelectionModel newModel) {
if (newModel == null)
throw new IllegalArgumentException("newModel may not be null");
if (newModel != selectionModel) {
if (selectionModel != null)
selectionModel.removeListSelectionListener(this);
selectionModel = newModel;
selectionModel.addListSelectionListener(this);
}
}
/** @inheritDoc */
public ListSelectionModel getSelectionModel() {
return selectionModel;
}
/** @inheritDoc */
public void addColumnModelListener(TableColumnModelListener listener) {
listenerList.add(TableColumnModelListener.class, listener);
}
/** @inheritDoc */
public void removeColumnModelListener(TableColumnModelListener listener) {
listenerList.remove(TableColumnModelListener.class, listener);
}
/**
* Watch for changes to the column width or preferred column width and
* trigger a relayout of the table header when they change.
*/
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if (name == "width" || name == "preferredWidth") {
invalidateWidthCache();
fireColumnMarginChanged();
}
}
public void valueChanged(ListSelectionEvent e) {
fireColumnSelectionChanged(e);
}
public void listChanged(ListEvent<T> listChanges) {
// arbitrary changes have occurred so we begin by invalidating the cached total width of all TableColumns
invalidateWidthCache();
while (listChanges.next()) {
final int index = listChanges.getIndex();
final int changeType = listChanges.getType();
if (changeType == ListEvent.DELETE) {
if (selectionModel != null)
selectionModel.removeIndexInterval(index, index);
final TableColumn oldColumn = listChanges.getOldValue();
oldColumn.removePropertyChangeListener(this);
fireColumnRemoved(new TableColumnModelEvent(this, index, 0));
} else if (changeType == ListEvent.INSERT) {
final TableColumn newColumn = listChanges.getSourceList().get(index);
if (newColumn == null)
throw new IllegalStateException("null TableColumn objects are not allowed in EventTableColumnModel");
newColumn.addPropertyChangeListener(this);
fireColumnAdded(new TableColumnModelEvent(this, 0, getColumnCount() - 1));
} else if (changeType == ListEvent.UPDATE) {
final TableColumn oldColumn = listChanges.getOldValue();
final TableColumn newColumn = listChanges.getSourceList().get(index);
if (newColumn == null)
throw new IllegalStateException("null TableColumn objects are not allowed in EventTableColumnModel");
if (oldColumn != newColumn) {
oldColumn.removePropertyChangeListener(this);
newColumn.addPropertyChangeListener(this);
}
fireColumnMoved(new TableColumnModelEvent(this, index, index));
}
}
}
/**
* Releases the resources consumed by this {@link EventTableColumnModel} so that it
* may eventually be garbage collected.
*
* <p>An {@link EventTableColumnModel} will be garbage collected without a
* call to {@link #dispose()}, but not before its source {@link EventList}
* is garbage collected. By calling {@link #dispose()}, you allow the
* {@link EventTableColumnModel} to be garbage collected before its source
* {@link EventList}. This is necessary for situations where an
* {@link EventTableColumnModel} is short-lived but its source
* {@link EventList} is long-lived.
*
* <p><strong><font color="#FF0000">Warning:</font></strong> It is an error
* to call any method on an {@link EventTableColumnModel} after it has been
* disposed.
*/
public void dispose() {
swingThreadSource.getReadWriteLock().readLock().lock();
try {
// stop listening to each of the TableColumns for property changes
for (int i = 0, n = swingThreadSource.size(); i < n; i++)
swingThreadSource.get(i).removePropertyChangeListener(this);
swingThreadSource.removeListEventListener(this);
// if we created the swingThreadSource then we must also dispose it
if (disposeSwingThreadSource)
swingThreadSource.dispose();
} finally {
swingThreadSource.getReadWriteLock().readLock().unlock();
}
// this encourages exceptions to be thrown if this model is incorrectly accessed again
swingThreadSource = null;
}
/**
* Creates a new default list selection model.
*/
protected ListSelectionModel createSelectionModel() {
return new DefaultListSelectionModel();
}
//
// Convenience methods to fire types of TableColumnModelEvent objects to
// registered TableColumnModelListeners.
//
protected void fireColumnAdded(TableColumnModelEvent e) {
final Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TableColumnModelListener.class)
((TableColumnModelListener) listeners[i + 1]).columnAdded(e);
}
}
protected void fireColumnRemoved(TableColumnModelEvent e) {
final Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TableColumnModelListener.class)
((TableColumnModelListener) listeners[i + 1]).columnRemoved(e);
}
}
protected void fireColumnMoved(TableColumnModelEvent e) {
final Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TableColumnModelListener.class)
((TableColumnModelListener) listeners[i + 1]).columnMoved(e);
}
}
protected void fireColumnSelectionChanged(ListSelectionEvent e) {
final Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TableColumnModelListener.class)
((TableColumnModelListener) listeners[i + 1]).columnSelectionChanged(e);
}
}
protected void fireColumnMarginChanged() {
final Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TableColumnModelListener.class)
((TableColumnModelListener) listeners[i + 1]).columnMarginChanged(changeEvent);
}
}
}
|