Author: tchemit Date: 2012-07-27 16:33:56 +0200 (Fri, 27 Jul 2012) New Revision: 2443 Url: http://nuiton.org/repositories/revision/jaxx/2443 Log: fixes #2212: Add new methods in SwingUtil fixes #2211: SwingUtil.editCell method does not work properly Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2012-07-19 09:44:56 UTC (rev 2442) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2012-07-27 14:33:56 UTC (rev 2443) @@ -60,6 +60,7 @@ import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; +import javax.swing.table.TableModel; import javax.swing.text.AbstractDocument; import javax.swing.text.JTextComponent; import javax.swing.tree.TreeNode; @@ -1237,40 +1238,62 @@ } /** - * Used to edit cell + * Used to edit a cell of a given table. * - * @param table to edit - * @param row of cell to editing - * @param colummn of cell to editing - * @return false if for any reason the cell cannot be edited, + * @param table the table to edit + * @param row row index of cell to editing + * @param colummn column index of cell to editing + * @return {@code false} if for any reason the cell cannot be edited, * or if the indices are invalid */ public static boolean editCell(JTable table, int row, int colummn) { + boolean result = false; if (table.isCellEditable(row, colummn)) { - // get table informations - int selectedColumn = table.getSelectedColumn(); - int selectedRow = table.getSelectedRow(); + if (table.isEditing()) { - // stop edition - TableCellEditor cellEditor = table.getCellEditor(selectedRow, selectedColumn); - cellEditor.stopCellEditing(); + int editingRow = table.getEditingRow(); + int editingColumn = table.getEditingColumn(); + // stop edition + TableCellEditor cellEditor = table.getCellEditor(editingRow, + editingColumn); + cellEditor.stopCellEditing(); + } + // select row table.setColumnSelectionInterval(colummn, colummn); table.setRowSelectionInterval(row, row); // edit cell - boolean result = table.editCellAt(row, colummn, new EventObject(table)); + result = table.editCellAt(row, colummn, new EventObject(table)); Component component = table.getEditorComponent(); component.requestFocus(); if (log.isDebugEnabled()) { - log.debug("Select row[" + row + "] column[" + colummn + "] return : " + result); + log.debug("Select row[" + row + "] column[" + colummn + + "] return : " + result); } - return result; } - return false; + return result; } + + public static void ensureRowIndex(TableModel model, int rowIndex) + throws ArrayIndexOutOfBoundsException { + if (rowIndex < -1 || rowIndex >= model.getRowCount()) { + throw new ArrayIndexOutOfBoundsException( + "the rowIndex was " + rowIndex + ", but should be int [0," + + (model.getRowCount() - 1) + "]"); + } + } + + public static void ensureColumnIndex(TableModel model, int index) + throws ArrayIndexOutOfBoundsException { + if (index < -1 || index >= model.getColumnCount()) { + throw new ArrayIndexOutOfBoundsException( + "the columnIndex was " + index + ", but should be int [0," + + (model.getColumnCount() - 1) + "]"); + } + } }