This repository was archived by the owner on Dec 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGridStackLayout.java
More file actions
736 lines (654 loc) · 28 KB
/
GridStackLayout.java
File metadata and controls
736 lines (654 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/**
* GridStackLayout.java (GridStackLayout)
*
* Copyright 2015 Vaadin Ltd, Sami Viitanen <sami.viitanen@vaadin.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import com.vaadin.shared.EventId;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.Registration;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Component;
/**
* Vaadin layout using gridstack.js library to layout components
*
* gridstack.js by Pavel Reznikov: http://troolee.github.io/gridstack.js/
*/
@JavaScript({"jquery-3.3.1.min.js", "jquery-ui.min.js", "lodash.min.js", "gridstack.js", "gridstack.jQueryUI.js"})
public class GridStackLayout extends AbstractLayout implements LayoutEvents.LayoutClickNotifier {
protected final List<Component> components = new ArrayList<>();
private final List<GridStackMoveEvent.GridStackMoveListener> moveListeners = new ArrayList<>();
private final List<GridStackReadyEvent.GridStackReadyListener> readyListeners = new ArrayList<>();
private int readyCalls = 0;
/**
* Use this as x or y coordinate if you want to leave slot selection of component to client side
*/
public final static int CLIENT_SIDE_SELECTS = -1;
private GridStackServerRpc serverRpc = new GridStackServerRpc() {
@Override
public void layoutClick(MouseEventDetails mouseEventDetails, Connector connector) {
fireEvent(LayoutEvents.LayoutClickEvent.createEvent(GridStackLayout.this,
mouseEventDetails, connector));
}
@Override
public void onChildrenMoved(List<GridStackMoveData> moves) {
Collection<GridStackMoveEvent> events = new ArrayList<GridStackMoveEvent>();
for (GridStackMoveData move : moves) {
Component childComponent = (Component) move.child;
GridStackCoordinates oldCoordinates = getCoordinates(childComponent);
GridStackChildOptions info = getState(false).childOptions.get(move.child);
info.x = move.x;
info.y = move.y;
info.width = move.width;
info.height = move.height;
if(!oldCoordinates.equals(getCoordinates(childComponent))) {
events.add(createMoveEvent(childComponent, oldCoordinates));
}
}
fireMoveEvents(events);
}
@Override
public void onReady(int widthPx) {
readyCalls++;
final GridStackReadyEvent event = new GridStackReadyEvent(GridStackLayout.this, readyCalls == 1, widthPx);
readyListeners.forEach(l -> l.onGridStackReady(event));
}
};
/**
* Creates GridStackLayout with default 3 columns
*/
public GridStackLayout() {
super();
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(int columns) {
this();
if(columns <= 0) {
throw new IllegalArgumentException("Amount of columns can not be 0 or negative");
}
addStyleName("with-" + columns + "-columns");
getState().gridStackOptions.width = columns;
}
/**
* Create GridStackLayout with defined column and row count.
* @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) {
this(columns);
if(rows <= 0) {
throw new IllegalArgumentException("Amount of rows can not be 0 or negative");
}
getState().gridStackOptions.height = rows;
}
@Override
protected GridStackLayoutState getState() {
return (GridStackLayoutState)super.getState();
}
@Override
protected GridStackLayoutState getState(boolean markDirty) {
return (GridStackLayoutState)super.getState(markDirty);
}
/**
* {@inheritDoc}
*/
@Override
public void addComponent(Component component) {
addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS);
}
/**
* Add component to layout
* @param component Component added
* @param useDragHandle true to add component with a separate drag handle, or false to make whole content act as a
* 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) {
addComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS, useDragHandle);
}
/**
* Add component to given slot
* @param component Component added
* @param x Slot's X coordinate
* @param y Slot's Y coordinate
*/
public void addComponent(Component component, int x, int y) {
addComponent(component, x, y, 1, 1);
}
/**
* Add component to given slot
* @param component Component added
* @param x Slot's X coordinate
* @param y Slot's Y coordinate
* @param useDragHandle true to add component with a separate drag handle, or false to make whole content act as a
* 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) {
addComponent(component, x, y, 1, 1, useDragHandle);
}
/**
* Add component to given slot, and define it's size
* @param component Component added
* @param x Slot's X coordinate (use negative values if position can be defined on client side)
* @param y Slot's Y coordinate (use negative values if position can be defined on client side)
* @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) {
addComponent(component, x, y, width, height, true);
}
/**
* Add component to given slot, and define it's size
* @param component Component added
* @param x Slot's X coordinate (use negative values if position can be defined on client side)
* @param y Slot's Y coordinate (use negative values if position can be defined on client side)
* @param width Width of space reserved (in slots)
* @param height Height of space reserved (in slots)
* @param useDragHandle true to add component with a separate drag handle, or false to make whole content act as a
* 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) {
super.addComponent(component);
components.add(component);
GridStackChildOptions info = new GridStackChildOptions();
info.x = x;
info.y = y;
info.width = width;
info.height = height;
info.useDragHandle = useDragHandle;
getState().childOptions.put(component, info);
}
/**
* 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) {
moveComponent(component, CLIENT_SIDE_SELECTS, CLIENT_SIDE_SELECTS);
}
/**
* Move given child component
* @param component Child component moved and/or resized
* @param x When defined component's X value is updated, if null old value is kept
* @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 {
moveAndResizeComponent(component, x, y, null, null);
}
/**
* Move given child component
* @param component Child component moved and/or resized
* @param width When defined component's width is updated, if null old value is kept
* @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 {
moveAndResizeComponent(component, null, null, width, height);
}
/**
* Move and/or resize given child component
* @param component Child component moved and/or resized
* @param x When defined component's X value is updated, if null old value is kept
* @param y When defined component's Y value is updated, if null old value is kept
* @param width When defined component's width is updated, if null old value is kept
* @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, or
* coordinates are invalid).
*/
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");
}
GridStackChildOptions info = getState().childOptions.get(component);
if(info == null) {
throw new IllegalArgumentException("Given component is not child of GridStackLayout");
}
if(x != null) {
info.x = x;
}
if(y != null) {
info.y = y;
}
if(width != null) {
info.width = width;
}
if(height != null) {
info.height = height;
}
}
/**
* Get component with given slot coordinate
* @param x Slot's X coordinate
* @param y Slot's Y coordinate
* @return Component at slot, or null if component not found
*/
public Component getComponent(int x, int y) {
return getComponent(x, y, false);
}
/**
* Get component with given slot coordinate
* @param x Slot's X coordinate
* @param y Slot's Y coordinate
* @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);
if(acceptInsideHit) {
if(x >= info.x && x < (info.x + info.width) && y >= info.y && y < (info.y + info.height)) {
return (Component) connector;
}
} else {
if (info.x == x && info.y == y) {
return (Component) connector;
}
}
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public void removeComponent(Component component) {
getState().childOptions.remove(component);
components.remove(component);
super.removeComponent(component);
}
@Override
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");
}
GridStackChildOptions oldOptions = getState(false).childOptions.get(oldComponent);
removeComponent(oldComponent);
if(newComponent.getParent() == this) {
removeComponent(newComponent);
}
addComponent(newComponent, oldOptions.x, oldOptions.y, oldOptions.width, oldOptions.height);
}
/**
* {@inheritDoc}
*/
@Override
public int getComponentCount() {
return components.size();
}
/**
* {@inheritDoc}
*/
@Override
public Iterator<Component> iterator() {
return components.iterator();
}
/**
* {@inheritDoc}
*/
@Override
public Registration addLayoutClickListener(LayoutEvents.LayoutClickListener listener) {
return addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
LayoutEvents.LayoutClickEvent.class, listener,
LayoutEvents.LayoutClickListener.clickMethod);
}
/**
* {@inheritDoc}
*/
@Override
public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener) {
removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
LayoutEvents.LayoutClickEvent.class, listener);
}
/**
* Add listener for component move events
* @param listener Listener added
*/
public void addGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) {
moveListeners.add(listener);
}
/**
* Remove listener of component move events
* @param listener Listener removed
*/
public void removeGridStackMoveListener(GridStackMoveEvent.GridStackMoveListener listener) {
moveListeners.remove(listener);
}
/**
* Add listener for gridstack ready event
* @param listener Listener added
*/
public void addGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) {
readyListeners.add(listener);
}
/**
* Remove listener of GridStack ready event
* @param listener Listener removed
*/
public void removeGridStackReadyListener(GridStackReadyEvent.GridStackReadyListener listener) {
readyListeners.remove(listener);
}
/**
* Get coordinates (X,Y,width,height) of component
* @param child Child component of layout
* @return Coordinates (X,Y,width,height) of component
* @throws IllegalArgumentException If child not found
*/
public GridStackCoordinates getCoordinates(Component child) {
GridStackChildOptions opts = getComponentOptions(child, false);
return new GridStackCoordinates(opts.x, opts.y, opts.width, opts.height);
}
protected GridStackMoveEvent createMoveEvent(Component component, GridStackCoordinates oldCoordinates) {
return new GridStackMoveEvent(this, component, oldCoordinates,
getCoordinates(component));
}
protected void fireMoveEvents(Collection<GridStackMoveEvent> events) {
if(events.isEmpty()) {
return;
}
for (GridStackMoveEvent.GridStackMoveListener listener : moveListeners) {
listener.onGridStackMove(events);
}
}
/**
* Define size limitations to child component. For now some values must be defined before child has been rendered
* on client side.
* @param child Child of this layout
* @param minWidth Mininum width in slots (null is undefined)
* @param maxWidth Maxium width in slots (null is undefined)
* @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);
childOpts.minWidth = minWidth;
childOpts.maxWidth = maxWidth;
childOpts.minHeight = minHeight;
childOpts.maxHeight = maxHeight;
}
/**
* Check if given child is locked (not allowed to move because of other dragged children)
* @param child Child component of layout
* @return true if locked, false if not
* @throws IllegalArgumentException If child not found
*/
public boolean isComponentLocked(Component child) {
return getComponentOptions(child, false).locked;
}
/**
* Change locked state of child. Locked children will not be moved away from other dragged children.
* @param child Child component of layout
* @param locked true if locked, false if not
* @throws IllegalArgumentException If child not found
*/
public void setComponentLocked(Component child, boolean locked) {
getComponentOptions(child).locked = locked;
}
protected GridStackChildOptions getComponentOptions(Component child) {
return getComponentOptions(child, true, true);
}
protected GridStackChildOptions getComponentOptions(Component child, boolean modify) {
return getComponentOptions(child, modify, true);
}
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");
}
GridStackChildOptions opt = getState(modify).childOptions.get(child);
if(opt == null) {
throw new IllegalStateException("Missing child options");
}
return opt;
}
/**
* Define if layout is animated when child components are moved
* @param animate true to animate, false to not animate
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setAnimate(boolean animate) {
getState().gridStackOptions.animate = animate;
return this;
}
/**
* Check if layout is animated
* @return true if animated, false if not
*/
public boolean isAnimate() {
return getState(false).gridStackOptions.animate;
}
/**
* Set layout static (no dragging of resizing) or dynamic (dragging and resizing allowed)
* @param staticGrid true to set static (no dragging of resizing), false to set dynamic (dragging and resizing
* allowed)
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setStaticGrid(boolean staticGrid) {
getState().gridStackOptions.staticGrid = staticGrid;
return this;
}
/**
* Check if layout is in static mode
* @return true if in static mode, false if not, null if not defined by server side
*/
public Boolean isStaticGrid() {
return getState(false).gridStackOptions.staticGrid;
}
/**
* Check if component has specific dragging handle
* @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) {
return getComponentOptions(child, false).useDragHandle;
}
/**
* Define vertical margin between components on GridStack layout. Value is only read when rendered on client side
* first time, so changing value after that will not have any effect (unless client side is detached).
* @param marginPx Vertical margin in pixels
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setVerticalMargin(int marginPx) {
return setVerticalMargin(marginPx + "px");
}
/**
* Define vertical margin between components on GridStack layout. Value is only read when rendered on client side
* first time, so changing value after that will not have any effect (unless client side is detached).
* @param margin Vertical margin in CSS units (eg. '10px' or '3em')
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setVerticalMargin(String margin) {
getState(true).gridStackOptions.verticalMargin = margin;
return this;
}
/**
* Get vertical margin between components. Value might be mismatch to actual value used, if changed after client
* side was last attached.
* @return Vertical margin in CSS units (eg. '3px' or '0.2em')
*/
public String getVerticalMargin() {
return getState(false).gridStackOptions.verticalMargin;
}
/**
* Define height of cell in pixels.
* @param heightPx Cell height in pixels
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setCellHeight(Integer heightPx) {
return setCellHeight(Objects.requireNonNull(heightPx) + "px");
}
/**
* Define height as CSS unit (eg. '20px' or '3em')
* @param height Cell height in CSS units
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setCellHeight(String height) {
getState(true).gridStackOptions.cellHeight = height;
return this;
}
/**
* Get height of cell in CSS units, if not defined will return empty.
* @return Cell height in CSS units (eg. '30px' or '3em'), empty if gridstack.js default is used
*/
public Optional<String> getCellHeight() {
return Optional.ofNullable(getState(false).gridStackOptions.cellHeight);
}
/**
* Set minimal width. If window width is less, grid will be shown in one-column mode. Changing value after
* it has been attached on client side will not apply until client side is detached and attached.
* @param minWidthPx Minimal width in pixels
* @return This GridStackLayout for command chaining
*/
public GridStackLayout setMinWidth(int minWidthPx) {
getState(true).gridStackOptions.minWidth = minWidthPx;
return this;
}
/**
* Get minimal width. If window width is less, grid will be shown in one-column mode. Value might be mismatch to
* actual value used, if changed after client side was last attached.
* @return Minimal width in pixels, if null the gridstack.js default is used.
*/
public Integer getMinWidth() {
return getState(false).gridStackOptions.minWidth;
}
/**
* Define if wrapper around child should allow vertical scrolling or not
* @param child Child of layout
* @param scrolling true to enable vertical scrolling, false to disable it
* @throws IllegalArgumentException If child not found
*/
public void setWrapperScrolling(Component child, boolean scrolling) {
getComponentOptions(child, true).disableScrolling = !scrolling;
}
/**
* Check if wrapper around child allows vertical scrolling or not
* @param child Child of layout
* @return true if wrapper allows vertical scrolling, false if wrapper hides vertical overflow
* @throws IllegalArgumentException If child not found
*/
public boolean isWrapperScrolling(Component child) {
return getComponentOptions(child, false).disableScrolling;
}
/**
* Check if given area is empty. Remember that any client side defined positioning not yet reported back to
* server side will be unknown and so can result work results.
* @param x Left edge coordinate of area
* @param x Top edge coordinate of area
* @param width Width of area in slots
* @param height Height of area in slots
* @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 {
return isAreaEmpty(new GridStackCoordinates(x, y, width, height));
}
/**
* Check if given area is empty. Remember that any client side defined positioning not yet reported back to
* server side will be unknown and so can result work results. Will also return false if area would go outside the
* right edge.
* @param coordinates Coordinate area checked (x, y, width, height)
* @return true if area is available and valid for use
* @throws IllegalArgumentException If invalid values given
*/
public boolean isAreaEmpty(GridStackCoordinates coordinates) throws IllegalArgumentException {
if(coordinates.getX() < 0) {
throw new IllegalArgumentException("X can not be negative");
}
if(coordinates.getY() < 0) {
throw new IllegalArgumentException("Y can not be negative");
}
if(coordinates.getWidth() <= 0) {
throw new IllegalArgumentException("Width most be larger than zero");
}
if(coordinates.getHeight() <= 0) {
throw new IllegalArgumentException("Height most be larger than zero");
}
// If item would drop out of left side, return false
if(coordinates.getX() + coordinates.getWidth() > getState(false).gridStackOptions.width) {
return false;
}
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);
if(occupant != null) {
return false;
}
}
}
return true;
}
/**
* Set style name applied to item wrapper around given child component. Currently this style name is only applied
* when child is added to layout. So remember to set it right after adding child to layout (before first client
* update after it).
* @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);
if(childOptions == null) {
throw new IllegalArgumentException("Child not found");
}
childOptions.styleName = styleName;
}
/**
* Get style name applied to item wrapper of given child component
* @param child Child component
* @return Style name applied
*/
public Optional<String> getChildItemStyleName(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;
}
}