aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main
diff options
context:
space:
mode:
authorAleksi Hietanen <aleksi@vaadin.com>2016-09-21 09:13:12 +0300
committerVaadin Code Review <review@vaadin.com>2016-09-23 06:15:02 +0000
commit4d16c72c32ce67204ff49a5b45c5ce3e5288b86e (patch)
tree0110a3afd8157875bfa88118ffaddd9157dbd922 /server/src/main
parentf12fbfb7044defc8442d05e6810c43875c04710c (diff)
downloadvaadin-framework-4d16c72c32ce67204ff49a5b45c5ce3e5288b86e.tar.gz
vaadin-framework-4d16c72c32ce67204ff49a5b45c5ce3e5288b86e.zip
Reintroduce grid column sizing
Change-Id: Ie5e91c3e9c8f2c9d8c05415d5602e2eaf3bd960b
Diffstat (limited to 'server/src/main')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java361
1 files changed, 347 insertions, 14 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index b77e2ca7f7..94f8c9aee7 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -70,6 +70,11 @@ import elemental.json.JsonValue;
public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
@Deprecated
+ private static final Method COLUMN_RESIZE_METHOD = ReflectTools.findMethod(
+ ColumnResizeListener.class, "columnResize",
+ ColumnResizeEvent.class);
+
+ @Deprecated
private static final Method ITEM_CLICK_METHOD = ReflectTools
.findMethod(ItemClickListener.class, "accept", ItemClick.class);
@@ -80,6 +85,69 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
ColumnVisibilityChangeEvent.class);
/**
+ * An event listener for column resize events in the Grid.
+ *
+ * @since 7.6
+ */
+ public interface ColumnResizeListener extends Serializable {
+
+ /**
+ * Called when the columns of the grid have been resized.
+ *
+ * @param event
+ * An event providing more information
+ */
+ void columnResize(ColumnResizeEvent event);
+ }
+
+ /**
+ * An event that is fired when a column is resized, either programmatically
+ * or by the user.
+ *
+ * @since 7.6
+ */
+ public static class ColumnResizeEvent extends Component.Event {
+
+ private final Column<?, ?> column;
+ private final boolean userOriginated;
+
+ /**
+ *
+ * @param source
+ * the grid where the event originated from
+ * @param userOriginated
+ * <code>true</code> if event is a result of user
+ * interaction, <code>false</code> if from API call
+ */
+ public ColumnResizeEvent(Grid<?> source, Column<?, ?> column,
+ boolean userOriginated) {
+ super(source);
+ this.column = column;
+ this.userOriginated = userOriginated;
+ }
+
+ /**
+ * Returns the column that was resized.
+ *
+ * @return the resized column.
+ */
+ public Column<?, ?> getColumn() {
+ return column;
+ }
+
+ /**
+ * Returns <code>true</code> if the column resize was done by the user,
+ * <code>false</code> if not and it was triggered by server side code.
+ *
+ * @return <code>true</code> if event is a result of user interaction
+ */
+ public boolean isUserOriginated() {
+ return userOriginated;
+ }
+
+ }
+
+ /**
* An event fired when an item in the Grid has been clicked.
*
* @param <T>
@@ -466,7 +534,12 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
@Override
public void columnResized(String id, double pixels) {
- // TODO Auto-generated method stub
+ final Column<T, ?> column = getColumn(id);
+ if (column != null && column.isResizable()) {
+ column.getState().width = pixels;
+ fireColumnResizeEvent(column, true);
+ markAsDirty();
+ }
}
}
@@ -943,6 +1016,242 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
}
/**
+ * Sets the ratio with which the column expands.
+ * <p>
+ * By default, all columns expand equally (treated as if all of them had
+ * an expand ratio of 1). Once at least one column gets a defined expand
+ * ratio, the implicit expand ratio is removed, and only the defined
+ * expand ratios are taken into account.
+ * <p>
+ * If a column has a defined width ({@link #setWidth(double)}), it
+ * overrides this method's effects.
+ * <p>
+ * <em>Example:</em> A grid with three columns, with expand ratios 0, 1
+ * and 2, respectively. The column with a <strong>ratio of 0 is exactly
+ * as wide as its contents requires</strong>. The column with a ratio of
+ * 1 is as wide as it needs, <strong>plus a third of any excess
+ * space</strong>, because we have 3 parts total, and this column
+ * reserves only one of those. The column with a ratio of 2, is as wide
+ * as it needs to be, <strong>plus two thirds</strong> of the excess
+ * width.
+ *
+ * @param expandRatio
+ * the expand ratio of this column. {@code 0} to not have it
+ * expand at all. A negative number to clear the expand
+ * value.
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ * @see #setWidth(double)
+ */
+ public Column<T, V> setExpandRatio(int expandRatio)
+ throws IllegalStateException {
+ checkColumnIsAttached();
+ if (expandRatio != getExpandRatio()) {
+ getState().expandRatio = expandRatio;
+ getParent().markAsDirty();
+ }
+ return this;
+ }
+
+ /**
+ * Returns the column's expand ratio.
+ *
+ * @return the column's expand ratio
+ * @see #setExpandRatio(int)
+ */
+ public int getExpandRatio() {
+ return getState(false).expandRatio;
+ }
+
+ /**
+ * Clears the expand ratio for this column.
+ * <p>
+ * Equal to calling {@link #setExpandRatio(int) setExpandRatio(-1)}
+ *
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ */
+ public Column<T, V> clearExpandRatio() throws IllegalStateException {
+ return setExpandRatio(-1);
+ }
+
+ /**
+ * Returns the width (in pixels). By default a column is 100px wide.
+ *
+ * @return the width in pixels of the column
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ */
+ public double getWidth() throws IllegalStateException {
+ checkColumnIsAttached();
+ return getState(false).width;
+ }
+
+ /**
+ * Sets the width (in pixels).
+ * <p>
+ * This overrides any configuration set by any of
+ * {@link #setExpandRatio(int)}, {@link #setMinimumWidth(double)} or
+ * {@link #setMaximumWidth(double)}.
+ *
+ * @param pixelWidth
+ * the new pixel width of the column
+ * @return the column itself
+ *
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ * @throws IllegalArgumentException
+ * thrown if pixel width is less than zero
+ */
+ public Column<T, V> setWidth(double pixelWidth)
+ throws IllegalStateException, IllegalArgumentException {
+ checkColumnIsAttached();
+ if (pixelWidth < 0) {
+ throw new IllegalArgumentException(
+ "Pixel width should be greated than 0 (in " + toString()
+ + ")");
+ }
+ if (pixelWidth != getWidth()) {
+ getState().width = pixelWidth;
+ getParent().markAsDirty();
+ getParent().fireColumnResizeEvent(this, false);
+ }
+ return this;
+ }
+
+ /**
+ * Returns whether this column has an undefined width.
+ *
+ * @since 7.6
+ * @return whether the width is undefined
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ */
+ public boolean isWidthUndefined() {
+ checkColumnIsAttached();
+ return getState(false).width < 0;
+ }
+
+ /**
+ * Marks the column width as undefined. An undefined width means the
+ * grid is free to resize the column based on the cell contents and
+ * available space in the grid.
+ *
+ * @return the column itself
+ */
+ public Column<T, V> setWidthUndefined() {
+ checkColumnIsAttached();
+ if (!isWidthUndefined()) {
+ getState().width = -1;
+ getParent().markAsDirty();
+ getParent().fireColumnResizeEvent(this, false);
+ }
+ return this;
+ }
+
+ /**
+ * Sets the minimum width for this column.
+ * <p>
+ * This defines the minimum guaranteed pixel width of the column
+ * <em>when it is set to expand</em>.
+ *
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ * @see #setExpandRatio(int)
+ */
+ public Column<T, V> setMinimumWidth(double pixels)
+ throws IllegalStateException {
+ checkColumnIsAttached();
+
+ final double maxwidth = getMaximumWidth();
+ if (pixels >= 0 && pixels > maxwidth && maxwidth >= 0) {
+ throw new IllegalArgumentException("New minimum width ("
+ + pixels + ") was greater than maximum width ("
+ + maxwidth + ")");
+ }
+ getState().minWidth = pixels;
+ getParent().markAsDirty();
+ return this;
+ }
+
+ /**
+ * Return the minimum width for this column.
+ *
+ * @return the minimum width for this column
+ * @see #setMinimumWidth(double)
+ */
+ public double getMinimumWidth() {
+ return getState(false).minWidth;
+ }
+
+ /**
+ * Sets the maximum width for this column.
+ * <p>
+ * This defines the maximum allowed pixel width of the column <em>when
+ * it is set to expand</em>.
+ *
+ * @param pixels
+ * the maximum width
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ * @see #setExpandRatio(int)
+ */
+ public Column<T, V> setMaximumWidth(double pixels) {
+ checkColumnIsAttached();
+
+ final double minwidth = getMinimumWidth();
+ if (pixels >= 0 && pixels < minwidth && minwidth >= 0) {
+ throw new IllegalArgumentException("New maximum width ("
+ + pixels + ") was less than minimum width (" + minwidth
+ + ")");
+ }
+
+ getState().maxWidth = pixels;
+ getParent().markAsDirty();
+ return this;
+ }
+
+ /**
+ * Returns the maximum width for this column.
+ *
+ * @return the maximum width for this column
+ * @see #setMaximumWidth(double)
+ */
+ public double getMaximumWidth() {
+ return getState(false).maxWidth;
+ }
+
+ /**
+ * Sets whether this column can be resized by the user.
+ *
+ * @since 7.6
+ * @param resizable
+ * {@code true} if this column should be resizable,
+ * {@code false} otherwise
+ * @throws IllegalStateException
+ * if the column is no longer attached to any grid
+ */
+ public Column<T, V> setResizable(boolean resizable) {
+ checkColumnIsAttached();
+ if (resizable != isResizable()) {
+ getState().resizable = resizable;
+ getParent().markAsDirty();
+ }
+ return this;
+ }
+
+ /**
+ * Gets the caption of the hiding toggle for this column.
+ *
+ * @since 7.5.0
+ * @see #setHidingToggleCaption(String)
+ * @return the caption for the hiding toggle for this column
+ */
+ public String getHidingToggleCaption() {
+ return getState(false).hidingToggleCaption;
+ }
+
+ /**
* Sets the caption of the hiding toggle for this column. Shown in the
* toggle for this column in the grid's sidebar when the column is
* {@link #isHidable() hidable}.
@@ -966,17 +1275,6 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
}
/**
- * Gets the caption of the hiding toggle for this column.
- *
- * @since 7.5.0
- * @see #setHidingToggleCaption(String)
- * @return the caption for the hiding toggle for this column
- */
- public String getHidingToggleCaption() {
- return getState(false).hidingToggleCaption;
- }
-
- /**
* Hides or shows the column. By default columns are visible before
* explicitly hiding them.
*
@@ -1042,8 +1340,24 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
}
/**
- * Checks whether this column is attached and throws an
- * {@link IllegalStateException} if it is not.
+ * Returns whether this column can be resized by the user. Default is
+ * {@code true}.
+ * <p>
+ * <em>Note:</em> the column can be programmatically resized using
+ * {@link #setWidth(double)} and {@link #setWidthUndefined()} regardless
+ * of the returned value.
+ *
+ * @since 7.6
+ * @return {@code true} if this column is resizable, {@code false}
+ * otherwise
+ */
+ public boolean isResizable() {
+ return getState(false).resizable;
+ }
+
+ /**
+ * Checks if column is attached and throws an
+ * {@link IllegalStateException} if it is not
*
* @throws IllegalStateException
* if the column is no longer attached to any grid
@@ -1449,6 +1763,20 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
}
/**
+ * Registers a new column resize listener.
+ *
+ * @param listener
+ * the listener to register, not null
+ * @return a registration for the listener
+ */
+ public Registration addColumnResizeListener(ColumnResizeListener listener) {
+ Objects.requireNonNull(listener, "listener cannot be null");
+ addListener(ColumnResizeEvent.class, listener, COLUMN_RESIZE_METHOD);
+ return () -> removeListener(ColumnResizeEvent.class, listener,
+ COLUMN_RESIZE_METHOD);
+ }
+
+ /**
* Adds an item click listener. The listener is called when an item of this
* {@code Grid} is clicked.
*
@@ -1503,4 +1831,9 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
markAsDirty();
}
}
+
+ private void fireColumnResizeEvent(Column<?, ?> column,
+ boolean userOriginated) {
+ fireEvent(new ColumnResizeEvent(this, column, userOriginated));
+ }
}