2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax
.swing
.table
;
41 import java
.beans
.PropertyChangeEvent
;
42 import java
.beans
.PropertyChangeListener
;
43 import java
.io
.Serializable
;
45 import javax
.swing
.event
.SwingPropertyChangeSupport
;
48 * Represents the attributes of a column in a table, including the column index,
49 * width, minimum width, preferred width and maximum width.
51 * @author Andrew Selkirk
54 public class TableColumn
55 implements Serializable
57 static final long serialVersionUID
= -6113660025878112608L;
60 * The name for the <code>columnWidth</code> property. Note that the typo
61 * in the name value is deliberate, to match the specification.
63 public static final String COLUMN_WIDTH_PROPERTY
= "columWidth";
66 * The name for the <code>headerValue</code> property.
68 public static final String HEADER_VALUE_PROPERTY
= "headerValue";
71 * The name for the <code>headerRenderer</code> property.
73 public static final String HEADER_RENDERER_PROPERTY
= "headerRenderer";
76 * The name for the <code>cellRenderer</code> property.
78 public static final String CELL_RENDERER_PROPERTY
= "cellRenderer";
81 * The index of the corresponding column in the table model.
83 protected int modelIndex
;
86 * The identifier for the column.
88 protected Object identifier
;
98 protected int minWidth
= 15;
101 * The preferred width.
103 private int preferredWidth
;
108 protected int maxWidth
= Integer
.MAX_VALUE
;
113 protected TableCellRenderer headerRenderer
;
118 protected Object headerValue
;
123 protected TableCellRenderer cellRenderer
;
128 protected TableCellEditor cellEditor
;
133 protected boolean isResizable
= true;
136 * resizedPostingDisableCount
140 protected transient int resizedPostingDisableCount
;
145 private SwingPropertyChangeSupport changeSupport
=
146 new SwingPropertyChangeSupport(this);
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.
154 this(0, 75, null, null);
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.
161 * @param modelIndex the index of the column in the model
163 public TableColumn(int modelIndex
)
165 this(modelIndex
, 75, null, null);
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>.
172 * @param modelIndex the index of the column in the model
173 * @param width the width
175 public TableColumn(int modelIndex
, int width
)
177 this(modelIndex
, width
, null, null);
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>.
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).
190 public TableColumn(int modelIndex
, int width
,
191 TableCellRenderer cellRenderer
, TableCellEditor cellEditor
)
193 this.modelIndex
= modelIndex
;
195 this.preferredWidth
= width
;
196 this.cellRenderer
= cellRenderer
;
197 this.cellEditor
= cellEditor
;
198 this.headerValue
= null;
199 this.identifier
= null;
205 * @param property the name of the property
206 * @param oldValue the old value
207 * @param newValue the new value
209 private void firePropertyChange(String property
, Object oldValue
,
212 changeSupport
.firePropertyChange(property
, oldValue
, newValue
);
218 * @param property the name of the property
219 * @param oldValue the old value
220 * @param newValue the new value
222 private void firePropertyChange(String property
, int oldValue
, int newValue
)
224 firePropertyChange(property
, new Integer(oldValue
), new Integer(newValue
));
230 * @param property the name of the property
231 * @param oldValue the old value
232 * @param newValue the new value
234 private void firePropertyChange(String property
, boolean oldValue
,
237 firePropertyChange(property
, Boolean
.valueOf(oldValue
),
238 Boolean
.valueOf(newValue
));
242 * Sets the index of the column in the related {@link TableModel} that this
243 * <code>TableColumn</code> maps to.
245 * @param modelIndex the column index in the model.
247 public void setModelIndex(int modelIndex
)
249 this.modelIndex
= modelIndex
;
253 * Returns the index of the column in the related {@link TableModel} that
254 * this <code>TableColumn</code> maps to.
256 * @return the model index
258 public int getModelIndex()
264 * Sets the identifier for the column.
266 * @param identifier the identifier
268 public void setIdentifier(Object identifier
)
270 this.identifier
= identifier
;
274 * Returns the identifier for the column, or {@link #getHeaderValue()} if the
275 * identifier is <code>null</code>.
277 * @return The identifier (or {@link #getHeaderValue()} if the identifier is
278 * <code>null</code>).
280 public Object
getIdentifier()
282 if (identifier
== null)
283 return getHeaderValue();
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}.
292 * @param headerValue the value of the header
294 public void setHeaderValue(Object headerValue
)
296 if (this.headerValue
== headerValue
)
299 Object oldValue
= this.headerValue
;
300 this.headerValue
= headerValue
;
301 firePropertyChange(HEADER_VALUE_PROPERTY
, oldValue
, headerValue
);
305 * Returns the header value.
307 * @return the value of the header
309 public Object
getHeaderValue()
317 * @param renderer the renderer to use
319 public void setHeaderRenderer(TableCellRenderer renderer
)
321 if (headerRenderer
== renderer
)
324 TableCellRenderer oldRenderer
= headerRenderer
;
325 headerRenderer
= renderer
;
326 firePropertyChange(HEADER_RENDERER_PROPERTY
,
327 oldRenderer
, headerRenderer
);
332 * @return TableCellRenderer
334 public TableCellRenderer
getHeaderRenderer()
336 return headerRenderer
;
340 * Sets the renderer for cells in this column and sends a
341 * {@link PropertyChangeEvent} to all registered listeners.
343 * @param renderer the cell renderer (<code>null</code> permitted).
345 public void setCellRenderer(TableCellRenderer renderer
)
347 if (cellRenderer
== renderer
)
350 TableCellRenderer oldRenderer
= cellRenderer
;
351 cellRenderer
= renderer
;
352 firePropertyChange(CELL_RENDERER_PROPERTY
,
353 oldRenderer
, cellRenderer
);
357 * Returns the renderer for the table cells in this column.
359 * @return The cell renderer.
361 public TableCellRenderer
getCellRenderer()
369 * @param cellEditor the cell editor
371 public void setCellEditor(TableCellEditor cellEditor
)
373 this.cellEditor
= cellEditor
;
379 * @return the cell editor
381 public TableCellEditor
getCellEditor()
389 * @param newWidth the width
391 public void setWidth(int newWidth
)
393 int oldWidth
= width
;
395 if (newWidth
< minWidth
)
397 else if (newWidth
> maxWidth
)
402 if (width
== oldWidth
)
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
409 firePropertyChange("width", oldWidth
, width
);
417 public int getWidth()
425 * @param preferredWidth the preferred width
427 public void setPreferredWidth(int preferredWidth
)
429 int oldPrefWidth
= this.preferredWidth
;
431 if (preferredWidth
< minWidth
)
432 this.preferredWidth
= minWidth
;
433 else if (preferredWidth
> maxWidth
)
434 this.preferredWidth
= maxWidth
;
436 this.preferredWidth
= preferredWidth
;
438 firePropertyChange("preferredWidth", oldPrefWidth
, this.preferredWidth
);
444 * @return the preferred width
446 public int getPreferredWidth()
448 return preferredWidth
;
452 * Sets the minimum width for the column and, if necessary, updates the
453 * <code>width</code> and <code>preferredWidth</code>.
455 * @param minWidth the minimum width
457 public void setMinWidth(int minWidth
)
459 this.minWidth
= minWidth
;
460 setWidth(getWidth());
461 setPreferredWidth(getPreferredWidth());
465 * Returns the <code>TableColumn</code>'s minimum width.
467 * @return The minimum width.
469 public int getMinWidth()
475 * Sets the maximum width and, if necessary, updates the <code>width</code>
476 * and <code>preferredWidth</code>.
478 * @param maxWidth the maximum width
480 public void setMaxWidth(int maxWidth
)
482 this.maxWidth
= maxWidth
;
483 setWidth(getWidth());
484 setPreferredWidth(getPreferredWidth());
488 * Returns the maximum width.
490 * @return The maximum width.
492 public int getMaxWidth()
500 * @param isResizable <code>true</code> if this column is resizable,
501 * <code>false</code> otherwise
503 public void setResizable(boolean isResizable
)
505 this.isResizable
= isResizable
;
511 * @return <code>true</code> if this column is resizable,
512 * <code>false</code> otherwise
514 public boolean getResizable()
522 public void sizeWidthToFit()
528 * This method is empty, unused and deprecated.
531 public void disableResizedPosting()
537 * This method is empty, unused and deprecated.
540 public void enableResizedPosting()
546 * Adds a property change listener.
548 * @param listener the listener to add
550 public synchronized void addPropertyChangeListener(PropertyChangeListener listener
)
552 changeSupport
.addPropertyChangeListener(listener
);
556 * removePropertyChangeListener
557 * @param listener the listener to remove
559 public synchronized void removePropertyChangeListener(PropertyChangeListener listener
)
561 changeSupport
.removePropertyChangeListener(listener
);
565 * Returns the property change listeners for this <code>TableColumn</code>.
568 public PropertyChangeListener
[] getPropertyChangeListeners()
570 return changeSupport
.getPropertyChangeListeners();
574 * createDefaultHeaderRenderer
575 * @return TableCellRenderer
577 protected TableCellRenderer
createDefaultHeaderRenderer()
579 return new DefaultTableCellRenderer();