1 7 8 package javax.swing.table; 9 10 import javax.swing.*; 11 import javax.swing.border.*; 12 import javax.swing.event.SwingPropertyChangeSupport ; 13 import java.lang.Integer ; 14 import java.awt.Color ; 15 import java.awt.Component ; 16 import java.io.Serializable ; 17 import java.beans.PropertyChangeEvent ; 18 import java.beans.PropertyChangeListener ; 19 20 68 public class TableColumn extends Object implements Serializable { 69 70 74 78 public final static String COLUMN_WIDTH_PROPERTY = "columWidth"; 79 80 84 public final static String HEADER_VALUE_PROPERTY = "headerValue"; 85 86 90 public final static String HEADER_RENDERER_PROPERTY = "headerRenderer"; 91 92 96 public final static String CELL_RENDERER_PROPERTY = "cellRenderer"; 97 98 102 107 protected int modelIndex; 108 109 119 protected Object identifier; 120 121 122 protected int width; 123 124 125 protected int minWidth; 126 127 128 private int preferredWidth; 129 130 131 protected int maxWidth; 132 133 134 protected TableCellRenderer headerRenderer; 135 136 137 protected Object headerValue; 138 139 140 protected TableCellRenderer cellRenderer; 141 142 143 protected TableCellEditor cellEditor; 144 145 146 protected boolean isResizable; 147 148 154 158 @Deprecated 159 transient protected int resizedPostingDisableCount; 160 161 165 private SwingPropertyChangeSupport changeSupport; 166 167 171 178 public TableColumn() { 179 this(0); 180 } 181 182 187 public TableColumn(int modelIndex) { 188 this(modelIndex, 75, null, null); 189 } 190 191 196 public TableColumn(int modelIndex, int width) { 197 this(modelIndex, width, null, null); 198 } 199 200 225 public TableColumn(int modelIndex, int width, 226 TableCellRenderer cellRenderer, 227 TableCellEditor cellEditor) { 228 super(); 229 this.modelIndex = modelIndex; 230 this.width = width; 231 this.preferredWidth = width; 232 233 this.cellRenderer = cellRenderer; 234 this.cellEditor = cellEditor; 235 236 minWidth = 15; 238 maxWidth = Integer.MAX_VALUE; 239 isResizable = true; 240 resizedPostingDisableCount = 0; 241 headerValue = null; 242 } 243 244 248 private void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 249 if (changeSupport != null) { 250 changeSupport.firePropertyChange(propertyName, oldValue, newValue); 251 } 252 } 253 254 private void firePropertyChange(String propertyName, int oldValue, int newValue) { 255 if (oldValue != newValue) { 256 firePropertyChange(propertyName, new Integer (oldValue), new Integer (newValue)); 257 } 258 } 259 260 private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { 261 if (oldValue != newValue) { 262 firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); 263 } 264 } 265 266 276 public void setModelIndex(int modelIndex) { 277 int old = this.modelIndex; 278 this.modelIndex = modelIndex; 279 firePropertyChange("modelIndex", old, modelIndex); 280 } 281 282 286 public int getModelIndex() { 287 return modelIndex; 288 } 289 290 303 public void setIdentifier(Object identifier) { 304 Object old = this.identifier; 305 this.identifier = identifier; 306 firePropertyChange("identifier", old, identifier); 307 } 308 309 310 321 public Object getIdentifier() { 322 return (identifier != null) ? identifier : getHeaderValue(); 323 324 } 325 326 337 public void setHeaderValue(Object headerValue) { 338 Object old = this.headerValue; 339 this.headerValue = headerValue; 340 firePropertyChange("headerValue", old, headerValue); 341 } 342 343 350 public Object getHeaderValue() { 351 return headerValue; 352 } 353 354 358 369 public void setHeaderRenderer(TableCellRenderer headerRenderer) { 370 TableCellRenderer old = this.headerRenderer; 371 this.headerRenderer = headerRenderer; 372 firePropertyChange("headerRenderer", old, headerRenderer); 373 } 374 375 387 public TableCellRenderer getHeaderRenderer() { 388 return headerRenderer; 389 } 390 391 401 public void setCellRenderer(TableCellRenderer cellRenderer) { 402 TableCellRenderer old = this.cellRenderer; 403 this.cellRenderer = cellRenderer; 404 firePropertyChange("cellRenderer", old, cellRenderer); 405 } 406 407 422 public TableCellRenderer getCellRenderer() { 423 return cellRenderer; 424 } 425 426 435 public void setCellEditor(TableCellEditor cellEditor){ 436 TableCellEditor old = this.cellEditor; 437 this.cellEditor = cellEditor; 438 firePropertyChange("cellEditor", old, cellEditor); 439 } 440 441 453 public TableCellEditor getCellEditor() { 454 return cellEditor; 455 } 456 457 480 public void setWidth(int width) { 481 int old = this.width; 482 this.width = Math.min(Math.max(width, minWidth), maxWidth); 483 firePropertyChange("width", old, this.width); 484 } 485 486 493 public int getWidth() { 494 return width; 495 } 496 497 514 public void setPreferredWidth(int preferredWidth) { 515 int old = this.preferredWidth; 516 this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth); 517 firePropertyChange("preferredWidth", old, this.preferredWidth); 518 } 519 520 527 public int getPreferredWidth() { 528 return preferredWidth; 529 } 530 531 544 public void setMinWidth(int minWidth) { 545 int old = this.minWidth; 546 this.minWidth = Math.max(minWidth, 0); 547 if (width < minWidth) { 548 setWidth(minWidth); 549 } 550 if (preferredWidth < minWidth) { 551 setPreferredWidth(minWidth); 552 } 553 firePropertyChange("minWidth", old, this.minWidth); 554 } 555 556 564 public int getMinWidth() { 565 return minWidth; 566 } 567 568 581 public void setMaxWidth(int maxWidth) { 582 int old = this.maxWidth; 583 this.maxWidth = Math.max(minWidth, maxWidth); 584 if (width > maxWidth) { 585 setWidth(maxWidth); 586 } 587 if (preferredWidth > maxWidth) { 588 setPreferredWidth(maxWidth); 589 } 590 firePropertyChange("maxWidth", old, this.maxWidth); 591 } 592 593 602 public int getMaxWidth() { 603 return maxWidth; 604 } 605 606 615 public void setResizable(boolean isResizable) { 616 boolean old = this.isResizable; 617 this.isResizable = isResizable; 618 firePropertyChange("isResizable", old, this.isResizable); 619 } 620 621 630 public boolean getResizable() { 631 return isResizable; 632 } 633 634 646 public void sizeWidthToFit() { 647 if (headerRenderer == null) { 648 return; 649 } 650 Component c = headerRenderer.getTableCellRendererComponent(null, 651 getHeaderValue(), false, false, 0, 0); 652 653 setMinWidth(c.getMinimumSize().width); 654 setMaxWidth(c.getMaximumSize().width); 655 setPreferredWidth(c.getPreferredSize().width); 656 657 setWidth(getPreferredWidth()); 658 } 659 660 666 @Deprecated 667 public void disableResizedPosting() { 668 resizedPostingDisableCount++; 669 } 670 671 677 @Deprecated 678 public void enableResizedPosting() { 679 resizedPostingDisableCount--; 680 } 681 682 686 701 public synchronized void addPropertyChangeListener( 702 PropertyChangeListener listener) { 703 if (changeSupport == null) { 704 changeSupport = new SwingPropertyChangeSupport (this); 705 } 706 changeSupport.addPropertyChangeListener(listener); 707 } 708 709 717 718 public synchronized void removePropertyChangeListener( 719 PropertyChangeListener listener) { 720 if (changeSupport != null) { 721 changeSupport.removePropertyChangeListener(listener); 722 } 723 } 724 725 733 public synchronized PropertyChangeListener [] getPropertyChangeListeners() { 734 if (changeSupport == null) { 735 return new PropertyChangeListener [0]; 736 } 737 return changeSupport.getPropertyChangeListeners(); 738 } 739 740 744 755 protected TableCellRenderer createDefaultHeaderRenderer() { 756 DefaultTableCellRenderer label = new DefaultTableCellRenderer () { 757 public Component getTableCellRendererComponent(JTable table, Object value, 758 boolean isSelected, boolean hasFocus, int row, int column) { 759 if (table != null) { 760 JTableHeader header = table.getTableHeader(); 761 if (header != null) { 762 setForeground(header.getForeground()); 763 setBackground(header.getBackground()); 764 setFont(header.getFont()); 765 } 766 } 767 768 setText((value == null) ? "" : value.toString()); 769 setBorder(UIManager.getBorder("TableHeader.cellBorder")); 770 return this; 771 } 772 }; 773 label.setHorizontalAlignment(JLabel.CENTER); 774 return label; 775 } 776 777 } 779 780 781 782 783 | Popular Tags |