diff options
Diffstat (limited to 'libjava/javax/swing/JTable.java')
-rw-r--r-- | libjava/javax/swing/JTable.java | 355 |
1 files changed, 318 insertions, 37 deletions
diff --git a/libjava/javax/swing/JTable.java b/libjava/javax/swing/JTable.java index ae2c4af..ca38aa0 100644 --- a/libjava/javax/swing/JTable.java +++ b/libjava/javax/swing/JTable.java @@ -1,5 +1,5 @@ /* JTable.java -- - Copyright (C) 2002, 2004 Free Software Foundation, Inc. + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -41,9 +41,10 @@ package javax.swing; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.Point; import java.awt.Rectangle; -import java.util.Vector; import java.util.Hashtable; +import java.util.Vector; import javax.accessibility.Accessible; import javax.accessibility.AccessibleContext; @@ -113,14 +114,35 @@ public class JTable extends JComponent /** * A table mapping {@link java.lang.Class} objects to * {@link TableCellEditor} objects. This table is consulted by the - * + * FIXME */ protected Hashtable defaultEditorsByColumnClass; + + /** + * A table mapping {@link java.lang.Class} objects to + * {@link TableCellEditor} objects. This table is consulted by the + * FIXME + */ protected Hashtable defaultRenderersByColumnClass; + + /** + * The column that is edited, -1 if the table is not edited currently. + */ protected int editingColumn; + + /** + * The row that is edited, -1 if the table is not edited currently. + */ protected int editingRow; /** + * The component that is used for editing. + * <code>null</code> if the table is not editing currently. + * + */ + protected transient Component editorComp; + + /** * Whether or not the table should automatically compute a matching * {@link TableColumnModel} and assign it to the {@link #columnModel} * property when the {@link #dataModel} property is changed. @@ -163,8 +185,8 @@ public class JTable extends JComponent * * @see #setRowMargin() * @see #getRowHeight() - * @see #getInterCellSpacing() - * @see #setInterCellSpacing() + * @see #getIntercellSpacing() + * @see #setIntercellSpacing() * @see TableColumnModel#getColumnMargin() * @see TableColumnModel#setColumnMargin() */ @@ -257,7 +279,7 @@ public class JTable extends JComponent * @see #setDragEnabled() * @see #getDragEnabled() */ - protected boolean dragEnabled; + private boolean dragEnabled; /** * The color to paint the grid lines of the table, when either {@link @@ -275,7 +297,7 @@ public class JTable extends JComponent * @see #setPreferredScrollableViewportSize() * @see #getPreferredScrollableViewportSize() */ - protected Dimension preferredScrollableViewportSize; + protected Dimension preferredViewportSize; /** * The color to paint the background of selected cells. Fires a property @@ -285,7 +307,7 @@ public class JTable extends JComponent * @see #setSelectionBackground() * @see #getSelectionBackground() */ - Color selectionBackground; + protected Color selectionBackground; /** * The name carried in property change events when the {@link @@ -301,7 +323,7 @@ public class JTable extends JComponent * @see #setSelectionForeground() * @see #getSelectionForeground() */ - Color selectionForeground; + protected Color selectionForeground; /** * The name carried in property change events when the @@ -386,11 +408,17 @@ public class JTable extends JComponent public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) { this.dataModel = dm == null ? createDefaultDataModel() : dm; - setSelectionModel(sm == null ? createDefaultListSelectionModel() : sm); + setSelectionModel(sm == null ? createDefaultSelectionModel() : sm); this.columnModel = cm; + initializeLocalVars(); + updateUI(); + } + + protected void initializeLocalVars() + { this.autoCreateColumnsFromModel = false; - if (cm == null) + if (columnModel == null) { this.autoCreateColumnsFromModel = true; createColumnsFromModel(); @@ -398,7 +426,10 @@ public class JTable extends JComponent this.columnModel.addColumnModelListener(this); this.defaultRenderersByColumnClass = new Hashtable(); + createDefaultRenderers(); + this.defaultEditorsByColumnClass = new Hashtable(); + createDefaultEditors(); this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS; this.rowHeight = 16; @@ -407,12 +438,13 @@ public class JTable extends JComponent // this.accessibleContext = new AccessibleJTable(); this.cellEditor = null; this.dragEnabled = false; - this.preferredScrollableViewportSize = new Dimension(450,400); + this.preferredViewportSize = new Dimension(450,400); this.showHorizontalLines = true; this.showVerticalLines = true; - setInterCellSpacing(new Dimension(1,1)); - setTableHeader(new JTableHeader(columnModel)); - updateUI(); + this.editingColumn = -1; + this.editingRow = -1; + setIntercellSpacing(new Dimension(1,1)); + setTableHeader(createDefaultTableHeader()); } /** @@ -426,6 +458,27 @@ public class JTable extends JComponent this(new DefaultTableModel(data, columnNames)); } + public void addColumn(TableColumn column) + { + if (column.getHeaderValue() == null) + { + String name = getColumnName(column.getModelIndex()); + column.setHeaderValue(name); + } + + columnModel.addColumn(column); + } + + protected void createDefaultEditors() + { + //FIXME: Create the editor object. + } + + protected void createDefaultRenderers() + { + //FIXME: Create the renderer object. + } + /** * @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code> */ @@ -433,7 +486,7 @@ public class JTable extends JComponent { return new JScrollPane(table); } - + protected TableColumnModel createDefaultColumnModel() { return new DefaultTableColumnModel(); @@ -444,11 +497,16 @@ public class JTable extends JComponent return new DefaultTableModel(); } - protected ListSelectionModel createDefaultListSelectionModel() + protected ListSelectionModel createDefaultSelectionModel() { return new DefaultListSelectionModel(); } + protected JTableHeader createDefaultTableHeader() + { + return new JTableHeader(columnModel); + } + private void createColumnsFromModel() { if (dataModel == null) @@ -514,6 +572,58 @@ public class JTable extends JComponent repaint(); } + /** + * Returns index of the column that contains specified point + * or -1 if this table doesn't contain this point. + * + * @param point point to identify the column + * @return index of the column that contains specified point or + * -1 if this table doesn't contain this point. + */ + public int columnAtPoint(Point point) + { + int x0 = getLocation().x; + int ncols = getColumnCount(); + Dimension gap = getIntercellSpacing(); + TableColumnModel cols = getColumnModel(); + int x = point.x; + + for (int i = 0; i < ncols; ++i) + { + int width = cols.getColumn(i).getWidth() + (gap == null ? 0 : gap.width); + if (0 <= x && x < width) + return i; + x -= width; + } + + return -1; + } + + /** + * Returns index of the row that contains specified point or + * -1 if this table doesn't contain this point. + * + * @param point point to identify the row + * @return index of the row that contains specified point or + * -1 if this table doesn't contain this point. + */ + public int rowAtPoint(Point point) + { + int y0 = getLocation().y; + int nrows = getRowCount(); + Dimension gap = getIntercellSpacing(); + int height = getRowHeight() + (gap == null ? 0 : gap.height); + int y = point.y; + + for (int i = 0; i < nrows; ++i) + { + if (0 <= y && y < height) + return i; + y -= height; + } + + return -1; + } /** * Calculate the visible rectangle for a particular row and column. The @@ -656,6 +766,7 @@ public class JTable extends JComponent return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass); else { + // FIXME: We have at least an editor for Object.class in our defaults. TableCellEditor r = new DefaultCellEditor(new JTextField()); defaultEditorsByColumnClass.put(columnClass, r); return r; @@ -729,7 +840,7 @@ public class JTable extends JComponent return renderer.getTableCellRendererComponent(this, dataModel.getValueAt(row, - convertColumnIndexToView(column)), + convertColumnIndexToModel(column)), isSelected, false, // hasFocus row, column); @@ -808,13 +919,13 @@ public class JTable extends JComponent /** * Get the value of the {@link #columnCount} property by - * delegation to the @{link #dataModel} field. + * delegation to the @{link #columnModel} field. * * @return The current value of the columnCount property */ public int getColumnCount() { - return dataModel.getColumnCount(); + return columnModel.getColumnCount(); } /** @@ -863,11 +974,11 @@ public class JTable extends JComponent break; case ListSelectionModel.SINGLE_INTERVAL_SELECTION: - sum = hi - lo; + sum = hi - lo + 1; break; case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: - for (int i = lo; i < hi; ++i) + for (int i = lo; i <= hi; ++i) if (lsm.isSelectedIndex(i)) ++sum; break; @@ -894,12 +1005,12 @@ public class JTable extends JComponent break; case ListSelectionModel.SINGLE_INTERVAL_SELECTION: - for (int i = lo; i < hi; ++i) + for (int i = lo; i <= hi; ++i) ret[j++] = i; break; case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: - for (int i = lo; i < hi; ++i) + for (int i = lo; i <= hi; ++i) if (lsm.isSelectedIndex(i)) ret[j++] = i; break; @@ -1003,23 +1114,23 @@ public class JTable extends JComponent } /** - * Get the value of the {@link #interCellSpacing} property. + * Get the value of the {@link #intercellSpacing} property. * * @return The current value of the property */ - public Dimension getInterCellSpacing() + public Dimension getIntercellSpacing() { return new Dimension(columnModel.getColumnMargin(), rowMargin); } /** - * Get the value of the {@link #preferredScrollableViewportSize} property. + * Get the value of the {@link #preferredViewportSize} property. * * @return The current value of the property */ public Dimension getPreferredScrollableViewportSize() { - return preferredScrollableViewportSize; + return preferredViewportSize; } /** @@ -1073,6 +1184,27 @@ public class JTable extends JComponent } /** + * Removes specified column from displayable columns of this table. + * + * @param column column to removed + */ + public void removeColumn(TableColumn column) + { + columnModel.removeColumn(column); + } + + /** + * Moves column at the specified index to new given location. + * + * @param column index of the column to move + * @param targetColumn index specifying new location of the column + */ + public void moveColumn(int column,int targetColumn) + { + columnModel.moveColumn(column, targetColumn); + } + + /** * Set the value of the {@link #autoCreateColumnsFromModel} property. * * @param a The new value of the autoCreateColumnsFromModel property @@ -1101,6 +1233,9 @@ public class JTable extends JComponent */ public void setRowHeight(int r) { + if (rowHeight < 1) + throw new IllegalArgumentException(); + rowHeight = r; revalidate(); repaint(); @@ -1246,13 +1381,16 @@ public class JTable extends JComponent /** * Set the value of the {@link #selectionMode} property by - * delegation to the {@link #selectionModel} field. + * delegation to the {@link #selectionModel} field. The same selection + * mode is set for row and column selection models. * * @param s The new value of the property */ public void setSelectionMode(int s) - { - selectionModel.setSelectionMode(s); + { + selectionModel.setSelectionMode(s); + columnModel.getSelectionModel().setSelectionMode(s); + repaint(); } @@ -1297,11 +1435,11 @@ public class JTable extends JComponent } /** - * Set the value of the {@link #interCellSpacing} property. + * Set the value of the {@link #intercellSpacing} property. * - * @param i The new value of the interCellSpacing property + * @param i The new value of the intercellSpacing property */ - public void setInterCellSpacing(Dimension i) + public void setIntercellSpacing(Dimension i) { rowMargin = i.height; columnModel.setColumnMargin(i.width); @@ -1309,13 +1447,13 @@ public class JTable extends JComponent } /** - * Set the value of the {@link #preferredScrollableViewportSize} property. + * Set the value of the {@link #preferredViewportSize} property. * - * @param p The new value of the preferredScrollableViewportSize property + * @param p The new value of the preferredViewportSize property */ public void setPreferredScrollableViewportSize(Dimension p) { - preferredScrollableViewportSize = p; + preferredViewportSize = p; revalidate(); repaint(); } @@ -1551,11 +1689,17 @@ public class JTable extends JComponent } } + /** + * @deprecated Replaced by <code>doLayout()</code> + */ public void sizeColumnsToFit(boolean lastColumnOnly) { doLayout(); } + /** + * Obsolete since JDK 1.4. Please use <code>doLayout()</code>. + */ public void sizeColumnsToFit(int resizingColumn) { doLayout(); @@ -1593,4 +1737,141 @@ public class JTable extends JComponent repaint(); } + public Class getColumnClass(int column) + { + return dataModel.getColumnClass(column); + } + + public String getColumnName(int column) + { + return dataModel.getColumnName(column); + } + + public int getEditingColumn() + { + return editingColumn; + } + + public void setEditingColumn(int column) + { + editingColumn = column; + } + + public int getEditingRow() + { + return editingRow; + } + + public void setEditingRow(int column) + { + editingRow = column; + } + + public Component getEditorComponent() + { + return editorComp; + } + + public boolean isEditing() + { + return editorComp != null; + } + + public void setDefaultEditor(Class columnClass, TableCellEditor editor) + { + if (editor != null) + defaultEditorsByColumnClass.put(columnClass, editor); + else + defaultEditorsByColumnClass.remove(columnClass); + } + + public void addColumnSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getColumnCount()-1) + || index1 < 0 || index1 > (getColumnCount()-1))) + throw new IllegalArgumentException("Column index out of range."); + + getColumnModel().getSelectionModel().addSelectionInterval(index0, index1); + } + + public void addRowSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getRowCount()-1) + || index1 < 0 || index1 > (getRowCount()-1))) + throw new IllegalArgumentException("Row index out of range."); + + getSelectionModel().addSelectionInterval(index0, index1); + } + + public void setColumnSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getColumnCount()-1) + || index1 < 0 || index1 > (getColumnCount()-1))) + throw new IllegalArgumentException("Column index out of range."); + + getColumnModel().getSelectionModel().setSelectionInterval(index0, index1); + } + + public void setRowSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getRowCount()-1) + || index1 < 0 || index1 > (getRowCount()-1))) + throw new IllegalArgumentException("Row index out of range."); + + getSelectionModel().setSelectionInterval(index0, index1); + } + + public void removeColumnSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getColumnCount()-1) + || index1 < 0 || index1 > (getColumnCount()-1))) + throw new IllegalArgumentException("Column index out of range."); + + getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1); + } + + public void removeRowSelectionInterval(int index0, int index1) + { + if ((index0 < 0 || index0 > (getRowCount()-1) + || index1 < 0 || index1 > (getRowCount()-1))) + throw new IllegalArgumentException("Row index out of range."); + + getSelectionModel().removeSelectionInterval(index0, index1); + } + + public boolean isColumnSelected(int column) + { + return getColumnModel().getSelectionModel().isSelectedIndex(column); + } + + public boolean isRowSelected(int row) + { + return getSelectionModel().isSelectedIndex(row); + } + + public boolean isCellSelected(int row, int column) + { + return isRowSelected(row) && isColumnSelected(column); + } + + public void selectAll() + { + setColumnSelectionInterval(0, getColumnCount() - 1); + setRowSelectionInterval(0, getRowCount() - 1); + } + + public Object getValueAt(int row, int column) + { + return dataModel.getValueAt(row, convertColumnIndexToModel(column)); + } + + public void setValueAt(Object value, int row, int column) + { + dataModel.setValueAt(value, row, convertColumnIndexToModel(column)); + } + + public TableColumn getColumn(Object identifier) + { + return columnModel.getColumn(columnModel.getColumnIndex(identifier)); + } } |