39 |
package javax.swing; |
package javax.swing; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
42 |
|
import java.awt.Component; |
43 |
import java.awt.Dimension; |
import java.awt.Dimension; |
44 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
45 |
import java.util.Hashtable; |
import java.util.Hashtable; |
46 |
import java.util.Vector; |
import java.util.Vector; |
47 |
|
|
48 |
import javax.accessibility.Accessible; |
import javax.accessibility.Accessible; |
49 |
|
import javax.accessibility.AccessibleContext; |
50 |
import javax.swing.event.CellEditorListener; |
import javax.swing.event.CellEditorListener; |
51 |
import javax.swing.event.ChangeEvent; |
import javax.swing.event.ChangeEvent; |
52 |
import javax.swing.event.ListSelectionEvent; |
import javax.swing.event.ListSelectionEvent; |
55 |
import javax.swing.event.TableColumnModelListener; |
import javax.swing.event.TableColumnModelListener; |
56 |
import javax.swing.event.TableModelEvent; |
import javax.swing.event.TableModelEvent; |
57 |
import javax.swing.event.TableModelListener; |
import javax.swing.event.TableModelListener; |
58 |
|
import javax.swing.plaf.TableUI; |
59 |
import javax.swing.table.DefaultTableColumnModel; |
import javax.swing.table.DefaultTableColumnModel; |
60 |
|
import javax.swing.table.DefaultTableCellRenderer; |
61 |
import javax.swing.table.DefaultTableModel; |
import javax.swing.table.DefaultTableModel; |
62 |
import javax.swing.table.JTableHeader; |
import javax.swing.table.JTableHeader; |
63 |
import javax.swing.table.TableCellEditor; |
import javax.swing.table.TableCellEditor; |
64 |
import javax.swing.table.TableCellRenderer; |
import javax.swing.table.TableCellRenderer; |
65 |
|
import javax.swing.table.TableColumn; |
66 |
import javax.swing.table.TableColumnModel; |
import javax.swing.table.TableColumnModel; |
67 |
import javax.swing.table.TableModel; |
import javax.swing.table.TableModel; |
68 |
|
|
72 |
{ |
{ |
73 |
private static final long serialVersionUID = 3876025080382781659L; |
private static final long serialVersionUID = 3876025080382781659L; |
74 |
|
|
75 |
public static final int AUTO_RESIZE_ALL_COLUMNS = 4; |
|
76 |
public static final int AUTO_RESIZE_LAST_COLUMN = 3; |
/** |
77 |
public static final int AUTO_RESIZE_NEXT_COLUMN = 1; |
* When resizing columns, do not automatically change any columns. In this |
78 |
|
* case the table should be enclosed in a {@link JScrollPane} in order to |
79 |
|
* accomodate cases in which the table size exceeds its visible area. |
80 |
|
*/ |
81 |
public static final int AUTO_RESIZE_OFF = 0; |
public static final int AUTO_RESIZE_OFF = 0; |
82 |
|
|
83 |
|
/** |
84 |
|
* When resizing column <code>i</code>, automatically change only the |
85 |
|
* single column <code>i+1</code> to provide or absorb excess space |
86 |
|
* requirements. |
87 |
|
*/ |
88 |
|
public static final int AUTO_RESIZE_NEXT_COLUMN = 1; |
89 |
|
|
90 |
|
/** |
91 |
|
* When resizing column <code>i</code> in a table of <code>n</code> |
92 |
|
* columns, automatically change all columns in the range <code>[i+1, |
93 |
|
* n)</code>, uniformly, to provide or absorb excess space requirements. |
94 |
|
*/ |
95 |
public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2; |
public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2; |
96 |
|
|
97 |
protected boolean autoCreateColumnsFromModel; |
/** |
98 |
protected int autoResizeMode; |
* When resizing column <code>i</code> in a table of <code>n</code> |
99 |
protected TableCellEditor cellEditor; |
* columns, automatically change all columns in the range <code>[0, |
100 |
protected boolean cellSelectionEnabled; |
* n)</code> (with the exception of column i) uniformly, to provide or |
101 |
protected TableColumnModel columnModel; |
* absorb excess space requirements. |
102 |
protected TableModel dataModel; |
*/ |
103 |
|
public static final int AUTO_RESIZE_ALL_COLUMNS = 4; |
104 |
|
|
105 |
|
/** |
106 |
|
* When resizing column <code>i</code> in a table of <code>n</code> |
107 |
|
* columns, automatically change column <code>n-1</code> (the last column |
108 |
|
* in the table) to provide or absorb excess space requirements. |
109 |
|
*/ |
110 |
|
public static final int AUTO_RESIZE_LAST_COLUMN = 3; |
111 |
|
|
112 |
|
|
113 |
|
/** |
114 |
|
* A table mapping {@link java.lang.Class} objects to |
115 |
|
* {@link TableCellEditor} objects. This table is consulted by the |
116 |
|
* |
117 |
|
*/ |
118 |
protected Hashtable defaultEditorsByColumnClass; |
protected Hashtable defaultEditorsByColumnClass; |
119 |
protected Hashtable defaultRenderersByColumnClass; |
protected Hashtable defaultRenderersByColumnClass; |
120 |
protected int editingColumn; |
protected int editingColumn; |
121 |
protected int editingRow; |
protected int editingRow; |
122 |
protected Color gridColor; |
|
123 |
protected Dimension preferredViewportSize; |
/** |
124 |
|
* Whether or not the table should automatically compute a matching |
125 |
|
* {@link TableColumnModel} and assign it to the {@link #columnModel} |
126 |
|
* property when the {@link #dataModel} property is changed. |
127 |
|
* |
128 |
|
* @see #setModel() |
129 |
|
* @see #createColumnsFromModel() |
130 |
|
* @see #setColumnModel() |
131 |
|
* @see #setAutoCreateColumnsFromModel() |
132 |
|
* @see #getAutoCreateColumnsFromModel() |
133 |
|
*/ |
134 |
|
protected boolean autoCreateColumnsFromModel; |
135 |
|
|
136 |
|
/** |
137 |
|
* A numeric code specifying the resizing behavior of the table. Must be |
138 |
|
* one of {@link #AUTO_RESIZE_ALL_COLUMNS} (the default), {@link |
139 |
|
* #AUTO_RESIZE_LAST_COLUMN}, {@link #AUTO_RESIZE_NEXT_COLUMN}, {@link |
140 |
|
* #AUTO_RESIZE_SUBSEQUENT_COLUMNS}, or {@link #AUTO_RESIZE_OFF}. |
141 |
|
* |
142 |
|
* @see #doLayout() |
143 |
|
* @see #setAutoResizeMode() |
144 |
|
* @see #getAutoResizeMode() |
145 |
|
*/ |
146 |
|
protected int autoResizeMode; |
147 |
|
|
148 |
|
/** |
149 |
|
* The height in pixels of any row of the table. All rows in a table are |
150 |
|
* of uniform height. This differs from column width, which varies on a |
151 |
|
* per-column basis, and is stored in the individual columns of the |
152 |
|
* {@link #columnModel}. |
153 |
|
* |
154 |
|
* @see #getRowHeight() |
155 |
|
* @see #setRowHeight() |
156 |
|
* @see TableColumn#getWidth() |
157 |
|
* @see TableColumn#setWidth() |
158 |
|
*/ |
159 |
protected int rowHeight; |
protected int rowHeight; |
160 |
|
|
161 |
|
/** |
162 |
|
* The height in pixels of the gap left between any two rows of the table. |
163 |
|
* |
164 |
|
* @see #setRowMargin() |
165 |
|
* @see #getRowHeight() |
166 |
|
* @see #getInterCellSpacing() |
167 |
|
* @see #setInterCellSpacing() |
168 |
|
* @see TableColumnModel#getColumnMargin() |
169 |
|
* @see TableColumnModel#setColumnMargin() |
170 |
|
*/ |
171 |
protected int rowMargin; |
protected int rowMargin; |
172 |
|
|
173 |
|
/** |
174 |
|
* Whether or not the table should allow row selection. If the table |
175 |
|
* allows both row <em>and</em> column selection, it is said to allow |
176 |
|
* "cell selection". Previous versions of the JDK supported cell |
177 |
|
* selection as an independent concept, but it is now represented solely |
178 |
|
* in terms of simultaneous row and column selection. |
179 |
|
* |
180 |
|
* @see TableColumnModel#columnSelectionAllowed() |
181 |
|
* @see #setRowSelectionAllowed() |
182 |
|
* @see #getRowSelectionAllowed() |
183 |
|
* @see #getCellSelectionEnabled() |
184 |
|
* @see #setCellSelectionEnabled() |
185 |
|
*/ |
186 |
protected boolean rowSelectionAllowed; |
protected boolean rowSelectionAllowed; |
187 |
protected Color selectionBackground; |
|
188 |
protected Color selectionForeground; |
/** |
189 |
|
* @deprecated Use {@link #rowSelectionAllowed}, {@link |
190 |
|
* #columnSelectionAllowed}, or the combined methods {@link |
191 |
|
* getCellSelectionEnabled} and {@link setCellSelectionEnabled}. |
192 |
|
*/ |
193 |
|
protected boolean cellSelectionEnabled; |
194 |
|
|
195 |
|
/** |
196 |
|
* The model for data stored in the table. Confusingly, the published API |
197 |
|
* requires that this field be called <code>dataModel</code>, despite its |
198 |
|
* property name. The table listens to its model as a {@link |
199 |
|
* TableModelListener}. |
200 |
|
* |
201 |
|
* @see #tableChanged() |
202 |
|
* @see TableModel#addTableModelListener() |
203 |
|
*/ |
204 |
|
protected TableModel dataModel; |
205 |
|
|
206 |
|
/** |
207 |
|
* <p>A model of various aspects of the columns of the table, <em>not |
208 |
|
* including</em> the data stored in them. The {@link TableColumnModel} |
209 |
|
* is principally concerned with holding a set of {@link TableColumn} |
210 |
|
* objects, each of which describes the display parameters of a column |
211 |
|
* and the numeric index of the column from the data model which the |
212 |
|
* column is presenting.</p> |
213 |
|
* |
214 |
|
* <p>The TableColumnModel also contains a {@link ListSelectionModel} which |
215 |
|
* indicates which columns are currently selected. This selection model |
216 |
|
* works in combination with the {@link selectionModel} of the table |
217 |
|
* itself to specify a <em>table selection</em>: a combination of row and |
218 |
|
* column selections.</p> |
219 |
|
* |
220 |
|
* <p>Most application programmers do not need to work with this property |
221 |
|
* at all: setting {@link #autoCreateColumnsFromModel} will construct the |
222 |
|
* columnModel automatically, and the table acts as a facade for most of |
223 |
|
* the interesting properties of the columnModel anyways.</p> |
224 |
|
* |
225 |
|
* @see #setColumnModel() |
226 |
|
* @see #getColumnModel() |
227 |
|
*/ |
228 |
|
protected TableColumnModel columnModel; |
229 |
|
|
230 |
|
/** |
231 |
|
* A model of the rows of this table which are currently selected. This |
232 |
|
* model is used in combination with the column selection model held as a |
233 |
|
* member of the {@link columnModel} property, to represent the rows and |
234 |
|
* columns (or both: cells) of the table which are currently selected. |
235 |
|
* |
236 |
|
* @see #rowSelectionAllowed |
237 |
|
* @see #setSelectionModel() |
238 |
|
* @see #getSelectionModel() |
239 |
|
* @see TableColumnModel#getSelectionModel() |
240 |
|
* @see ListSelectionModel#addListSelectionListener() |
241 |
|
*/ |
242 |
protected ListSelectionModel selectionModel; |
protected ListSelectionModel selectionModel; |
243 |
|
|
244 |
|
/** |
245 |
|
* The accessibleContext property. |
246 |
|
*/ |
247 |
|
protected AccessibleContext accessibleContext; |
248 |
|
|
249 |
|
/** |
250 |
|
* The current cell editor. |
251 |
|
*/ |
252 |
|
protected TableCellEditor cellEditor; |
253 |
|
|
254 |
|
/** |
255 |
|
* Whether or not drag-and-drop is enabled on this table. |
256 |
|
* |
257 |
|
* @see #setDragEnabled() |
258 |
|
* @see #getDragEnabled() |
259 |
|
*/ |
260 |
|
protected boolean dragEnabled; |
261 |
|
|
262 |
|
/** |
263 |
|
* The color to paint the grid lines of the table, when either {@link |
264 |
|
* #showHorizontalLines} or {@link #showVerticalLines} is set. |
265 |
|
* |
266 |
|
* @see #setGridColor() |
267 |
|
* @see #getGridColor() |
268 |
|
*/ |
269 |
|
protected Color gridColor; |
270 |
|
|
271 |
|
/** |
272 |
|
* The size this table would prefer its viewport assume, if it is |
273 |
|
* contained in a {@link JScrollPane}. |
274 |
|
* |
275 |
|
* @see #setPreferredScrollableViewportSize() |
276 |
|
* @see #getPreferredScrollableViewportSize() |
277 |
|
*/ |
278 |
|
protected Dimension preferredScrollableViewportSize; |
279 |
|
|
280 |
|
/** |
281 |
|
* The color to paint the background of selected cells. Fires a property |
282 |
|
* change event with name {@link #SELECTION_BACKGROUND_CHANGED_PROPERTY} |
283 |
|
* when its value changes. |
284 |
|
* |
285 |
|
* @see #setSelectionBackground() |
286 |
|
* @see #getSelectionBackground() |
287 |
|
*/ |
288 |
|
Color selectionBackground; |
289 |
|
|
290 |
|
/** |
291 |
|
* The name carried in property change events when the {@link |
292 |
|
* #selectionBackground} property changes. |
293 |
|
*/ |
294 |
|
private static final String SELECTION_BACKGROUND_CHANGED_PROPERTY = "selectionBackground"; |
295 |
|
|
296 |
|
/** |
297 |
|
* The color to paint the foreground of selected cells. Fires a property |
298 |
|
* change event with name {@link #SELECTION_FOREGROUND_CHANGED_PROPERTY} |
299 |
|
* when its value changes. |
300 |
|
* |
301 |
|
* @see #setSelectionForeground() |
302 |
|
* @see #getSelectionForeground() |
303 |
|
*/ |
304 |
|
Color selectionForeground; |
305 |
|
|
306 |
|
/** |
307 |
|
* The name carried in property change events when the |
308 |
|
* {@link #selectionForeground} property changes. |
309 |
|
*/ |
310 |
|
private static final String SELECTION_FOREGROUND_CHANGED_PROPERTY = "selectionForeground"; |
311 |
|
|
312 |
|
/** |
313 |
|
* The showHorizontalLines property. |
314 |
|
*/ |
315 |
protected boolean showHorizontalLines; |
protected boolean showHorizontalLines; |
316 |
|
|
317 |
|
/** |
318 |
|
* The showVerticalLines property. |
319 |
|
*/ |
320 |
protected boolean showVerticalLines; |
protected boolean showVerticalLines; |
321 |
|
|
322 |
|
/** |
323 |
|
* The tableHeader property. |
324 |
|
*/ |
325 |
protected JTableHeader tableHeader; |
protected JTableHeader tableHeader; |
326 |
|
|
327 |
|
|
328 |
/** |
/** |
329 |
* Creates a new <code>JTable</code> instance. |
* Creates a new <code>JTable</code> instance. |
330 |
*/ |
*/ |
386 |
public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) |
public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) |
387 |
{ |
{ |
388 |
this.dataModel = dm == null ? createDefaultDataModel() : dm; |
this.dataModel = dm == null ? createDefaultDataModel() : dm; |
389 |
this.columnModel = cm == null ? createDefaultColumnModel() : cm; |
setSelectionModel(sm == null ? createDefaultListSelectionModel() : sm); |
390 |
this.selectionModel = sm == null ? createDefaultListSelectionModel() : sm; |
|
391 |
|
this.columnModel = cm; |
392 |
|
this.autoCreateColumnsFromModel = false; |
393 |
|
if (cm == null) |
394 |
|
{ |
395 |
|
this.autoCreateColumnsFromModel = true; |
396 |
|
createColumnsFromModel(); |
397 |
|
} |
398 |
|
this.columnModel.addColumnModelListener(this); |
399 |
|
|
400 |
|
this.defaultRenderersByColumnClass = new Hashtable(); |
401 |
|
this.defaultEditorsByColumnClass = new Hashtable(); |
402 |
|
|
403 |
|
this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS; |
404 |
|
this.rowHeight = 16; |
405 |
|
this.rowMargin = 1; |
406 |
|
this.rowSelectionAllowed = true; |
407 |
|
// this.accessibleContext = new AccessibleJTable(); |
408 |
|
this.cellEditor = null; |
409 |
|
this.dragEnabled = false; |
410 |
|
this.preferredScrollableViewportSize = new Dimension(450,400); |
411 |
|
this.showHorizontalLines = true; |
412 |
|
this.showVerticalLines = true; |
413 |
|
setInterCellSpacing(new Dimension(1,1)); |
414 |
|
setTableHeader(new JTableHeader(columnModel)); |
415 |
|
updateUI(); |
416 |
} |
} |
417 |
|
|
418 |
/** |
/** |
434 |
return new JScrollPane(table); |
return new JScrollPane(table); |
435 |
} |
} |
436 |
|
|
437 |
public void clearSelection() |
protected TableColumnModel createDefaultColumnModel() |
438 |
{ |
{ |
439 |
selectionModel.clearSelection(); |
return new DefaultTableColumnModel(); |
440 |
} |
} |
441 |
|
|
442 |
public void columnAdded (TableColumnModelEvent event) |
protected TableModel createDefaultDataModel() |
443 |
{ |
{ |
444 |
throw new Error ("Not implemented"); |
return new DefaultTableModel(); |
445 |
} |
} |
446 |
|
|
447 |
public void columnMarginChanged (ChangeEvent event) |
protected ListSelectionModel createDefaultListSelectionModel() |
448 |
{ |
{ |
449 |
throw new Error ("Not implemented"); |
return new DefaultListSelectionModel(); |
450 |
} |
} |
451 |
|
|
452 |
public void columnMoved (TableColumnModelEvent event) |
private void createColumnsFromModel() |
453 |
|
{ |
454 |
|
if (dataModel == null) |
455 |
|
return; |
456 |
|
|
457 |
|
TableColumnModel cm = createDefaultColumnModel(); |
458 |
|
|
459 |
|
for (int i = 0; i < dataModel.getColumnCount(); ++i) |
460 |
{ |
{ |
461 |
throw new Error ("Not implemented"); |
cm.addColumn(new TableColumn(i)); |
462 |
|
} |
463 |
|
this.setColumnModel(cm); |
464 |
} |
} |
465 |
|
|
466 |
public void columnRemoved (TableColumnModelEvent event) |
// listener support |
467 |
|
|
468 |
|
public void columnAdded (TableColumnModelEvent event) |
469 |
{ |
{ |
470 |
throw new Error ("Not implemented"); |
revalidate(); |
471 |
|
repaint(); |
472 |
} |
} |
473 |
|
|
474 |
public void columnSelectionChanged (ListSelectionEvent event) |
public void columnMarginChanged (ChangeEvent event) |
475 |
{ |
{ |
476 |
throw new Error ("Not implemented"); |
revalidate(); |
477 |
|
repaint(); |
478 |
} |
} |
479 |
|
|
480 |
protected TableColumnModel createDefaultColumnModel() |
public void columnMoved (TableColumnModelEvent event) |
481 |
{ |
{ |
482 |
return new DefaultTableColumnModel(); |
revalidate(); |
483 |
|
repaint(); |
484 |
} |
} |
485 |
|
|
486 |
protected TableModel createDefaultDataModel() |
public void columnRemoved (TableColumnModelEvent event) |
487 |
{ |
{ |
488 |
return new DefaultTableModel(); |
revalidate(); |
489 |
|
repaint(); |
490 |
} |
} |
491 |
|
|
492 |
protected ListSelectionModel createDefaultListSelectionModel() |
public void columnSelectionChanged (ListSelectionEvent event) |
493 |
{ |
{ |
494 |
return new DefaultListSelectionModel(); |
repaint(); |
495 |
} |
} |
496 |
|
|
497 |
public void editingCanceled (ChangeEvent event) |
public void editingCanceled (ChangeEvent event) |
498 |
{ |
{ |
499 |
throw new Error ("Not implemented"); |
repaint(); |
500 |
} |
} |
501 |
|
|
502 |
public void editingStopped (ChangeEvent event) |
public void editingStopped (ChangeEvent event) |
503 |
{ |
{ |
504 |
throw new Error ("Not implemented"); |
repaint(); |
505 |
} |
} |
506 |
|
|
507 |
public TableColumnModel getColumnModel () |
public void tableChanged (TableModelEvent event) |
508 |
{ |
{ |
509 |
return columnModel; |
repaint(); |
510 |
} |
} |
511 |
|
|
512 |
public TableModel getModel() |
public void valueChanged (ListSelectionEvent event) |
513 |
{ |
{ |
514 |
return dataModel; |
repaint(); |
515 |
} |
} |
516 |
|
|
|
public Dimension getPreferredScrollableViewportSize () |
|
|
{ |
|
|
throw new Error ("Not implemented"); |
|
|
} |
|
517 |
|
|
518 |
public int getScrollableBlockIncrement (Rectangle visibleRect, int orientation, int direction) |
/** |
519 |
|
* Calculate the visible rectangle for a particular row and column. The |
520 |
|
* row and column are specified in visual terms; the column may not match |
521 |
|
* the {@link #dataModel} column. |
522 |
|
* |
523 |
|
* @param row the visible row to get the cell rectangle of |
524 |
|
* |
525 |
|
* @param column the visible column to get the cell rectangle of, which may |
526 |
|
* differ from the {@link #dataModel} column |
527 |
|
* |
528 |
|
* @param includeSpacing whether or not to include the cell margins in the |
529 |
|
* resulting cell. If <code>false</code>, the result will only contain the |
530 |
|
* inner area of the target cell, not including its margins. |
531 |
|
* |
532 |
|
* @return a rectangle enclosing the specified cell |
533 |
|
*/ |
534 |
|
public Rectangle getCellRect(int row, |
535 |
|
int column, |
536 |
|
boolean includeSpacing) |
537 |
{ |
{ |
538 |
throw new Error ("Not implemented"); |
int height = getHeight(); |
539 |
} |
int width = columnModel.getColumn(column).getWidth(); |
540 |
|
int x_gap = columnModel.getColumnMargin(); |
541 |
|
int y_gap = rowMargin; |
542 |
|
|
543 |
|
column = Math.max(0, Math.min(column, getColumnCount() - 1)); |
544 |
|
row = Math.max(0, Math.min(row, getRowCount() - 1)); |
545 |
|
|
546 |
public boolean getScrollableTracksViewportHeight () |
int x = 0; |
547 |
|
int y = (height + y_gap) * row; |
548 |
|
|
549 |
|
for (int i = 0; i < column; ++i) |
550 |
{ |
{ |
551 |
throw new Error ("Not implemented"); |
x += columnModel.getColumn(i).getWidth(); |
552 |
|
x += x_gap; |
553 |
} |
} |
554 |
|
|
555 |
public boolean getScrollableTracksViewportWidth () |
if (includeSpacing) |
556 |
{ |
return new Rectangle(x, y, width, height); |
557 |
throw new Error ("Not implemented"); |
else |
558 |
|
return new Rectangle(x, y, width - x_gap, height - y_gap); |
559 |
} |
} |
560 |
|
|
561 |
public int getScrollableUnitIncrement (Rectangle visibleRect, int orientation, int direction) |
public void clearSelection() |
562 |
{ |
{ |
563 |
throw new Error ("Not implemented"); |
selectionModel.clearSelection(); |
564 |
} |
} |
565 |
|
|
566 |
|
/** |
567 |
|
* Get the value of the {@link #selectedRow} property by delegation to |
568 |
|
* the {@link ListSelectionModel#getMinSelectionIndex} method of the |
569 |
|
* {@link #selectionModel} field. |
570 |
|
* |
571 |
|
* @return The current value of the selectedRow property |
572 |
|
*/ |
573 |
public int getSelectedRow () |
public int getSelectedRow () |
574 |
{ |
{ |
575 |
return selectionModel.getMinSelectionIndex(); |
return selectionModel.getMinSelectionIndex(); |
576 |
} |
} |
577 |
|
|
578 |
|
/** |
579 |
|
* Get the value of the {@link #selectionModel} property. |
580 |
|
* |
581 |
|
* @return The current value of the property |
582 |
|
*/ |
583 |
public ListSelectionModel getSelectionModel () |
public ListSelectionModel getSelectionModel () |
584 |
{ |
{ |
585 |
if (! rowSelectionAllowed) |
if (! rowSelectionAllowed) |
588 |
return selectionModel; |
return selectionModel; |
589 |
} |
} |
590 |
|
|
591 |
public void tableChanged (TableModelEvent event) |
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) |
592 |
{ |
{ |
593 |
throw new Error ("Not implemented"); |
if (orientation == SwingConstants.VERTICAL) |
594 |
|
return visibleRect.height * direction; |
595 |
|
else |
596 |
|
return visibleRect.width * direction; |
597 |
} |
} |
598 |
|
|
599 |
public void setModel (TableModel model) |
/** |
600 |
|
* Get the value of the {@link #scrollableTracksViewportHeight} property. |
601 |
|
* |
602 |
|
* @return The constant value <code>false</code> |
603 |
|
*/ |
604 |
|
|
605 |
|
public boolean getScrollableTracksViewportHeight() |
606 |
{ |
{ |
607 |
if (model == null) |
return false; |
608 |
throw new IllegalArgumentException(); |
} |
609 |
|
|
610 |
// FIXME: Should we deregister from old model ? |
/** |
611 |
|
* Get the value of the {@link #scrollableTracksViewportWidth} property. |
612 |
|
* |
613 |
|
* @return <code>true</code> unless the {@link autoResizeMode} prperty is |
614 |
|
* <code>AUTO_RESIZE_OFF</code> |
615 |
|
*/ |
616 |
|
|
617 |
dataModel = model; |
public boolean getScrollableTracksViewportWidth() |
618 |
dataModel.addTableModelListener(this); |
{ |
619 |
|
if (autoResizeMode == AUTO_RESIZE_OFF) |
620 |
|
return false; |
621 |
|
else |
622 |
|
return true; |
623 |
} |
} |
624 |
|
|
625 |
public void setSelectionMode (int selectionMode) |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) |
626 |
|
{ |
627 |
|
// FIXME: I don't exactly know what sun does here. in both cases they |
628 |
|
// pick values which do *not* simply expose the next cell in a given |
629 |
|
// scroll direction. |
630 |
|
|
631 |
|
if (orientation == SwingConstants.VERTICAL) |
632 |
|
return rowHeight; |
633 |
|
else |
634 |
{ |
{ |
635 |
throw new Error ("Not implemented"); |
int sum = 0; |
636 |
|
for (int i = 0; i < getColumnCount(); ++i) |
637 |
|
sum += columnModel.getColumn(0).getWidth(); |
638 |
|
return getColumnCount() == 0 ? 10 : sum / getColumnCount(); |
639 |
|
} |
640 |
} |
} |
641 |
|
|
642 |
public void setSelectionModel (ListSelectionModel model) |
|
643 |
|
public TableCellEditor getCellEditor(int row, int column) |
644 |
{ |
{ |
645 |
if (model == null) |
TableCellEditor editor = columnModel.getColumn(column).getCellEditor(); |
|
throw new IllegalArgumentException(); |
|
646 |
|
|
647 |
// FIXME: Should we deregister from old model ? |
if (editor == null) |
648 |
|
editor = getDefaultEditor(dataModel.getColumnClass(column)); |
649 |
|
|
650 |
selectionModel = model; |
return editor; |
|
selectionModel.addListSelectionListener(this); |
|
651 |
} |
} |
652 |
|
|
653 |
public void setShowGrid (boolean showGrid) |
public TableCellEditor getDefaultEditor(Class columnClass) |
654 |
{ |
{ |
655 |
throw new Error ("Not implemented"); |
if (defaultEditorsByColumnClass.containsKey(columnClass)) |
656 |
|
return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); |
657 |
|
else |
658 |
|
{ |
659 |
|
TableCellEditor r = new DefaultCellEditor(new JTextField()); |
660 |
|
defaultEditorsByColumnClass.put(columnClass, r); |
661 |
|
return r; |
662 |
|
} |
663 |
} |
} |
664 |
|
|
665 |
public void valueChanged (ListSelectionEvent event) |
|
666 |
|
|
667 |
|
public TableCellRenderer getCellRenderer(int row, int column) |
668 |
{ |
{ |
669 |
throw new Error ("Not implemented"); |
TableCellRenderer renderer = |
670 |
|
columnModel.getColumn(column).getCellRenderer(); |
671 |
|
|
672 |
|
if (renderer == null) |
673 |
|
renderer = getDefaultRenderer(dataModel.getColumnClass(column)); |
674 |
|
|
675 |
|
return renderer; |
676 |
} |
} |
677 |
|
|
678 |
public JTableHeader getTableHeader() |
public TableCellRenderer getDefaultRenderer(Class columnClass) |
679 |
{ |
{ |
680 |
return tableHeader; |
if (defaultRenderersByColumnClass.containsKey(columnClass)) |
681 |
|
return (TableCellRenderer) defaultRenderersByColumnClass.get(columnClass); |
682 |
|
else |
683 |
|
{ |
684 |
|
TableCellRenderer r = new DefaultTableCellRenderer(); |
685 |
|
defaultRenderersByColumnClass.put(columnClass, r); |
686 |
|
return r; |
687 |
|
} |
688 |
|
} |
689 |
|
|
690 |
|
public int convertColumnIndexToModel(int vc) |
691 |
|
{ |
692 |
|
if (vc < 0) |
693 |
|
return vc; |
694 |
|
else if (vc > getColumnCount()) |
695 |
|
return -1; |
696 |
|
else |
697 |
|
return columnModel.getColumn(vc).getModelIndex(); |
698 |
} |
} |
699 |
|
|
700 |
public void setTableHeader(JTableHeader newHeader) |
public int convertColumnIndexToView(int mc) |
701 |
{ |
{ |
702 |
tableHeader = newHeader; |
if (mc < 0) |
703 |
|
return mc; |
704 |
|
int ncols = getColumnCount(); |
705 |
|
for (int vc = 0; vc < ncols; ++vc) |
706 |
|
{ |
707 |
|
if (columnModel.getColumn(vc).getModelIndex() == mc) |
708 |
|
return vc; |
709 |
|
} |
710 |
|
return -1; |
711 |
|
} |
712 |
|
|
713 |
|
public Component prepareRenderer(TableCellRenderer renderer, |
714 |
|
int row, |
715 |
|
int column) |
716 |
|
{ |
717 |
|
boolean rsa = getRowSelectionAllowed(); |
718 |
|
boolean csa = getColumnSelectionAllowed(); |
719 |
|
boolean rs = rsa ? getSelectionModel().isSelectedIndex(row) : false; |
720 |
|
boolean cs = csa ? columnModel.getSelectionModel().isSelectedIndex(column) : false; |
721 |
|
boolean isSelected = ((rsa && csa && rs && cs) |
722 |
|
|| (rsa && !csa && rs) |
723 |
|
|| (!rsa && csa && cs)); |
724 |
|
|
725 |
|
return renderer.getTableCellRendererComponent(this, |
726 |
|
dataModel.getValueAt(row, |
727 |
|
convertColumnIndexToView(column)), |
728 |
|
isSelected, |
729 |
|
false, // hasFocus |
730 |
|
row, column); |
731 |
} |
} |
732 |
|
|
733 |
|
|
734 |
public boolean getColumnSelectionAllowed() |
/** |
735 |
|
* Get the value of the {@link #autoCreateColumnsFromModel} property. |
736 |
|
* |
737 |
|
* @return The current value of the property |
738 |
|
*/ |
739 |
|
public boolean getAutoCreateColumnsFromModel() |
740 |
{ |
{ |
741 |
return columnModel.getColumnSelectionAllowed(); |
return autoCreateColumnsFromModel; |
742 |
} |
} |
743 |
|
|
744 |
public void setColumnSelectionAllowed(boolean flag) |
/** |
745 |
|
* Get the value of the {@link #autoResizeMode} property. |
746 |
|
* |
747 |
|
* @return The current value of the property |
748 |
|
*/ |
749 |
|
public int getAutoResizeMode() |
750 |
{ |
{ |
751 |
columnModel.setColumnSelectionAllowed(flag); |
return autoResizeMode; |
752 |
} |
} |
753 |
|
|
754 |
public boolean getRowSelectionAllowed() |
/** |
755 |
|
* Get the value of the {@link #rowHeight} property. |
756 |
|
* |
757 |
|
* @return The current value of the property |
758 |
|
*/ |
759 |
|
public int getRowHeight() |
760 |
{ |
{ |
761 |
return rowSelectionAllowed; |
return rowHeight; |
762 |
} |
} |
763 |
|
|
764 |
public void setRowSelectionAllowed(boolean flag) |
/** |
765 |
|
* Get the value of the {@link #rowMargin} property. |
766 |
|
* |
767 |
|
* @return The current value of the property |
768 |
|
*/ |
769 |
|
public int getRowMargin() |
770 |
{ |
{ |
771 |
rowSelectionAllowed = flag; |
return rowMargin; |
772 |
} |
} |
773 |
|
|
774 |
public int getAutoResizeMode() |
/** |
775 |
|
* Get the value of the {@link #rowSelectionAllowed} property. |
776 |
|
* |
777 |
|
* @return The current value of the property |
778 |
|
*/ |
779 |
|
public boolean getRowSelectionAllowed() |
780 |
{ |
{ |
781 |
return autoResizeMode; |
return rowSelectionAllowed; |
782 |
} |
} |
783 |
|
|
784 |
public void setAutoResizeMode(int mode) |
/** |
785 |
|
* Get the value of the {@link #cellSelectionEnabled} property. |
786 |
|
* |
787 |
|
* @return The current value of the property |
788 |
|
*/ |
789 |
|
public boolean getCellSelectionEnabled() |
790 |
{ |
{ |
791 |
autoResizeMode = mode; |
return getColumnSelectionAllowed() && getRowSelectionAllowed(); |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
|
* Get the value of the {@link #dataModel} property. |
796 |
|
* |
797 |
|
* @return The current value of the property |
798 |
|
*/ |
799 |
|
public TableModel getModel() |
800 |
|
{ |
801 |
|
return dataModel; |
802 |
} |
} |
803 |
|
|
804 |
|
/** |
805 |
|
* Get the value of the {@link #columnCount} property by |
806 |
|
* delegation to the @{link #dataModel} field. |
807 |
|
* |
808 |
|
* @return The current value of the columnCount property |
809 |
|
*/ |
810 |
public int getColumnCount() |
public int getColumnCount() |
811 |
{ |
{ |
812 |
return dataModel.getColumnCount(); |
return dataModel.getColumnCount(); |
813 |
} |
} |
814 |
|
|
815 |
|
/** |
816 |
|
* Get the value of the {@link #rowCount} property by |
817 |
|
* delegation to the @{link #dataModel} field. |
818 |
|
* |
819 |
|
* @return The current value of the rowCount property |
820 |
|
*/ |
821 |
public int getRowCount() |
public int getRowCount() |
822 |
{ |
{ |
823 |
return dataModel.getRowCount(); |
return dataModel.getRowCount(); |
824 |
} |
} |
825 |
|
|
826 |
public TableCellRenderer getCellRenderer(int row, int column) |
/** |
827 |
|
* Get the value of the {@link #columnModel} property. |
828 |
|
* |
829 |
|
* @return The current value of the property |
830 |
|
*/ |
831 |
|
public TableColumnModel getColumnModel() |
832 |
{ |
{ |
833 |
TableCellRenderer renderer = |
return columnModel; |
|
columnModel.getColumn(column).getCellRenderer(); |
|
|
|
|
|
if (renderer == null) |
|
|
renderer = getDefaultRenderer(dataModel.getColumnClass(column)); |
|
|
|
|
|
return renderer; |
|
834 |
} |
} |
835 |
|
|
836 |
public TableCellRenderer getDefaultRenderer(Class columnClass) |
/** |
837 |
|
* Get the value of the {@link #selectedColumn} property by |
838 |
|
* delegation to the @{link #columnModel} field. |
839 |
|
* |
840 |
|
* @return The current value of the selectedColumn property |
841 |
|
*/ |
842 |
|
public int getSelectedColumn() |
843 |
|
{ |
844 |
|
return columnModel.getSelectionModel().getMinSelectionIndex(); |
845 |
|
} |
846 |
|
|
847 |
|
private static int countSelections(ListSelectionModel lsm) |
848 |
|
{ |
849 |
|
int lo = lsm.getMinSelectionIndex(); |
850 |
|
int hi = lsm.getMaxSelectionIndex(); |
851 |
|
int sum = 0; |
852 |
|
if (lo != -1 && hi != -1) |
853 |
|
{ |
854 |
|
switch (lsm.getSelectionMode()) |
855 |
|
{ |
856 |
|
case ListSelectionModel.SINGLE_SELECTION: |
857 |
|
sum = 1; |
858 |
|
break; |
859 |
|
|
860 |
|
case ListSelectionModel.SINGLE_INTERVAL_SELECTION: |
861 |
|
sum = hi - lo; |
862 |
|
break; |
863 |
|
|
864 |
|
case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: |
865 |
|
for (int i = lo; i < hi; ++i) |
866 |
|
if (lsm.isSelectedIndex(i)) |
867 |
|
++sum; |
868 |
|
break; |
869 |
|
} |
870 |
|
} |
871 |
|
return sum; |
872 |
|
} |
873 |
|
|
874 |
|
private static int[] getSelections(ListSelectionModel lsm) |
875 |
|
{ |
876 |
|
int sz = countSelections(lsm); |
877 |
|
int [] ret = new int[sz]; |
878 |
|
|
879 |
|
int lo = lsm.getMinSelectionIndex(); |
880 |
|
int hi = lsm.getMaxSelectionIndex(); |
881 |
|
int j = 0; |
882 |
|
java.util.ArrayList ls = new java.util.ArrayList(); |
883 |
|
if (lo != -1 && hi != -1) |
884 |
|
{ |
885 |
|
switch (lsm.getSelectionMode()) |
886 |
|
{ |
887 |
|
case ListSelectionModel.SINGLE_SELECTION: |
888 |
|
ret[0] = lo; |
889 |
|
break; |
890 |
|
|
891 |
|
case ListSelectionModel.SINGLE_INTERVAL_SELECTION: |
892 |
|
for (int i = lo; i < hi; ++i) |
893 |
|
ret[j++] = i; |
894 |
|
break; |
895 |
|
|
896 |
|
case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: |
897 |
|
for (int i = lo; i < hi; ++i) |
898 |
|
if (lsm.isSelectedIndex(i)) |
899 |
|
ret[j++] = i; |
900 |
|
break; |
901 |
|
} |
902 |
|
} |
903 |
|
return ret; |
904 |
|
} |
905 |
|
|
906 |
|
/** |
907 |
|
* Get the value of the {@link #selectedColumnCount} property by |
908 |
|
* delegation to the @{link #columnModel} field. |
909 |
|
* |
910 |
|
* @return The current value of the selectedColumnCount property |
911 |
|
*/ |
912 |
|
public int getSelectedColumnCount() |
913 |
|
{ |
914 |
|
return countSelections(columnModel.getSelectionModel()); |
915 |
|
} |
916 |
|
|
917 |
|
/** |
918 |
|
* Get the value of the {@link #selectedColumns} property by |
919 |
|
* delegation to the @{link #columnModel} field. |
920 |
|
* |
921 |
|
* @return The current value of the selectedColumns property |
922 |
|
*/ |
923 |
|
public int[] getSelectedColumns() |
924 |
|
{ |
925 |
|
return getSelections(columnModel.getSelectionModel()); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
929 |
|
* Get the value of the {@link #columnSelectionAllowed} property. |
930 |
|
* |
931 |
|
* @return The current value of the columnSelectionAllowed property |
932 |
|
*/ |
933 |
|
public boolean getColumnSelectionAllowed() |
934 |
|
{ |
935 |
|
return getColumnModel().getColumnSelectionAllowed(); |
936 |
|
} |
937 |
|
|
938 |
|
/** |
939 |
|
* Get the value of the {@link #selectedRowCount} property by |
940 |
|
* delegation to the @{link #selectionModel} field. |
941 |
|
* |
942 |
|
* @return The current value of the selectedRowCount property |
943 |
|
*/ |
944 |
|
public int getSelectedRowCount() |
945 |
|
{ |
946 |
|
return countSelections(selectionModel); |
947 |
|
} |
948 |
|
|
949 |
|
/** |
950 |
|
* Get the value of the {@link #selectedRows} property by |
951 |
|
* delegation to the @{link #selectionModel} field. |
952 |
|
* |
953 |
|
* @return The current value of the selectedRows property |
954 |
|
*/ |
955 |
|
public int[] getSelectedRows() |
956 |
|
{ |
957 |
|
return getSelections(selectionModel); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
961 |
|
* Get the value of the {@link #accessibleContext} property. |
962 |
|
* |
963 |
|
* @return The current value of the property |
964 |
|
*/ |
965 |
|
public AccessibleContext getAccessibleContext() |
966 |
|
{ |
967 |
|
return accessibleContext; |
968 |
|
} |
969 |
|
|
970 |
|
/** |
971 |
|
* Get the value of the {@link #cellEditor} property. |
972 |
|
* |
973 |
|
* @return The current value of the property |
974 |
|
*/ |
975 |
|
public TableCellEditor getCellEditor() |
976 |
|
{ |
977 |
|
return cellEditor; |
978 |
|
} |
979 |
|
|
980 |
|
/** |
981 |
|
* Get the value of the {@link #dragEnabled} property. |
982 |
|
* |
983 |
|
* @return The current value of the property |
984 |
|
*/ |
985 |
|
public boolean getDragEnabled() |
986 |
|
{ |
987 |
|
return dragEnabled; |
988 |
|
} |
989 |
|
|
990 |
|
/** |
991 |
|
* Get the value of the {@link #gridColor} property. |
992 |
|
* |
993 |
|
* @return The current value of the property |
994 |
|
*/ |
995 |
|
public Color getGridColor() |
996 |
|
{ |
997 |
|
return gridColor; |
998 |
|
} |
999 |
|
|
1000 |
|
/** |
1001 |
|
* Get the value of the {@link #interCellSpacing} property. |
1002 |
|
* |
1003 |
|
* @return The current value of the property |
1004 |
|
*/ |
1005 |
|
public Dimension getInterCellSpacing() |
1006 |
|
{ |
1007 |
|
return new Dimension(columnModel.getColumnMargin(), rowMargin); |
1008 |
|
} |
1009 |
|
|
1010 |
|
/** |
1011 |
|
* Get the value of the {@link #preferredScrollableViewportSize} property. |
1012 |
|
* |
1013 |
|
* @return The current value of the property |
1014 |
|
*/ |
1015 |
|
public Dimension getPreferredScrollableViewportSize() |
1016 |
|
{ |
1017 |
|
return preferredScrollableViewportSize; |
1018 |
|
} |
1019 |
|
|
1020 |
|
/** |
1021 |
|
* Get the value of the {@link #selectionBackground} property. |
1022 |
|
* |
1023 |
|
* @return The current value of the property |
1024 |
|
*/ |
1025 |
|
public Color getSelectionBackground() |
1026 |
|
{ |
1027 |
|
return selectionBackground; |
1028 |
|
} |
1029 |
|
|
1030 |
|
/** |
1031 |
|
* Get the value of the {@link #selectionForeground} property. |
1032 |
|
* |
1033 |
|
* @return The current value of the property |
1034 |
|
*/ |
1035 |
|
public Color getSelectionForeground() |
1036 |
|
{ |
1037 |
|
return selectionForeground; |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1041 |
|
* Get the value of the {@link #showHorizontalLines} property. |
1042 |
|
* |
1043 |
|
* @return The current value of the property |
1044 |
|
*/ |
1045 |
|
public boolean getShowHorizontalLines() |
1046 |
|
{ |
1047 |
|
return showHorizontalLines; |
1048 |
|
} |
1049 |
|
|
1050 |
|
/** |
1051 |
|
* Get the value of the {@link #showVerticalLines} property. |
1052 |
|
* |
1053 |
|
* @return The current value of the property |
1054 |
|
*/ |
1055 |
|
public boolean getShowVerticalLines() |
1056 |
|
{ |
1057 |
|
return showVerticalLines; |
1058 |
|
} |
1059 |
|
|
1060 |
|
/** |
1061 |
|
* Get the value of the {@link #tableHeader} property. |
1062 |
|
* |
1063 |
|
* @return The current value of the property |
1064 |
|
*/ |
1065 |
|
public JTableHeader getTableHeader() |
1066 |
|
{ |
1067 |
|
return tableHeader; |
1068 |
|
} |
1069 |
|
|
1070 |
|
/** |
1071 |
|
* Set the value of the {@link #autoCreateColumnsFromModel} property. |
1072 |
|
* |
1073 |
|
* @param a The new value of the autoCreateColumnsFromModel property |
1074 |
|
*/ |
1075 |
|
public void setAutoCreateColumnsFromModel(boolean a) |
1076 |
|
{ |
1077 |
|
autoCreateColumnsFromModel = a; |
1078 |
|
} |
1079 |
|
|
1080 |
|
/** |
1081 |
|
* Set the value of the {@link #autoResizeMode} property. |
1082 |
|
* |
1083 |
|
* @param a The new value of the autoResizeMode property |
1084 |
|
*/ |
1085 |
|
public void setAutoResizeMode(int a) |
1086 |
|
{ |
1087 |
|
autoResizeMode = a; |
1088 |
|
revalidate(); |
1089 |
|
repaint(); |
1090 |
|
} |
1091 |
|
|
1092 |
|
/** |
1093 |
|
* Set the value of the {@link #rowHeight} property. |
1094 |
|
* |
1095 |
|
* @param r The new value of the rowHeight property |
1096 |
|
*/ |
1097 |
|
public void setRowHeight(int r) |
1098 |
|
{ |
1099 |
|
rowHeight = r; |
1100 |
|
revalidate(); |
1101 |
|
repaint(); |
1102 |
|
} |
1103 |
|
|
1104 |
|
/** |
1105 |
|
* Set the value of the {@link #rowMargin} property. |
1106 |
|
* |
1107 |
|
* @param r The new value of the rowMargin property |
1108 |
|
*/ |
1109 |
|
public void setRowMargin(int r) |
1110 |
|
{ |
1111 |
|
rowMargin = r; |
1112 |
|
revalidate(); |
1113 |
|
repaint(); |
1114 |
|
} |
1115 |
|
|
1116 |
|
/** |
1117 |
|
* Set the value of the {@link #rowSelectionAllowed} property. |
1118 |
|
* |
1119 |
|
* @param r The new value of the rowSelectionAllowed property |
1120 |
|
*/ |
1121 |
|
public void setRowSelectionAllowed(boolean r) |
1122 |
{ |
{ |
1123 |
// FIXME: |
rowSelectionAllowed = r; |
1124 |
return null; |
repaint(); |
1125 |
} |
} |
1126 |
|
|
1127 |
|
/** |
1128 |
|
* Set the value of the {@link #cellSelectionEnabled} property. |
1129 |
|
* |
1130 |
|
* @param c The new value of the cellSelectionEnabled property |
1131 |
|
*/ |
1132 |
|
public void setCellSelectionEnabled(boolean c) |
1133 |
|
{ |
1134 |
|
setColumnSelectionAllowed(c); |
1135 |
|
setRowSelectionAllowed(c); |
1136 |
|
// for backward-compatibility sake: |
1137 |
|
cellSelectionEnabled = true; |
1138 |
|
} |
1139 |
|
|
1140 |
|
/** |
1141 |
|
* <p>Set the value of the {@link #dataModel} property.</p> |
1142 |
|
* |
1143 |
|
* <p>Unregister <code>this</code> as a {@link TableModelListener} from |
1144 |
|
* previous {@link #dataModel} and register it with new parameter |
1145 |
|
* <code>m</code>.</p> |
1146 |
|
* |
1147 |
|
* @param m The new value of the model property |
1148 |
|
*/ |
1149 |
|
public void setModel(TableModel m) |
1150 |
|
{ |
1151 |
|
if (m == null) |
1152 |
|
throw new IllegalArgumentException(); |
1153 |
|
TableModel tmp = dataModel; |
1154 |
|
if (autoCreateColumnsFromModel) |
1155 |
|
createColumnsFromModel(); |
1156 |
|
if (tmp != null) |
1157 |
|
tmp.removeTableModelListener(this); |
1158 |
|
if (m != null) |
1159 |
|
m.addTableModelListener(this); |
1160 |
|
dataModel = m; |
1161 |
|
revalidate(); |
1162 |
|
repaint(); |
1163 |
|
} |
1164 |
|
|
1165 |
|
/** |
1166 |
|
* <p>Set the value of the {@link #columnModel} property.</p> |
1167 |
|
* |
1168 |
|
* <p>Unregister <code>this</code> as a {@link TableColumnModelListener} |
1169 |
|
* from previous {@link #columnModel} and register it with new parameter |
1170 |
|
* <code>c</code>.</p> |
1171 |
|
* |
1172 |
|
* @param c The new value of the columnModel property |
1173 |
|
*/ |
1174 |
|
public void setColumnModel(TableColumnModel c) |
1175 |
|
{ |
1176 |
|
if (c == null) |
1177 |
|
throw new IllegalArgumentException(); |
1178 |
|
TableColumnModel tmp = columnModel; |
1179 |
|
if (tmp != null) |
1180 |
|
tmp.removeColumnModelListener(this); |
1181 |
|
if (c != null) |
1182 |
|
c.addColumnModelListener(this); |
1183 |
|
columnModel = c; |
1184 |
|
if (dataModel != null && columnModel != null) |
1185 |
|
{ |
1186 |
|
int ncols = getColumnCount(); |
1187 |
|
for (int i = 0; i < ncols; ++i) |
1188 |
|
columnModel.getColumn(i).setHeaderValue(dataModel.getColumnName(i)); |
1189 |
|
} |
1190 |
|
revalidate(); |
1191 |
|
repaint(); |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1195 |
|
* Set the value of the {@link #columnSelectionAllowed} property. |
1196 |
|
* |
1197 |
|
* @param c The new value of the property |
1198 |
|
*/ |
1199 |
|
public void setColumnSelectionAllowed(boolean c) |
1200 |
|
{ |
1201 |
|
getColumnModel().setColumnSelectionAllowed(c); |
1202 |
|
repaint(); |
1203 |
|
} |
1204 |
|
|
1205 |
|
/** |
1206 |
|
* <p>Set the value of the {@link #selectionModel} property.</p> |
1207 |
|
* |
1208 |
|
* <p>Unregister <code>this</code> as a {@link ListSelectionListener} |
1209 |
|
* from previous {@link #selectionModel} and register it with new |
1210 |
|
* parameter <code>s</code>.</p> |
1211 |
|
* |
1212 |
|
* @param s The new value of the selectionModel property |
1213 |
|
*/ |
1214 |
|
public void setSelectionModel(ListSelectionModel s) |
1215 |
|
{ |
1216 |
|
if (s == null) |
1217 |
|
throw new IllegalArgumentException(); |
1218 |
|
ListSelectionModel tmp = selectionModel; |
1219 |
|
if (tmp != null) |
1220 |
|
tmp.removeListSelectionListener(this); |
1221 |
|
if (s != null) |
1222 |
|
s.addListSelectionListener(this); |
1223 |
|
selectionModel = s; |
1224 |
|
} |
1225 |
|
|
1226 |
|
/** |
1227 |
|
* Set the value of the {@link #selectionMode} property by |
1228 |
|
* delegation to the {@link #selectionModel} field. |
1229 |
|
* |
1230 |
|
* @param s The new value of the property |
1231 |
|
*/ |
1232 |
|
public void setSelectionMode(int s) |
1233 |
|
{ |
1234 |
|
selectionModel.setSelectionMode(s); |
1235 |
|
repaint(); |
1236 |
|
} |
1237 |
|
|
1238 |
|
/** |
1239 |
|
* <p>Set the value of the {@link #cellEditor} property.</p> |
1240 |
|
* |
1241 |
|
* <p>Unregister <code>this</code> as a {@link CellEditorListener} from |
1242 |
|
* previous {@link #cellEditor} and register it with new parameter |
1243 |
|
* <code>c</code>.</p> |
1244 |
|
* |
1245 |
|
* @param c The new value of the cellEditor property |
1246 |
|
*/ |
1247 |
|
public void setCellEditor(TableCellEditor c) |
1248 |
|
{ |
1249 |
|
TableCellEditor tmp = cellEditor; |
1250 |
|
if (tmp != null) |
1251 |
|
tmp.removeCellEditorListener(this); |
1252 |
|
if (c != null) |
1253 |
|
c.addCellEditorListener(this); |
1254 |
|
cellEditor = c; |
1255 |
|
} |
1256 |
|
|
1257 |
|
/** |
1258 |
|
* Set the value of the {@link #dragEnabled} property. |
1259 |
|
* |
1260 |
|
* @param d The new value of the dragEnabled property |
1261 |
|
*/ |
1262 |
|
public void setDragEnabled(boolean d) |
1263 |
|
{ |
1264 |
|
dragEnabled = d; |
1265 |
|
} |
1266 |
|
|
1267 |
|
/** |
1268 |
|
* Set the value of the {@link #gridColor} property. |
1269 |
|
* |
1270 |
|
* @param g The new value of the gridColor property |
1271 |
|
*/ |
1272 |
|
public void setGridColor(Color g) |
1273 |
|
{ |
1274 |
|
gridColor = g; |
1275 |
|
repaint(); |
1276 |
|
} |
1277 |
|
|
1278 |
|
/** |
1279 |
|
* Set the value of the {@link #interCellSpacing} property. |
1280 |
|
* |
1281 |
|
* @param i The new value of the interCellSpacing property |
1282 |
|
*/ |
1283 |
|
public void setInterCellSpacing(Dimension i) |
1284 |
|
{ |
1285 |
|
rowMargin = i.height; |
1286 |
|
columnModel.setColumnMargin(i.width); |
1287 |
|
repaint(); |
1288 |
|
} |
1289 |
|
|
1290 |
|
/** |
1291 |
|
* Set the value of the {@link #preferredScrollableViewportSize} property. |
1292 |
|
* |
1293 |
|
* @param p The new value of the preferredScrollableViewportSize property |
1294 |
|
*/ |
1295 |
|
public void setPreferredScrollableViewportSize(Dimension p) |
1296 |
|
{ |
1297 |
|
preferredScrollableViewportSize = p; |
1298 |
|
revalidate(); |
1299 |
|
repaint(); |
1300 |
|
} |
1301 |
|
|
1302 |
|
/** |
1303 |
|
* <p>Set the value of the {@link #selectionBackground} property.</p> |
1304 |
|
* |
1305 |
|
* <p>Fire a PropertyChangeEvent with name {@link |
1306 |
|
* #SELECTION_BACKGROUND_CHANGED_PROPERTY} to registered listeners, if |
1307 |
|
* selectionBackground changed.</p> |
1308 |
|
* |
1309 |
|
* @param s The new value of the selectionBackground property |
1310 |
|
*/ |
1311 |
|
public void setSelectionBackground(Color s) |
1312 |
|
{ |
1313 |
|
Color tmp = selectionBackground; |
1314 |
|
selectionBackground = s; |
1315 |
|
if (((tmp == null && s != null) |
1316 |
|
|| (s == null && tmp != null) |
1317 |
|
|| (tmp != null && s != null && !tmp.equals(s)))) |
1318 |
|
firePropertyChange(SELECTION_BACKGROUND_CHANGED_PROPERTY, tmp, s); |
1319 |
|
repaint(); |
1320 |
|
} |
1321 |
|
|
1322 |
|
/** |
1323 |
|
* <p>Set the value of the {@link #selectionForeground} property.</p> |
1324 |
|
* |
1325 |
|
* <p>Fire a PropertyChangeEvent with name {@link |
1326 |
|
* SELECTION_FOREGROUND_CHANGED_PROPERTY} to registered listeners, if |
1327 |
|
* selectionForeground changed.</p> |
1328 |
|
* |
1329 |
|
* @param s The new value of the selectionForeground property |
1330 |
|
*/ |
1331 |
|
public void setSelectionForeground(Color s) |
1332 |
|
{ |
1333 |
|
Color tmp = selectionForeground; |
1334 |
|
selectionForeground = s; |
1335 |
|
if (((tmp == null && s != null) |
1336 |
|
|| (s == null && tmp != null) |
1337 |
|
|| (tmp != null && s != null && !tmp.equals(s)))) |
1338 |
|
firePropertyChange(SELECTION_FOREGROUND_CHANGED_PROPERTY, tmp, s); |
1339 |
|
repaint(); |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1343 |
|
* Set the value of the {@link #showGrid} property. |
1344 |
|
* |
1345 |
|
* @param s The new value of the showGrid property |
1346 |
|
*/ |
1347 |
|
public void setShowGrid(boolean s) |
1348 |
|
{ |
1349 |
|
setShowVerticalLines(s); |
1350 |
|
setShowHorizontalLines(s); |
1351 |
|
} |
1352 |
|
|
1353 |
|
/** |
1354 |
|
* Set the value of the {@link #showHorizontalLines} property. |
1355 |
|
* |
1356 |
|
* @param s The new value of the showHorizontalLines property |
1357 |
|
*/ |
1358 |
|
public void setShowHorizontalLines(boolean s) |
1359 |
|
{ |
1360 |
|
showHorizontalLines = s; |
1361 |
|
repaint(); |
1362 |
|
} |
1363 |
|
|
1364 |
|
/** |
1365 |
|
* Set the value of the {@link #showVerticalLines} property. |
1366 |
|
* |
1367 |
|
* @param s The new value of the showVerticalLines property |
1368 |
|
*/ |
1369 |
|
public void setShowVerticalLines(boolean s) |
1370 |
|
{ |
1371 |
|
showVerticalLines = s; |
1372 |
|
repaint(); |
1373 |
|
} |
1374 |
|
|
1375 |
|
/** |
1376 |
|
* Set the value of the {@link #tableHeader} property. |
1377 |
|
* |
1378 |
|
* @param t The new value of the tableHeader property |
1379 |
|
*/ |
1380 |
|
public void setTableHeader(JTableHeader t) |
1381 |
|
{ |
1382 |
|
if (tableHeader != null) |
1383 |
|
tableHeader.setTable(null); |
1384 |
|
tableHeader = t; |
1385 |
|
if (tableHeader != null) |
1386 |
|
tableHeader.setTable(this); |
1387 |
|
revalidate(); |
1388 |
|
repaint(); |
1389 |
|
} |
1390 |
|
|
1391 |
|
protected void configureEnclosingScrollPane() |
1392 |
|
{ |
1393 |
|
JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this); |
1394 |
|
if (jsp != null && tableHeader != null) |
1395 |
|
{ |
1396 |
|
jsp.setColumnHeaderView(tableHeader); |
1397 |
|
} |
1398 |
|
} |
1399 |
|
|
1400 |
|
protected void unconfigureEnclosingScrollPane() |
1401 |
|
{ |
1402 |
|
JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this); |
1403 |
|
if (jsp != null) |
1404 |
|
{ |
1405 |
|
jsp.setColumnHeaderView(null); |
1406 |
|
} |
1407 |
|
} |
1408 |
|
|
1409 |
|
|
1410 |
|
public void addNotify() |
1411 |
|
{ |
1412 |
|
super.addNotify(); |
1413 |
|
configureEnclosingScrollPane(); |
1414 |
|
} |
1415 |
|
|
1416 |
|
public void removeNotify() |
1417 |
|
{ |
1418 |
|
super.addNotify(); |
1419 |
|
unconfigureEnclosingScrollPane(); |
1420 |
|
} |
1421 |
|
|
1422 |
|
|
1423 |
|
/** |
1424 |
|
* Sun javadocs describe an unusual implementation of |
1425 |
|
* <code>doLayout</code> which involves some private interfaces. We try |
1426 |
|
* to implement the same algorithm as is documented, but using the |
1427 |
|
* columnModel directly. We still use a private helper method, but it has |
1428 |
|
* a simpler signature. |
1429 |
|
*/ |
1430 |
|
|
1431 |
|
private void distributeSpill(TableColumn[] cols, int spill) |
1432 |
|
{ |
1433 |
|
int MIN = 0; |
1434 |
|
int MAX = 0; |
1435 |
|
int PREF = 0; |
1436 |
|
|
1437 |
|
int[] min = new int[cols.length]; |
1438 |
|
int[] max = new int[cols.length]; |
1439 |
|
int[] pref = new int[cols.length]; |
1440 |
|
|
1441 |
|
for (int i = 0; i < cols.length; ++i) |
1442 |
|
{ |
1443 |
|
pref[i] = cols[i].getPreferredWidth(); |
1444 |
|
min[i] = cols[i].getMinWidth(); |
1445 |
|
max[i] = cols[i].getMaxWidth(); |
1446 |
|
PREF += pref[i]; |
1447 |
|
MIN += min[i]; |
1448 |
|
MAX += max[i]; |
1449 |
|
} |
1450 |
|
|
1451 |
|
for (int i = 0; i < cols.length; ++i) |
1452 |
|
{ |
1453 |
|
int adj = 0; |
1454 |
|
if (spill > 0) |
1455 |
|
adj = (spill * (pref[i] - min[i])) / (PREF - MIN); |
1456 |
|
else |
1457 |
|
adj = (spill * (max[i] - pref[i])) / (MAX - PREF); |
1458 |
|
cols[i].setWidth(pref[i] + adj); |
1459 |
|
} |
1460 |
|
} |
1461 |
|
|
1462 |
|
public void doLayout() |
1463 |
|
{ |
1464 |
|
TableColumn resizingColumn = null; |
1465 |
|
|
1466 |
|
int ncols = getColumnCount(); |
1467 |
|
if (ncols < 1) |
1468 |
|
return; |
1469 |
|
|
1470 |
|
int[] pref = new int[ncols]; |
1471 |
|
int prefSum = 0; |
1472 |
|
int rCol = -1; |
1473 |
|
|
1474 |
|
if (tableHeader != null) |
1475 |
|
resizingColumn = tableHeader.getResizingColumn(); |
1476 |
|
|
1477 |
|
for (int i = 0; i < ncols; ++i) |
1478 |
|
{ |
1479 |
|
TableColumn col = columnModel.getColumn(i); |
1480 |
|
int p = col.getWidth(); |
1481 |
|
pref[i] = p; |
1482 |
|
prefSum += p; |
1483 |
|
if (resizingColumn == col) |
1484 |
|
rCol = i; |
1485 |
|
} |
1486 |
|
|
1487 |
|
int spill = prefSum - getWidth(); |
1488 |
|
|
1489 |
|
if (resizingColumn != null) |
1490 |
|
{ |
1491 |
|
TableColumn col; |
1492 |
|
TableColumn [] cols; |
1493 |
|
|
1494 |
|
switch (getAutoResizeMode()) |
1495 |
|
{ |
1496 |
|
case AUTO_RESIZE_LAST_COLUMN: |
1497 |
|
col = columnModel.getColumn(ncols-1); |
1498 |
|
col.setWidth(col.getPreferredWidth() + spill); |
1499 |
|
break; |
1500 |
|
|
1501 |
|
case AUTO_RESIZE_NEXT_COLUMN: |
1502 |
|
col = columnModel.getColumn(ncols-1); |
1503 |
|
col.setWidth(col.getPreferredWidth() + spill); |
1504 |
|
break; |
1505 |
|
|
1506 |
|
case AUTO_RESIZE_ALL_COLUMNS: |
1507 |
|
cols = new TableColumn[ncols]; |
1508 |
|
for (int i = 0; i < ncols; ++i) |
1509 |
|
cols[i] = columnModel.getColumn(i); |
1510 |
|
distributeSpill(cols, spill); |
1511 |
|
break; |
1512 |
|
|
1513 |
|
case AUTO_RESIZE_SUBSEQUENT_COLUMNS: |
1514 |
|
cols = new TableColumn[ncols]; |
1515 |
|
for (int i = rCol; i < ncols; ++i) |
1516 |
|
cols[i] = columnModel.getColumn(i); |
1517 |
|
distributeSpill(cols, spill); |
1518 |
|
break; |
1519 |
|
|
1520 |
|
case AUTO_RESIZE_OFF: |
1521 |
|
default: |
1522 |
|
} |
1523 |
|
} |
1524 |
|
else |
1525 |
|
{ |
1526 |
|
TableColumn [] cols = new TableColumn[ncols]; |
1527 |
|
for (int i = 0; i < ncols; ++i) |
1528 |
|
cols[i] = columnModel.getColumn(i); |
1529 |
|
distributeSpill(cols, spill); |
1530 |
|
} |
1531 |
|
} |
1532 |
|
|
1533 |
|
public void sizeColumnsToFit(boolean lastColumnOnly) |
1534 |
|
{ |
1535 |
|
doLayout(); |
1536 |
|
} |
1537 |
|
|
1538 |
|
public void sizeColumnsToFit(int resizingColumn) |
1539 |
|
{ |
1540 |
|
doLayout(); |
1541 |
|
} |
1542 |
|
|
1543 |
|
|
1544 |
|
public String getUIClassID() |
1545 |
|
{ |
1546 |
|
return "TableUI"; |
1547 |
|
} |
1548 |
|
|
1549 |
|
public TableUI getUI() |
1550 |
|
{ |
1551 |
|
return (TableUI) ui; |
1552 |
|
} |
1553 |
|
|
1554 |
|
public void updateUI() |
1555 |
|
{ |
1556 |
|
setUI((TableUI) UIManager.getUI(this)); |
1557 |
|
revalidate(); |
1558 |
|
repaint(); |
1559 |
|
} |
1560 |
|
|
1561 |
} |
} |