From a15f2847950126bc87751c54d977d2f4edd45c04 Mon Sep 17 00:00:00 2001 From: Johannes Dahlström Date: Wed, 4 Feb 2015 18:33:30 +0200 Subject: Grid columns can now be marked as non-editable (#16538) Non-editable columns are not assigned editor fields. When the editor is active, any non-editable content is not displayed (this should changein the future). This is separate from setting the property or editor field read-only - in those cases the field is still used to display the data which may not be desired and will fail if there is no converter. Also add Column.setEditorField(Field) and the corresponding getter. Change-Id: Ice17c357895cb63a8e1bfd6abaffc1d803399e98 --- .../vaadin/client/connectors/GridConnector.java | 1 + client/src/com/vaadin/client/widgets/Grid.java | 82 +++++++++++++++++++--- 2 files changed, 73 insertions(+), 10 deletions(-) (limited to 'client') diff --git a/client/src/com/vaadin/client/connectors/GridConnector.java b/client/src/com/vaadin/client/connectors/GridConnector.java index 827e366de0..8d383ab0ae 100644 --- a/client/src/com/vaadin/client/connectors/GridConnector.java +++ b/client/src/com/vaadin/client/connectors/GridConnector.java @@ -756,6 +756,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements column.setExpandRatio(state.expandRatio); column.setSortable(state.sortable); + column.setEditable(state.editable); column.setEditorConnector((AbstractFieldConnector) state.editorConnector); } diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index a870a732b5..303b94763d 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -1254,11 +1254,13 @@ public class Grid extends ResizeComposite implements /** * Returns the editor widget associated with the given column. If the - * editor is not active, returns null. + * editor is not active or the column is not + * {@link Grid.Column#isEditable() editable}, returns null. * * @param column * the column - * @return the widget if the editor is open, null otherwise + * @return the widget if the editor is open and the column is editable, + * null otherwise */ protected Widget getWidget(Column column) { return columnToWidget.get(column); @@ -1305,14 +1307,12 @@ public class Grid extends ResizeComposite implements editorOverlay.appendChild(cell); Column column = grid.getColumn(i); - if (column == grid.selectionColumn) { - continue; - } - - Widget editor = getHandler().getWidget(column); - if (editor != null) { - columnToWidget.put(column, editor); - attachWidget(editor, cell); + if (column.isEditable()) { + Widget editor = getHandler().getWidget(column); + if (editor != null) { + columnToWidget.put(column, editor); + attachWidget(editor, cell); + } } } @@ -1987,6 +1987,7 @@ public class Grid extends ResizeComposite implements } public final class SelectionColumn extends Column { + private boolean initDone = false; SelectionColumn(final Renderer selectColumnRenderer) { @@ -2022,6 +2023,8 @@ public class Grid extends ResizeComposite implements setWidth(-1); + setEditable(false); + initDone = true; } @@ -2074,6 +2077,17 @@ public class Grid extends ResizeComposite implements public double getMinimumWidth() { return -1; } + + @Override + public Column setEditable(boolean editable) { + if (initDone) { + throw new UnsupportedOperationException( + "can't set the selection column editable"); + } + super.setEditable(editable); + return this; + } + } /** @@ -2738,6 +2752,8 @@ public class Grid extends ResizeComposite implements private boolean sortable = false; + private boolean editable = true; + private String headerCaption = ""; private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH; @@ -3159,6 +3175,43 @@ public class Grid extends ResizeComposite implements return expandRatio; } + /** + * Sets whether the values in this column should be editable by the user + * when the row editor is active. By default columns are editable. + * + * @param editable + * {@code true} to set this column editable, {@code false} + * otherwise + * @return this column + * + * @throws IllegalStateException + * if the editor is currently active + * + * @see Grid#editRow(int) + * @see Grid#isEditorActive() + */ + public Column setEditable(boolean editable) { + if (editable != this.editable && grid.isEditorActive()) { + throw new IllegalStateException( + "Cannot change column editable status while the editor is active"); + } + this.editable = editable; + return this; + } + + /** + * Returns whether the values in this column are editable by the user + * when the row editor is active. + * + * @return {@code true} if this column is editable, {@code false} + * otherwise + * + * @see #setEditable(boolean) + */ + public boolean isEditable() { + return editable; + } + private void scheduleColumnWidthRecalculator() { if (grid != null) { grid.autoColumnWidthsRecalculator.schedule(); @@ -5781,6 +5834,15 @@ public class Grid extends ResizeComposite implements editor.editRow(rowIndex); } + /** + * Returns whether the editor is currently open on some row. + * + * @return {@code true} if the editor is active, {@code false} otherwise. + */ + public boolean isEditorActive() { + return editor.getState() != State.INACTIVE; + } + /** * Saves any unsaved changes in the editor to the data source. * -- cgit v1.2.3