Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Commit 0ff4fe7

Browse files
committed
Adds animate, move and resize functionality
1 parent 1a0778b commit 0ff4fe7

File tree

11 files changed

+268
-1023
lines changed

11 files changed

+268
-1023
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ Official releases of this add-on are available at Vaadin Directory. For Maven in
6060

6161
## Release notes
6262

63+
### Version 0.3.0 (2016-05-18)
64+
- Possibility to move and resize already added children
65+
- Adds animate option to allow nice transitions when moving children
66+
- gridstack.js updated to 0.2.5
67+
- Deprecated API removed
68+
- Fix error in getComponent method with coordinates (could have returned wrong components)
69+
6370
### Version 0.2.1 (2015-12-23)
6471
- Add missing API to define cell height, min width and vertical margins
6572
- API to set wrappers un-scrollable

gridstack-addon/assembly/assembly.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<directory>..</directory>
2121
<includes>
2222
<include>LICENSE.txt</include>
23+
<include>GRIDSTACK_LICENSE.txt</include>
24+
<include>LODASH_LICENSE.txt</include>
2325
<include>README.md</include>
2426
</includes>
2527
</fileSet>

gridstack-addon/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<groupId>org.vaadin.alump.gridstack</groupId>
66
<artifactId>gridstack-addon</artifactId>
77
<packaging>bundle</packaging>
8-
<version>0.2.1</version>
8+
<version>0.3.0</version>
99
<name>GridStack Add-on</name>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<vaadin.version>7.5.10</vaadin.version>
13+
<vaadin.version>7.6.6</vaadin.version>
1414
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
1515

1616
<!-- ZIP Manifest fields -->

gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackCoordinates.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public GridStackCoordinates(int x, int y, int width, int height) {
3333
this.height = height;
3434
}
3535

36+
public boolean isXAndY(int x, int y) {
37+
return this.x == x && this.y == y;
38+
}
39+
3640
public int getX() {
3741
return x;
3842
}

gridstack-addon/src/main/java/org/vaadin/alump/gridstack/GridStackLayout.java

Lines changed: 132 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class GridStackLayout extends AbstractLayout implements LayoutEvents.Layo
4545

4646
private final List<GridStackMoveEvent.GridStackMoveListener> moveListeners = new ArrayList<GridStackMoveEvent.GridStackMoveListener>();
4747

48+
/**
49+
* Use this as x or y coordinate if you want to leave slot selection of component to client side
50+
*/
51+
public final static int CLIENT_SIDE_SELECTS = -1;
52+
4853
private GridStackServerRpc serverRpc = new GridStackServerRpc() {
4954

5055
@Override
@@ -130,7 +135,7 @@ protected GridStackLayoutState getState(boolean markDirty) {
130135
*/
131136
@Override
132137
public void addComponent(Component component) {
133-
addComponent(component, -1, -1);
138+
addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS);
134139
}
135140

136141
/**
@@ -141,7 +146,7 @@ public void addComponent(Component component) {
141146
* is or contains any active components (buttons etc..)
142147
*/
143148
public void addComponent(Component component, boolean useDragHandle) {
144-
addComponent(component, -1, -1, useDragHandle);
149+
addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS, useDragHandle);
145150
}
146151

