From 16c67cfab9b3dd2dbf324caa612fa3a2d15550d0 Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Sun, 22 Mar 2015 22:20:57 +0200 Subject: Grid column hiding info from client to server #(17023) Fixes mismatched client-server API regarding ColumnVisibilityChangeEvent Adds and removes the column hiding toggle as needed when columns added / removed. Known bug when a hidable column added, column toggle won't get the caption of column. Change-Id: I708e19432dc822f713bf11f5b8e6eadb528a3961 --- server/src/com/vaadin/ui/Grid.java | 65 ++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 7 deletions(-) (limited to 'server') diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 4a52dba173..e093a99159 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -182,7 +182,7 @@ public class Grid extends AbstractComponent implements SelectionNotifier, } /** - * An event that is fired when a column becomes hidden or unhidden. + * An event that is fired when a column's visibility changes. * * @since */ @@ -190,6 +190,7 @@ public class Grid extends AbstractComponent implements SelectionNotifier, private final Column column; private final boolean userOriginated; + private final boolean hidden; /** * Constructor for a column visibility change event. @@ -198,27 +199,41 @@ public class Grid extends AbstractComponent implements SelectionNotifier, * the grid from which this event originates * @param column * the column that changed its visibility + * @param hidden + * true if the column was hidden, + * false if it became visible * @param isUserOriginated * true iff the event was triggered by an UI * interaction */ public ColumnVisibilityChangeEvent(Grid source, Column column, - boolean isUserOriginated) { + boolean hidden, boolean isUserOriginated) { super(source); this.column = column; + this.hidden = hidden; userOriginated = isUserOriginated; } /** - * Gets the column that became hidden or unhidden. + * Gets the column that became hidden or visible. * - * @return the column that became hidden or unhidden. + * @return the column that became hidden or visible. * @see Column#isHidden() */ public Column getColumn() { return column; } + /** + * Was the column set hidden or visible. + * + * @return true if the column was hidden false + * if it was set visible + */ + public boolean isHidden() { + return hidden; + } + /** * Returns true if the column reorder was done by the user, * false if not and it was triggered by server side code. @@ -2828,7 +2843,7 @@ public class Grid extends AbstractComponent implements SelectionNotifier, if (hidden != getState().hidden) { getState().hidden = hidden; grid.markAsDirty(); - grid.fireColumnVisibilityChangeEvent(this, false); + grid.fireColumnVisibilityChangeEvent(this, hidden, false); } } @@ -3358,6 +3373,42 @@ public class Grid extends AbstractComponent implements SelectionNotifier, } } + @Override + public void columnVisibilityChanged(String id, boolean hidden, + boolean userOriginated) { + final Column column = getColumnByColumnId(id); + final GridColumnState columnState = column.getState(); + + if (columnState.hidden != hidden) { + columnState.hidden = hidden; + + final String diffStateKey = "columns"; + ConnectorTracker connectorTracker = getUI() + .getConnectorTracker(); + JsonObject diffState = connectorTracker + .getDiffState(Grid.this); + + assert diffState.hasKey(diffStateKey) : "Field name has changed"; + Type type = null; + try { + type = (getState(false).getClass().getDeclaredField( + diffStateKey).getGenericType()); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + EncodeResult encodeResult = JsonCodec.encode( + getState(false).columns, diffState, type, + connectorTracker); + + diffState.put(diffStateKey, encodeResult.getEncodedValue()); + + fireColumnVisibilityChangeEvent(column, hidden, + userOriginated); + } + } + @Override public void sendDetailsComponents(int fetchId) { getRpcProxy(GridClientRpc.class).setDetailsConnectorChanges( @@ -5455,9 +5506,9 @@ public class Grid extends AbstractComponent implements SelectionNotifier, COLUMN_VISIBILITY_METHOD); } - private void fireColumnVisibilityChangeEvent(Column column, + private void fireColumnVisibilityChangeEvent(Column column, boolean hidden, boolean isUserOriginated) { - fireEvent(new ColumnVisibilityChangeEvent(this, column, + fireEvent(new ColumnVisibilityChangeEvent(this, column, hidden, isUserOriginated)); } -- cgit v1.2.3