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
|
package com.vaadin.terminal.gwt.client.ui.layout;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.BrowserInfo;
import com.vaadin.terminal.gwt.client.Container;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.ui.VMarginInfo;
public abstract class CellBasedLayout extends ComplexPanel implements Container {
protected Map<Widget, ChildComponentContainer> widgetToComponentContainer = new HashMap<Widget, ChildComponentContainer>();
protected ApplicationConnection client = null;
protected DivElement root;
public static final int ORIENTATION_VERTICAL = 0;
public static final int ORIENTATION_HORIZONTAL = 1;
protected Margins activeMargins = new Margins(0, 0, 0, 0);
protected VMarginInfo activeMarginsInfo = new VMarginInfo(-1);
protected boolean spacingEnabled = false;
protected final Spacing spacingFromCSS = new Spacing(12, 12);
protected final Spacing activeSpacing = new Spacing(0, 0);
private boolean dynamicWidth;
private boolean dynamicHeight;
private final DivElement clearElement = Document.get().createDivElement();
private String lastStyleName = "";
private boolean marginsNeedsRecalculation = false;
protected String STYLENAME_SPACING = "";
protected String STYLENAME_MARGIN_TOP = "";
protected String STYLENAME_MARGIN_RIGHT = "";
protected String STYLENAME_MARGIN_BOTTOM = "";
protected String STYLENAME_MARGIN_LEFT = "";
public static class Spacing {
public int hSpacing = 0;
public int vSpacing = 0;
public Spacing(int hSpacing, int vSpacing) {
this.hSpacing = hSpacing;
this.vSpacing = vSpacing;
}
@Override
public String toString() {
return "Spacing [hSpacing=" + hSpacing + ",vSpacing=" + vSpacing
+ "]";
}
}
public CellBasedLayout() {
super();
setElement(Document.get().createDivElement());
getElement().getStyle().setProperty("overflow", "hidden");
if (BrowserInfo.get().isIE()) {
getElement().getStyle().setProperty("position", "relative");
getElement().getStyle().setProperty("zoom", "1");
}
root = Document.get().createDivElement();
root.getStyle().setProperty("overflow", "hidden");
if (BrowserInfo.get().isIE()) {
root.getStyle().setProperty("position", "relative");
}
getElement().appendChild(root);
Style style = clearElement.getStyle();
style.setProperty("width", "0px");
style.setProperty("height", "0px");
style.setProperty("clear", "both");
style.setProperty("overflow", "hidden");
root.appendChild(clearElement);
}
public boolean hasChildComponent(Widget component) {
return widgetToComponentContainer.containsKey(component);
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
this.client = client;
// Only non-cached UIDL:s can introduce changes
if (uidl.getBooleanAttribute("cached")) {
return;
}
/**
* Margin and spacind detection depends on classNames and must be set
* before setting size. Here just update the details from UIDL and from
* overridden setStyleName run actual margin detections.
*/
updateMarginAndSpacingInfo(uidl);
/*
* This call should be made first. Ensure correct implementation, handle
* size etc.
*/
if (client.updateComponent(this, uidl, true)) {
return;
}
handleDynamicDimensions(uidl);
}
@Override
public void setStyleName(String styleName) {
super.setStyleName(styleName);
if (isAttached() && marginsNeedsRecalculation
|| !lastStyleName.equals(styleName)) {
measureMarginsAndSpacing();
lastStyleName = styleName;
marginsNeedsRecalculation = false;
}
}
private void handleDynamicDimensions(UIDL uidl) {
String w = uidl.hasAttribute("width") ? uidl
.getStringAttribute("width") : "";
String h = uidl.hasAttribute("height") ? uidl
.getStringAttribute("height") : "";
if (w.equals("")) {
dynamicWidth = true;
} else {
dynamicWidth = false;
}
if (h.equals("")) {
dynamicHeight = true;
} else {
dynamicHeight = false;
}
}
protected void addOrMoveChild(ChildComponentContainer childComponent,
int position) {
if (childComponent.getParent() == this) {
if (getWidgetIndex(childComponent) != position) {
// Detach from old position child.
childComponent.removeFromParent();
// Logical attach.
getChildren().insert(childComponent, position);
root.insertBefore(childComponent.getElement(), root
.getChildNodes().getItem(position));
adopt(childComponent);
}
} else {
widgetToComponentContainer.put(childComponent.getWidget(),
childComponent);
// Logical attach.
getChildren().insert(childComponent, position);
// avoid inserts (they are slower than appends)
boolean insert = true;
if (widgetToComponentContainer.size() == position) {
insert = false;
}
if (insert) {
root.insertBefore(childComponent.getElement(), root
.getChildNodes().getItem(position));
} else {
root.insertBefore(childComponent.getElement(), clearElement);
}
// Adopt.
adopt(childComponent);
}
}
protected ChildComponentContainer getComponentContainer(Widget child) {
return widgetToComponentContainer.get(child);
}
protected boolean isDynamicWidth() {
return dynamicWidth;
}
protected boolean isDynamicHeight() {
return dynamicHeight;
}
private void updateMarginAndSpacingInfo(UIDL uidl) {
if (!uidl.hasAttribute("invisible")) {
int bitMask = uidl.getIntAttribute("margins");
if (activeMarginsInfo.getBitMask() != bitMask) {
activeMarginsInfo = new VMarginInfo(bitMask);
marginsNeedsRecalculation = true;
}
boolean spacing = uidl.getBooleanAttribute("spacing");
if (spacing != spacingEnabled) {
marginsNeedsRecalculation = true;
spacingEnabled = spacing;
}
}
}
private static DivElement measurement;
private static DivElement measurement2;
private static DivElement measurement3;
private static DivElement helper;
static {
helper = Document.get().createDivElement();
helper
.setInnerHTML("<div style=\"position:absolute;top:0;left:0;height:0;visibility:hidden;overflow:hidden;\">"
+ "<div style=\"width:0;height:0;visibility:hidden;overflow;hidden;\"></div></div><div style=\"position:absolute;height:0;overflow:hidden;\"></div>");
NodeList<Node> childNodes = helper.getChildNodes();
measurement = (DivElement) childNodes.getItem(0);
measurement2 = (DivElement) measurement.getFirstChildElement();
measurement3 = (DivElement) childNodes.getItem(1);
}
protected boolean measureMarginsAndSpacing() {
if (!isAttached()) {
return false;
}
// Measure spacing (actually CSS padding)
measurement3.setClassName(STYLENAME_SPACING
+ (spacingEnabled ? "-on" : "-off"));
String sn = getStylePrimaryName() + "-margin";
if (activeMarginsInfo.hasTop()) {
sn += " " + STYLENAME_MARGIN_TOP;
}
if (activeMarginsInfo.hasBottom()) {
sn += " " + STYLENAME_MARGIN_BOTTOM;
}
if (activeMarginsInfo.hasLeft()) {
sn += " " + STYLENAME_MARGIN_LEFT;
}
if (activeMarginsInfo.hasRight()) {
sn += " " + STYLENAME_MARGIN_RIGHT;
}
// Measure top and left margins (actually CSS padding)
measurement.setClassName(sn);
root.appendChild(helper);
activeSpacing.vSpacing = measurement3.getOffsetHeight();
activeSpacing.hSpacing = measurement3.getOffsetWidth();
activeMargins.setMarginTop(measurement2.getOffsetTop());
activeMargins.setMarginLeft(measurement2.getOffsetLeft());
activeMargins.setMarginRight(measurement.getOffsetWidth()
- activeMargins.getMarginLeft());
activeMargins.setMarginBottom(measurement.getOffsetHeight()
- activeMargins.getMarginTop());
// ApplicationConnection.getConsole().log("Margins: " + activeMargins);
// ApplicationConnection.getConsole().log("Spacing: " + activeSpacing);
// Util.alert("Margins: " + activeMargins);
root.removeChild(helper);
// apply margin
Style style = root.getStyle();
style.setPropertyPx("marginLeft", activeMargins.getMarginLeft());
style.setPropertyPx("marginRight", activeMargins.getMarginRight());
style.setPropertyPx("marginTop", activeMargins.getMarginTop());
style.setPropertyPx("marginBottom", activeMargins.getMarginBottom());
return true;
}
protected ChildComponentContainer getFirstChildComponentContainer() {
int size = getChildren().size();
if (size < 1) {
return null;
}
return (ChildComponentContainer) getChildren().get(0);
}
protected void removeChildrenAfter(int pos) {
// Remove all children after position "pos" but leave the clear element
// in place
int toRemove = getChildren().size() - pos;
while (toRemove-- > 0) {
/* flag to not if widget has been moved and rendered elsewhere */
boolean relocated = false;
ChildComponentContainer child = (ChildComponentContainer) getChildren()
.get(pos);
Widget widget = child.getWidget();
if (widget == null) {
// a rare case where child component has been relocated and
// rendered elsewhere
// clean widgetToComponentContainer map by iterating the correct
// mapping
Iterator<Widget> iterator = widgetToComponentContainer.keySet()
.iterator();
while (iterator.hasNext()) {
Widget key = iterator.next();
if (widgetToComponentContainer.get(key) == child) {
widget = key;
relocated = true;
break;
}
}
if (widget == null) {
throw new NullPointerException();
}
}
ChildComponentContainer remove = widgetToComponentContainer
.remove(widget);
remove(child);
if (!relocated) {
Paintable p = (Paintable) widget;
client.unregisterPaintable(p);
}
}
}
public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
ChildComponentContainer componentContainer = widgetToComponentContainer
.remove(oldComponent);
if (componentContainer == null) {
return;
}
componentContainer.setWidget(newComponent);
client.unregisterPaintable((Paintable) oldComponent);
widgetToComponentContainer.put(newComponent, componentContainer);
}
}
|