@@ -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}
0 commit comments