-
Notifications
You must be signed in to change notification settings - Fork 477
/
CustomDeckLayoutPanel.java
421 lines (370 loc) · 11.9 KB
/
CustomDeckLayoutPanel.java
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
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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.kaaproject.kaa.server.admin.client.layout;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.layout.client.Layout;
import com.google.gwt.layout.client.Layout.AnimationCallback;
import com.google.gwt.layout.client.Layout.Layer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.AnimatedLayout;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.InsertPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LayoutCommand;
import com.google.gwt.user.client.ui.ProvidesResize;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetCollection;
public class CustomDeckLayoutPanel extends ComplexPanel implements AnimatedLayout,
RequiresResize, ProvidesResize, InsertPanel.ForIsWidget, AcceptsOneWidget {
private final Layout layout;
private final LayoutCommand layoutCmd;
private int animationDuration = 0;
private boolean isAnimationVertical;
private Widget hidingWidget;
private Widget lastVisibleWidget;
private Widget visibleWidget;
/**
* Creates an empty deck panel.
*/
public CustomDeckLayoutPanel() {
setElement(Document.get().createDivElement());
layout = new Layout(getElement());
layoutCmd = new DeckAnimateCommand(layout);
}
@Override
public void add(Widget widget) {
insert(widget, getWidgetCount());
}
@Override
public void animate(int duration) {
animate(duration, null);
}
@Override
public void animate(int duration, AnimationCallback callback) {
layoutCmd.schedule(duration, callback);
}
@Override
public void forceLayout() {
layoutCmd.cancel();
doBeforeLayout();
layout.layout();
doAfterLayout();
onResize();
}
/**
* Get the duration of the animated transition between tabs.
*
* @return the duration in milliseconds
*/
public int getAnimationDuration() {
return animationDuration;
}
/**
* Set the duration of the animated transition between tabs.
*
* @param duration the duration in milliseconds.
*/
public void setAnimationDuration(int duration) {
this.animationDuration = duration;
}
/**
* Gets the currently-visible widget.
*
* @return the visible widget, or null if not visible
*/
public Widget getVisibleWidget() {
return visibleWidget;
}
/**
* Gets the index of the currently-visible widget.
*
* @return the visible widget's index
*/
public int getVisibleWidgetIndex() {
return getWidgetIndex(visibleWidget);
}
@Override
public void insert(IsWidget widget, int beforeIndex) {
insert(asWidgetOrNull(widget), beforeIndex);
}
@Override
public void insert(Widget widget, int beforeIndex) {
Widget before = (beforeIndex < getWidgetCount()) ? getWidget(beforeIndex)
: null;
insert(widget, before);
}
/**
* Insert a widget before the specified widget. If the widget is already a
* child of this panel, this method behaves as though {@link #remove(Widget)}
* had already been called.
*
* @param widget the widget to be added
* @param before the widget before which to insert the new child, or <code>null</code> to append
*/
public void insert(Widget widget, Widget before) {
assertIsChild(before);
// Detach new child.
widget.removeFromParent();
// Logical attach.
WidgetCollection children = getChildren();
if (before == null) {
children.add(widget);
} else {
int index = children.indexOf(before);
children.insert(widget, index);
}
// Physical attach.
Layer layer = layout.attachChild(widget.getElement(), (before != null)
? before.getElement() : null, widget);
setWidgetVisible(widget, layer, false);
widget.setLayoutData(layer);
// Adopt.
adopt(widget);
// Update the layout.
animate(0);
}
/**
* Check whether or not transitions slide in vertically or horizontally.
* Defaults to horizontally.
*
* @return true for vertical transitions, false for horizontal
*/
public boolean isAnimationVertical() {
return isAnimationVertical;
}
/**
* Set whether or not transitions slide in vertically or horizontally.
*
* @param isVertical true for vertical transitions, false for horizontal
*/
public void setAnimationVertical(boolean isVertical) {
this.isAnimationVertical = isVertical;
}
@Override
public void onResize() {
for (Widget child : getChildren()) {
if (child instanceof RequiresResize) {
((RequiresResize) child).onResize();
}
}
}
@Override
public boolean remove(Widget widget) {
boolean removed = super.remove(widget);
if (removed) {
Layer layer = (Layer) widget.getLayoutData();
layout.removeChild(layer);
widget.setLayoutData(null);
if (visibleWidget == widget) {
visibleWidget = null;
}
if (hidingWidget == widget) {
hidingWidget = null;
}
if (lastVisibleWidget == widget) {
lastVisibleWidget = null;
}
}
return removed;
}
/**
* Show the specified widget. If the widget is not a child of this panel, it
* is added to the end of the panel. If the specified widget is null, the
* currently-visible widget will be hidden.
*
* @param widget the widget to show, and add if not a child
*/
@Override
public void setWidget(IsWidget widget) {
// Hide the currently visible widget.
if (widget == null) {
showWidget(null);
return;
}
// Add the widget if it is not already a child.
if (widget.asWidget().getParent() != this) {
add(widget);
}
// Show the widget.
showWidget(widget.asWidget());
}
/**
* Shows the widget at the specified index. This causes the currently- visible
* widget to be hidden.
*
* @param index the index of the widget to be shown
*/
public void showWidget(int index) {
checkIndexBoundsForAccess(index);
showWidget(getWidget(index));
}
public void showWidget(int index, AnimationCallback callback) {
checkIndexBoundsForAccess(index);
showWidget(getWidget(index), callback);
}
/**
* Shows the widget at the specified index. This causes the currently- visible
* widget to be hidden.
*
* @param widget the widget to be shown
*/
public void showWidget(Widget widget) {
if (widget == visibleWidget) {
return;
}
assertIsChild(widget);
visibleWidget = widget;
animate((widget == null) ? 0 : animationDuration);
}
public void showWidget(Widget widget, AnimationCallback callback) {
if (widget == visibleWidget) {
return;
}
assertIsChild(widget);
visibleWidget = widget;
animate((widget == null) ? 0 : animationDuration, callback);
}
@Override
protected void onAttach() {
super.onAttach();
layout.onAttach();
}
@Override
protected void onDetach() {
super.onDetach();
layout.onDetach();
}
/**
* Assert that the specified widget is null or a child of this widget.
*
* @param widget the widget to check
*/
void assertIsChild(Widget widget) {
assert (widget == null)
|| (widget.getParent() == this) : "The specified widget is not a child of this panel";
}
/**
* Hide the widget that just slid out of view.
*/
private void doAfterLayout() {
if (hidingWidget != null) {
Layer layer = (Layer) hidingWidget.getLayoutData();
setWidgetVisible(hidingWidget, layer, false);
layout.layout();
hidingWidget = null;
}
}
/**
* Initialize the location of the widget that will slide into view.
*/
private void doBeforeLayout() {
Layer oldLayer = (lastVisibleWidget == null) ? null
: (Layer) lastVisibleWidget.getLayoutData();
Layer newLayer = (visibleWidget == null) ? null
: (Layer) visibleWidget.getLayoutData();
// Calculate the direction that the new widget will enter.
int oldIndex = getWidgetIndex(lastVisibleWidget);
int newIndex = getWidgetIndex(visibleWidget);
double direction = (oldIndex < newIndex) ? 100.0 : -100.0;
double vertDirection = isAnimationVertical ? direction : 0.0;
double horizDirection = isAnimationVertical ? 0.0
: LocaleInfo.getCurrentLocale().isRTL() ? -direction : direction;
/*
* Position the old widget in the center of the panel, and the new widget
* off to one side. If the old widget is the same as the new widget, then
* skip this step.
*/
hidingWidget = null;
if (visibleWidget != lastVisibleWidget) {
// Position the layers in their start positions.
if (oldLayer != null) {
// The old layer starts centered in the panel.
oldLayer.setTopHeight(0.0, Unit.PCT, 100.0, Unit.PCT);
oldLayer.setLeftWidth(0.0, Unit.PCT, 100.0, Unit.PCT);
setWidgetVisible(lastVisibleWidget, oldLayer, true);
}
if (newLayer != null) {
// The new layer starts off to one side.
newLayer.setTopHeight(vertDirection, Unit.PCT, 100.0, Unit.PCT);
newLayer.setLeftWidth(horizDirection, Unit.PCT, 100.0, Unit.PCT);
setWidgetVisible(visibleWidget, newLayer, true);
}
layout.layout();
hidingWidget = lastVisibleWidget;
}
// Set the end positions of the layers.
if (oldLayer != null) {
// The old layer ends off to one side.
oldLayer.setTopHeight(-vertDirection, Unit.PCT, 100.0, Unit.PCT);
oldLayer.setLeftWidth(-horizDirection, Unit.PCT, 100.0, Unit.PCT);
setWidgetVisible(lastVisibleWidget, oldLayer, true);
}
if (newLayer != null) {
// The new layer ends centered in the panel.
newLayer.setTopHeight(0.0, Unit.PCT, 100.0, Unit.PCT);
newLayer.setLeftWidth(0.0, Unit.PCT, 100.0, Unit.PCT);
/*
* The call to layout() above could have canceled an existing layout
* animation, which could cause this widget to be hidden if the user
* toggles between two visible widgets. We set it visible again to ensure
* that it ends up visible.
*/
setWidgetVisible(visibleWidget, newLayer, true);
}
lastVisibleWidget = visibleWidget;
}
private void setWidgetVisible(Widget widget, Layer layer, boolean visible) {
layer.setVisible(visible);
/*
* Set the visibility of the widget. This is used by lazy panel to
* initialize the widget.
*/
widget.setVisible(visible);
}
/**
* {@link LayoutCommand} used by this widget.
*/
private class DeckAnimateCommand extends LayoutCommand {
public DeckAnimateCommand(Layout layout) {
super(layout);
}
@Override
public void schedule(int duration, final AnimationCallback callback) {
super.schedule(duration, new AnimationCallback() {
@Override
public void onAnimationComplete() {
CustomDeckLayoutPanel.this.doAfterLayout();
if (callback != null) {
callback.onAnimationComplete();
}
}
@Override
public void onLayout(Layer layer, double progress) {
if (callback != null) {
callback.onLayout(layer, progress);
}
}
});
}
@Override
protected void doBeforeLayout() {
CustomDeckLayoutPanel.this.doBeforeLayout();
}
}
}