Source for javax.swing.table.TableColumn

   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:     // We do have a constant field COLUMN_WIDTH_PROPERTY,
 406:     // however, tests show that the actual fired property name is 'width'
 407:     // and even Sun's API docs say that this constant field is obsolete and
 408:     // not used.
 409:     firePropertyChange("width", oldWidth, width);
 410:   }
 411: 
 412:   /**
 413:    * getWidth
 414:    * 
 415:    * @return int
 416:    */
 417:   public int getWidth()
 418:   {
 419:     return width;
 420:   }
 421: 
 422:   /**
 423:    * setPreferredWidth
 424:    * 
 425:    * @param preferredWidth the preferred width
 426:    */
 427:   public void setPreferredWidth(int preferredWidth)
 428:   {
 429:     int oldPrefWidth = this.preferredWidth;
 430: 
 431:     if (preferredWidth < minWidth)
 432:       this.preferredWidth = minWidth;
 433:     else if (preferredWidth > maxWidth)
 434:       this.preferredWidth = maxWidth;
 435:     else
 436:       this.preferredWidth = preferredWidth;
 437: 
 438:     firePropertyChange("preferredWidth", oldPrefWidth, this.preferredWidth);
 439:   }
 440: 
 441:   /**
 442:    * getPreferredWidth
 443:    * 
 444:    * @return the preferred width
 445:    */
 446:   public int getPreferredWidth()
 447:   {
 448:     return preferredWidth;
 449:   }
 450: 
 451:   /**
 452:    * Sets the minimum width for the column and, if necessary, updates the
 453:    * <code>width</code> and <code>preferredWidth</code>.
 454:    * 
 455:    * @param minWidth the minimum width
 456:    */
 457:   public void setMinWidth(int minWidth)
 458:   {
 459:     this.minWidth = minWidth;
 460:     setWidth(getWidth());
 461:     setPreferredWidth(getPreferredWidth());
 462:   }
 463: 
 464:   /**
 465:    * Returns the <code>TableColumn</code>'s minimum width.
 466:    * 
 467:    * @return The minimum width.
 468:    */
 469:   public int getMinWidth()
 470:   {
 471:     return minWidth;
 472:   }
 473: 
 474:   /**
 475:    * Sets the maximum width and, if necessary, updates the <code>width</code>
 476:    * and <code>preferredWidth</code>.
 477:    * 
 478:    * @param maxWidth the maximum width
 479:    */
 480:   public void setMaxWidth(int maxWidth)
 481:   {
 482:     this.maxWidth = maxWidth;
 483:     setWidth(getWidth());
 484:     setPreferredWidth(getPreferredWidth());
 485:   }
 486: 
 487:   /**
 488:    * Returns the maximum width.
 489:    * 
 490:    * @return The maximum width.
 491:    */
 492:   public int getMaxWidth()
 493:   {
 494:     return maxWidth;
 495:   }
 496: 
 497:   /**
 498:    * setResizable
 499:    * 
 500:    * @param isResizable <code>true</code> if this column is resizable,
 501:    * <code>false</code> otherwise
 502:    */
 503:   public void setResizable(boolean isResizable)
 504:   {
 505:     this.isResizable = isResizable;
 506:   }
 507: 
 508:   /**
 509:    * getResizable
 510:    * 
 511:    * @return <code>true</code> if this column is resizable,
 512:    * <code>false</code> otherwise
 513:    */
 514:   public boolean getResizable()
 515:   {
 516:     return isResizable;
 517:   }
 518: 
 519:   /**
 520:    * sizeWidthToFit
 521:    */
 522:   public void sizeWidthToFit()
 523:   {
 524:     // TODO
 525:   }
 526: 
 527:   /**
 528:    * This method is empty, unused and deprecated.
 529:    * @deprecated 1.3
 530:    */
 531:   public void disableResizedPosting()
 532:   {
 533:     // Does nothing
 534:   }
 535: 
 536:   /**
 537:    * This method is empty, unused and deprecated.
 538:    * @deprecated 1.3
 539:    */
 540:   public void enableResizedPosting()
 541:   {
 542:     // Does nothing
 543:   }
 544: 
 545:   /**
 546:    * Adds a property change listener.
 547:    * 
 548:    * @param listener the listener to add
 549:    */
 550:   public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
 551:   {
 552:     changeSupport.addPropertyChangeListener(listener);
 553:   }
 554: 
 555:   /**
 556:    * removePropertyChangeListener
 557:    * @param listener the listener to remove
 558:    */
 559:   public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
 560:   {
 561:     changeSupport.removePropertyChangeListener(listener);
 562:   }
 563: 
 564:   /**
 565:    * Returns the property change listeners for this <code>TableColumn</code>.
 566:    * @since 1.4
 567:    */
 568:   public PropertyChangeListener[] getPropertyChangeListeners()
 569:   {
 570:     return changeSupport.getPropertyChangeListeners();
 571:   }
 572: 
 573:   /**
 574:    * createDefaultHeaderRenderer
 575:    * @return TableCellRenderer
 576:    */
 577:   protected TableCellRenderer createDefaultHeaderRenderer()
 578:   {
 579:     return new DefaultTableCellRenderer();
 580:   }
 581: }