42 |
import java.awt.Component; |
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.Date; |
46 |
import java.util.Hashtable; |
import java.util.Hashtable; |
47 |
import java.util.Vector; |
import java.util.Vector; |
48 |
|
|
114 |
/** |
/** |
115 |
* A table mapping {@link java.lang.Class} objects to |
* A table mapping {@link java.lang.Class} objects to |
116 |
* {@link TableCellEditor} objects. This table is consulted by the |
* {@link TableCellEditor} objects. This table is consulted by the |
117 |
* |
* FIXME |
118 |
*/ |
*/ |
119 |
protected Hashtable defaultEditorsByColumnClass; |
protected Hashtable defaultEditorsByColumnClass; |
120 |
|
|
121 |
|
/** |
122 |
|
* A table mapping {@link java.lang.Class} objects to |
123 |
|
* {@link TableCellEditor} objects. This table is consulted by the |
124 |
|
* FIXME |
125 |
|
*/ |
126 |
protected Hashtable defaultRenderersByColumnClass; |
protected Hashtable defaultRenderersByColumnClass; |
127 |
|
|
128 |
|
/** |
129 |
|
* The column that is edited, -1 if the table is not edited currently. |
130 |
|
*/ |
131 |
protected int editingColumn; |
protected int editingColumn; |
132 |
|
|
133 |
|
/** |
134 |
|
* The row that is edited, -1 if the table is not edited currently. |
135 |
|
*/ |
136 |
protected int editingRow; |
protected int editingRow; |
137 |
|
|
138 |
/** |
/** |
139 |
|
* The component that is used for editing. |
140 |
|
* <code>null</code> if the table is not editing currently. |
141 |
|
* |
142 |
|
*/ |
143 |
|
protected transient Component editorComp; |
144 |
|
|
145 |
|
/** |
146 |
* Whether or not the table should automatically compute a matching |
* Whether or not the table should automatically compute a matching |
147 |
* {@link TableColumnModel} and assign it to the {@link #columnModel} |
* {@link TableColumnModel} and assign it to the {@link #columnModel} |
148 |
* property when the {@link #dataModel} property is changed. |
* property when the {@link #dataModel} property is changed. |
185 |
* |
* |
186 |
* @see #setRowMargin() |
* @see #setRowMargin() |
187 |
* @see #getRowHeight() |
* @see #getRowHeight() |
188 |
* @see #getInterCellSpacing() |
* @see #getIntercellSpacing() |
189 |
* @see #setInterCellSpacing() |
* @see #setIntercellSpacing() |
190 |
* @see TableColumnModel#getColumnMargin() |
* @see TableColumnModel#getColumnMargin() |
191 |
* @see TableColumnModel#setColumnMargin() |
* @see TableColumnModel#setColumnMargin() |
192 |
*/ |
*/ |
307 |
* @see #setSelectionBackground() |
* @see #setSelectionBackground() |
308 |
* @see #getSelectionBackground() |
* @see #getSelectionBackground() |
309 |
*/ |
*/ |
310 |
Color selectionBackground; |
protected Color selectionBackground; |
311 |
|
|
312 |
/** |
/** |
313 |
* The name carried in property change events when the {@link |
* The name carried in property change events when the {@link |
323 |
* @see #setSelectionForeground() |
* @see #setSelectionForeground() |
324 |
* @see #getSelectionForeground() |
* @see #getSelectionForeground() |
325 |
*/ |
*/ |
326 |
Color selectionForeground; |
protected Color selectionForeground; |
327 |
|
|
328 |
/** |
/** |
329 |
* The name carried in property change events when the |
* The name carried in property change events when the |
408 |
public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) |
public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) |
409 |
{ |
{ |
410 |
this.dataModel = dm == null ? createDefaultDataModel() : dm; |
this.dataModel = dm == null ? createDefaultDataModel() : dm; |
411 |
setSelectionModel(sm == null ? createDefaultListSelectionModel() : sm); |
setSelectionModel(sm == null ? createDefaultSelectionModel() : sm); |
412 |
|
|
413 |
this.columnModel = cm; |
this.columnModel = cm; |
414 |
|
initializeLocalVars(); |
415 |
|
updateUI(); |
416 |
|
} |
417 |
|
|
418 |
|
protected void initializeLocalVars() |
419 |
|
{ |
420 |
this.autoCreateColumnsFromModel = false; |
this.autoCreateColumnsFromModel = false; |
421 |
if (cm == null) |
if (columnModel == null) |
422 |
{ |
{ |
423 |
this.autoCreateColumnsFromModel = true; |
this.autoCreateColumnsFromModel = true; |
424 |
createColumnsFromModel(); |
createColumnsFromModel(); |
426 |
this.columnModel.addColumnModelListener(this); |
this.columnModel.addColumnModelListener(this); |
427 |
|
|
428 |
this.defaultRenderersByColumnClass = new Hashtable(); |
this.defaultRenderersByColumnClass = new Hashtable(); |
429 |
|
createDefaultRenderers(); |
430 |
|
|
431 |
this.defaultEditorsByColumnClass = new Hashtable(); |
this.defaultEditorsByColumnClass = new Hashtable(); |
432 |
|
createDefaultEditors(); |
433 |
|
|
434 |
this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS; |
this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS; |
435 |
this.rowHeight = 16; |
this.rowHeight = 16; |
441 |
this.preferredScrollableViewportSize = new Dimension(450,400); |
this.preferredScrollableViewportSize = new Dimension(450,400); |
442 |
this.showHorizontalLines = true; |
this.showHorizontalLines = true; |
443 |
this.showVerticalLines = true; |
this.showVerticalLines = true; |
444 |
|
this.editingColumn = -1; |
445 |
|
this.editingRow = -1; |
446 |
setIntercellSpacing(new Dimension(1,1)); |
setIntercellSpacing(new Dimension(1,1)); |
447 |
setTableHeader(new JTableHeader(columnModel)); |
setTableHeader(createDefaultTableHeader()); |
|
updateUI(); |
|
448 |
} |
} |
449 |
|
|
450 |
/** |
/** |
468 |
|
|
469 |
columnModel.addColumn(column); |
columnModel.addColumn(column); |
470 |
} |
} |
471 |
|
|
472 |
|
protected void createDefaultEditors() |
473 |
|
{ |
474 |
|
//FIXME: Create the editor object. |
475 |
|
} |
476 |
|
|
477 |
|
protected void createDefaultRenderers() |
478 |
|
{ |
479 |
|
//FIXME: Create the renderer object. |
480 |
|
} |
481 |
|
|
482 |
/** |
/** |
483 |
* @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code> |
* @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code> |
486 |
{ |
{ |
487 |
return new JScrollPane(table); |
return new JScrollPane(table); |
488 |
} |
} |
489 |
|
|
490 |
protected TableColumnModel createDefaultColumnModel() |
protected TableColumnModel createDefaultColumnModel() |
491 |
{ |
{ |
492 |
return new DefaultTableColumnModel(); |
return new DefaultTableColumnModel(); |
497 |
return new DefaultTableModel(); |
return new DefaultTableModel(); |
498 |
} |
} |
499 |
|
|
500 |
protected ListSelectionModel createDefaultListSelectionModel() |
protected ListSelectionModel createDefaultSelectionModel() |
501 |
{ |
{ |
502 |
return new DefaultListSelectionModel(); |
return new DefaultListSelectionModel(); |
503 |
} |
} |
504 |
|
|
505 |
|
protected JTableHeader createDefaultTableHeader() |
506 |
|
{ |
507 |
|
return new JTableHeader(columnModel); |
508 |
|
} |
509 |
|
|
510 |
private void createColumnsFromModel() |
private void createColumnsFromModel() |
511 |
{ |
{ |
512 |
if (dataModel == null) |
if (dataModel == null) |
714 |
return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); |
return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); |
715 |
else |
else |
716 |
{ |
{ |
717 |
|
// FIXME: We have at least an editor for Object.class in our defaults. |
718 |
TableCellEditor r = new DefaultCellEditor(new JTextField()); |
TableCellEditor r = new DefaultCellEditor(new JTextField()); |
719 |
defaultEditorsByColumnClass.put(columnClass, r); |
defaultEditorsByColumnClass.put(columnClass, r); |
720 |
return r; |
return r; |
1131 |
return tableHeader; |
return tableHeader; |
1132 |
} |
} |
1133 |
|
|
1134 |
|
public void removeColumn(TableColumn column) |
1135 |
|
{ |
1136 |
|
// FIXME: Implement me. |
1137 |
|
throw new Error("not implemented"); |
1138 |
|
} |
1139 |
|
|
1140 |
/** |
/** |
1141 |
* Set the value of the {@link #autoCreateColumnsFromModel} property. |
* Set the value of the {@link #autoCreateColumnsFromModel} property. |
1142 |
* |
* |
1667 |
{ |
{ |
1668 |
return dataModel.getColumnName(column); |
return dataModel.getColumnName(column); |
1669 |
} |
} |
1670 |
|
|
1671 |
|
public int getEditingColumn() |
1672 |
|
{ |
1673 |
|
return editingColumn; |
1674 |
|
} |
1675 |
|
|
1676 |
|
public void setEditingColumn(int column) |
1677 |
|
{ |
1678 |
|
editingColumn = column; |
1679 |
|
} |
1680 |
|
|
1681 |
|
public int getEditingRow() |
1682 |
|
{ |
1683 |
|
return editingRow; |
1684 |
|
} |
1685 |
|
|
1686 |
|
public void setEditingRow(int column) |
1687 |
|
{ |
1688 |
|
editingRow = column; |
1689 |
|
} |
1690 |
|
|
1691 |
|
public Component getEditorComponent() |
1692 |
|
{ |
1693 |
|
return editorComp; |
1694 |
|
} |
1695 |
|
|
1696 |
|
public boolean isEditing() |
1697 |
|
{ |
1698 |
|
return editorComp != null; |
1699 |
|
} |
1700 |
|
|
1701 |
|
public void setDefaultEditor(Class columnClass, TableCellEditor editor) |
1702 |
|
{ |
1703 |
|
if (editor != null) |
1704 |
|
defaultEditorsByColumnClass.put(columnClass, editor); |
1705 |
|
else |
1706 |
|
defaultEditorsByColumnClass.remove(columnClass); |
1707 |
|
} |
1708 |
|
|
1709 |
|
public void addColumnSelectionInterval(int index0, int index1) |
1710 |
|
{ |
1711 |
|
if ((index0 < 0 || index0 > (getColumnCount()-1) |
1712 |
|
|| index1 < 0 || index1 > (getColumnCount()-1))) |
1713 |
|
throw new IllegalArgumentException("Column index out of range."); |
1714 |
|
|
1715 |
|
getColumnModel().getSelectionModel().addSelectionInterval(index0, index1); |
1716 |
|
} |
1717 |
|
|
1718 |
|
public void addRowSelectionInterval(int index0, int index1) |
1719 |
|
{ |
1720 |
|
if ((index0 < 0 || index0 > (getRowCount()-1) |
1721 |
|
|| index1 < 0 || index1 > (getRowCount()-1))) |
1722 |
|
throw new IllegalArgumentException("Row index out of range."); |
1723 |
|
|
1724 |
|
getSelectionModel().addSelectionInterval(index0, index1); |
1725 |
|
} |
1726 |
|
|
1727 |
|
public void setColumnSelectionInterval(int index0, int index1) |
1728 |
|
{ |
1729 |
|
if ((index0 < 0 || index0 > (getColumnCount()-1) |
1730 |
|
|| index1 < 0 || index1 > (getColumnCount()-1))) |
1731 |
|
throw new IllegalArgumentException("Column index out of range."); |
1732 |
|
|
1733 |
|
getColumnModel().getSelectionModel().setSelectionInterval(index0, index1); |
1734 |
|
} |
1735 |
|
|
1736 |
|
public void setRowSelectionInterval(int index0, int index1) |
1737 |
|
{ |
1738 |
|
if ((index0 < 0 || index0 > (getRowCount()-1) |
1739 |
|
|| index1 < 0 || index1 > (getRowCount()-1))) |
1740 |
|
throw new IllegalArgumentException("Row index out of range."); |
1741 |
|
|
1742 |
|
getSelectionModel().setSelectionInterval(index0, index1); |
1743 |
|
} |
1744 |
|
|
1745 |
|
public void removeColumnSelectionInterval(int index0, int index1) |
1746 |
|
{ |
1747 |
|
if ((index0 < 0 || index0 > (getColumnCount()-1) |
1748 |
|
|| index1 < 0 || index1 > (getColumnCount()-1))) |
1749 |
|
throw new IllegalArgumentException("Column index out of range."); |
1750 |
|
|
1751 |
|
getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1); |
1752 |
|
} |
1753 |
|
|
1754 |
|
public void removeRowSelectionInterval(int index0, int index1) |
1755 |
|
{ |
1756 |
|
if ((index0 < 0 || index0 > (getRowCount()-1) |
1757 |
|
|| index1 < 0 || index1 > (getRowCount()-1))) |
1758 |
|
throw new IllegalArgumentException("Row index out of range."); |
1759 |
|
|
1760 |
|
getSelectionModel().removeSelectionInterval(index0, index1); |
1761 |
|
} |
1762 |
|
|
1763 |
|
public boolean isColumnSelected(int column) |
1764 |
|
{ |
1765 |
|
return getColumnModel().getSelectionModel().isSelectedIndex(column); |
1766 |
|
} |
1767 |
|
|
1768 |
|
public boolean isRowSelected(int row) |
1769 |
|
{ |
1770 |
|
return getSelectionModel().isSelectedIndex(row); |
1771 |
|
} |
1772 |
|
|
1773 |
|
public boolean isCellSelected(int row, int column) |
1774 |
|
{ |
1775 |
|
return isRowSelected(row) && isColumnSelected(column); |
1776 |
|
} |
1777 |
|
|
1778 |
|
public void selectAll() |
1779 |
|
{ |
1780 |
|
setColumnSelectionInterval(0, getColumnCount() - 1); |
1781 |
|
setRowSelectionInterval(0, getRowCount() - 1); |
1782 |
|
} |
1783 |
} |
} |