From 8607aed71ddf73dd7bb96147def2f0f637aa3acf Mon Sep 17 00:00:00 2001 From: gvachkov Date: Mon, 8 Oct 2018 14:20:33 +0200 Subject: [PATCH 1/5] add read only state --- .../alump/gridstack/GridStackLayout.java | 192 ++++++++++-------- .../alump/gridstack/client/GwtGridStack.java | 151 ++++++++------ .../client/shared/GridStackChildOptions.java | 1 + 3 files changed, 199 insertions(+), 145 deletions(-) diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java index 50d3bec..8ecc822 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java @@ -17,6 +17,18 @@ */ package org.vaadin.alump.gridstack; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.vaadin.alump.gridstack.client.shared.GridStackChildOptions; +import org.vaadin.alump.gridstack.client.shared.GridStackLayoutState; +import org.vaadin.alump.gridstack.client.shared.GridStackMoveData; +import org.vaadin.alump.gridstack.client.shared.GridStackServerRpc; + import com.vaadin.annotations.JavaScript; import com.vaadin.event.LayoutEvents; import com.vaadin.shared.Connector; @@ -25,9 +37,6 @@ import com.vaadin.shared.Registration; import com.vaadin.ui.AbstractLayout; import com.vaadin.ui.Component; -import org.vaadin.alump.gridstack.client.shared.*; - -import java.util.*; /** * Vaadin layout using gridstack.js library to layout components @@ -50,22 +59,22 @@ public class GridStackLayout extends AbstractLayout implements LayoutEvents.Layo */ public final static int CLIENT_SIDE_SELECTS = -1; - private GridStackServerRpc serverRpc = new GridStackServerRpc() { + private final GridStackServerRpc serverRpc = new GridStackServerRpc() { @Override - public void layoutClick(MouseEventDetails mouseEventDetails, Connector connector) { + public void layoutClick(final MouseEventDetails mouseEventDetails, final Connector connector) { fireEvent(LayoutEvents.LayoutClickEvent.createEvent(GridStackLayout.this, mouseEventDetails, connector)); } @Override - public void onChildrenMoved(List moves) { - Collection events = new ArrayList(); - for(GridStackMoveData move : moves) { - Component childComponent = (Component)move.child; - GridStackCoordinates oldCoordinates = getCoordinates(childComponent); + public void onChildrenMoved(final List moves) { + final Collection events = new ArrayList(); + for (final GridStackMoveData move : moves) { + final Component childComponent = (Component) move.child; + final GridStackCoordinates oldCoordinates = getCoordinates(childComponent); - GridStackChildOptions info = getState(false).childOptions.get(move.child); + final GridStackChildOptions info = getState(false).childOptions.get(move.child); info.x = move.x; info.y = move.y; info.width = move.width; @@ -79,10 +88,10 @@ public void onChildrenMoved(List moves) { } @Override - public void onReady(int widthPx) { - readyCalls++; - final GridStackReadyEvent event = new GridStackReadyEvent(GridStackLayout.this, readyCalls == 1, widthPx); - readyListeners.forEach(l -> l.onGridStackReady(event)); + public void onReady(final int widthPx) { + GridStackLayout.this.readyCalls++; + final GridStackReadyEvent event = new GridStackReadyEvent(GridStackLayout.this, GridStackLayout.this.readyCalls == 1, widthPx); + GridStackLayout.this.readyListeners.forEach(l -> l.onGridStackReady(event)); } }; @@ -91,14 +100,14 @@ public void onReady(int widthPx) { */ public GridStackLayout() { super(); - registerRpc(serverRpc, GridStackServerRpc.class); + registerRpc(this.serverRpc, GridStackServerRpc.class); } /** * Create GridStackLayout with defined column count. * @param columns Number of columns, if more than 8 see documentation (extra SCSS including required) */ - public GridStackLayout(int columns) { + public GridStackLayout(final int columns) { this(); if(columns <= 0) { throw new IllegalArgumentException("Amount of columns can not be 0 or negative"); @@ -112,7 +121,7 @@ public GridStackLayout(int columns) { * @param columns Number of columns, if more than 8 see documentation (extra SCSS including required) * @param rows Maxium amount of rows allowed */ - public GridStackLayout(int columns, int rows) { + public GridStackLayout(final int columns, final int rows) { this(columns); if(rows <= 0) { throw new IllegalArgumentException("Amount of rows can not be 0 or negative"); @@ -126,7 +135,7 @@ protected GridStackLayoutState getState() { } @Override - protected GridStackLayoutState getState(boolean markDirty) { + protected GridStackLayoutState getState(final boolean markDirty) { return (GridStackLayoutState)super.getState(markDirty); } @@ -134,7 +143,7 @@ protected GridStackLayoutState getState(boolean markDirty) { * {@inheritDoc} */ @Override - public void addComponent(Component component) { + public void addComponent(final Component component) { addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS); } @@ -145,7 +154,7 @@ public void addComponent(Component component) { * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(Component component, boolean useDragHandle) { + public void addComponent(final Component component, final boolean useDragHandle) { addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS, useDragHandle); } @@ -155,7 +164,7 @@ public void addComponent(Component component, boolean useDragHandle) { * @param x Slot's X coordinate * @param y Slot's Y coordinate */ - public void addComponent(Component component, int x, int y) { + public void addComponent(final Component component, final int x, final int y) { addComponent(component, x, y, 1, 1); } @@ -168,7 +177,7 @@ public void addComponent(Component component, int x, int y) { * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(Component component, int x, int y, boolean useDragHandle) { + public void addComponent(final Component component, final int x, final int y, final boolean useDragHandle) { addComponent(component, x, y, 1, 1, useDragHandle); } @@ -180,7 +189,7 @@ public void addComponent(Component component, int x, int y, boolean useDragHandl * @param width Width of space reserved (in slots) * @param height Height of space reserved (in slots) */ - public void addComponent(Component component, int x, int y, int width, int height) { + public void addComponent(final Component component, final int x, final int y, final int width, final int height) { addComponent(component, x, y, width, height, true); } @@ -195,11 +204,11 @@ public void addComponent(Component component, int x, int y, int width, int heigh * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(Component component, int x, int y, int width, int height, boolean useDragHandle) { + public void addComponent(final Component component, final int x, final int y, final int width, final int height, final boolean useDragHandle) { super.addComponent(component); - components.add(component); + this.components.add(component); - GridStackChildOptions info = new GridStackChildOptions(); + final GridStackChildOptions info = new GridStackChildOptions(); info.x = x; info.y = y; info.width = width; @@ -212,7 +221,7 @@ public void addComponent(Component component, int x, int y, int width, int heigh * Reset component's position and allow child side define new position for it. * @param component Child component which position is reset */ - public void resetComponentPosition(Component component) { + public void resetComponentPosition(final Component component) { moveComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS); } @@ -223,7 +232,7 @@ public void resetComponentPosition(Component component) { * @param y When defined component's Y value is updated, if null old value is kept * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout) */ - public void moveComponent(Component component, Integer x, Integer y) throws IllegalArgumentException { + public void moveComponent(final Component component, final Integer x, final Integer y) throws IllegalArgumentException { moveAndResizeComponent(component, x, y, null, null); } @@ -234,7 +243,7 @@ public void moveComponent(Component component, Integer x, Integer y) throws Ille * @param height When defined component's height is updated, if null old value is kept * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout) */ - public void resizeComponent(Component component, Integer width, Integer height) throws IllegalArgumentException { + public void resizeComponent(final Component component, final Integer width, final Integer height) throws IllegalArgumentException { moveAndResizeComponent(component, null, null, width, height); } @@ -248,14 +257,14 @@ public void resizeComponent(Component component, Integer width, Integer height) * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout, or * coordinates are invalid). */ - public void moveAndResizeComponent(Component component, Integer x, Integer y, Integer width, Integer height) + public void moveAndResizeComponent(final Component component, final Integer x, final Integer y, final Integer width, final Integer height) throws IllegalArgumentException { if(x != null & width != null && x >= 0 && x + width > getState(false).gridStackOptions.width) { throw new IllegalArgumentException("Component would go outside the right edge of layout"); } - GridStackChildOptions info = getState().childOptions.get(component); + final GridStackChildOptions info = getState().childOptions.get(component); if(info == null) { throw new IllegalArgumentException("Given component is not child of GridStackLayout"); } @@ -279,7 +288,7 @@ public void moveAndResizeComponent(Component component, Integer x, Integer y, In * @param y Slot's Y coordinate * @return Component at slot, or null if component not found */ - public Component getComponent(int x, int y) { + public Component getComponent(final int x, final int y) { return getComponent(x, y, false); } @@ -290,9 +299,9 @@ public Component getComponent(int x, int y) { * @param acceptInsideHit If true also other slots reserved by component are accepted * @return Component at slot, or null if component not found */ - public Component getComponent(int x, int y, boolean acceptInsideHit) { - for(Connector connector : getState().childOptions.keySet()) { - GridStackChildOptions info = getState().childOptions.get(connector); + public Component getComponent(final int x, final int y, final boolean acceptInsideHit) { + for (final Connector connector : getState().childOptions.keySet()) { + final GridStackChildOptions info = getState().childOptions.get(connector); if(acceptInsideHit) { if(x >= info.x && x < (info.x + info.width) && y >= info.y && y < (info.y + info.height)) { return (Component) connector; @@ -310,21 +319,21 @@ public Component getComponent(int x, int y, boolean acceptInsideHit) { * {@inheritDoc} */ @Override - public void removeComponent(Component component) { + public void removeComponent(final Component component) { getState().childOptions.remove(component); - components.remove(component); + this.components.remove(component); super.removeComponent(component); } @Override - public void replaceComponent(Component oldComponent, Component newComponent) { + public void replaceComponent(final Component oldComponent, final Component newComponent) { if(oldComponent == newComponent) { return; } if(oldComponent.getParent() != this) { throw new IllegalArgumentException("Replacable component not child of this layout"); } - GridStackChildOptions oldOptions = getState(false).childOptions.get(oldComponent); + final GridStackChildOptions oldOptions = getState(false).childOptions.get(oldComponent); removeComponent(oldComponent); if(newComponent.getParent() == this) { @@ -338,7 +347,7 @@ public void replaceComponent(Component oldComponent, Component newComponent) { */ @Override public int getComponentCount() { - return components.size(); + return this.components.size(); } /** @@ -346,14 +355,14 @@ public int getComponentCount() { */ @Override public Iterator iterator() { - return components.iterator(); + return this.components.iterator(); } /** * {@inheritDoc} */ @Override - public Registration addLayoutClickListener(LayoutEvents.LayoutClickListener listener) { + public Registration addLayoutClickListener(final LayoutEvents.LayoutClickListener listener) { return addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutEvents.LayoutClickEvent.class, listener, LayoutEvents.LayoutClickListener.clickMethod); @@ -363,7 +372,7 @@ public Registration addLayoutClickListener(LayoutEvents.LayoutClickListener list * {@inheritDoc} */ @Override - public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener) { + public void removeLayoutClickListener(final LayoutEvents.LayoutClickListener listener) { removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutEvents.LayoutClickEvent.class, listener); } @@ -372,32 +381,32 @@ public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener) * Add listener for component move events * @param listener Listener added */ - public void addGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) { - moveListeners.add(listener); + public void addGridStackMoveListener(final GridStackMoveEvent.GridStackMoveListener listener) { + this.moveListeners.add(listener); } /** * Remove listener of component move events * @param listener Listener removed */ - public void removeGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) { - moveListeners.remove(listener); + public void removeGridStackMoveListener(final GridStackMoveEvent.GridStackMoveListener listener) { + this.moveListeners.remove(listener); } /** * Add listener for gridstack ready event * @param listener Listener added */ - public void addGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) { - readyListeners.add(listener); + public void addGridStackReadyListener(final GridStackReadyEvent.GridStackReadyListener listener) { + this.readyListeners.add(listener); } /** * Remove listener of GridStack ready event * @param listener Listener removed */ - public void removeGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) { - readyListeners.remove(listener); + public void removeGridStackReadyListener(final GridStackReadyEvent.GridStackReadyListener listener) { + this.readyListeners.remove(listener); } /** @@ -406,22 +415,22 @@ public void removeGridStackReadyListener(GridStackReadyEvent.GridStackReadyListe * @return Coordinates (X,Y,width,height) of component * @throws IllegalArgumentException If child not found */ - public GridStackCoordinates getCoordinates(Component child) { - GridStackChildOptions opts = getComponentOptions(child, false); + public GridStackCoordinates getCoordinates(final Component child) { + final GridStackChildOptions opts = getComponentOptions(child, false); return new GridStackCoordinates(opts.x, opts.y, opts.width, opts.height); } - protected GridStackMoveEvent createMoveEvent(Component component, GridStackCoordinates oldCoordinates) { + protected GridStackMoveEvent createMoveEvent(final Component component, final GridStackCoordinates oldCoordinates) { return new GridStackMoveEvent(this, component, oldCoordinates, getCoordinates(component)); } - protected void fireMoveEvents(Collection events) { + protected void fireMoveEvents(final Collection events) { if(events.isEmpty()) { return; } - for(GridStackMoveEvent.GridStackMoveListener listener : moveListeners) { + for (final GridStackMoveEvent.GridStackMoveListener listener : this.moveListeners) { listener.onGridStackMove(events); } } @@ -435,8 +444,9 @@ protected void fireMoveEvents(Collection events) { * @param minHeight Mininum height in slots (null is undefined) * @param maxHeight Maximum height in slots (null is undefined) */ - public void setComponentSizeLimits(Component child, Integer minWidth, Integer maxWidth, Integer minHeight, Integer maxHeight) { - GridStackChildOptions childOpts = getComponentOptions(child); + public void setComponentSizeLimits(final Component child, final Integer minWidth, final Integer maxWidth, final Integer minHeight, + final Integer maxHeight) { + final GridStackChildOptions childOpts = getComponentOptions(child); childOpts.minWidth = minWidth; childOpts.maxWidth = maxWidth; childOpts.minHeight = minHeight; @@ -449,7 +459,7 @@ public void setComponentSizeLimits(Component child, Integer minWidth, Integer ma * @return true if locked, false if not * @throws IllegalArgumentException If child not found */ - public boolean isComponentLocked(Component child) { + public boolean isComponentLocked(final Component child) { return getComponentOptions(child, false).locked; } @@ -459,23 +469,23 @@ public boolean isComponentLocked(Component child) { * @param locked true if locked, false if not * @throws IllegalArgumentException If child not found */ - public void setComponentLocked(Component child, boolean locked) { + public void setComponentLocked(final Component child, final boolean locked) { getComponentOptions(child).locked = locked; } - protected GridStackChildOptions getComponentOptions(Component child) { + protected GridStackChildOptions getComponentOptions(final Component child) { return getComponentOptions(child, true, true); } - protected GridStackChildOptions getComponentOptions(Component child, boolean modify) { + protected GridStackChildOptions getComponentOptions(final Component child, final boolean modify) { return getComponentOptions(child, modify, true); } - protected GridStackChildOptions getComponentOptions(Component child, boolean modify, boolean throwIfMissing) { + protected GridStackChildOptions getComponentOptions(final Component child, final boolean modify, final boolean throwIfMissing) { if(child == null || child.getParent() != this) { throw new IllegalArgumentException("Given component is not child of this layout"); } - GridStackChildOptions opt = getState(modify).childOptions.get(child); + final GridStackChildOptions opt = getState(modify).childOptions.get(child); if(opt == null) { throw new IllegalStateException("Missing child options"); } @@ -487,7 +497,7 @@ protected GridStackChildOptions getComponentOptions(Component child, boolean mod * @param animate true to animate, false to not animate * @return This GridStackLayout for command chaining */ - public GridStackLayout setAnimate(boolean animate) { + public GridStackLayout setAnimate(final boolean animate) { getState().gridStackOptions.animate = animate; return this; } @@ -506,7 +516,7 @@ public boolean isAnimate() { * allowed) * @return This GridStackLayout for command chaining */ - public GridStackLayout setStaticGrid(boolean staticGrid) { + public GridStackLayout setStaticGrid(final boolean staticGrid) { getState().gridStackOptions.staticGrid = staticGrid; return this; } @@ -525,7 +535,7 @@ public Boolean isStaticGrid() { * @param child Child component of layout * @return true if component has separate dragging handle, false if whole content acts as dragging handle */ - public boolean isComponentWithDragHandle(Component child) { + public boolean isComponentWithDragHandle(final Component child) { return getComponentOptions(child, false).useDragHandle; } @@ -535,7 +545,7 @@ public boolean isComponentWithDragHandle(Component child) { * @param marginPx Vertical margin in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setVerticalMargin(int marginPx) { + public GridStackLayout setVerticalMargin(final int marginPx) { return setVerticalMargin(marginPx + "px"); } @@ -545,7 +555,7 @@ public GridStackLayout setVerticalMargin(int marginPx) { * @param margin Vertical margin in CSS units (eg. '10px' or '3em') * @return This GridStackLayout for command chaining */ - public GridStackLayout setVerticalMargin(String margin) { + public GridStackLayout setVerticalMargin(final String margin) { getState(true).gridStackOptions.verticalMargin = margin; return this; } @@ -564,7 +574,7 @@ public String getVerticalMargin() { * @param heightPx Cell height in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setCellHeight(Integer heightPx) { + public GridStackLayout setCellHeight(final Integer heightPx) { return setCellHeight(Objects.requireNonNull(heightPx) + "px"); } @@ -573,7 +583,7 @@ public GridStackLayout setCellHeight(Integer heightPx) { * @param height Cell height in CSS units * @return This GridStackLayout for command chaining */ - public GridStackLayout setCellHeight(String height) { + public GridStackLayout setCellHeight(final String height) { getState(true).gridStackOptions.cellHeight = height; return this; } @@ -592,7 +602,7 @@ public Optional getCellHeight() { * @param minWidthPx Minimal width in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setMinWidth(int minWidthPx) { + public GridStackLayout setMinWidth(final int minWidthPx) { getState(true).gridStackOptions.minWidth = minWidthPx; return this; } @@ -612,7 +622,7 @@ public Integer getMinWidth() { * @param scrolling true to enable vertical scrolling, false to disable it * @throws IllegalArgumentException If child not found */ - public void setWrapperScrolling(Component child, boolean scrolling) { + public void setWrapperScrolling(final Component child, final boolean scrolling) { getComponentOptions(child, true).disableScrolling = !scrolling; } @@ -622,7 +632,7 @@ public void setWrapperScrolling(Component child, boolean scrolling) { * @return true if wrapper allows vertical scrolling, false if wrapper hides vertical overflow * @throws IllegalArgumentException If child not found */ - public boolean isWrapperScrolling(Component child) { + public boolean isWrapperScrolling(final Component child) { return getComponentOptions(child, false).disableScrolling; } @@ -636,7 +646,7 @@ public boolean isWrapperScrolling(Component child) { * @return true if area is available and valid for use * @throws IllegalArgumentException If invalid values given */ - public boolean isAreaEmpty(int x, int y, int width, int height) throws IllegalArgumentException { + public boolean isAreaEmpty(final int x, final int y, final int width, final int height) throws IllegalArgumentException { return isAreaEmpty(new GridStackCoordinates(x, y, width, height)); } @@ -648,7 +658,7 @@ public boolean isAreaEmpty(int x, int y, int width, int height) throws IllegalAr * @return true if area is available and valid for use * @throws IllegalArgumentException If invalid values given */ - public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgumentException { + public boolean isAreaEmpty(final GridStackCoordinates coordinates) throws IllegalArgumentException { if(coordinates.getX() < 0) { throw new IllegalArgumentException("X can not be negative"); } @@ -669,7 +679,7 @@ public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgum for(int dx = 0; dx < coordinates.getWidth(); ++dx) { for(int dy = 0; dy < coordinates.getHeight(); ++dy) { - Component occupant = getComponent(coordinates.getX() + dx, coordinates.getY() + dy, true); + final Component occupant = getComponent(coordinates.getX() + dx, coordinates.getY() + dy, true); if(occupant != null) { return false; } @@ -686,8 +696,8 @@ public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgum * @param child Child component * @param styleName Style name applied to item wrapper */ - public void setChildItemStyleName(Component child, String styleName) { - GridStackChildOptions childOptions = getState().childOptions.get(child); + public void setChildItemStyleName(final Component child, final String styleName) { + final GridStackChildOptions childOptions = getState().childOptions.get(child); if(childOptions == null) { throw new IllegalArgumentException("Child not found"); } @@ -699,9 +709,29 @@ public void setChildItemStyleName(Component child, String styleName) { * @param child Child component * @return Style name applied */ - public Optional getChildItemStyleName(Component child) { + public Optional getChildItemStyleName(final Component child) { return Optional.ofNullable(getState(false).childOptions.get(child)) .flatMap(o -> Optional.ofNullable(o.styleName)); } + /** + * Sets read only state to child component. If the child is set to read only it will not be able to be moved, resized or moved by another component + * + * @param child - Child component + * @param readOnly - State of the component + */ + public void setComponentReadOnly(final Component child, final boolean readOnly) { + getComponentOptions(child).readOnly = readOnly; + } + + /** + * Check if the child component is in read only state + * + * @param child - Child component + * @return true if the component is in read only state + */ + public boolean isComponentReadOnly(final Component child) { + return getComponentOptions(child).readOnly; + } + } diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java index c3f4ea7..f0aa71e 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java @@ -17,6 +17,17 @@ */ package org.vaadin.alump.gridstack.client; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import org.vaadin.alump.gridstack.client.shared.GridStackChildOptions; +import org.vaadin.alump.gridstack.client.shared.GridStackOptions; + import com.google.gwt.core.client.Duration; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; @@ -24,11 +35,6 @@ import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.Widget; -import org.vaadin.alump.gridstack.client.shared.GridStackChildOptions; -import org.vaadin.alump.gridstack.client.shared.GridStackOptions; - -import java.util.*; -import java.util.logging.Logger; public class GwtGridStack extends ComplexPanel { @@ -54,8 +60,8 @@ public class GwtGridStack extends ComplexPanel { public final static String INITIALIZING_CLASSNAME = "gridstack-initializing"; public final static String READY_CLASSNAME = "gridstack-ready"; - private Map widgetWrappers = new HashMap(); - private Map moveQueue = new HashMap(); + private final Map widgetWrappers = new HashMap(); + private final Map moveQueue = new HashMap(); public interface GwtGridStackMoveListener { void onWidgetsMoved(Map movedChildren); @@ -69,16 +75,16 @@ public GwtGridStack() { setElement(Document.get().createDivElement()); setStyleName("grid-stack"); - elementId = "gridstack-" + (idCounter++); - getElement().setId(elementId); + this.elementId = "gridstack-" + (idCounter++); + getElement().setId(this.elementId); } public boolean isInitialized() { - return initialized; + return this.initialized; } - public void setOptions(Integer width, Integer height, GridStackOptions options) { - if(!initialized) { + public void setOptions(final Integer width, final Integer height, final GridStackOptions options) { + if (!this.initialized) { initialize(width, height, GwtGridStackOptions.createFrom(options)); } else { if(options.cellHeight != null) { @@ -90,8 +96,8 @@ public void setOptions(Integer width, Integer height, GridStackOptions options) } } - public void initialize(Integer width, Integer height, GwtGridStackOptions options) { - if(initialized) { + public void initialize(final Integer width, final Integer height, final GwtGridStackOptions options) { + if (this.initialized) { LOGGER.severe("gridstack already initialized"); return; } @@ -104,48 +110,51 @@ public void initialize(Integer width, Integer height, GwtGridStackOptions option if(height != null) { getElement().setAttribute("data-gs-height", height.toString()); } - Duration duration = new Duration(); + final Duration duration = new Duration(); initializeGridStack(options); LOGGER.info("Initialize grid stack took " + duration.elapsedMillis()); - initialized = true; + this.initialized = true; getElement().removeClassName(INITIALIZING_CLASSNAME); getElement().addClassName(READY_CLASSNAME); - for (GwtGridStackReadyListener readyListener : readyListeners) { + for (final GwtGridStackReadyListener readyListener : this.readyListeners) { readyListener.onReady(); } } @Override - public void add(Widget widget) { + public void add(final Widget widget) { add(widget, new GridStackChildOptions()); } - public void add(Widget widget, GridStackChildOptions info) { - Element wrapper = createWrapper(info); - if(initialized) { + public void add(final Widget widget, final GridStackChildOptions info) { + final Element wrapper = createWrapper(info); + if (this.initialized) { addWidgetWrapperToGridStack(wrapper); } else { getElement().appendChild(wrapper); } - widgetWrappers.put(wrapper, widget); + this.widgetWrappers.put(wrapper, widget); super.add(widget, wrapper.getFirstChildElement()); } @Override - public boolean remove(Widget widget) { - if(initialized) { - Element wrapper = widget.getElement().getParentElement().getParentElement(); - widgetWrappers.remove(wrapper); + public boolean remove(final Widget widget) { + if (this.initialized) { + final Element wrapper = widget.getElement() + .getParentElement() + .getParentElement(); + this.widgetWrappers.remove(wrapper); removeWidgetWrapperFromGridStack(wrapper); wrapper.removeFromParent(); } return super.remove(widget); } - protected Element createWrapper(GridStackChildOptions info) { - Element wrapper = Document.get().createDivElement(); + protected Element createWrapper(final GridStackChildOptions info) { + final Element wrapper = Document.get() + .createDivElement(); wrapper.addClassName("grid-stack-item"); if(info.x >= 0 && info.y >= 0) { @@ -174,7 +183,8 @@ protected Element createWrapper(GridStackChildOptions info) { wrapper.setAttribute("data-gs-locked", "yes"); } - Element content = Document.get().createDivElement(); + final Element content = Document.get() + .createDivElement(); content.addClassName(CONTENT_CLASSNAME); if(!info.useDragHandle) { @@ -191,8 +201,9 @@ protected Element createWrapper(GridStackChildOptions info) { wrapper.appendChild(content); - if(info.useDragHandle) { - Element dragHandle = Document.get().createDivElement(); + if (info.useDragHandle && !info.readOnly) { + final Element dragHandle = Document.get() + .createDivElement(); dragHandle.addClassName("separate-handle"); dragHandle.addClassName(DRAG_HANDLE_CLASSNAME); wrapper.appendChild(dragHandle); @@ -201,31 +212,31 @@ protected Element createWrapper(GridStackChildOptions info) { return wrapper; } - public Element getWrapper(Widget child) { + public Element getWrapper(final Widget child) { if(child.getParent() == this) { throw new IllegalArgumentException("Given widget is not child of this GridStack"); } return child.getElement().getParentElement(); } - protected void onGridStackChange(Event event, GwtGridStackChangedItem[] items) { + protected void onGridStackChange(final Event event, final GwtGridStackChangedItem[] items) { // This gets called sometimes with undefined items, not sure why, but ignoring it for now. if(items == null) { return; } for(int i = 0; i < items.length; ++i) { - GwtGridStackChangedItem item = items[i]; - Widget child = mapElementToWidget(item.getElement()); + final GwtGridStackChangedItem item = items[i]; + final Widget child = mapElementToWidget(item.getElement()); if(child == null) { // Null children in list can be ignored? continue; } else { - moveQueue.put(child, item); + this.moveQueue.put(child, item); } } - flushMovedTimer.delay(); + this.flushMovedTimer.delay(); } protected class FlushMovedTimer extends Timer { @@ -236,44 +247,44 @@ public void delay() { @Override public void run() { - for (GwtGridStackMoveListener moveListener : moveListeners) { - moveListener.onWidgetsMoved(moveQueue); + for (final GwtGridStackMoveListener moveListener : GwtGridStack.this.moveListeners) { + moveListener.onWidgetsMoved(GwtGridStack.this.moveQueue); } - moveQueue.clear(); + GwtGridStack.this.moveQueue.clear(); } } - private FlushMovedTimer flushMovedTimer = new FlushMovedTimer(); + private final FlushMovedTimer flushMovedTimer = new FlushMovedTimer(); - protected void onGridStackDragStart(Event event) { + protected void onGridStackDragStart(final Event event) { updateEventFlag(true); } - protected void onGridStackDragStop(Event event) { + protected void onGridStackDragStop(final Event event) { updateEventFlag(false); } - protected void onGridStackResizeStart(Event event) { + protected void onGridStackResizeStart(final Event event) { updateEventFlag(true); } - protected void onGridStackResizeStop(Event event) { + protected void onGridStackResizeStop(final Event event) { updateEventFlag(false); } - protected void updateEventFlag(boolean start) { - draggedOrResized = start; - lastEvent = new Date().getTime(); + protected void updateEventFlag(final boolean start) { + this.draggedOrResized = start; + this.lastEvent = new Date().getTime(); } - protected Widget mapElementToWidget(Element element) { + protected Widget mapElementToWidget(final Element element) { - Widget child = widgetWrappers.get(element); + Widget child = this.widgetWrappers.get(element); if(child != null) { return child; } - Iterator iter = getChildren().iterator(); + final Iterator iter = getChildren().iterator(); while(iter.hasNext()) { child = iter.next(); if(element.isOrHasChild(child.getElement())) { @@ -327,25 +338,27 @@ protected native void removeWidgetWrapperFromGridStack(Element element) }); }-*/; - public void addMoveListener(GwtGridStackMoveListener listener) { - moveListeners.add(listener); + public void addMoveListener(final GwtGridStackMoveListener listener) { + this.moveListeners.add(listener); } - public void removeMoveListener(GwtGridStackMoveListener listener) { - moveListeners.remove(listener); + public void removeMoveListener(final GwtGridStackMoveListener listener) { + this.moveListeners.remove(listener); } - public void addReadyListener(GwtGridStackReadyListener listener) { - readyListeners.add(listener); + public void addReadyListener(final GwtGridStackReadyListener listener) { + this.readyListeners.add(listener); } - public void removeReadyListener(GwtGridStackReadyListener listener) { - readyListeners.remove(listener); + public void removeReadyListener(final GwtGridStackReadyListener listener) { + this.readyListeners.remove(listener); } - public void updateChild(Widget widget, GridStackChildOptions options) { - Element wrapper = widget.getElement().getParentElement().getParentElement(); + public void updateChild(final Widget widget, final GridStackChildOptions options) { + final Element wrapper = widget.getElement() + .getParentElement() + .getParentElement(); updateWidgetWrapper(wrapper, options.x, options.y, options.width, options.height); updateWidgetSizeLimits(wrapper, GwtGridSizeLimits.create(options)); setLocked(wrapper, options.locked); @@ -391,9 +404,19 @@ protected native final void setLocked(Element element, boolean locked) }); }-*/; + protected native final void setReadOnly(Element element, boolean readOnly) + /*-{ + var elementId = this.@org.vaadin.alump.gridstack.client.GwtGridStack::elementId; + $wnd.$(function () { + var grid = $wnd.$('#' + elementId).data('gridstack'); + grid.locked(element, readOnly); + grid.resizable(element, !readOnly); + grid.movable(element, !readOnly); + }); + }-*/; public void commit() { - if(initialized && isAttached()) { + if (this.initialized && isAttached()) { nativeCommit(); } } @@ -408,7 +431,7 @@ protected native final void nativeCommit() }-*/; public void batchUpdate() { - if(initialized && isAttached()) { + if (this.initialized && isAttached()) { nativeBatchUpdate(); } } @@ -423,7 +446,7 @@ protected native final void nativeBatchUpdate() }-*/; public boolean isClickOk() { - return !draggedOrResized && new Date().getTime() > (lastEvent + DISABLE_CLICK_AFTER_EVENT_MS); + return !this.draggedOrResized && new Date().getTime() > (this.lastEvent + DISABLE_CLICK_AFTER_EVENT_MS); } protected native final void nativeSetGridStatic(boolean gridStatic) diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/shared/GridStackChildOptions.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/shared/GridStackChildOptions.java index 818d016..2ce3967 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/shared/GridStackChildOptions.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/shared/GridStackChildOptions.java @@ -32,6 +32,7 @@ public class GridStackChildOptions implements Serializable { public Integer maxWidth = null; public Integer maxHeight = null; public boolean locked = false; + public boolean readOnly = false; public boolean useDragHandle = false; public String styleName = null; From 07405bf8183b6cb420a42cdbd8aa100fcb370d24 Mon Sep 17 00:00:00 2001 From: gvachkov Date: Thu, 11 Oct 2018 11:27:12 +0200 Subject: [PATCH 2/5] add read only state --- .../alump/gridstack/client/GwtGridStack.java | 1 + .../vaadin/alump/gridstack/demo/TestView.java | 183 ++++++++++-------- 2 files changed, 107 insertions(+), 77 deletions(-) diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java index f0aa71e..c442eaa 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java @@ -362,6 +362,7 @@ public void updateChild(final Widget widget, final GridStackChildOptions options updateWidgetWrapper(wrapper, options.x, options.y, options.width, options.height); updateWidgetSizeLimits(wrapper, GwtGridSizeLimits.create(options)); setLocked(wrapper, options.locked); + setReadOnly(wrapper, options.readOnly); if(options.disableScrolling) { wrapper.addClassName(DISABLE_SCROLLING_CLASSNAME); diff --git a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java index bd90e60..7a2b092 100644 --- a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java +++ b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java @@ -1,21 +1,35 @@ package org.vaadin.alump.gridstack.demo; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; + +import org.vaadin.alump.gridstack.GridStackButton; +import org.vaadin.alump.gridstack.GridStackCoordinates; +import org.vaadin.alump.gridstack.GridStackLayout; + import com.vaadin.event.LayoutEvents; import com.vaadin.icons.VaadinIcons; -import com.vaadin.navigator.Navigator; -import com.vaadin.navigator.View; -import com.vaadin.navigator.ViewChangeListener; -import com.vaadin.server.ExternalResource; import com.vaadin.server.Resource; import com.vaadin.server.ThemeResource; -import com.vaadin.ui.*; +import com.vaadin.ui.Alignment; +import com.vaadin.ui.Button; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.Component; +import com.vaadin.ui.CssLayout; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Image; +import com.vaadin.ui.Label; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Panel; +import com.vaadin.ui.PasswordField; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; -import org.vaadin.alump.gridstack.GridStackButton; -import org.vaadin.alump.gridstack.GridStackCoordinates; -import org.vaadin.alump.gridstack.GridStackLayout; - -import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; /** * Main test/demo view of GridStackLayout @@ -26,13 +40,14 @@ public class TestView extends AbstractView { private GridStackLayout gridStack; - private AtomicInteger eventCounter = new AtomicInteger(0); - private AtomicInteger itemCounter = new AtomicInteger(0); - private TextArea eventConsole = new TextArea(); + private final AtomicInteger eventCounter = new AtomicInteger(0); + private final AtomicInteger itemCounter = new AtomicInteger(0); + private final TextArea eventConsole = new TextArea(); - private Random rand = new Random(0xDEADBEEF); + private final Random rand = new Random(0xDEADBEEF); - private Component locked; + private final Component locked; + private final Component readOnly; // This value can be used as x and y when client side can pick the best slot private final static int CLIENT_SELECTS = GridStackLayout.CLIENT_SIDE_SELECTS; @@ -45,20 +60,20 @@ public TestView() { setSpacing(true); // By default gridstack has three columns (and calls that only work before client side attachment) - gridStack = new GridStackLayout(8) + this.gridStack = new GridStackLayout(8) .setVerticalMargin(12) .setMinWidth(300) .setAnimate(true); // See styles.scss of this demo project how to handle columns sizes on CSS size - gridStack.addStyleName("eight-column-grid-stack"); + this.gridStack.addStyleName("eight-column-grid-stack"); // One cell height is set to 80 pixels - gridStack.setCellHeight("80px"); + this.gridStack.setCellHeight("80px"); addComponent(createToolbar()); - Panel gridStackWrapper = new Panel(); + final Panel gridStackWrapper = new Panel(); gridStackWrapper.addStyleName("gridstack-wrapper"); gridStackWrapper.setSizeFull(); addComponent(gridStackWrapper); @@ -66,31 +81,35 @@ public TestView() { // ---- - gridStackWrapper.setContent(gridStack); - gridStack.setSizeFull(); + gridStackWrapper.setContent(this.gridStack); + this.gridStack.setSizeFull(); - Label label = new Label("This child can be dragged without handle. Please use separate handle " + final Label label = new Label("This child can be dragged without handle. Please use separate handle " + "(default mode) when you child component is, or has, an active Vaadin component."); label.setWidth(100, Unit.PERCENTAGE); - gridStack.addComponent(label, 0, 0, 1, 3, false); + this.gridStack.addComponent(label, 0, 0, 1, 3, false); - locked = new Label("This component can be \"locked\" (moving other children will not move this)"); - locked.setWidth(100, Unit.PERCENTAGE); - gridStack.addComponent(locked, 1, 0, 3, 1); + this.locked = new Label("This component can be \"locked\" (moving other children will not move this)"); + this.locked.setWidth(100, Unit.PERCENTAGE); + this.gridStack.addComponent(this.locked, 1, 0, 3, 1); - gridStack.addComponent(createForm(), 0, 5, 2, 3, false); - gridStack.addComponent(createConsole(), 0, 3, 4, 2); + this.readOnly = new Label("This component can be set to read only (moving and resizing is disabled and moving children over will not move this)"); + this.readOnly.setWidth(100, Unit.PERCENTAGE); + this.gridStack.addComponent(this.readOnly, 1, 4, 3, 1); - Component image = createImage(); - gridStack.addComponent(image, 2, 1, 3, 2); - gridStack.setWrapperScrolling(image, false); - gridStack.setComponentSizeLimits(image, 3, null, 2, null); + this.gridStack.addComponent(createForm(), 0, 5, 2, 3, false); + this.gridStack.addComponent(createConsole(), 0, 3, 4, 2); - gridStack.addGridStackMoveListener(events -> { - final int eventId = eventCounter.getAndIncrement(); + final Component image = createImage(); + this.gridStack.addComponent(image, 2, 1, 3, 2); + this.gridStack.setWrapperScrolling(image, false); + this.gridStack.setComponentSizeLimits(image, 3, null, 2, null); + + this.gridStack.addGridStackMoveListener(events -> { + final int eventId = this.eventCounter.getAndIncrement(); events.stream().forEach(event -> { if(event.getMovedChild() instanceof TestItem) { - TestItem item = (TestItem)event.getMovedChild(); + final TestItem item = (TestItem) event.getMovedChild(); item.setHeader(event.getNew()); } addEvent("event #" + eventId + ": Moved from " + event.getOld().toString() + " to " @@ -100,7 +119,7 @@ public TestView() { } private Component createToolbar() { - HorizontalLayout toolbar = new HorizontalLayout(); + final HorizontalLayout toolbar = new HorizontalLayout(); toolbar.setSpacing(true); toolbar.addComponent(createButton(VaadinIcons.MENU, "Back to menu", @@ -109,31 +128,31 @@ private Component createToolbar() { toolbar.addComponent(new Label("GridStack Demo")); toolbar.addComponent(createButton(VaadinIcons.PLUS, "Add component", e -> { - TestItem layout = new TestItem(itemCounter); - gridStack.addComponent(layout, CLIENT_SELECTS, CLIENT_SELECTS, 1 + rand.nextInt(3), 1); - layout.addRemoveClickListener(re -> gridStack.removeComponent(layout)); + final TestItem layout = new TestItem(this.itemCounter); + this.gridStack.addComponent(layout, CLIENT_SELECTS, CLIENT_SELECTS, 1 + this.rand.nextInt(3), 1); + layout.addRemoveClickListener(re -> this.gridStack.removeComponent(layout)); })); toolbar.addComponent(createButton(VaadinIcons.TRASH, "Remove component", e -> { - if(gridStack.getComponentCount() < 1) { + if (this.gridStack.getComponentCount() < 1) { Notification.show("Nothing to remove!"); return; } - int index = rand.nextInt(gridStack.getComponentCount()); - Iterator iter = gridStack.iterator(); + final int index = this.rand.nextInt(this.gridStack.getComponentCount()); + final Iterator iter = this.gridStack.iterator(); for(int i = 0; i < index; ++i) { iter.next(); } - gridStack.removeComponent(iter.next()); + this.gridStack.removeComponent(iter.next()); })); - CheckBox layoutClicks = new CheckBox("Layout clicks"); + final CheckBox layoutClicks = new CheckBox("Layout clicks"); layoutClicks.setDescription("Adds layout click listener to GridStackLayout"); layoutClicks.addValueChangeListener(e -> { if(e.getValue()) { - gridStack.addLayoutClickListener(layoutClickListener); + this.gridStack.addLayoutClickListener(this.layoutClickListener); } else { - gridStack.removeLayoutClickListener(layoutClickListener); + this.gridStack.removeLayoutClickListener(this.layoutClickListener); } }); toolbar.addComponent(layoutClicks); @@ -143,29 +162,36 @@ private Component createToolbar() { toolbar.addComponent(createButton(VaadinIcons.RANDOM, "Reorder all items to new order", e -> reorderAll())); - CheckBox staticGrid = new CheckBox("Static"); + final CheckBox staticGrid = new CheckBox("Static"); staticGrid.setDescription("If static, dragging and resizing are not allowed by user"); staticGrid.addValueChangeListener(e -> { - gridStack.setStaticGrid(e.getValue()); + this.gridStack.setStaticGrid(e.getValue()); }); toolbar.addComponent(staticGrid); - CheckBox lockItem = new CheckBox("Lock child"); + final CheckBox lockItem = new CheckBox("Lock child"); lockItem.setDescription("Define if item with text \"can be locked\" is locked or not"); lockItem.addValueChangeListener(e -> { - gridStack.setComponentLocked(locked, e.getValue()); + this.gridStack.setComponentLocked(this.locked, e.getValue()); }); toolbar.addComponent(lockItem); + final CheckBox readOnlyItem = new CheckBox("Read only child"); + readOnlyItem.setDescription("Define if item with text \"Read only\" is read only or not"); + readOnlyItem.addValueChangeListener(e -> { + this.gridStack.setComponentReadOnly(this.readOnly, e.getValue()); + }); + toolbar.addComponent(readOnlyItem); + return toolbar; } - private void addEvent(String message) { - eventConsole.setValue(message + "\r\n" + eventConsole.getValue()); + private void addEvent(final String message) { + this.eventConsole.setValue(message + "\r\n" + this.eventConsole.getValue()); } - private Button createButton(Resource icon, String caption, String description, Button.ClickListener listener) { - Button button = new Button(); + private Button createButton(final Resource icon, final String caption, final String description, final Button.ClickListener listener) { + final Button button = new Button(); button.addStyleName(ValoTheme.BUTTON_SMALL); button.addClickListener(listener); if(icon != null) { @@ -180,35 +206,36 @@ private Button createButton(Resource icon, String caption, String description, B return button; } - private Button createButton(Resource icon, String description, Button.ClickListener listener) { + private Button createButton(final Resource icon, final String description, final Button.ClickListener listener) { return createButton(icon, null, description, listener); } - private Button createButton(String caption, String description, Button.ClickListener listener) { + private Button createButton(final String caption, final String description, final Button.ClickListener listener) { return createButton(null, caption, description, listener); } private Component createForm() { - VerticalLayout layout = new VerticalLayout(); + final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true); layout.setSpacing(true); layout.setWidth(100, Unit.PERCENTAGE); - TextField username = new TextField(); + final TextField username = new TextField(); username.setWidth(100, Unit.PERCENTAGE); username.addStyleName(ValoTheme.TEXTFIELD_SMALL); username.setCaption("Username:"); layout.addComponent(username); - PasswordField password = new PasswordField(); + final PasswordField password = new PasswordField(); password.setWidth(100, Unit.PERCENTAGE); password.addStyleName(ValoTheme.TEXTFIELD_SMALL); password.setCaption("Password:"); layout.addComponent(password); - Button login = new GridStackButton("Login", e-> Notification.show("Logged in?")); + final Button login = new GridStackButton("Login", e -> Notification.show("Logged in?")); login.addStyleName(ValoTheme.BUTTON_FRIENDLY); login.addStyleName(ValoTheme.BUTTON_SMALL); layout.addComponent(login); layout.setComponentAlignment(login, Alignment.BOTTOM_RIGHT); - Label info = new Label("Also this child can be dragged without handle. GridStackButton used to resolve event " + final Label info = new Label( + "Also this child can be dragged without handle. GridStackButton used to resolve event " + "issues caused by normal Button."); info.setWidth(100, Unit.PERCENTAGE); info.addStyleName("info-text"); @@ -217,22 +244,22 @@ private Component createForm() { } private Component createConsole() { - VerticalLayout layout = new VerticalLayout(); + final VerticalLayout layout = new VerticalLayout(); layout.addStyleName("eventconsole-wrapper"); layout.setSizeFull(); - eventConsole.setCaption("Event console"); - eventConsole.setSizeFull(); - eventConsole.setValue("Events will be written here."); - layout.addComponent(eventConsole); + this.eventConsole.setCaption("Event console"); + this.eventConsole.setSizeFull(); + this.eventConsole.setValue("Events will be written here."); + layout.addComponent(this.eventConsole); return layout; } private Component createImage() { - CssLayout wrapper = new CssLayout(); + final CssLayout wrapper = new CssLayout(); wrapper.setSizeFull(); wrapper.addStyleName("image-wrapper"); - Image image = new Image(null, new ThemeResource("images/rude.jpg")); + final Image image = new Image(null, new ThemeResource("images/rude.jpg")); wrapper.addComponent(image); return wrapper; } @@ -249,7 +276,8 @@ private Component createImage() { if(clickedAtChild) { sb.append(": "); - sb.append(gridStack.getCoordinates(e.getChildComponent()).toString()); + sb.append(this.gridStack.getCoordinates(e.getChildComponent()) + .toString()); } addEvent(sb.toString()); @@ -257,25 +285,26 @@ private Component createImage() { private void reorderAll() { addEvent("Reorder all components..."); - GridStackDemoUtil.reorderAll(gridStack, rand, 8, 8); + GridStackDemoUtil.reorderAll(this.gridStack, this.rand, 8, 8); } private void moveRandomChildToAnotherFreePosition() { - List children = new ArrayList<>(); - gridStack.iterator().forEachRemaining(child -> children.add(child)); + final List children = new ArrayList<>(); + this.gridStack.iterator() + .forEachRemaining(child -> children.add(child)); if(!children.isEmpty()) { addEvent("Move random child to new position..."); - Collections.shuffle(children, rand); + Collections.shuffle(children, this.rand); moveChildToAnotherFreePosition(children.get(0)); } } // Just ugly hack to find suitable slot on server side. This will onlu work if server side has actual location of // child component - private void moveChildToAnotherFreePosition(Component child) { - Component movedComponent = GridStackDemoUtil.moveChildToAnotherFreePosition(gridStack, child, 8, 8); + private void moveChildToAnotherFreePosition(final Component child) { + final Component movedComponent = GridStackDemoUtil.moveChildToAnotherFreePosition(this.gridStack, child, 8, 8); if(movedComponent != null) { - GridStackCoordinates coords = gridStack.getCoordinates(movedComponent); + final GridStackCoordinates coords = this.gridStack.getCoordinates(movedComponent); addEvent("Moving child server side to new position x:" + coords.getX() + " y:" + coords.getY() + "..."); } else { addEvent("!!! Failed to find new available position for child"); From 630f57fe084e92a57f6724304bea2b067a2d300c Mon Sep 17 00:00:00 2001 From: gvachkov Date: Thu, 11 Oct 2018 14:39:58 +0200 Subject: [PATCH 3/5] add read only state --- .../vaadin/alump/gridstack/client/GwtGridStack.java | 10 ++++++++-- .../resources/VAADIN/addons/gridstack/gridstack.scss | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java index c442eaa..f02e04e 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java @@ -183,6 +183,12 @@ protected Element createWrapper(final GridStackChildOptions info) { wrapper.setAttribute("data-gs-locked", "yes"); } + if (info.readOnly) { + wrapper.setAttribute("data-gs-locked", "yes"); + wrapper.setAttribute("data-gs-no-resize", "yes"); + wrapper.setAttribute("data-gs-no-move", "yes"); + } + final Element content = Document.get() .createDivElement(); content.addClassName(CONTENT_CLASSNAME); @@ -201,7 +207,7 @@ protected Element createWrapper(final GridStackChildOptions info) { wrapper.appendChild(content); - if (info.useDragHandle && !info.readOnly) { + if (info.useDragHandle) { final Element dragHandle = Document.get() .createDivElement(); dragHandle.addClassName("separate-handle"); @@ -364,7 +370,7 @@ public void updateChild(final Widget widget, final GridStackChildOptions options setLocked(wrapper, options.locked); setReadOnly(wrapper, options.readOnly); - if(options.disableScrolling) { + if (options.disableScrolling) { wrapper.addClassName(DISABLE_SCROLLING_CLASSNAME); } else { wrapper.removeClassName(DISABLE_SCROLLING_CLASSNAME); diff --git a/gridstack-addon/src/main/resources/VAADIN/addons/gridstack/gridstack.scss b/gridstack-addon/src/main/resources/VAADIN/addons/gridstack/gridstack.scss index 81d5688..bd0b789 100644 --- a/gridstack-addon/src/main/resources/VAADIN/addons/gridstack/gridstack.scss +++ b/gridstack-addon/src/main/resources/VAADIN/addons/gridstack/gridstack.scss @@ -171,6 +171,10 @@ $gridstack_item_padding: $gridstack_horizontal_padding / 2 !default; } } + &.ui-draggable-disabled .grid-stack-item-drag-handle.separate-handle { + display: none !important; + } + &:hover .grid-stack-item-drag-handle.separate-handle { display: block; } From c75926cfca53e8db93b0df79d9f1a4199e2a46ab Mon Sep 17 00:00:00 2001 From: gvachkov Date: Thu, 11 Oct 2018 14:52:59 +0200 Subject: [PATCH 4/5] add read only state --- .../src/main/java/org/vaadin/alump/gridstack/demo/TestView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java index 7a2b092..ed485e1 100644 --- a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java +++ b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java @@ -95,7 +95,7 @@ public TestView() { this.readOnly = new Label("This component can be set to read only (moving and resizing is disabled and moving children over will not move this)"); this.readOnly.setWidth(100, Unit.PERCENTAGE); - this.gridStack.addComponent(this.readOnly, 1, 4, 3, 1); + this.gridStack.addComponent(this.readOnly, 0, 8, 3, 1); this.gridStack.addComponent(createForm(), 0, 5, 2, 3, false); this.gridStack.addComponent(createConsole(), 0, 3, 4, 2); From 6178fda3b6dde8abb54356441b2496bbf016aa88 Mon Sep 17 00:00:00 2001 From: gvachkov Date: Thu, 11 Oct 2018 15:54:11 +0200 Subject: [PATCH 5/5] add read only state --- .../alump/gridstack/GridStackLayout.java | 157 +++++++++--------- .../alump/gridstack/client/GwtGridStack.java | 114 ++++++------- .../vaadin/alump/gridstack/demo/TestView.java | 133 ++++++++------- 3 files changed, 201 insertions(+), 203 deletions(-) diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java index 966f6ac..d7c0747 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java @@ -59,22 +59,22 @@ public class GridStackLayout extends AbstractLayout implements LayoutEvents.Layo */ public final static int CLIENT_SIDE_SELECTS = -1; - private final GridStackServerRpc serverRpc = new GridStackServerRpc() { + private GridStackServerRpc serverRpc = new GridStackServerRpc() { @Override - public void layoutClick(final MouseEventDetails mouseEventDetails, final Connector connector) { + public void layoutClick(MouseEventDetails mouseEventDetails, Connector connector) { fireEvent(LayoutEvents.LayoutClickEvent.createEvent(GridStackLayout.this, mouseEventDetails, connector)); } @Override - public void onChildrenMoved(final List moves) { - final Collection events = new ArrayList(); - for (final GridStackMoveData move : moves) { - final Component childComponent = (Component) move.child; - final GridStackCoordinates oldCoordinates = getCoordinates(childComponent); + public void onChildrenMoved(List moves) { + Collection events = new ArrayList(); + for (GridStackMoveData move : moves) { + Component childComponent = (Component) move.child; + GridStackCoordinates oldCoordinates = getCoordinates(childComponent); - final GridStackChildOptions info = getState(false).childOptions.get(move.child); + GridStackChildOptions info = getState(false).childOptions.get(move.child); info.x = move.x; info.y = move.y; info.width = move.width; @@ -88,10 +88,10 @@ public void onChildrenMoved(final List moves) { } @Override - public void onReady(final int widthPx) { - GridStackLayout.this.readyCalls++; - final GridStackReadyEvent event = new GridStackReadyEvent(GridStackLayout.this, GridStackLayout.this.readyCalls == 1, widthPx); - GridStackLayout.this.readyListeners.forEach(l -> l.onGridStackReady(event)); + public void onReady(int widthPx) { + readyCalls++; + final GridStackReadyEvent event = new GridStackReadyEvent(GridStackLayout.this, readyCalls == 1, widthPx); + readyListeners.forEach(l -> l.onGridStackReady(event)); } }; @@ -100,14 +100,14 @@ public void onReady(final int widthPx) { */ public GridStackLayout() { super(); - registerRpc(this.serverRpc, GridStackServerRpc.class); + registerRpc(serverRpc, GridStackServerRpc.class); } /** * Create GridStackLayout with defined column count. * @param columns Number of columns, if more than 8 see documentation (extra SCSS including required) */ - public GridStackLayout(final int columns) { + public GridStackLayout(int columns) { this(); if(columns <= 0) { throw new IllegalArgumentException("Amount of columns can not be 0 or negative"); @@ -121,7 +121,7 @@ public GridStackLayout(final int columns) { * @param columns Number of columns, if more than 8 see documentation (extra SCSS including required) * @param rows Maxium amount of rows allowed */ - public GridStackLayout(final int columns, final int rows) { + public GridStackLayout(int columns, int rows) { this(columns); if(rows <= 0) { throw new IllegalArgumentException("Amount of rows can not be 0 or negative"); @@ -135,7 +135,7 @@ protected GridStackLayoutState getState() { } @Override - protected GridStackLayoutState getState(final boolean markDirty) { + protected GridStackLayoutState getState(boolean markDirty) { return (GridStackLayoutState)super.getState(markDirty); } @@ -143,7 +143,7 @@ protected GridStackLayoutState getState(final boolean markDirty) { * {@inheritDoc} */ @Override - public void addComponent(final Component component) { + public void addComponent(Component component) { addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS); } @@ -154,7 +154,7 @@ public void addComponent(final Component component) { * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(final Component component, final boolean useDragHandle) { + public void addComponent(Component component, boolean useDragHandle) { addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS, useDragHandle); } @@ -164,7 +164,7 @@ public void addComponent(final Component component, final boolean useDragHandle) * @param x Slot's X coordinate * @param y Slot's Y coordinate */ - public void addComponent(final Component component, final int x, final int y) { + public void addComponent(Component component, int x, int y) { addComponent(component, x, y, 1, 1); } @@ -177,7 +177,7 @@ public void addComponent(final Component component, final int x, final int y) { * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(final Component component, final int x, final int y, final boolean useDragHandle) { + public void addComponent(Component component, int x, int y, boolean useDragHandle) { addComponent(component, x, y, 1, 1, useDragHandle); } @@ -189,7 +189,7 @@ public void addComponent(final Component component, final int x, final int y, fi * @param width Width of space reserved (in slots) * @param height Height of space reserved (in slots) */ - public void addComponent(final Component component, final int x, final int y, final int width, final int height) { + public void addComponent(Component component, int x, int y, int width, int height) { addComponent(component, x, y, width, height, true); } @@ -204,11 +204,11 @@ public void addComponent(final Component component, final int x, final int y, fi * drag handle. Notice that using a separate drag handle is recommended if you component * is or contains any active components (buttons etc..) */ - public void addComponent(final Component component, final int x, final int y, final int width, final int height, final boolean useDragHandle) { + public void addComponent(Component component, int x, int y, int width, int height, boolean useDragHandle) { super.addComponent(component); - this.components.add(component); + components.add(component); - final GridStackChildOptions info = new GridStackChildOptions(); + GridStackChildOptions info = new GridStackChildOptions(); info.x = x; info.y = y; info.width = width; @@ -221,7 +221,7 @@ public void addComponent(final Component component, final int x, final int y, fi * Reset component's position and allow child side define new position for it. * @param component Child component which position is reset */ - public void resetComponentPosition(final Component component) { + public void resetComponentPosition(Component component) { moveComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS); } @@ -232,7 +232,7 @@ public void resetComponentPosition(final Component component) { * @param y When defined component's Y value is updated, if null old value is kept * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout) */ - public void moveComponent(final Component component, final Integer x, final Integer y) throws IllegalArgumentException { + public void moveComponent(Component component, Integer x, Integer y) throws IllegalArgumentException { moveAndResizeComponent(component, x, y, null, null); } @@ -243,7 +243,7 @@ public void moveComponent(final Component component, final Integer x, final Inte * @param height When defined component's height is updated, if null old value is kept * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout) */ - public void resizeComponent(final Component component, final Integer width, final Integer height) throws IllegalArgumentException { + public void resizeComponent(Component component, Integer width, Integer height) throws IllegalArgumentException { moveAndResizeComponent(component, null, null, width, height); } @@ -257,14 +257,14 @@ public void resizeComponent(final Component component, final Integer width, fina * @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout, or * coordinates are invalid). */ - public void moveAndResizeComponent(final Component component, final Integer x, final Integer y, final Integer width, final Integer height) + public void moveAndResizeComponent(Component component, Integer x, Integer y, Integer width, Integer height) throws IllegalArgumentException { if(x != null & width != null && x >= 0 && x + width > getState(false).gridStackOptions.width) { throw new IllegalArgumentException("Component would go outside the right edge of layout"); } - final GridStackChildOptions info = getState().childOptions.get(component); + GridStackChildOptions info = getState().childOptions.get(component); if(info == null) { throw new IllegalArgumentException("Given component is not child of GridStackLayout"); } @@ -288,7 +288,7 @@ public void moveAndResizeComponent(final Component component, final Integer x, f * @param y Slot's Y coordinate * @return Component at slot, or null if component not found */ - public Component getComponent(final int x, final int y) { + public Component getComponent(int x, int y) { return getComponent(x, y, false); } @@ -299,9 +299,9 @@ public Component getComponent(final int x, final int y) { * @param acceptInsideHit If true also other slots reserved by component are accepted * @return Component at slot, or null if component not found */ - public Component getComponent(final int x, final int y, final boolean acceptInsideHit) { - for (final Connector connector : getState().childOptions.keySet()) { - final GridStackChildOptions info = getState().childOptions.get(connector); + public Component getComponent(int x, int y, boolean acceptInsideHit) { + for (Connector connector : getState().childOptions.keySet()) { + GridStackChildOptions info = getState().childOptions.get(connector); if(acceptInsideHit) { if(x >= info.x && x < (info.x + info.width) && y >= info.y && y < (info.y + info.height)) { return (Component) connector; @@ -319,21 +319,21 @@ public Component getComponent(final int x, final int y, final boolean acceptInsi * {@inheritDoc} */ @Override - public void removeComponent(final Component component) { + public void removeComponent(Component component) { getState().childOptions.remove(component); - this.components.remove(component); + components.remove(component); super.removeComponent(component); } @Override - public void replaceComponent(final Component oldComponent, final Component newComponent) { + public void replaceComponent(Component oldComponent, Component newComponent) { if(oldComponent == newComponent) { return; } if(oldComponent.getParent() != this) { throw new IllegalArgumentException("Replacable component not child of this layout"); } - final GridStackChildOptions oldOptions = getState(false).childOptions.get(oldComponent); + GridStackChildOptions oldOptions = getState(false).childOptions.get(oldComponent); removeComponent(oldComponent); if(newComponent.getParent() == this) { @@ -347,7 +347,7 @@ public void replaceComponent(final Component oldComponent, final Component newCo */ @Override public int getComponentCount() { - return this.components.size(); + return components.size(); } /** @@ -355,14 +355,14 @@ public int getComponentCount() { */ @Override public Iterator iterator() { - return this.components.iterator(); + return components.iterator(); } /** * {@inheritDoc} */ @Override - public Registration addLayoutClickListener(final LayoutEvents.LayoutClickListener listener) { + public Registration addLayoutClickListener(LayoutEvents.LayoutClickListener listener) { return addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutEvents.LayoutClickEvent.class, listener, LayoutEvents.LayoutClickListener.clickMethod); @@ -372,7 +372,7 @@ public Registration addLayoutClickListener(final LayoutEvents.LayoutClickListene * {@inheritDoc} */ @Override - public void removeLayoutClickListener(final LayoutEvents.LayoutClickListener listener) { + public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener) { removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutEvents.LayoutClickEvent.class, listener); } @@ -381,32 +381,32 @@ public void removeLayoutClickListener(final LayoutEvents.LayoutClickListener lis * Add listener for component move events * @param listener Listener added */ - public void addGridStackMoveListener(final GridStackMoveEvent.GridStackMoveListener listener) { - this.moveListeners.add(listener); + public void addGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) { + moveListeners.add(listener); } /** * Remove listener of component move events * @param listener Listener removed */ - public void removeGridStackMoveListener(final GridStackMoveEvent.GridStackMoveListener listener) { - this.moveListeners.remove(listener); + public void removeGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) { + moveListeners.remove(listener); } /** * Add listener for gridstack ready event * @param listener Listener added */ - public void addGridStackReadyListener(final GridStackReadyEvent.GridStackReadyListener listener) { - this.readyListeners.add(listener); + public void addGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) { + readyListeners.add(listener); } /** * Remove listener of GridStack ready event * @param listener Listener removed */ - public void removeGridStackReadyListener(final GridStackReadyEvent.GridStackReadyListener listener) { - this.readyListeners.remove(listener); + public void removeGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) { + readyListeners.remove(listener); } /** @@ -415,22 +415,22 @@ public void removeGridStackReadyListener(final GridStackReadyEvent.GridStackRead * @return Coordinates (X,Y,width,height) of component * @throws IllegalArgumentException If child not found */ - public GridStackCoordinates getCoordinates(final Component child) { - final GridStackChildOptions opts = getComponentOptions(child, false); + public GridStackCoordinates getCoordinates(Component child) { + GridStackChildOptions opts = getComponentOptions(child, false); return new GridStackCoordinates(opts.x, opts.y, opts.width, opts.height); } - protected GridStackMoveEvent createMoveEvent(final Component component, final GridStackCoordinates oldCoordinates) { + protected GridStackMoveEvent createMoveEvent(Component component, GridStackCoordinates oldCoordinates) { return new GridStackMoveEvent(this, component, oldCoordinates, getCoordinates(component)); } - protected void fireMoveEvents(final Collection events) { + protected void fireMoveEvents(Collection events) { if(events.isEmpty()) { return; } - for (final GridStackMoveEvent.GridStackMoveListener listener : this.moveListeners) { + for (GridStackMoveEvent.GridStackMoveListener listener : moveListeners) { listener.onGridStackMove(events); } } @@ -444,9 +444,8 @@ protected void fireMoveEvents(final Collection events) { * @param minHeight Mininum height in slots (null is undefined) * @param maxHeight Maximum height in slots (null is undefined) */ - public void setComponentSizeLimits(final Component child, final Integer minWidth, final Integer maxWidth, final Integer minHeight, - final Integer maxHeight) { - final GridStackChildOptions childOpts = getComponentOptions(child); + public void setComponentSizeLimits(Component child, Integer minWidth, Integer maxWidth, Integer minHeight, Integer maxHeight) { + GridStackChildOptions childOpts = getComponentOptions(child); childOpts.minWidth = minWidth; childOpts.maxWidth = maxWidth; childOpts.minHeight = minHeight; @@ -459,7 +458,7 @@ public void setComponentSizeLimits(final Component child, final Integer minWidth * @return true if locked, false if not * @throws IllegalArgumentException If child not found */ - public boolean isComponentLocked(final Component child) { + public boolean isComponentLocked(Component child) { return getComponentOptions(child, false).locked; } @@ -469,23 +468,23 @@ public boolean isComponentLocked(final Component child) { * @param locked true if locked, false if not * @throws IllegalArgumentException If child not found */ - public void setComponentLocked(final Component child, final boolean locked) { + public void setComponentLocked(Component child, boolean locked) { getComponentOptions(child).locked = locked; } - protected GridStackChildOptions getComponentOptions(final Component child) { + protected GridStackChildOptions getComponentOptions(Component child) { return getComponentOptions(child, true, true); } - protected GridStackChildOptions getComponentOptions(final Component child, final boolean modify) { + protected GridStackChildOptions getComponentOptions(Component child, boolean modify) { return getComponentOptions(child, modify, true); } - protected GridStackChildOptions getComponentOptions(final Component child, final boolean modify, final boolean throwIfMissing) { + protected GridStackChildOptions getComponentOptions(Component child, boolean modify, boolean throwIfMissing) { if(child == null || child.getParent() != this) { throw new IllegalArgumentException("Given component is not child of this layout"); } - final GridStackChildOptions opt = getState(modify).childOptions.get(child); + GridStackChildOptions opt = getState(modify).childOptions.get(child); if(opt == null) { throw new IllegalStateException("Missing child options"); } @@ -497,7 +496,7 @@ protected GridStackChildOptions getComponentOptions(final Component child, final * @param animate true to animate, false to not animate * @return This GridStackLayout for command chaining */ - public GridStackLayout setAnimate(final boolean animate) { + public GridStackLayout setAnimate(boolean animate) { getState().gridStackOptions.animate = animate; return this; } @@ -516,7 +515,7 @@ public boolean isAnimate() { * allowed) * @return This GridStackLayout for command chaining */ - public GridStackLayout setStaticGrid(final boolean staticGrid) { + public GridStackLayout setStaticGrid(boolean staticGrid) { getState().gridStackOptions.staticGrid = staticGrid; return this; } @@ -535,7 +534,7 @@ public Boolean isStaticGrid() { * @param child Child component of layout * @return true if component has separate dragging handle, false if whole content acts as dragging handle */ - public boolean isComponentWithDragHandle(final Component child) { + public boolean isComponentWithDragHandle(Component child) { return getComponentOptions(child, false).useDragHandle; } @@ -545,7 +544,7 @@ public boolean isComponentWithDragHandle(final Component child) { * @param marginPx Vertical margin in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setVerticalMargin(final int marginPx) { + public GridStackLayout setVerticalMargin(int marginPx) { return setVerticalMargin(marginPx + "px"); } @@ -555,7 +554,7 @@ public GridStackLayout setVerticalMargin(final int marginPx) { * @param margin Vertical margin in CSS units (eg. '10px' or '3em') * @return This GridStackLayout for command chaining */ - public GridStackLayout setVerticalMargin(final String margin) { + public GridStackLayout setVerticalMargin(String margin) { getState(true).gridStackOptions.verticalMargin = margin; return this; } @@ -574,7 +573,7 @@ public String getVerticalMargin() { * @param heightPx Cell height in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setCellHeight(final Integer heightPx) { + public GridStackLayout setCellHeight(Integer heightPx) { return setCellHeight(Objects.requireNonNull(heightPx) + "px"); } @@ -583,7 +582,7 @@ public GridStackLayout setCellHeight(final Integer heightPx) { * @param height Cell height in CSS units * @return This GridStackLayout for command chaining */ - public GridStackLayout setCellHeight(final String height) { + public GridStackLayout setCellHeight(String height) { getState(true).gridStackOptions.cellHeight = height; return this; } @@ -602,7 +601,7 @@ public Optional getCellHeight() { * @param minWidthPx Minimal width in pixels * @return This GridStackLayout for command chaining */ - public GridStackLayout setMinWidth(final int minWidthPx) { + public GridStackLayout setMinWidth(int minWidthPx) { getState(true).gridStackOptions.minWidth = minWidthPx; return this; } @@ -622,7 +621,7 @@ public Integer getMinWidth() { * @param scrolling true to enable vertical scrolling, false to disable it * @throws IllegalArgumentException If child not found */ - public void setWrapperScrolling(final Component child, final boolean scrolling) { + public void setWrapperScrolling(Component child, boolean scrolling) { getComponentOptions(child, true).disableScrolling = !scrolling; } @@ -632,7 +631,7 @@ public void setWrapperScrolling(final Component child, final boolean scrolling) * @return true if wrapper allows vertical scrolling, false if wrapper hides vertical overflow * @throws IllegalArgumentException If child not found */ - public boolean isWrapperScrolling(final Component child) { + public boolean isWrapperScrolling(Component child) { return getComponentOptions(child, false).disableScrolling; } @@ -646,7 +645,7 @@ public boolean isWrapperScrolling(final Component child) { * @return true if area is available and valid for use * @throws IllegalArgumentException If invalid values given */ - public boolean isAreaEmpty(final int x, final int y, final int width, final int height) throws IllegalArgumentException { + public boolean isAreaEmpty(int x, int y, int width, int height) throws IllegalArgumentException { return isAreaEmpty(new GridStackCoordinates(x, y, width, height)); } @@ -658,7 +657,7 @@ public boolean isAreaEmpty(final int x, final int y, final int width, final int * @return true if area is available and valid for use * @throws IllegalArgumentException If invalid values given */ - public boolean isAreaEmpty(final GridStackCoordinates coordinates) throws IllegalArgumentException { + public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgumentException { if(coordinates.getX() < 0) { throw new IllegalArgumentException("X can not be negative"); } @@ -679,7 +678,7 @@ public boolean isAreaEmpty(final GridStackCoordinates coordinates) throws Illega for(int dx = 0; dx < coordinates.getWidth(); ++dx) { for(int dy = 0; dy < coordinates.getHeight(); ++dy) { - final Component occupant = getComponent(coordinates.getX() + dx, coordinates.getY() + dy, true); + Component occupant = getComponent(coordinates.getX() + dx, coordinates.getY() + dy, true); if(occupant != null) { return false; } @@ -696,8 +695,8 @@ public boolean isAreaEmpty(final GridStackCoordinates coordinates) throws Illega * @param child Child component * @param styleName Style name applied to item wrapper */ - public void setChildItemStyleName(final Component child, final String styleName) { - final GridStackChildOptions childOptions = getState().childOptions.get(child); + public void setChildItemStyleName(Component child, String styleName) { + GridStackChildOptions childOptions = getState().childOptions.get(child); if(childOptions == null) { throw new IllegalArgumentException("Child not found"); } @@ -709,7 +708,7 @@ public void setChildItemStyleName(final Component child, final String styleName) * @param child Child component * @return Style name applied */ - public Optional getChildItemStyleName(final Component child) { + public Optional getChildItemStyleName(Component child) { return Optional.ofNullable(getState(false).childOptions.get(child)) .flatMap(o -> Optional.ofNullable(o.styleName)); } diff --git a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java index f02e04e..864dc66 100644 --- a/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java +++ b/gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GwtGridStack.java @@ -60,8 +60,8 @@ public class GwtGridStack extends ComplexPanel { public final static String INITIALIZING_CLASSNAME = "gridstack-initializing"; public final static String READY_CLASSNAME = "gridstack-ready"; - private final Map widgetWrappers = new HashMap(); - private final Map moveQueue = new HashMap(); + private Map widgetWrappers = new HashMap(); + private Map moveQueue = new HashMap(); public interface GwtGridStackMoveListener { void onWidgetsMoved(Map movedChildren); @@ -75,16 +75,16 @@ public GwtGridStack() { setElement(Document.get().createDivElement()); setStyleName("grid-stack"); - this.elementId = "gridstack-" + (idCounter++); - getElement().setId(this.elementId); + elementId = "gridstack-" + (idCounter++); + getElement().setId(elementId); } public boolean isInitialized() { - return this.initialized; + return initialized; } - public void setOptions(final Integer width, final Integer height, final GridStackOptions options) { - if (!this.initialized) { + public void setOptions(Integer width, Integer height, GridStackOptions options) { + if (!initialized) { initialize(width, height, GwtGridStackOptions.createFrom(options)); } else { if(options.cellHeight != null) { @@ -96,8 +96,8 @@ public void setOptions(final Integer width, final Integer height, final GridStac } } - public void initialize(final Integer width, final Integer height, final GwtGridStackOptions options) { - if (this.initialized) { + public void initialize(Integer width, Integer height, GwtGridStackOptions options) { + if (initialized) { LOGGER.severe("gridstack already initialized"); return; } @@ -110,50 +110,50 @@ public void initialize(final Integer width, final Integer height, final GwtGridS if(height != null) { getElement().setAttribute("data-gs-height", height.toString()); } - final Duration duration = new Duration(); + Duration duration = new Duration(); initializeGridStack(options); LOGGER.info("Initialize grid stack took " + duration.elapsedMillis()); - this.initialized = true; + initialized = true; getElement().removeClassName(INITIALIZING_CLASSNAME); getElement().addClassName(READY_CLASSNAME); - for (final GwtGridStackReadyListener readyListener : this.readyListeners) { + for (GwtGridStackReadyListener readyListener : readyListeners) { readyListener.onReady(); } } @Override - public void add(final Widget widget) { + public void add(Widget widget) { add(widget, new GridStackChildOptions()); } - public void add(final Widget widget, final GridStackChildOptions info) { - final Element wrapper = createWrapper(info); - if (this.initialized) { + public void add(Widget widget, GridStackChildOptions info) { + Element wrapper = createWrapper(info); + if (initialized) { addWidgetWrapperToGridStack(wrapper); } else { getElement().appendChild(wrapper); } - this.widgetWrappers.put(wrapper, widget); + widgetWrappers.put(wrapper, widget); super.add(widget, wrapper.getFirstChildElement()); } @Override - public boolean remove(final Widget widget) { - if (this.initialized) { - final Element wrapper = widget.getElement() + public boolean remove(Widget widget) { + if (initialized) { + Element wrapper = widget.getElement() .getParentElement() .getParentElement(); - this.widgetWrappers.remove(wrapper); + widgetWrappers.remove(wrapper); removeWidgetWrapperFromGridStack(wrapper); wrapper.removeFromParent(); } return super.remove(widget); } - protected Element createWrapper(final GridStackChildOptions info) { - final Element wrapper = Document.get() + protected Element createWrapper(GridStackChildOptions info) { + Element wrapper = Document.get() .createDivElement(); wrapper.addClassName("grid-stack-item"); @@ -208,7 +208,7 @@ protected Element createWrapper(final GridStackChildOptions info) { wrapper.appendChild(content); if (info.useDragHandle) { - final Element dragHandle = Document.get() + Element dragHandle = Document.get() .createDivElement(); dragHandle.addClassName("separate-handle"); dragHandle.addClassName(DRAG_HANDLE_CLASSNAME); @@ -218,31 +218,31 @@ protected Element createWrapper(final GridStackChildOptions info) { return wrapper; } - public Element getWrapper(final Widget child) { + public Element getWrapper(Widget child) { if(child.getParent() == this) { throw new IllegalArgumentException("Given widget is not child of this GridStack"); } return child.getElement().getParentElement(); } - protected void onGridStackChange(final Event event, final GwtGridStackChangedItem[] items) { + protected void onGridStackChange(Event event, GwtGridStackChangedItem[] items) { // This gets called sometimes with undefined items, not sure why, but ignoring it for now. if(items == null) { return; } for(int i = 0; i < items.length; ++i) { - final GwtGridStackChangedItem item = items[i]; - final Widget child = mapElementToWidget(item.getElement()); + GwtGridStackChangedItem item = items[i]; + Widget child = mapElementToWidget(item.getElement()); if(child == null) { // Null children in list can be ignored? continue; } else { - this.moveQueue.put(child, item); + moveQueue.put(child, item); } } - this.flushMovedTimer.delay(); + flushMovedTimer.delay(); } protected class FlushMovedTimer extends Timer { @@ -253,44 +253,44 @@ public void delay() { @Override public void run() { - for (final GwtGridStackMoveListener moveListener : GwtGridStack.this.moveListeners) { - moveListener.onWidgetsMoved(GwtGridStack.this.moveQueue); + for (GwtGridStackMoveListener moveListener : moveListeners) { + moveListener.onWidgetsMoved(moveQueue); } - GwtGridStack.this.moveQueue.clear(); + moveQueue.clear(); } } - private final FlushMovedTimer flushMovedTimer = new FlushMovedTimer(); + private FlushMovedTimer flushMovedTimer = new FlushMovedTimer(); - protected void onGridStackDragStart(final Event event) { + protected void onGridStackDragStart(Event event) { updateEventFlag(true); } - protected void onGridStackDragStop(final Event event) { + protected void onGridStackDragStop(Event event) { updateEventFlag(false); } - protected void onGridStackResizeStart(final Event event) { + protected void onGridStackResizeStart(Event event) { updateEventFlag(true); } - protected void onGridStackResizeStop(final Event event) { + protected void onGridStackResizeStop(Event event) { updateEventFlag(false); } - protected void updateEventFlag(final boolean start) { - this.draggedOrResized = start; - this.lastEvent = new Date().getTime(); + protected void updateEventFlag(boolean start) { + draggedOrResized = start; + lastEvent = new Date().getTime(); } - protected Widget mapElementToWidget(final Element element) { + protected Widget mapElementToWidget(Element element) { - Widget child = this.widgetWrappers.get(element); + Widget child = widgetWrappers.get(element); if(child != null) { return child; } - final Iterator iter = getChildren().iterator(); + Iterator iter = getChildren().iterator(); while(iter.hasNext()) { child = iter.next(); if(element.isOrHasChild(child.getElement())) { @@ -344,25 +344,25 @@ protected native void removeWidgetWrapperFromGridStack(Element element) }); }-*/; - public void addMoveListener(final GwtGridStackMoveListener listener) { - this.moveListeners.add(listener); + public void addMoveListener(GwtGridStackMoveListener listener) { + moveListeners.add(listener); } - public void removeMoveListener(final GwtGridStackMoveListener listener) { - this.moveListeners.remove(listener); + public void removeMoveListener(GwtGridStackMoveListener listener) { + moveListeners.remove(listener); } - public void addReadyListener(final GwtGridStackReadyListener listener) { - this.readyListeners.add(listener); + public void addReadyListener(GwtGridStackReadyListener listener) { + readyListeners.add(listener); } - public void removeReadyListener(final GwtGridStackReadyListener listener) { - this.readyListeners.remove(listener); + public void removeReadyListener(GwtGridStackReadyListener listener) { + readyListeners.remove(listener); } - public void updateChild(final Widget widget, final GridStackChildOptions options) { - final Element wrapper = widget.getElement() + public void updateChild(Widget widget, GridStackChildOptions options) { + Element wrapper = widget.getElement() .getParentElement() .getParentElement(); updateWidgetWrapper(wrapper, options.x, options.y, options.width, options.height); @@ -423,7 +423,7 @@ protected native final void setReadOnly(Element element, boolean readOnly) }-*/; public void commit() { - if (this.initialized && isAttached()) { + if (initialized && isAttached()) { nativeCommit(); } } @@ -438,7 +438,7 @@ protected native final void nativeCommit() }-*/; public void batchUpdate() { - if (this.initialized && isAttached()) { + if (initialized && isAttached()) { nativeBatchUpdate(); } } @@ -453,7 +453,7 @@ protected native final void nativeBatchUpdate() }-*/; public boolean isClickOk() { - return !this.draggedOrResized && new Date().getTime() > (this.lastEvent + DISABLE_CLICK_AFTER_EVENT_MS); + return !draggedOrResized && new Date().getTime() > (lastEvent + DISABLE_CLICK_AFTER_EVENT_MS); } protected native final void nativeSetGridStatic(boolean gridStatic) diff --git a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java index ed485e1..96dc4ed 100644 --- a/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java +++ b/gridstack-demo/src/main/java/org/vaadin/alump/gridstack/demo/TestView.java @@ -40,13 +40,13 @@ public class TestView extends AbstractView { private GridStackLayout gridStack; - private final AtomicInteger eventCounter = new AtomicInteger(0); - private final AtomicInteger itemCounter = new AtomicInteger(0); - private final TextArea eventConsole = new TextArea(); + private AtomicInteger eventCounter = new AtomicInteger(0); + private AtomicInteger itemCounter = new AtomicInteger(0); + private TextArea eventConsole = new TextArea(); - private final Random rand = new Random(0xDEADBEEF); + private Random rand = new Random(0xDEADBEEF); - private final Component locked; + private Component locked; private final Component readOnly; // This value can be used as x and y when client side can pick the best slot @@ -60,20 +60,20 @@ public TestView() { setSpacing(true); // By default gridstack has three columns (and calls that only work before client side attachment) - this.gridStack = new GridStackLayout(8) + gridStack = new GridStackLayout(8) .setVerticalMargin(12) .setMinWidth(300) .setAnimate(true); // See styles.scss of this demo project how to handle columns sizes on CSS size - this.gridStack.addStyleName("eight-column-grid-stack"); + gridStack.addStyleName("eight-column-grid-stack"); // One cell height is set to 80 pixels - this.gridStack.setCellHeight("80px"); + gridStack.setCellHeight("80px"); addComponent(createToolbar()); - final Panel gridStackWrapper = new Panel(); + Panel gridStackWrapper = new Panel(); gridStackWrapper.addStyleName("gridstack-wrapper"); gridStackWrapper.setSizeFull(); addComponent(gridStackWrapper); @@ -81,35 +81,35 @@ public TestView() { // ---- - gridStackWrapper.setContent(this.gridStack); - this.gridStack.setSizeFull(); + gridStackWrapper.setContent(gridStack); + gridStack.setSizeFull(); - final Label label = new Label("This child can be dragged without handle. Please use separate handle " + Label label = new Label("This child can be dragged without handle. Please use separate handle " + "(default mode) when you child component is, or has, an active Vaadin component."); label.setWidth(100, Unit.PERCENTAGE); - this.gridStack.addComponent(label, 0, 0, 1, 3, false); + gridStack.addComponent(label, 0, 0, 1, 3, false); - this.locked = new Label("This component can be \"locked\" (moving other children will not move this)"); - this.locked.setWidth(100, Unit.PERCENTAGE); - this.gridStack.addComponent(this.locked, 1, 0, 3, 1); + locked = new Label("This component can be \"locked\" (moving other children will not move this)"); + locked.setWidth(100, Unit.PERCENTAGE); + gridStack.addComponent(locked, 1, 0, 3, 1); this.readOnly = new Label("This component can be set to read only (moving and resizing is disabled and moving children over will not move this)"); this.readOnly.setWidth(100, Unit.PERCENTAGE); this.gridStack.addComponent(this.readOnly, 0, 8, 3, 1); - this.gridStack.addComponent(createForm(), 0, 5, 2, 3, false); - this.gridStack.addComponent(createConsole(), 0, 3, 4, 2); + gridStack.addComponent(createForm(), 0, 5, 2, 3, false); + gridStack.addComponent(createConsole(), 0, 3, 4, 2); - final Component image = createImage(); - this.gridStack.addComponent(image, 2, 1, 3, 2); - this.gridStack.setWrapperScrolling(image, false); - this.gridStack.setComponentSizeLimits(image, 3, null, 2, null); + Component image = createImage(); + gridStack.addComponent(image, 2, 1, 3, 2); + gridStack.setWrapperScrolling(image, false); + gridStack.setComponentSizeLimits(image, 3, null, 2, null); - this.gridStack.addGridStackMoveListener(events -> { - final int eventId = this.eventCounter.getAndIncrement(); + gridStack.addGridStackMoveListener(events -> { + final int eventId = eventCounter.getAndIncrement(); events.stream().forEach(event -> { if(event.getMovedChild() instanceof TestItem) { - final TestItem item = (TestItem) event.getMovedChild(); + TestItem item = (TestItem) event.getMovedChild(); item.setHeader(event.getNew()); } addEvent("event #" + eventId + ": Moved from " + event.getOld().toString() + " to " @@ -119,7 +119,7 @@ public TestView() { } private Component createToolbar() { - final HorizontalLayout toolbar = new HorizontalLayout(); + HorizontalLayout toolbar = new HorizontalLayout(); toolbar.setSpacing(true); toolbar.addComponent(createButton(VaadinIcons.MENU, "Back to menu", @@ -128,31 +128,31 @@ private Component createToolbar() { toolbar.addComponent(new Label("GridStack Demo")); toolbar.addComponent(createButton(VaadinIcons.PLUS, "Add component", e -> { - final TestItem layout = new TestItem(this.itemCounter); - this.gridStack.addComponent(layout, CLIENT_SELECTS, CLIENT_SELECTS, 1 + this.rand.nextInt(3), 1); - layout.addRemoveClickListener(re -> this.gridStack.removeComponent(layout)); + TestItem layout = new TestItem(itemCounter); + gridStack.addComponent(layout, CLIENT_SELECTS, CLIENT_SELECTS, 1 + rand.nextInt(3), 1); + layout.addRemoveClickListener(re -> gridStack.removeComponent(layout)); })); toolbar.addComponent(createButton(VaadinIcons.TRASH, "Remove component", e -> { - if (this.gridStack.getComponentCount() < 1) { + if (gridStack.getComponentCount() < 1) { Notification.show("Nothing to remove!"); return; } - final int index = this.rand.nextInt(this.gridStack.getComponentCount()); - final Iterator iter = this.gridStack.iterator(); + int index = rand.nextInt(gridStack.getComponentCount()); + Iterator iter = gridStack.iterator(); for(int i = 0; i < index; ++i) { iter.next(); } - this.gridStack.removeComponent(iter.next()); + gridStack.removeComponent(iter.next()); })); - final CheckBox layoutClicks = new CheckBox("Layout clicks"); + CheckBox layoutClicks = new CheckBox("Layout clicks"); layoutClicks.setDescription("Adds layout click listener to GridStackLayout"); layoutClicks.addValueChangeListener(e -> { if(e.getValue()) { - this.gridStack.addLayoutClickListener(this.layoutClickListener); + gridStack.addLayoutClickListener(layoutClickListener); } else { - this.gridStack.removeLayoutClickListener(this.layoutClickListener); + gridStack.removeLayoutClickListener(layoutClickListener); } }); toolbar.addComponent(layoutClicks); @@ -162,17 +162,17 @@ private Component createToolbar() { toolbar.addComponent(createButton(VaadinIcons.RANDOM, "Reorder all items to new order", e -> reorderAll())); - final CheckBox staticGrid = new CheckBox("Static"); + CheckBox staticGrid = new CheckBox("Static"); staticGrid.setDescription("If static, dragging and resizing are not allowed by user"); staticGrid.addValueChangeListener(e -> { - this.gridStack.setStaticGrid(e.getValue()); + gridStack.setStaticGrid(e.getValue()); }); toolbar.addComponent(staticGrid); - final CheckBox lockItem = new CheckBox("Lock child"); + CheckBox lockItem = new CheckBox("Lock child"); lockItem.setDescription("Define if item with text \"can be locked\" is locked or not"); lockItem.addValueChangeListener(e -> { - this.gridStack.setComponentLocked(this.locked, e.getValue()); + gridStack.setComponentLocked(locked, e.getValue()); }); toolbar.addComponent(lockItem); @@ -186,12 +186,12 @@ private Component createToolbar() { return toolbar; } - private void addEvent(final String message) { - this.eventConsole.setValue(message + "\r\n" + this.eventConsole.getValue()); + private void addEvent(String message) { + eventConsole.setValue(message + "\r\n" + eventConsole.getValue()); } - private Button createButton(final Resource icon, final String caption, final String description, final Button.ClickListener listener) { - final Button button = new Button(); + private Button createButton(Resource icon, String caption, String description, Button.ClickListener listener) { + Button button = new Button(); button.addStyleName(ValoTheme.BUTTON_SMALL); button.addClickListener(listener); if(icon != null) { @@ -206,36 +206,35 @@ private Button createButton(final Resource icon, final String caption, final Str return button; } - private Button createButton(final Resource icon, final String description, final Button.ClickListener listener) { + private Button createButton(Resource icon, String description, Button.ClickListener listener) { return createButton(icon, null, description, listener); } - private Button createButton(final String caption, final String description, final Button.ClickListener listener) { + private Button createButton(String caption, String description, Button.ClickListener listener) { return createButton(null, caption, description, listener); } private Component createForm() { - final VerticalLayout layout = new VerticalLayout(); + VerticalLayout layout = new VerticalLayout(); layout.setMargin(true); layout.setSpacing(true); layout.setWidth(100, Unit.PERCENTAGE); - final TextField username = new TextField(); + TextField username = new TextField(); username.setWidth(100, Unit.PERCENTAGE); username.addStyleName(ValoTheme.TEXTFIELD_SMALL); username.setCaption("Username:"); layout.addComponent(username); - final PasswordField password = new PasswordField(); + PasswordField password = new PasswordField(); password.setWidth(100, Unit.PERCENTAGE); password.addStyleName(ValoTheme.TEXTFIELD_SMALL); password.setCaption("Password:"); layout.addComponent(password); - final Button login = new GridStackButton("Login", e -> Notification.show("Logged in?")); + Button login = new GridStackButton("Login", e -> Notification.show("Logged in?")); login.addStyleName(ValoTheme.BUTTON_FRIENDLY); login.addStyleName(ValoTheme.BUTTON_SMALL); layout.addComponent(login); layout.setComponentAlignment(login, Alignment.BOTTOM_RIGHT); - final Label info = new Label( - "Also this child can be dragged without handle. GridStackButton used to resolve event " + Label info = new Label("Also this child can be dragged without handle. GridStackButton used to resolve event " + "issues caused by normal Button."); info.setWidth(100, Unit.PERCENTAGE); info.addStyleName("info-text"); @@ -244,22 +243,22 @@ private Component createForm() { } private Component createConsole() { - final VerticalLayout layout = new VerticalLayout(); + VerticalLayout layout = new VerticalLayout(); layout.addStyleName("eventconsole-wrapper"); layout.setSizeFull(); - this.eventConsole.setCaption("Event console"); - this.eventConsole.setSizeFull(); - this.eventConsole.setValue("Events will be written here."); - layout.addComponent(this.eventConsole); + eventConsole.setCaption("Event console"); + eventConsole.setSizeFull(); + eventConsole.setValue("Events will be written here."); + layout.addComponent(eventConsole); return layout; } private Component createImage() { - final CssLayout wrapper = new CssLayout(); + CssLayout wrapper = new CssLayout(); wrapper.setSizeFull(); wrapper.addStyleName("image-wrapper"); - final Image image = new Image(null, new ThemeResource("images/rude.jpg")); + Image image = new Image(null, new ThemeResource("images/rude.jpg")); wrapper.addComponent(image); return wrapper; } @@ -276,7 +275,7 @@ private Component createImage() { if(clickedAtChild) { sb.append(": "); - sb.append(this.gridStack.getCoordinates(e.getChildComponent()) + sb.append(gridStack.getCoordinates(e.getChildComponent()) .toString()); } @@ -285,26 +284,26 @@ private Component createImage() { private void reorderAll() { addEvent("Reorder all components..."); - GridStackDemoUtil.reorderAll(this.gridStack, this.rand, 8, 8); + GridStackDemoUtil.reorderAll(gridStack, rand, 8, 8); } private void moveRandomChildToAnotherFreePosition() { - final List children = new ArrayList<>(); - this.gridStack.iterator() + List children = new ArrayList<>(); + gridStack.iterator() .forEachRemaining(child -> children.add(child)); if(!children.isEmpty()) { addEvent("Move random child to new position..."); - Collections.shuffle(children, this.rand); + Collections.shuffle(children, rand); moveChildToAnotherFreePosition(children.get(0)); } } // Just ugly hack to find suitable slot on server side. This will onlu work if server side has actual location of // child component - private void moveChildToAnotherFreePosition(final Component child) { - final Component movedComponent = GridStackDemoUtil.moveChildToAnotherFreePosition(this.gridStack, child, 8, 8); + private void moveChildToAnotherFreePosition(Component child) { + Component movedComponent = GridStackDemoUtil.moveChildToAnotherFreePosition(gridStack, child, 8, 8); if(movedComponent != null) { - final GridStackCoordinates coords = this.gridStack.getCoordinates(movedComponent); + GridStackCoordinates coords = gridStack.getCoordinates(movedComponent); addEvent("Moving child server side to new position x:" + coords.getX() + " y:" + coords.getY() + "..."); } else { addEvent("!!! Failed to find new available position for child");