GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* TableColumn.java -- 2: Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing.table; 40: 41: import java.beans.PropertyChangeEvent; 42: import java.beans.PropertyChangeListener; 43: import java.io.Serializable; 44: 45: import javax.swing.event.SwingPropertyChangeSupport; 46: 47: /** 48: * Represents the attributes of a column in a table, including the column index, 49: * width, minimum width, preferred width and maximum width. 50: * 51: * @author Andrew Selkirk 52: * @version 1.0 53: */ 54: public class TableColumn 55: implements Serializable 56: { 57: static final long serialVersionUID = -6113660025878112608L; 58: 59: /** 60: * The name for the <code>columnWidth</code> property. Note that the typo 61: * in the name value is deliberate, to match the specification. 62: */ 63: public static final String COLUMN_WIDTH_PROPERTY = "columWidth"; 64: 65: /** 66: * The name for the <code>headerValue</code> property. 67: */ 68: public static final String HEADER_VALUE_PROPERTY = "headerValue"; 69: 70: /** 71: * The name for the <code>headerRenderer</code> property. 72: */ 73: public static final String HEADER_RENDERER_PROPERTY = "headerRenderer"; 74: 75: /** 76: * The name for the <code>cellRenderer</code> property. 77: */ 78: public static final String CELL_RENDERER_PROPERTY = "cellRenderer"; 79: 80: /** 81: * The index of the corresponding column in the table model. 82: */ 83: protected int modelIndex; 84: 85: /** 86: * The identifier for the column. 87: */ 88: protected Object identifier; 89: 90: /** 91: * The width. 92: */ 93: protected int width; 94: 95: /** 96: * The minimum width. 97: */ 98: protected int minWidth = 15; 99: 100: /** 101: * The preferred width. 102: */ 103: private int preferredWidth; 104: 105: /** 106: * The maximum width. 107: */ 108: protected int maxWidth = Integer.MAX_VALUE; 109: 110: /** 111: * headerRenderer 112: */ 113: protected TableCellRenderer headerRenderer; 114: 115: /** 116: * The header value. 117: */ 118: protected Object headerValue; 119: 120: /** 121: * cellRenderer 122: */ 123: protected TableCellRenderer cellRenderer; 124: 125: /** 126: * cellEditor 127: */ 128: protected TableCellEditor cellEditor; 129: 130: /** 131: * isResizable 132: */ 133: protected boolean isResizable = true; 134: 135: /** 136: * resizedPostingDisableCount 137: * 138: * @deprecated 1.3 139: */ 140: protected transient int resizedPostingDisableCount; 141: 142: /** 143: * changeSupport 144: */ 145: private SwingPropertyChangeSupport changeSupport = 146: new SwingPropertyChangeSupport(this); 147: 148: /** 149: * Creates a new <code>TableColumn</code> that maps to column 0 in the 150: * related table model. The default width is <code>75</code> units. 151: */ 152: public TableColumn() 153: { 154: this(0, 75, null, null); 155: } 156: 157: /** 158: * Creates a new <code>TableColumn</code> that maps to the specified column 159: * in the related table model. The default width is <code>75</code> units. 160: * 161: * @param modelIndex the index of the column in the model 162: */ 163: public TableColumn(int modelIndex) 164: { 165: this(modelIndex, 75, null, null); 166: } 167: 168: /** 169: * Creates a new <code>TableColumn</code> that maps to the specified column 170: * in the related table model, and has the specified <code>width</code>. 171: * 172: * @param modelIndex the index of the column in the model 173: * @param width the width 174: */ 175: public TableColumn(int modelIndex, int width) 176: { 177: this(modelIndex, width, null, null); 178: } 179: 180: /** 181: * Creates a new <code>TableColumn</code> that maps to the specified column 182: * in the related table model, and has the specified <code>width</code>, 183: * <code>cellRenderer</code> and <code>cellEditor</code>. 184: * 185: * @param modelIndex the index of the column in the model 186: * @param width the width 187: * @param cellRenderer the cell renderer (<code>null</code> permitted). 188: * @param cellEditor the cell editor (<code>null</code> permitted). 189: */ 190: public TableColumn(int modelIndex, int width, 191: TableCellRenderer cellRenderer, TableCellEditor cellEditor) 192: { 193: this.modelIndex = modelIndex; 194: this.width = width; 195: this.preferredWidth = width; 196: this.cellRenderer = cellRenderer; 197: this.cellEditor = cellEditor; 198: this.headerValue = null; 199: this.identifier = null; 200: } 201: 202: /** 203: * firePropertyChange 204: * 205: * @param property the name of the property 206: * @param oldValue the old value 207: * @param newValue the new value 208: */ 209: private void firePropertyChange(String property, Object oldValue, 210: Object newValue) 211: { 212: changeSupport.firePropertyChange(property, oldValue, newValue); 213: } 214: 215: /** 216: * firePropertyChange 217: * 218: * @param property the name of the property 219: * @param oldValue the old value 220: * @param newValue the new value 221: */ 222: private void firePropertyChange(String property, int oldValue, int newValue) 223: { 224: firePropertyChange(property, new Integer(oldValue), new Integer(newValue)); 225: } 226: 227: /** 228: * firePropertyChange 229: * 230: * @param property the name of the property 231: * @param oldValue the old value 232: * @param newValue the new value 233: */ 234: private void firePropertyChange(String property, boolean oldValue, 235: boolean newValue) 236: { 237: firePropertyChange(property, Boolean.valueOf(oldValue), 238: Boolean.valueOf(newValue)); 239: } 240: 241: /** 242: * Sets the index of the column in the related {@link TableModel} that this 243: * <code>TableColumn</code> maps to. 244: * 245: * @param modelIndex the column index in the model. 246: */ 247: public void setModelIndex(int modelIndex) 248: { 249: this.modelIndex = modelIndex; 250: } 251: 252: /** 253: * Returns the index of the column in the related {@link TableModel} that 254: * this <code>TableColumn</code> maps to. 255: * 256: * @return the model index 257: */ 258: public int getModelIndex() 259: { 260: return modelIndex; 261: } 262: 263: /** 264: * Sets the identifier for the column. 265: * 266: * @param identifier the identifier 267: */ 268: public void setIdentifier(Object identifier) 269: { 270: this.identifier = identifier; 271: } 272: 273: /** 274: * Returns the identifier for the column, or {@link #getHeaderValue()} if the 275: * identifier is <code>null</code>. 276: * 277: * @return The identifier (or {@link #getHeaderValue()} if the identifier is 278: * <code>null</code>). 279: */ 280: public Object getIdentifier() 281: { 282: if (identifier == null) 283: return getHeaderValue(); 284: return identifier; 285: } 286: 287: /** 288: * Sets the header value and sends a {@link PropertyChangeEvent} to all 289: * registered listeners. The header value property uses the name 290: * {@link #HEADER_VALUE_PROPERTY}. 291: * 292: * @param headerValue the value of the header 293: */ 294: public void setHeaderValue(Object headerValue) 295: { 296: if (this.headerValue == headerValue) 297: return; 298: 299: Object oldValue = this.headerValue; 300: this.headerValue = headerValue; 301: firePropertyChange(HEADER_VALUE_PROPERTY, oldValue, headerValue); 302: } 303: 304: /** 305: * Returns the header value. 306: * 307: * @return the value of the header 308: */ 309: public Object getHeaderValue() 310: { 311: return headerValue; 312: } 313: 314: /** 315: * setHeaderRenderer 316: * 317: * @param renderer the renderer to use 318: */ 319: public void setHeaderRenderer(TableCellRenderer renderer) 320: { 321: if (headerRenderer == renderer) 322: return; 323: 324: TableCellRenderer oldRenderer = headerRenderer; 325: headerRenderer = renderer; 326: firePropertyChange(HEADER_RENDERER_PROPERTY, 327: oldRenderer, headerRenderer); 328: } 329: 330: /** 331: * getHeaderRenderer 332: * @return TableCellRenderer 333: */ 334: public TableCellRenderer getHeaderRenderer() 335: { 336: return headerRenderer; 337: } 338: 339: /** 340: * Sets the renderer for cells in this column and sends a 341: * {@link PropertyChangeEvent} to all registered listeners. 342: * 343: * @param renderer the cell renderer (<code>null</code> permitted). 344: */ 345: public void setCellRenderer(TableCellRenderer renderer) 346: { 347: if (cellRenderer == renderer) 348: return; 349: 350: TableCellRenderer oldRenderer = cellRenderer; 351: cellRenderer = renderer; 352: firePropertyChange(CELL_RENDERER_PROPERTY, 353: oldRenderer, cellRenderer); 354: } 355: 356: /** 357: * Returns the renderer for the table cells in this column. 358: * 359: * @return The cell renderer. 360: */ 361: public TableCellRenderer getCellRenderer() 362: { 363: return cellRenderer; 364: } 365: 366: /** 367: * setCellEditor 368: * 369: * @param cellEditor the cell editor 370: */ 371: public void setCellEditor(TableCellEditor cellEditor) 372: { 373: this.cellEditor = cellEditor; 374: } 375: 376: /** 377: * getCellEditor 378: * 379: * @return the cell editor 380: */ 381: public TableCellEditor getCellEditor() 382: { 383: return cellEditor; 384: } 385: 386: /** 387: * setWidth 388: * 389: * @param newWidth the width 390: */ 391: public void setWidth(int newWidth) 392: { 393: int oldWidth = width; 394: 395: if (newWidth < minWidth) 396: width = minWidth; 397: else if (newWidth > maxWidth) 398: width = maxWidth; 399: else 400: width = newWidth; 401: 402: if (width == oldWidth) 403: return; 404: 405: firePropertyChange(COLUMN_WIDTH_PROPERTY, oldWidth, width); 406: } 407: 408: /** 409: * getWidth 410: * 411: * @return int 412: */ 413: public int getWidth() 414: { 415: return width; 416: } 417: 418: /** 419: * setPreferredWidth 420: * 421: * @param preferredWidth the preferred width 422: */ 423: public void setPreferredWidth(int preferredWidth) 424: { 425: if (preferredWidth < minWidth) 426: this.preferredWidth = minWidth; 427: else if (preferredWidth > maxWidth) 428: this.preferredWidth = maxWidth; 429: else 430: this.preferredWidth = preferredWidth; 431: } 432: 433: /** 434: * getPreferredWidth 435: * 436: * @return the preferred width 437: */ 438: public int getPreferredWidth() 439: { 440: return preferredWidth; 441: } 442: 443: /** 444: * Sets the minimum width for the column and, if necessary, updates the 445: * <code>width</code> and <code>preferredWidth</code>. 446: * 447: * @param minWidth the minimum width 448: */ 449: public void setMinWidth(int minWidth) 450: { 451: this.minWidth = minWidth; 452: setWidth(getWidth()); 453: setPreferredWidth(getPreferredWidth()); 454: } 455: 456: /** 457: * Returns the <code>TableColumn</code>'s minimum width. 458: * 459: * @return The minimum width. 460: */ 461: public int getMinWidth() 462: { 463: return minWidth; 464: } 465: 466: /** 467: * Sets the maximum width and, if necessary, updates the <code>width</code> 468: * and <code>preferredWidth</code>. 469: * 470: * @param maxWidth the maximum width 471: */ 472: public void setMaxWidth(int maxWidth) 473: { 474: this.maxWidth = maxWidth; 475: setWidth(getWidth()); 476: setPreferredWidth(getPreferredWidth()); 477: } 478: 479: /** 480: * Returns the maximum width. 481: * 482: * @return The maximum width. 483: */ 484: public int getMaxWidth() 485: { 486: return maxWidth; 487: } 488: 489: /** 490: * setResizable 491: * 492: * @param isResizable <code>true</code> if this column is resizable, 493: * <code>false</code> otherwise 494: */ 495: public void setResizable(boolean isResizable) 496: { 497: this.isResizable = isResizable; 498: } 499: 500: /** 501: * getResizable 502: * 503: * @return <code>true</code> if this column is resizable, 504: * <code>false</code> otherwise 505: */ 506: public boolean getResizable() 507: { 508: return isResizable; 509: } 510: 511: /** 512: * sizeWidthToFit 513: */ 514: public void sizeWidthToFit() 515: { 516: // TODO 517: } 518: 519: /** 520: * This method is empty, unused and deprecated. 521: * @deprecated 1.3 522: */ 523: public void disableResizedPosting() 524: { 525: // Does nothing 526: } 527: 528: /** 529: * This method is empty, unused and deprecated. 530: * @deprecated 1.3 531: */ 532: public void enableResizedPosting() 533: { 534: // Does nothing 535: } 536: 537: /** 538: * Adds a property change listener. 539: * 540: * @param listener the listener to add 541: */ 542: public synchronized void addPropertyChangeListener(PropertyChangeListener listener) 543: { 544: changeSupport.addPropertyChangeListener(listener); 545: } 546: 547: /** 548: * removePropertyChangeListener 549: * @param listener the listener to remove 550: */ 551: public synchronized void removePropertyChangeListener(PropertyChangeListener listener) 552: { 553: changeSupport.removePropertyChangeListener(listener); 554: } 555: 556: /** 557: * Returns the property change listeners for this <code>TableColumn</code>. 558: * @since 1.4 559: */ 560: public PropertyChangeListener[] getPropertyChangeListeners() 561: { 562: return changeSupport.getPropertyChangeListeners(); 563: } 564: 565: /** 566: * createDefaultHeaderRenderer 567: * @return TableCellRenderer 568: */ 569: protected TableCellRenderer createDefaultHeaderRenderer() 570: { 571: return new DefaultTableCellRenderer(); 572: } 573: }
GNU Classpath (0.18) |