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
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
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.ComponentConnector;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.StyleConstants;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VCaption;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.ValueMap;
public class VCssLayout extends SimplePanel {
public static final String TAGNAME = "csslayout";
public static final String CLASSNAME = "v-" + TAGNAME;
FlowPane panel = new FlowPane();
Element margin = DOM.createDiv();
public VCssLayout() {
super();
getElement().appendChild(margin);
setStyleName(CLASSNAME);
margin.setClassName(CLASSNAME + "-margin");
setWidget(panel);
}
@Override
protected Element getContainerElement() {
return margin;
}
public class FlowPane extends FlowPanel {
private final HashMap<Widget, VCaption> widgetToCaption = new HashMap<Widget, VCaption>();
private ApplicationConnection client;
private int lastIndex;
public FlowPane() {
super();
setStyleName(CLASSNAME + "-container");
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
// for later requests
this.client = client;
final Collection<Widget> oldWidgets = new HashSet<Widget>();
for (final Iterator<Widget> iterator = iterator(); iterator
.hasNext();) {
oldWidgets.add(iterator.next());
}
ValueMap mapAttribute = null;
if (uidl.hasAttribute("css")) {
mapAttribute = uidl.getMapAttribute("css");
}
lastIndex = 0;
for (final Iterator<Object> i = uidl.getChildIterator(); i
.hasNext();) {
final UIDL r = (UIDL) i.next();
final ComponentConnector child = client.getPaintable(r);
final Widget widget = child.getWidget();
if (widget.getParent() == this) {
oldWidgets.remove(widget);
VCaption vCaption = widgetToCaption.get(widget);
if (vCaption != null) {
addOrMove(vCaption, lastIndex++);
oldWidgets.remove(vCaption);
}
}
addOrMove(widget, lastIndex++);
if (mapAttribute != null && mapAttribute.containsKey(r.getId())) {
String css = null;
try {
Style style = widget.getElement().getStyle();
css = mapAttribute.getString(r.getId());
String[] cssRules = css.split(";");
for (int j = 0; j < cssRules.length; j++) {
String[] rule = cssRules[j].split(":");
if (rule.length == 0) {
continue;
} else {
style.setProperty(
makeCamelCase(rule[0].trim()),
rule[1].trim());
}
}
} catch (Exception e) {
VConsole.log("CssLayout encounterd invalid css string: "
+ css);
}
}
if (!r.getBooleanAttribute("cached")) {
child.updateFromUIDL(r, client);
}
}
// loop oldWidgetWrappers that where not re-attached and unregister
// them
for (Widget w : oldWidgets) {
remove(w);
ConnectorMap paintableMap = ConnectorMap.get(client);
if (paintableMap.isConnector(w)) {
final ComponentConnector p = ConnectorMap.get(client)
.getConnector(w);
client.unregisterPaintable(p);
}
VCaption vCaption = widgetToCaption.remove(w);
if (vCaption != null) {
remove(vCaption);
}
}
}
private void addOrMove(Widget child, int index) {
if (child.getParent() == this) {
int currentIndex = getWidgetIndex(child);
if (index == currentIndex) {
return;
}
}
insert(child, index);
}
public void updateCaption(ComponentConnector paintable) {
Widget widget = paintable.getWidget();
VCaption caption = widgetToCaption.get(widget);
if (VCaption.isNeeded(paintable.getState())) {
if (caption == null) {
caption = new VCaption(paintable, client);
widgetToCaption.put(widget, caption);
insert(caption, getWidgetIndex(widget));
lastIndex++;
} else if (!caption.isAttached()) {
insert(caption, getWidgetIndex(widget));
lastIndex++;
}
caption.updateCaption();
} else if (caption != null) {
remove(caption);
widgetToCaption.remove(widget);
}
}
ComponentConnector getComponent(Element element) {
return Util
.getConnectorForElement(client, VCssLayout.this, element);
}
}
private static final String makeCamelCase(String cssProperty) {
// TODO this might be cleaner to implement with regexp
while (cssProperty.contains("-")) {
int indexOf = cssProperty.indexOf("-");
cssProperty = cssProperty.substring(0, indexOf)
+ String.valueOf(cssProperty.charAt(indexOf + 1))
.toUpperCase() + cssProperty.substring(indexOf + 2);
}
if ("float".equals(cssProperty)) {
if (BrowserInfo.get().isIE()) {
return "styleFloat";
} else {
return "cssFloat";
}
}
return cssProperty;
}
/**
* Sets CSS classes for margin and spacing based on the given parameters.
*
* @param margins
* A {@link VMarginInfo} object that provides info on
* top/left/bottom/right margins
* @param spacing
* true to enable spacing, false otherwise
*/
protected void setMarginAndSpacingStyles(VMarginInfo margins,
boolean spacing) {
setStyleName(margin, VCssLayout.CLASSNAME + "-"
+ StyleConstants.MARGIN_TOP, margins.hasTop());
setStyleName(margin, VCssLayout.CLASSNAME + "-"
+ StyleConstants.MARGIN_RIGHT, margins.hasRight());
setStyleName(margin, VCssLayout.CLASSNAME + "-"
+ StyleConstants.MARGIN_BOTTOM, margins.hasBottom());
setStyleName(margin, VCssLayout.CLASSNAME + "-"
+ StyleConstants.MARGIN_LEFT, margins.hasLeft());
setStyleName(margin, VCssLayout.CLASSNAME + "-" + "spacing", spacing);
}
}
|