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
|
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* 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 com.vaadin.client.ui;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.StyleConstants;
import com.vaadin.client.VCaption;
public class VAbsoluteLayout extends ComplexPanel {
/** Tag name for widget creation */
public static final String TAGNAME = "absolutelayout";
/** Class name, prefix in styling */
public static final String CLASSNAME = "v-absolutelayout";
private DivElement marginElement;
protected final Element canvas = DOM.createDiv();
/**
* Default constructor
*/
public VAbsoluteLayout() {
setElement(Document.get().createDivElement());
marginElement = Document.get().createDivElement();
canvas.getStyle().setProperty("position", "relative");
canvas.getStyle().setProperty("overflow", "hidden");
marginElement.appendChild(canvas);
getElement().appendChild(marginElement);
setStyleName(CLASSNAME);
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.client.ui.Panel#add(com.google.gwt.user.client.ui
* .Widget)
*/
@Override
public void add(Widget child) {
AbsoluteWrapper wrapper = new AbsoluteWrapper(child);
wrapper.updateStyleNames();
super.add(wrapper, canvas);
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.client.ui.ComplexPanel#remove(com.google.gwt.user
* .client.ui.Widget)
*/
@Override
public boolean remove(Widget w) {
AbsoluteWrapper wrapper = getChildWrapper(w);
if (wrapper != null) {
wrapper.destroy();
return super.remove(wrapper);
}
return super.remove(w);
}
/**
* Does this layout contain a widget
*
* @param widget
* The widget to check
* @return Returns true if the widget is in this layout, false if not
*/
public boolean contains(Widget widget) {
return getChildWrapper(widget) != null;
}
/*
* (non-Javadoc)
*
* @see com.google.gwt.user.client.ui.ComplexPanel#getWidget(int)
*/
@Override
public Widget getWidget(int index) {
for (int i = 0, j = 0; i < super.getWidgetCount(); i++) {
Widget w = super.getWidget(i);
if (w instanceof AbsoluteWrapper) {
if (j == index) {
return w;
} else {
j++;
}
}
}
return null;
}
/*
* (non-Javadoc)
*
* @see com.google.gwt.user.client.ui.ComplexPanel#getWidgetCount()
*/
@Override
public int getWidgetCount() {
int counter = 0;
for (int i = 0; i < super.getWidgetCount(); i++) {
if (super.getWidget(i) instanceof AbsoluteWrapper) {
counter++;
}
}
return counter;
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.client.ui.ComplexPanel#getWidgetIndex(com.google.
* gwt.user.client.ui.Widget)
*/
@Override
public int getWidgetIndex(Widget child) {
for (int i = 0, j = 0; i < super.getWidgetCount(); i++) {
Widget w = super.getWidget(i);
if (w instanceof AbsoluteWrapper) {
if (child == w) {
return j;
} else {
j++;
}
}
}
return -1;
}
/**
* Sets a caption for a contained widget
*
* @param child
* The child widget to set the caption for
* @param caption
* The caption of the widget
*/
public void setWidgetCaption(Widget child, VCaption caption) {
AbsoluteWrapper wrapper = getChildWrapper(child);
if (wrapper != null) {
if (caption != null) {
if (!getChildren().contains(caption)) {
super.add(caption, canvas);
}
wrapper.setCaption(caption);
caption.updateCaption();
wrapper.updateCaptionPosition();
} else if (wrapper.getCaption() != null) {
wrapper.setCaption(null);
}
}
}
/**
* Set the position of the widget in the layout. The position is a CSS
* property string using properties such as top,left,right,top
*
* @param child
* The child widget to set the position for
* @param position
* The position string
*/
public void setWidgetPosition(Widget child, String position) {
AbsoluteWrapper wrapper = getChildWrapper(child);
if (wrapper != null) {
wrapper.setPosition(position);
}
}
/**
* Get the caption for a widget
*
* @param child
* The child widget to get the caption of
*/
public VCaption getWidgetCaption(Widget child) {
AbsoluteWrapper wrapper = getChildWrapper(child);
if (wrapper != null) {
return wrapper.getCaption();
}
return null;
}
/**
* Get the pixel width of an slot in the layout
*
* @param child
* The widget in the layout.
* @return Returns the size in pixels, or 0 if child is not in the layout
*/
public int getWidgetSlotWidth(Widget child) {
AbsoluteWrapper wrapper = getChildWrapper(child);
if (wrapper != null) {
return wrapper.getOffsetWidth();
}
return 0;
}
/**
* Get the pixel height of an slot in the layout
*
* @param child
* The widget in the layout
* @return Returns the size in pixels, or 0 if the child is not in the
* layout
*/
public int getWidgetSlotHeight(Widget child) {
AbsoluteWrapper wrapper = getChildWrapper(child);
if (wrapper != null) {
return wrapper.getOffsetHeight();
}
return 0;
}
/**
* Get the wrapper for a widget
*
* @param child
* The child to get the wrapper for
* @return
*/
protected AbsoluteWrapper getChildWrapper(Widget child) {
for (Widget w : getChildren()) {
if (w instanceof AbsoluteWrapper) {
AbsoluteWrapper wrapper = (AbsoluteWrapper) w;
if (wrapper.getWidget() == child) {
return wrapper;
}
}
}
return null;
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.client.ui.UIObject#setStylePrimaryName(java.lang.
* String)
*/
@Override
public void setStylePrimaryName(String style) {
updateStylenames(style);
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.client.ui.UIObject#setStyleName(java.lang.String)
*/
@Override
public void setStyleName(String style) {
super.setStyleName(style);
updateStylenames(style);
addStyleName(StyleConstants.UI_LAYOUT);
}
/**
* Updates all style names contained in the layout
*
* @param primaryStyleName
* The style name to use as primary
*/
protected void updateStylenames(String primaryStyleName) {
super.setStylePrimaryName(primaryStyleName);
canvas.setClassName(getStylePrimaryName() + "-canvas");
canvas.setClassName(getStylePrimaryName() + "-margin");
for (Widget w : getChildren()) {
if (w instanceof AbsoluteWrapper) {
AbsoluteWrapper wrapper = (AbsoluteWrapper) w;
wrapper.updateStyleNames();
}
}
}
/**
* Performs a vertical layout of the layout. Should be called when a widget
* is added or removed
*/
public void layoutVertically() {
layout();
}
/**
* Performs an horizontal layout. Should be called when a widget is add or
* removed
*/
public void layoutHorizontally() {
layout();
}
private void layout() {
for (Widget widget : getChildren()) {
if (widget instanceof AbsoluteWrapper) {
AbsoluteWrapper wrapper = (AbsoluteWrapper) widget;
wrapper.updateCaptionPosition();
}
}
}
/**
* Cleanup old wrappers which have been left empty by other inner layouts
* moving the widget from the wrapper into their own hierarchy. This usually
* happens when a call to setWidget(widget) is done in an inner layout which
* automatically detaches the widget from the parent, in this case the
* wrapper, and re-attaches it somewhere else. This has to be done in the
* layout phase since the order of the hierarchy events are not defined.
*/
public void cleanupWrappers() {
for (Widget widget : getChildren()) {
if (widget instanceof AbsoluteWrapper) {
AbsoluteWrapper wrapper = (AbsoluteWrapper) widget;
if (wrapper.getWidget() == null) {
wrapper.destroy();
super.remove(wrapper);
continue;
}
}
}
}
/**
* Sets style names for the wrapper wrapping the widget in the layout. The
* style names will be prefixed with v-absolutelayout-wrapper.
*
* @param widget
* The widget which wrapper we want to add the stylenames to
* @param stylenames
* The style names that should be added to the wrapper
*/
public void setWidgetWrapperStyleNames(Widget widget, String... stylenames) {
AbsoluteWrapper wrapper = getChildWrapper(widget);
if (wrapper == null) {
throw new IllegalArgumentException(
"No wrapper for widget found, has the widget been added to the layout?");
}
wrapper.setWrapperStyleNames(stylenames);
}
/**
* Internal wrapper for wrapping widgets in the Absolute layout
*/
protected class AbsoluteWrapper extends SimplePanel {
private String css;
private String left;
private String top;
private String right;
private String bottom;
private String zIndex;
private VCaption caption;
private String[] extraStyleNames;
/**
* Constructor
*
* @param child
* The child to wrap
*/
public AbsoluteWrapper(Widget child) {
setWidget(child);
}
/**
* Get the caption of the wrapper
*/
public VCaption getCaption() {
return caption;
}
/**
* Set the caption for the wrapper
*
* @param caption
* The caption for the wrapper
*/
public void setCaption(VCaption caption) {
if (caption != null) {
this.caption = caption;
} else if (this.caption != null) {
this.caption.removeFromParent();
this.caption = caption;
}
}
/**
* Removes the wrapper caption and itself from the layout
*/
public void destroy() {
if (caption != null) {
caption.removeFromParent();
}
removeFromParent();
}
/**
* Set the position for the wrapper in the layout
*
* @param position
* The position string
*/
public void setPosition(String position) {
if (css == null || !css.equals(position)) {
css = position;
top = right = bottom = left = zIndex = null;
if (!css.equals("")) {
String[] properties = css.split(";");
for (int i = 0; i < properties.length; i++) {
String[] keyValue = properties[i].split(":");
if (keyValue[0].equals("left")) {
left = keyValue[1];
} else if (keyValue[0].equals("top")) {
top = keyValue[1];
} else if (keyValue[0].equals("right")) {
right = keyValue[1];
} else if (keyValue[0].equals("bottom")) {
bottom = keyValue[1];
} else if (keyValue[0].equals("z-index")) {
zIndex = keyValue[1];
}
}
}
// ensure ne values
Style style = getElement().getStyle();
/*
* IE8 dies when nulling zIndex, even in IE7 mode. All other css
* properties (and even in older IE's) accept null values just
* fine. Assign empty string instead of null.
*/
if (zIndex != null) {
style.setProperty("zIndex", zIndex);
} else {
style.setProperty("zIndex", "");
}
style.setProperty("top", top);
style.setProperty("left", left);
style.setProperty("right", right);
style.setProperty("bottom", bottom);
}
updateCaptionPosition();
}
/**
* Updates the caption position by using element offset left and top
*/
private void updateCaptionPosition() {
if (caption != null) {
Style style = caption.getElement().getStyle();
style.setProperty("position", "absolute");
style.setPropertyPx("left", getElement().getOffsetLeft());
style.setPropertyPx("top", getElement().getOffsetTop()
- caption.getHeight());
}
}
/**
* Sets the style names of the wrapper. Will be prefixed with the
* v-absolutelayout-wrapper prefix
*
* @param stylenames
* The wrapper style names
*/
public void setWrapperStyleNames(String... stylenames) {
extraStyleNames = stylenames;
updateStyleNames();
}
/**
* Updates the style names using the primary style name as prefix
*/
protected void updateStyleNames() {
setStyleName(VAbsoluteLayout.this.getStylePrimaryName()
+ "-wrapper");
if (extraStyleNames != null) {
for (String stylename : extraStyleNames) {
addStyleDependentName(stylename);
}
}
}
}
}
|