001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.table;
022:
023: import java.awt.Component;
024: import java.beans.PropertyChangeListener;
025: import java.io.Serializable;
026:
027: import javax.swing.JComponent;
028: import javax.swing.JTable;
029: import javax.swing.UIManager;
030: import javax.swing.border.Border;
031: import javax.swing.event.SwingPropertyChangeSupport;
032:
033: public class TableColumn implements Serializable {
034: public static final String COLUMN_WIDTH_PROPERTY = "columWidth";
035: public static final String HEADER_VALUE_PROPERTY = "headerValue";
036: public static final String HEADER_RENDERER_PROPERTY = "headerRenderer";
037: public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
038:
039: static final String WIDTH_PROPERTY = "width";
040: static final String PREFERRED_WIDTH_PROPERTY = "preferredWidth";
041: private static final String MODEL_INDEX_PROPERTY = "modelIndex";
042: private static final String IDENTIFIER_PROPERTY = "identifier";
043: private static final String CELL_EDITOR_PROPERTY = "cellEditor";
044: private static final String MIN_WIDTH_PROPERTY = "minWidth";
045: private static final String MAX_WIDTH_PROPERTY = "maxWidth";
046: private static final String IS_RESIZABLE_PROPERTY = "isResizable";
047:
048: protected int modelIndex;
049: protected Object identifier;
050: protected int width;
051: protected int minWidth = 15;
052: protected int maxWidth = Integer.MAX_VALUE;
053: protected TableCellRenderer headerRenderer;
054: protected TableCellRenderer cellRenderer;
055: protected TableCellEditor cellEditor;
056: protected Object headerValue;
057: protected boolean isResizable = true;
058: /**
059: * @deprecated
060: */
061: protected transient int resizedPostingDisableCount;
062:
063: private int preferredWidth;
064: private SwingPropertyChangeSupport propertyChangeSupport = new SwingPropertyChangeSupport(
065: this );
066:
067: public TableColumn() {
068: this (0, 75, null, null);
069: }
070:
071: public TableColumn(final int modelIndex) {
072: this (modelIndex, 75, null, null);
073: }
074:
075: public TableColumn(final int modelIndex, final int width) {
076: this (modelIndex, width, null, null);
077: }
078:
079: public TableColumn(final int modelIndex, final int width,
080: final TableCellRenderer cellRenderer,
081: final TableCellEditor cellEditor) {
082: this .modelIndex = modelIndex;
083: this .width = width;
084: this .preferredWidth = width;
085: this .cellRenderer = cellRenderer;
086: this .cellEditor = cellEditor;
087: }
088:
089: public void setModelIndex(final int modelIndex) {
090: int oldValue = this .modelIndex;
091: this .modelIndex = modelIndex;
092: propertyChangeSupport.firePropertyChange(MODEL_INDEX_PROPERTY,
093: oldValue, modelIndex);
094: }
095:
096: public int getModelIndex() {
097: return modelIndex;
098: }
099:
100: public void setIdentifier(final Object identifier) {
101: Object oldValue = this .identifier;
102: this .identifier = identifier;
103: propertyChangeSupport.firePropertyChange(IDENTIFIER_PROPERTY,
104: oldValue, identifier);
105: }
106:
107: public Object getIdentifier() {
108: return identifier != null ? identifier : getHeaderValue();
109: }
110:
111: public void setHeaderValue(final Object headerValue) {
112: Object oldValue = this .headerValue;
113: this .headerValue = headerValue;
114: propertyChangeSupport.firePropertyChange(HEADER_VALUE_PROPERTY,
115: oldValue, headerValue);
116: }
117:
118: public Object getHeaderValue() {
119: return headerValue;
120: }
121:
122: public void setHeaderRenderer(final TableCellRenderer headerRenderer) {
123: Object oldValue = this .headerRenderer;
124: this .headerRenderer = headerRenderer;
125: propertyChangeSupport.firePropertyChange(
126: HEADER_RENDERER_PROPERTY, oldValue, headerRenderer);
127: }
128:
129: public TableCellRenderer getHeaderRenderer() {
130: return headerRenderer;
131: }
132:
133: public void setCellRenderer(final TableCellRenderer cellRenderer) {
134: Object oldValue = this .cellRenderer;
135: this .cellRenderer = cellRenderer;
136: propertyChangeSupport.firePropertyChange(
137: CELL_RENDERER_PROPERTY, oldValue, cellRenderer);
138: }
139:
140: public TableCellRenderer getCellRenderer() {
141: return cellRenderer;
142: }
143:
144: public void setCellEditor(final TableCellEditor cellEditor) {
145: Object oldValue = this .cellEditor;
146: this .cellEditor = cellEditor;
147: propertyChangeSupport.firePropertyChange(CELL_EDITOR_PROPERTY,
148: oldValue, cellEditor);
149: }
150:
151: public TableCellEditor getCellEditor() {
152: return cellEditor;
153: }
154:
155: public void setWidth(final int width) {
156: int oldValue = this .width;
157: this .width = width;
158: adjustWidths();
159:
160: propertyChangeSupport.firePropertyChange(WIDTH_PROPERTY,
161: oldValue, this .width);
162: }
163:
164: public int getWidth() {
165: return width;
166: }
167:
168: public void setPreferredWidth(final int preferredWidth) {
169: int oldValue = this .preferredWidth;
170: this .preferredWidth = preferredWidth;
171: adjustWidths();
172:
173: propertyChangeSupport
174: .firePropertyChange(PREFERRED_WIDTH_PROPERTY, oldValue,
175: this .preferredWidth);
176: }
177:
178: public int getPreferredWidth() {
179: return preferredWidth;
180: }
181:
182: public void setMinWidth(final int minWidth) {
183: int oldValue = this .minWidth;
184: this .minWidth = minWidth;
185: adjustWidths();
186:
187: propertyChangeSupport.firePropertyChange(MIN_WIDTH_PROPERTY,
188: oldValue, this .minWidth);
189: }
190:
191: public int getMinWidth() {
192: return minWidth;
193: }
194:
195: public void setMaxWidth(final int maxWidth) {
196: int oldValue = this .maxWidth;
197: this .maxWidth = maxWidth;
198: adjustWidths();
199:
200: propertyChangeSupport.firePropertyChange(MAX_WIDTH_PROPERTY,
201: oldValue, this .maxWidth);
202: }
203:
204: public int getMaxWidth() {
205: return maxWidth;
206: }
207:
208: public void setResizable(final boolean isResizable) {
209: boolean oldValue = this .isResizable;
210: this .isResizable = isResizable;
211:
212: propertyChangeSupport.firePropertyChange(IS_RESIZABLE_PROPERTY,
213: oldValue, isResizable);
214: }
215:
216: public boolean getResizable() {
217: return isResizable;
218: }
219:
220: public void sizeWidthToFit() {
221: if (headerRenderer != null) {
222: Component renderingComponent = headerRenderer
223: .getTableCellRendererComponent(null,
224: getHeaderValue(), false, false, 0, 0);
225: maxWidth = renderingComponent.getMaximumSize().width;
226: minWidth = renderingComponent.getMinimumSize().width;
227: preferredWidth = renderingComponent.getPreferredSize().width;
228: width = preferredWidth;
229: }
230: }
231:
232: public void addPropertyChangeListener(
233: final PropertyChangeListener listener) {
234: propertyChangeSupport.addPropertyChangeListener(listener);
235: }
236:
237: public void removePropertyChangeListener(
238: final PropertyChangeListener listener) {
239: propertyChangeSupport.removePropertyChangeListener(listener);
240: }
241:
242: public PropertyChangeListener[] getPropertyChangeListeners() {
243: return propertyChangeSupport.getPropertyChangeListeners();
244: }
245:
246: /**
247: * @deprecated
248: */
249: public void disableResizedPosting() {
250: resizedPostingDisableCount++;
251: }
252:
253: /**
254: * @deprecated
255: */
256: public void enableResizedPosting() {
257: resizedPostingDisableCount--;
258: }
259:
260: protected TableCellRenderer createDefaultHeaderRenderer() {
261: return new DefaultTableCellRenderer() {
262: private JTable defaultTable;
263: private Border cellBorder = UIManager
264: .getBorder("TableHeader.cellBorder");
265:
266: public Component getTableCellRendererComponent(
267: final JTable table, final Object value,
268: final boolean isSelected, final boolean hasFocus,
269: final int row, final int column) {
270: JComponent result = (JComponent) super
271: .getTableCellRendererComponent(
272: table != null ? table
273: : getDefaultTable(), value,
274: isSelected, hasFocus, row, column);
275: result.setBorder(cellBorder);
276:
277: return result;
278: }
279:
280: private JTable getDefaultTable() {
281: if (defaultTable == null) {
282: defaultTable = new JTable();
283: }
284:
285: return defaultTable;
286: }
287: };
288: }
289:
290: private void adjustWidths() {
291: if (width < minWidth) {
292: width = minWidth;
293: }
294: if (width > maxWidth) {
295: width = maxWidth;
296: }
297:
298: if (preferredWidth < minWidth) {
299: preferredWidth = minWidth;
300: }
301: if (preferredWidth > maxWidth) {
302: preferredWidth = maxWidth;
303: }
304: }
305: }
|