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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayString;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Component;
/**
* When a component is updated, it's client side widget's
* {@link Paintable#updateFromUIDL(UIDL, ApplicationConnection)
* updateFromUIDL()} will be called with the updated ("changes") UIDL received
* from the server.
* <p>
* UIDL is hierarchical, and there are a few methods to retrieve the children,
* {@link #getChildCount()}, {@link #getChildIterator()}
* {@link #getChildString(int)}, {@link #getChildUIDL(int)}.
* </p>
* <p>
* It can be helpful to keep in mind that UIDL was originally modeled in XML, so
* it's structure is very XML -like. For instance, the first to children in the
* underlying UIDL representation will contain the "tag" name and attributes,
* but will be skipped by the methods mentioned above.
* </p>
*/
public final class UIDL extends JavaScriptObject {
protected UIDL() {
}
/**
* Shorthand for getting the attribute named "id", which for Paintables is
* the essential paintableId which binds the server side component to the
* client side widget.
*
* @return the value of the id attribute, if available
*/
public String getId() {
return getStringAttribute("id");
}
/**
* Gets the name of this UIDL section, as created with
* {@link PaintTarget#startTag(String) PaintTarget.startTag()} in the
* server-side {@link Component#paint(PaintTarget) Component.paint()} or
* (usually) {@link AbstractComponent#paintContent(PaintTarget)
* AbstractComponent.paintContent()}. Note that if the UIDL corresponds to a
* Paintable, a component identifier will be returned instead - this is used
* internally and is not needed within
* {@link Paintable#updateFromUIDL(UIDL, ApplicationConnection)
* updateFromUIDL()}.
*
* @return the name for this section
*/
public native String getTag()
/*-{
return this[0];
}-*/;
private native ValueMap attr()
/*-{
return this[1];
}-*/;
private native ValueMap var()
/*-{
return this[1]["v"];
}-*/;
private native boolean hasVariables()
/*-{
return Boolean(this[1]["v"]);
}-*/;
/**
* Gets the named attribute as a String.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public String getStringAttribute(String name) {
return attr().getString(name);
}
/**
* Gets the names of the attributes available.
*
* @return the names of available attributes
*/
public Set<String> getAttributeNames() {
Set<String> keySet = attr().getKeySet();
keySet.remove("v");
return keySet;
}
/**
* Gets the names of variables available.
*
* @return the names of available variables
*/
public Set<String> getVariableNames() {
if (!hasVariables()) {
return new HashSet<String>();
} else {
Set<String> keySet = var().getKeySet();
return keySet;
}
}
/**
* Gets the named attribute as an int.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public int getIntAttribute(String name) {
return attr().getInt(name);
}
/**
* Gets the named attribute as a long.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public long getLongAttribute(String name) {
return (long) attr().getRawNumber(name);
}
/**
* Gets the named attribute as a float.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public float getFloatAttribute(String name) {
return (float) attr().getRawNumber(name);
}
/**
* Gets the named attribute as a double.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public double getDoubleAttribute(String name) {
return attr().getRawNumber(name);
}
/**
* Gets the named attribute as a boolean.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public boolean getBooleanAttribute(String name) {
return attr().getBoolean(name);
}
/**
* Gets the named attribute as a Map of named values (key/value pairs).
*
* @param name
* the name of the attribute to get
* @return the attribute Map
*/
public ValueMap getMapAttribute(String name) {
return attr().getValueMap(name);
}
/**
* Gets the named attribute as an array of Strings.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public String[] getStringArrayAttribute(String name) {
return attr().getStringArray(name);
}
/**
* Gets the named attribute as an int array.
*
* @param name
* the name of the attribute to get
* @return the attribute value
*/
public int[] getIntArrayAttribute(final String name) {
return attr().getIntArray(name);
}
/**
* Get attributes value as string whatever the type is
*
* @param name
* @return string presentation of attribute
*/
native String getAttribute(String name)
/*-{
return '' + this[1][name];
}-*/;
native String getVariable(String name)
/*-{
return '' + this[1]['v'][name];
}-*/;
/**
* Indicates whether or not the named attribute is available.
*
* @param name
* the name of the attribute to check
* @return true if the attribute is available, false otherwise
*/
public boolean hasAttribute(final String name) {
return attr().containsKey(name);
}
/**
* Gets the UIDL for the child at the given index.
*
* @param i
* the index of the child to get
* @return the UIDL of the child if it exists
*/
public native UIDL getChildUIDL(int i)
/*-{
return this[i + 2];
}-*/;
/**
* Gets the child at the given index as a String.
*
* @param i
* the index of the child to get
* @return the String representation of the child if it exists
*/
public native String getChildString(int i)
/*-{
return this[i + 2];
}-*/;
private native XML getChildXML(int index)
/*-{
return this[index + 2];
}-*/;
/**
* Gets an iterator that can be used to iterate trough the children of this
* UIDL.
* <p>
* The Object returned by <code>next()</code> will be appropriately typed -
* if it's UIDL, {@link #getTag()} can be used to check which section is in
* question.
* </p>
* <p>
* The basic use case is to iterate over the children of an UIDL update, and
* update the appropriate part of the widget for each child encountered, e.g
* if <code>getTag()</code> returns "color", one would update the widgets
* color to reflect the value of the "color" section.
* </p>
*
* @return an iterator for iterating over UIDL children
*/
public Iterator<Object> getChildIterator() {
return new Iterator<Object>() {
int index = -1;
public void remove() {
throw new UnsupportedOperationException();
}
public Object next() {
if (hasNext()) {
int typeOfChild = typeOfChild(++index);
switch (typeOfChild) {
case CHILD_TYPE_UIDL:
UIDL childUIDL = getChildUIDL(index);
return childUIDL;
case CHILD_TYPE_STRING:
return getChildString(index);
case CHILD_TYPE_XML:
return getChildXML(index);
default:
throw new IllegalStateException(
"Illegal child in tag " + getTag()
+ " at index " + index);
}
}
return null;
}
public boolean hasNext() {
int count = getChildCount();
return count > index + 1;
}
};
}
private static final int CHILD_TYPE_STRING = 0;
private static final int CHILD_TYPE_UIDL = 1;
private static final int CHILD_TYPE_XML = 2;
private native int typeOfChild(int index)
/*-{
var t = typeof this[index + 2];
if(t == "object") {
if(typeof(t.length) == "number") {
return 1;
} else {
return 2;
}
} else if (t == "string") {
return 0;
}
return -1;
}-*/;
/**
* @deprecated
*/
@Deprecated
public String getChildrenAsXML() {
return toString();
}
/**
* Checks if the named variable is available.
*
* @param name
* the name of the variable desired
* @return true if the variable exists, false otherwise
*/
public boolean hasVariable(String name) {
return hasVariables() && var().containsKey(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public String getStringVariable(String name) {
return var().getString(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public int getIntVariable(String name) {
return var().getInt(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public long getLongVariable(String name) {
return (long) var().getRawNumber(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public float getFloatVariable(String name) {
return (float) var().getRawNumber(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public double getDoubleVariable(String name) {
return var().getRawNumber(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public boolean getBooleanVariable(String name) {
return var().getBoolean(name);
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public String[] getStringArrayVariable(String name) {
return var().getStringArray(name);
}
/**
* Gets the value of the named String[] variable as a Set of Strings.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public Set<String> getStringArrayVariableAsSet(final String name) {
final HashSet<String> s = new HashSet<String>();
JsArrayString a = var().getJSStringArray(name);
for (int i = 0; i < a.length(); i++) {
s.add(a.get(i));
}
return s;
}
/**
* Gets the value of the named variable.
*
* @param name
* the name of the variable
* @return the value of the variable
*/
public int[] getIntArrayVariable(String name) {
return var().getIntArray(name);
}
/**
* @deprecated should not be used anymore
*/
@Deprecated
public final static class XML extends JavaScriptObject {
protected XML() {
}
public native String getXMLAsString()
/*-{
var buf = new Array();
var self = this;
for(j in self) {
buf.push("<");
buf.push(j);
buf.push(">");
buf.push(self[j]);
buf.push("</");
buf.push(j);
buf.push(">");
}
return buf.join("");
}-*/;
}
/**
* Returns the number of children.
*
* @return the number of children
*/
public native int getChildCount()
/*-{
return this.length - 2;
}-*/;
/**
* Shorthand that returns the component errors as UIDL. Only applicable for
* Paintables.
*
* @return the error UIDL if available
*/
public native UIDL getErrors()
/*-{
return this[1]['error'];
}-*/;
native boolean isMapAttribute(String name)
/*-{
return typeof this[1][name] == "object";
}-*/;
/**
* Gets the Paintable with the id found in the named attributes's value.
*
* @param name
* the name of the attribute
* @return the Paintable referenced by the attribute, if it exists
*/
public Paintable getPaintableAttribute(String name,
ApplicationConnection connection) {
return connection.getPaintable(getStringAttribute(name));
}
/**
* Gets the Paintable with the id found in the named variable's value.
*
* @param name
* the name of the variable
* @return the Paintable referenced by the variable, if it exists
*/
public Paintable getPaintableVariable(String name,
ApplicationConnection connection) {
return connection.getPaintable(getStringVariable(name));
}
/**
* Returns the child UIDL by its name. If several child nodes exist with the
* given name, the first child UIDL will be returned.
*
* @param tagName
* @return the child UIDL or null if child wit given name was not found
*/
public UIDL getChildByTagName(String tagName) {
Iterator<Object> childIterator = getChildIterator();
while (childIterator.hasNext()) {
Object next = childIterator.next();
if (next instanceof UIDL) {
UIDL childUIDL = (UIDL) next;
if (childUIDL.getTag().equals(tagName)) {
return childUIDL;
}
}
}
return null;
}
}
|