This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: [Patch][gui] Fixes to JTable
On Wed, 2004-12-22 at 13:13 -0500, Olga Rodimina wrote:
> Hi,
>
> Here is the patch that makes improvements to JTable made my Michael
> Koch.
>
> I've added the exception handling for add/set/remove selection interval
> methods. I'll be committing this patch to java-gui-branch.
Forgot to post ChangeLog entry and patch separately.
* javax/swing/JTable.java
(editorComp): New field.
(JTable): Initialize local variables and call updateUI
(selectionBackground): Make protected.
(selectionForeground): Likewise.
(initializeLocalVars): Create default editors and renderers,
initialize editingColumn, editingRow variables.
(createDefaultEditors): New Method.
(createDefaultRenderers): Likewise.
(createDefaultListSelectionModel): Removed
(createDefaultSelectionModel): New Method.
(createDefaultTableHeader): Likewise
(removeColumn): Likewise.
(getEditingColumn): Likewise.
(setEditingColumn): Likewise.
(getEditingRow): Likewise.
(setEditingRow): Likewise.
(getEditorComponent): Likewise.
(isEditing): Likewise.
(setDefaultEditor): Likewise.
(addColumnSelectionInterval): Likewise.
(addRowSelectionInterval): Likewise.
(setColumnSelectionInterval): Likewise.
(setRowSelectionInterval): Likewise.
(removeColumnSelectionInterval): Likewise.
(removeRowSelectionInterval): Likewise.
(isColumnSelected): Likewise.
(isRowSelected): Likewise.
(isCellSelected): Likewise.
(selectAll): Likewise.
--
Olga Rodimina <rodimina@redhat.com>
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JTable.java,v
retrieving revision 1.4.18.12
diff -c -p -u -r1.4.18.12 JTable.java
--- javax/swing/JTable.java 17 Dec 2004 08:47:26 -0000 1.4.18.12
+++ javax/swing/JTable.java 22 Dec 2004 18:19:50 -0000
@@ -42,6 +42,7 @@ import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
+import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
@@ -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()
*/
@@ -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;
@@ -410,9 +441,10 @@ public class JTable extends JComponent
this.preferredScrollableViewportSize = new Dimension(450,400);
this.showHorizontalLines = true;
this.showVerticalLines = true;
+ this.editingColumn = -1;
+ this.editingRow = -1;
setIntercellSpacing(new Dimension(1,1));
- setTableHeader(new JTableHeader(columnModel));
- updateUI();
+ setTableHeader(createDefaultTableHeader());
}
/**
@@ -436,6 +468,16 @@ public class JTable extends JComponent
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>
@@ -444,7 +486,7 @@ public class JTable extends JComponent
{
return new JScrollPane(table);
}
-
+
protected TableColumnModel createDefaultColumnModel()
{
return new DefaultTableColumnModel();
@@ -455,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)
@@ -667,6 +714,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;
@@ -1083,6 +1131,12 @@ public class JTable extends JComponent
return tableHeader;
}
+ public void removeColumn(TableColumn column)
+ {
+ // FIXME: Implement me.
+ throw new Error("not implemented");
+ }
+
/**
* Set the value of the {@link #autoCreateColumnsFromModel} property.
*
@@ -1613,4 +1667,117 @@ public class JTable extends JComponent
{
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);
+ }
}