147152
/**
@@ -203,6 +208,66 @@ public void addComponent(Component component, int x, int y, int width, int heigh
203208
getState().childOptions.put(component, info);
204209
}
205210

211+
/**
212+
* Reset component's position and allow child side define new position for it.
213+
* @param component Child component which position is reset
214+
*/
215+
public void resetComponentPosition(Component component) {
216+
moveComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS);
217+
}
218+
219+
/**
220+
* Move given child component
221+
* @param component Child component moved and/or resized
222+
* @param x When defined component's X value is updated, if null old value is kept
223+
* @param y When defined component's Y value is updated, if null old value is kept
224+
* @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout)
225+
*/
226+
public void moveComponent(Component component, Integer x, Integer y) throws IllegalArgumentException {
227+
moveAndResizeComponent(component, x, y, null, null);
228+
}
229+
230+
/**
231+
* Move given child component
232+
* @param component Child component moved and/or resized
233+
* @param width When defined component's width is updated, if null old value is kept
234+
* @param height When defined component's height is updated, if null old value is kept
235+
* @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout)
236+
*/
237+
public void resizeComponent(Component component, Integer width, Integer height) throws IllegalArgumentException {
238+
moveAndResizeComponent(component, null, null, width, height);
239+
}
240+
241+
/**
242+
* Move and/or resize given child component
243+
* @param component Child component moved and/or resized
244+
* @param x When defined component's X value is updated, if null old value is kept
245+
* @param y When defined component's Y value is updated, if null old value is kept
246+
* @param width When defined component's width is updated, if null old value is kept
247+
* @param height When defined component's height is updated, if null old value is kept
248+
* @throws IllegalArgumentException If given value are invalid (eg. component is not child of this layout)
249+
*/
250+
public void moveAndResizeComponent(Component component, Integer x, Integer y, Integer width, Integer height)
251+
throws IllegalArgumentException {
252+
253+
GridStackChildOptions info = getState().childOptions.get(component);
254+
if(info == null) {
255+
throw new IllegalArgumentException("Given component is not child of GridStackLayout");
256+
}
257+
if(x != null) {
258+
info.x = x;
259+
}
260+
if(y != null) {
261+
info.y = y;
262+
}
263+
if(width != null) {
264+
info.width = width;
265+
}
266+
if(height != null) {
267+
info.height = height;
268+
}
269+
}
270+
206271
/**
207272
* Get component with given slot coordinate
208273
* @param x Slot's X coordinate
@@ -224,7 +289,7 @@ public Component getComponent(int x, int y, boolean acceptInsideHit) {
224289
for(Connector connector : getState().childOptions.keySet()) {
225290
GridStackChildOptions info = getState().childOptions.get(connector);
226291
if(acceptInsideHit) {
227-
if(x >= info.x && y < (info.x + info.width) && y >= info.y && y < (info.y + info.width)) {
292+
if(x >= info.x && x < (info.x + info.width) && y >= info.y && y < (info.y + info.width)) {
228293
return (Component) connector;
229294
}
230295
} else {
@@ -310,19 +375,6 @@ public void removeListener(LayoutEvents.LayoutClickListener layoutClickListener)
310375
removeLayoutClickListener(layoutClickListener);
311376
}
312377

313-
/**
314-
* This call is deprecated and will be removed in 0.3.0. Please use setVerticalMargin, setCellHeight, setMinWidth
315-
* and setStaticGrid methods.
316-
* @return Access to gridstack.js's grid options
317-
*/
318-
@Deprecated
319-
public GridStackOptions getOptions() {
320-
if(initialClientResponseSent) {
321-
throw new IllegalStateException("Options can not be modified after initial response has been sent to client");
322-
}
323-
return getState().gridStackOptions;
324-
}
325-
326378
/**
327379
* Add listener for component move events
328380
* @param listener Listener added
@@ -421,14 +473,32 @@ protected GridStackChildOptions getComponentOptions(Component child, boolean mod
421473
return opt;
422474
}
423475

476+
/**
477+
* Define if layout is animated when child components are moved
478+
* @param animate true to animate, false to not animate
479+
* @return This GridStackLayout for command chaining
480+
*/
481+
public GridStackLayout setAnimate(boolean animate) {
482+
getState().gridStackOptions.animate = animate;
483+
return this;
484+
}
485+
486+
/**
487+
* Check if layout is animated
488+
* @return true if animated, false if not
489+
*/
490+
public boolean isAnimate() {
491+
return getState(false).gridStackOptions.animate;
492+
}
493+
424494
/**
425495
* Set layout static (no dragging of resizing) or dynamic (dragging and resizing allowed)
426496
* @param staticGrid true to set static (no dragging of resizing), false to set dynamic (dragging and resizing
427497
* allowed)
428498
* @return This GridStackLayout for command chaining
429499
*/
430500
public GridStackLayout setStaticGrid(boolean staticGrid) {
431-
getState(true).gridStackOptions.staticGrid = staticGrid;
501+
getState().gridStackOptions.staticGrid = staticGrid;
432502
return this;
433503
}
434504

@@ -528,4 +598,49 @@ public boolean isWrapperScrolling(Component child) {
528598
return getComponentOptions(child, false).disableScrolling;
529599
}
530600

601+
/**
602+
* Check if given area is empty. Remember that any client side defined positioning not yet reported back to
603+
* server side will be unknown and so can result work results.
604+
* @param x Left edge coordinate of area
605+
* @param x Top edge coordinate of area
606+
* @param width Width of area in slots
607+
* @param height Height of area in slots
608+
* @throws IllegalArgumentException If invalid values given
609+
*/
610+
public boolean isAreaEmpty(int x, int y, int width, int height) throws IllegalArgumentException {
611+
return isAreaEmpty(new GridStackCoordinates(x, y, width, height));
612+
}
613+
614+
/**
615+
* Check if given area is empty. Remember that any client side defined positioning not yet reported back to
616+
* server side will be unknown and so can result work results.
617+
* @param coordinates Coordinate area checked (x, y, width, height)
618+
* @throws IllegalArgumentException If invalid values given
619+
*/
620+
public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgumentException {
621+
if(coordinates.getX() < 0) {
622+
throw new IllegalArgumentException("X can not be negative");
623+
}
624+
if(coordinates.getY() < 0) {
625+
throw new IllegalArgumentException("Y can not be negative");
626+
}
627+
if(coordinates.getWidth() <= 0) {
628+
throw new IllegalArgumentException("Width most be larger than zero");
629+
}
630+
if(coordinates.getHeight() <= 0) {
631+
throw new IllegalArgumentException("Height most be larger than zero");
632+
}
633+
634+
for(int dx = 0; dx < coordinates.getWidth(); ++dx) {
635+
for(int dy = 0; dy < coordinates.getHeight(); ++dy) {
636+
Component occupant = getComponent(coordinates.getX() + dx, coordinates.getY() + dy, true);
637+
if(occupant != null) {
638+
return false;
639+
}
640+
}
641+
}
642+
643+
return true;
644+
}
645+
531646
}

gridstack-addon/src/main/java/org/vaadin/alump/gridstack/client/GridStackLayoutConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@Connect(org.vaadin.alump.gridstack.GridStackLayout.class)
4242
public class GridStackLayoutConnector extends AbstractLayoutConnector {
4343

44-
private final static Logger LOGGER = Logger.getLogger(GridStackLayoutConnector.class.getName());
44+
private transient final static Logger LOGGER = Logger.getLogger(GridStackLayoutConnector.class.getName());
4545

4646
@Override
4747
public void init() {
@@ -129,7 +129,7 @@ public void updateCaption(ComponentConnector componentConnector) {
129129
//ignore for now
130130
}
131131

132-
private final LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(this) {
132+
private transient final LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(this) {
133133

134134
@Override
135135
protected ComponentConnector getChildComponent(Element element) {

0 commit comments

Comments
 (0)