1 7 8 package javax.swing.table; 9 10 import javax.swing.*; 11 import javax.swing.event.*; 12 import java.awt.*; 13 import java.util.Vector ; 14 import java.util.Enumeration ; 15 import java.util.EventListener ; 16 import java.beans.PropertyChangeListener ; 17 import java.beans.PropertyChangeEvent ; 18 import java.io.Serializable ; 19 20 37 public class DefaultTableColumnModel implements TableColumnModel , 38 PropertyChangeListener , ListSelectionListener, Serializable 39 { 40 44 45 protected Vector <TableColumn > tableColumns; 46 47 48 protected ListSelectionModel selectionModel; 49 50 51 protected int columnMargin; 52 53 54 protected EventListenerList listenerList = new EventListenerList(); 55 56 57 transient protected ChangeEvent changeEvent = null; 58 59 60 protected boolean columnSelectionAllowed; 61 62 63 protected int totalColumnWidth; 64 65 71 public DefaultTableColumnModel() { 72 super(); 73 74 tableColumns = new Vector <TableColumn >(); 76 setSelectionModel(createSelectionModel()); 77 setColumnMargin(1); 78 invalidateWidthCache(); 79 setColumnSelectionAllowed(false); 80 } 81 82 86 97 public void addColumn(TableColumn aColumn) { 98 if (aColumn == null) { 99 throw new IllegalArgumentException ("Object is null"); 100 } 101 102 tableColumns.addElement(aColumn); 103 aColumn.addPropertyChangeListener(this); 104 invalidateWidthCache(); 105 106 fireColumnAdded(new TableColumnModelEvent(this, 0, 108 getColumnCount() - 1)); 109 } 110 111 123 public void removeColumn(TableColumn column) { 124 int columnIndex = tableColumns.indexOf(column); 125 126 if (columnIndex != -1) { 127 if (selectionModel != null) { 129 selectionModel.removeIndexInterval(columnIndex,columnIndex); 130 } 131 132 column.removePropertyChangeListener(this); 133 tableColumns.removeElementAt(columnIndex); 134 invalidateWidthCache(); 135 136 fireColumnRemoved(new TableColumnModelEvent(this, 139 columnIndex, 0)); 140 } 141 } 142 143 158 public void moveColumn(int columnIndex, int newIndex) { 159 if ((columnIndex < 0) || (columnIndex >= getColumnCount()) || 160 (newIndex < 0) || (newIndex >= getColumnCount())) 161 throw new IllegalArgumentException ("moveColumn() - Index out of range"); 162 163 TableColumn aColumn; 164 165 if (columnIndex == newIndex) { 172 fireColumnMoved(new TableColumnModelEvent(this, columnIndex, newIndex)); 173 return; 174 } 175 aColumn = (TableColumn )tableColumns.elementAt(columnIndex); 176 177 tableColumns.removeElementAt(columnIndex); 178 boolean selected = selectionModel.isSelectedIndex(columnIndex); 179 selectionModel.removeIndexInterval(columnIndex,columnIndex); 180 181 tableColumns.insertElementAt(aColumn, newIndex); 182 selectionModel.insertIndexInterval(newIndex, 1, true); 183 if (selected) { 184 selectionModel.addSelectionInterval(newIndex, newIndex); 185 } 186 else { 187 selectionModel.removeSelectionInterval(newIndex, newIndex); 188 } 189 190 fireColumnMoved(new TableColumnModelEvent(this, columnIndex, 191 newIndex)); 192 } 193 194 203 public void setColumnMargin(int newMargin) { 204 if (newMargin != columnMargin) { 205 columnMargin = newMargin; 206 fireColumnMarginChanged(); 208 } 209 } 210 211 215 221 public int getColumnCount() { 222 return tableColumns.size(); 223 } 224 225 229 public Enumeration <TableColumn > getColumns() { 230 return tableColumns.elements(); 231 } 232 233 248 public int getColumnIndex(Object identifier) { 249 if (identifier == null) { 250 throw new IllegalArgumentException ("Identifier is null"); 251 } 252 253 Enumeration enumeration = getColumns(); 254 TableColumn aColumn; 255 int index = 0; 256 257 while (enumeration.hasMoreElements()) { 258 aColumn = (TableColumn )enumeration.nextElement(); 259 if (identifier.equals(aColumn.getIdentifier())) 261 return index; 262 index++; 263 } 264 throw new IllegalArgumentException ("Identifier not found"); 265 } 266 267 275 public TableColumn getColumn(int columnIndex) { 276 return (TableColumn )tableColumns.elementAt(columnIndex); 277 } 278 279 286 public int getColumnMargin() { 287 return columnMargin; 288 } 289 290 312 public int getColumnIndexAtX(int x) { 313 if (x < 0) { 314 return -1; 315 } 316 int cc = getColumnCount(); 317 for(int column = 0; column < cc; column++) { 318 x = x - getColumn(column).getWidth(); 319 if (x < 0) { 320 return column; 321 } 322 } 323 return -1; 324 } 325 326 330 public int getTotalColumnWidth() { 331 if (totalColumnWidth == -1) { 332 recalcWidthCache(); 333 } 334 return totalColumnWidth; 335 } 336 337 341 353 public void setSelectionModel(ListSelectionModel newModel) { 354 if (newModel == null) { 355 throw new IllegalArgumentException ("Cannot set a null SelectionModel"); 356 } 357 358 ListSelectionModel oldModel = selectionModel; 359 360 if (newModel != oldModel) { 361 if (oldModel != null) { 362 oldModel.removeListSelectionListener(this); 363 } 364 365 selectionModel= newModel; 366 newModel.addListSelectionListener(this); 367 } 368 } 369 370 378 public ListSelectionModel getSelectionModel() { 379 return selectionModel; 380 } 381 382 386 private void checkLeadAnchor() { 387 int lead = selectionModel.getLeadSelectionIndex(); 388 int count = tableColumns.size(); 389 if (count == 0) { 390 if (lead != -1) { 391 selectionModel.setValueIsAdjusting(true); 393 selectionModel.setAnchorSelectionIndex(-1); 394 selectionModel.setLeadSelectionIndex(-1); 395 selectionModel.setValueIsAdjusting(false); 396 } 397 } else { 398 if (lead == -1) { 399 if (selectionModel.isSelectedIndex(0)) { 402 selectionModel.addSelectionInterval(0, 0); 403 } else { 404 selectionModel.removeSelectionInterval(0, 0); 405 } 406 } 407 } 408 } 409 410 415 public void setColumnSelectionAllowed(boolean flag) { 416 columnSelectionAllowed = flag; 417 } 418 419 425 public boolean getColumnSelectionAllowed() { 426 return columnSelectionAllowed; 427 } 428 429 437 public int[] getSelectedColumns() { 438 if (selectionModel != null) { 439 int iMin = selectionModel.getMinSelectionIndex(); 440 int iMax = selectionModel.getMaxSelectionIndex(); 441 442 if ((iMin == -1) || (iMax == -1)) { 443 return new int[0]; 444 } 445 446 int[] rvTmp = new int[1+ (iMax - iMin)]; 447 int n = 0; 448 for(int i = iMin; i <= iMax; i++) { 449 if (selectionModel.isSelectedIndex(i)) { 450 rvTmp[n++] = i; 451 } 452 } 453 int[] rv = new int[n]; 454 System.arraycopy(rvTmp, 0, rv, 0, n); 455 return rv; 456 } 457 return new int[0]; 458 } 459 460 465 public int getSelectedColumnCount() { 466 if (selectionModel != null) { 467 int iMin = selectionModel.getMinSelectionIndex(); 468 int iMax = selectionModel.getMaxSelectionIndex(); 469 int count = 0; 470 471 for(int i = iMin; i <= iMax; i++) { 472 if (selectionModel.isSelectedIndex(i)) { 473 count++; 474 } 475 } 476 return count; 477 } 478 return 0; 479 } 480 481 485 490 public void addColumnModelListener(TableColumnModelListener x) { 491 listenerList.add(TableColumnModelListener.class, x); 492 } 493 494 499 public void removeColumnModelListener(TableColumnModelListener x) { 500 listenerList.remove(TableColumnModelListener.class, x); 501 } 502 503 516 public TableColumnModelListener[] getColumnModelListeners() { 517 return (TableColumnModelListener[])listenerList.getListeners( 518 TableColumnModelListener.class); 519 } 520 521 525 533 protected void fireColumnAdded(TableColumnModelEvent e) { 534 Object [] listeners = listenerList.getListenerList(); 536 for (int i = listeners.length-2; i>=0; i-=2) { 539 if (listeners[i]==TableColumnModelListener.class) { 540 ((TableColumnModelListener)listeners[i+1]). 544 columnAdded(e); 545 } 546 } 547 } 548 549 557 protected void fireColumnRemoved(TableColumnModelEvent e) { 558 Object [] listeners = listenerList.getListenerList(); 560 for (int i = listeners.length-2; i>=0; i-=2) { 563 if (listeners[i]==TableColumnModelListener.class) { 564 ((TableColumnModelListener)listeners[i+1]). 568 columnRemoved(e); 569 } 570 } 571 } 572 573 581 protected void fireColumnMoved(TableColumnModelEvent e) { 582 Object [] listeners = listenerList.getListenerList(); 584 for (int i = listeners.length-2; i>=0; i-=2) { 587 if (listeners[i]==TableColumnModelListener.class) { 588 ((TableColumnModelListener)listeners[i+1]). 592 columnMoved(e); 593 } 594 } 595 } 596 597 605 protected void fireColumnSelectionChanged(ListSelectionEvent e) { 606 Object [] listeners = listenerList.getListenerList(); 608 for (int i = listeners.length-2; i>=0; i-=2) { 611 if (listeners[i]==TableColumnModelListener.class) { 612 ((TableColumnModelListener)listeners[i+1]). 616 columnSelectionChanged(e); 617 } 618 } 619 } 620 621 628 protected void fireColumnMarginChanged() { 629 Object [] listeners = listenerList.getListenerList(); 631 for (int i = listeners.length-2; i>=0; i-=2) { 634 if (listeners[i]==TableColumnModelListener.class) { 635 if (changeEvent == null) 637 changeEvent = new ChangeEvent (this); 638 ((TableColumnModelListener)listeners[i+1]). 639 columnMarginChanged(changeEvent); 640 } 641 } 642 } 643 644 679 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 680 return listenerList.getListeners(listenerType); 681 } 682 683 687 695 public void propertyChange(PropertyChangeEvent evt) { 696 String name = evt.getPropertyName(); 697 698 if (name == "width" || name == "preferredWidth") { 699 invalidateWidthCache(); 700 fireColumnMarginChanged(); 703 } 704 705 } 706 707 711 719 public void valueChanged(ListSelectionEvent e) { 720 fireColumnSelectionChanged(e); 721 } 722 723 727 730 protected ListSelectionModel createSelectionModel() { 731 return new DefaultListSelectionModel(); 732 } 733 734 738 protected void recalcWidthCache() { 739 Enumeration enumeration = getColumns(); 740 totalColumnWidth = 0; 741 while (enumeration.hasMoreElements()) { 742 totalColumnWidth += ((TableColumn )enumeration.nextElement()).getWidth(); 743 } 744 } 745 746 private void invalidateWidthCache() { 747 totalColumnWidth = -1; 748 } 749 750 } 752 | Popular Tags |