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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.BrowserInfo;
import com.vaadin.terminal.gwt.client.MouseEventDetails;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
/**
*
*/
public class ITree extends FlowPanel implements Paintable {
public static final String CLASSNAME = "i-tree";
private Set<String> selectedIds = new HashSet<String>();
private ApplicationConnection client;
private String paintableId;
private boolean selectable;
private boolean isMultiselect;
private final HashMap<String, TreeNode> keyToNode = new HashMap<String, TreeNode>();
/**
* This map contains captions and icon urls for actions like: * "33_c" ->
* "Edit" * "33_i" -> "http://dom.com/edit.png"
*/
private final HashMap<String, String> actionMap = new HashMap<String, String>();
private boolean immediate;
private boolean isNullSelectionAllowed = true;
private boolean disabled = false;
private boolean readonly;
private boolean emitClickEvents;
private boolean rendering;
public ITree() {
super();
setStyleName(CLASSNAME);
}
private void updateActionMap(UIDL c) {
final Iterator it = c.getChildIterator();
while (it.hasNext()) {
final UIDL action = (UIDL) it.next();
final String key = action.getStringAttribute("key");
final String caption = action.getStringAttribute("caption");
actionMap.put(key + "_c", caption);
if (action.hasAttribute("icon")) {
// TODO need some uri handling ??
actionMap.put(key + "_i", client.translateToolkitUri(action
.getStringAttribute("icon")));
}
}
}
public String getActionCaption(String actionKey) {
return actionMap.get(actionKey + "_c");
}
public String getActionIcon(String actionKey) {
return actionMap.get(actionKey + "_i");
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
// Ensure correct implementation and let container manage caption
if (client.updateComponent(this, uidl, true)) {
return;
}
rendering = true;
this.client = client;
if (uidl.hasAttribute("partialUpdate")) {
handleUpdate(uidl);
rendering = false;
return;
}
paintableId = uidl.getId();
immediate = uidl.hasAttribute("immediate");
disabled = uidl.getBooleanAttribute("disabled");
readonly = uidl.getBooleanAttribute("readonly");
emitClickEvents = uidl.getBooleanAttribute("listenClicks");
isNullSelectionAllowed = uidl.getBooleanAttribute("nullselect");
clear();
for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
final UIDL childUidl = (UIDL) i.next();
if ("actions".equals(childUidl.getTag())) {
updateActionMap(childUidl);
continue;
}
final TreeNode childTree = new TreeNode();
this.add(childTree);
childTree.updateFromUIDL(childUidl, client);
}
final String selectMode = uidl.getStringAttribute("selectmode");
selectable = !"none".equals(selectMode);
isMultiselect = "multi".equals(selectMode);
selectedIds = uidl.getStringArrayVariableAsSet("selected");
rendering = false;
}
private void handleUpdate(UIDL uidl) {
final TreeNode rootNode = keyToNode.get(uidl
.getStringAttribute("rootKey"));
if (rootNode != null) {
if (!rootNode.getState()) {
// expanding node happened server side
rootNode.setState(true, false);
}
rootNode.renderChildNodes(uidl.getChildIterator());
}
if (uidl.hasVariable("selected")) {
// update selection in case selected nodes were not visible
selectedIds = uidl.getStringArrayVariableAsSet("selected");
}
}
public void setSelected(TreeNode treeNode, boolean selected) {
if (selected) {
if (!isMultiselect) {
while (selectedIds.size() > 0) {
final String id = selectedIds.iterator().next();
final TreeNode oldSelection = keyToNode.get(id);
if (oldSelection != null) {
// can be null if the node is not visible (parent
// collapsed)
oldSelection.setSelected(false);
}
selectedIds.remove(id);
}
}
treeNode.setSelected(true);
selectedIds.add(treeNode.key);
} else {
if (!isNullSelectionAllowed) {
if (!isMultiselect || selectedIds.size() == 1) {
return;
}
}
selectedIds.remove(treeNode.key);
treeNode.setSelected(false);
}
client.updateVariable(paintableId, "selected", selectedIds.toArray(),
immediate);
}
public boolean isSelected(TreeNode treeNode) {
return selectedIds.contains(treeNode.key);
}
protected class TreeNode extends SimplePanel implements ActionOwner {
public static final String CLASSNAME = "i-tree-node";
String key;
private String[] actionKeys = null;
private boolean childrenLoaded;
private Element nodeCaptionDiv;
protected Element nodeCaptionSpan;
private FlowPanel childNodeContainer;
private boolean open;
private Icon icon;
private Element ie6compatnode;
public TreeNode() {
constructDom();
sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.ONMOUSEUP
| Event.ONCONTEXTMENU);
}
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (disabled) {
return;
}
final int type = DOM.eventGetType(event);
final Element target = DOM.eventGetTarget(event);
if (emitClickEvents && target == nodeCaptionSpan
&& (type == Event.ONDBLCLICK || type == Event.ONMOUSEUP)) {
fireClick(event);
}
if (type == Event.ONCLICK) {
if (getElement() == target || ie6compatnode == target) {
// state change
toggleState();
} else if (!readonly && target == nodeCaptionSpan) {
// caption click = selection change && possible click event
toggleSelection();
}
DOM.eventCancelBubble(event, true);
} else if (type == Event.ONCONTEXTMENU) {
showContextMenu(event);
}
}
private void fireClick(Event evt) {
// non-immediate iff an immediate select event is going to happen
boolean imm = !immediate
|| !selectable
|| (!isNullSelectionAllowed && isSelected() && selectedIds
.size() == 1);
MouseEventDetails details = new MouseEventDetails(evt);
client.updateVariable(paintableId, "clickedKey", key, false);
client.updateVariable(paintableId, "clickEvent",
details.toString(), imm);
}
private void toggleSelection() {
if (selectable) {
ITree.this.setSelected(this, !isSelected());
}
}
private void toggleState() {
setState(!getState(), true);
}
protected void constructDom() {
// workaround for a very weird IE6 issue #1245
ie6compatnode = DOM.createDiv();
setStyleName(ie6compatnode, CLASSNAME + "-ie6compatnode");
DOM.setInnerText(ie6compatnode, " ");
DOM.appendChild(getElement(), ie6compatnode);
DOM.sinkEvents(ie6compatnode, Event.ONCLICK);
nodeCaptionDiv = DOM.createDiv();
DOM.setElementProperty(nodeCaptionDiv, "className", CLASSNAME
+ "-caption");
Element wrapper = DOM.createDiv();
nodeCaptionSpan = DOM.createSpan();
DOM.appendChild(getElement(), nodeCaptionDiv);
DOM.appendChild(nodeCaptionDiv, wrapper);
DOM.appendChild(wrapper, nodeCaptionSpan);
childNodeContainer = new FlowPanel();
childNodeContainer.setStylePrimaryName(CLASSNAME + "-children");
setWidget(childNodeContainer);
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
setText(uidl.getStringAttribute("caption"));
key = uidl.getStringAttribute("key");
keyToNode.put(key, this);
if (uidl.hasAttribute("al")) {
actionKeys = uidl.getStringArrayAttribute("al");
}
if (uidl.getTag().equals("node")) {
if (uidl.getChildCount() == 0) {
childNodeContainer.setVisible(false);
} else {
renderChildNodes(uidl.getChildIterator());
childrenLoaded = true;
}
} else {
addStyleName(CLASSNAME + "-leaf");
}
addStyleName(CLASSNAME);
if (uidl.getBooleanAttribute("expanded") && !getState()) {
setState(true, false);
}
if (uidl.getBooleanAttribute("selected")) {
setSelected(true);
}
if (uidl.hasAttribute("icon")) {
if (icon == null) {
icon = new Icon(client);
DOM.insertBefore(DOM.getFirstChild(nodeCaptionDiv), icon
.getElement(), nodeCaptionSpan);
}
icon.setUri(uidl.getStringAttribute("icon"));
} else {
if (icon != null) {
DOM.removeChild(DOM.getFirstChild(nodeCaptionDiv), icon
.getElement());
icon = null;
}
}
if (BrowserInfo.get().isIE6() && isAttached()) {
fixWidth();
}
}
private void setState(boolean state, boolean notifyServer) {
if (open == state) {
return;
}
if (state) {
if (!childrenLoaded && notifyServer) {
client.updateVariable(paintableId, "requestChildTree",
true, false);
}
if (notifyServer) {
client.updateVariable(paintableId, "expand",
new String[] { key }, true);
}
addStyleName(CLASSNAME + "-expanded");
childNodeContainer.setVisible(true);
} else {
removeStyleName(CLASSNAME + "-expanded");
childNodeContainer.setVisible(false);
if (notifyServer) {
client.updateVariable(paintableId, "collapse",
new String[] { key }, true);
}
}
open = state;
if (!rendering) {
Util.notifyParentOfSizeChange(ITree.this, false);
}
}
private boolean getState() {
return open;
}
private void setText(String text) {
DOM.setInnerText(nodeCaptionSpan, text);
}
private void renderChildNodes(Iterator i) {
childNodeContainer.clear();
childNodeContainer.setVisible(true);
while (i.hasNext()) {
final UIDL childUidl = (UIDL) i.next();
// actions are in bit weird place, don't mix them with children,
// but current node's actions
if ("actions".equals(childUidl.getTag())) {
updateActionMap(childUidl);
continue;
}
final TreeNode childTree = new TreeNode();
childNodeContainer.add(childTree);
childTree.updateFromUIDL(childUidl, client);
}
childrenLoaded = true;
}
public boolean isChildrenLoaded() {
return childrenLoaded;
}
public Action[] getActions() {
if (actionKeys == null) {
return new Action[] {};
}
final Action[] actions = new Action[actionKeys.length];
for (int i = 0; i < actions.length; i++) {
final String actionKey = actionKeys[i];
final TreeAction a = new TreeAction(this, String.valueOf(key),
actionKey);
a.setCaption(getActionCaption(actionKey));
a.setIconUrl(getActionIcon(actionKey));
actions[i] = a;
}
return actions;
}
public ApplicationConnection getClient() {
return client;
}
public String getPaintableId() {
return paintableId;
}
/**
* Adds/removes IT Mill Toolkit specific style name. This method ought
* to be called only from ITree.
*
* @param selected
*/
protected void setSelected(boolean selected) {
// add style name to caption dom structure only, not to subtree
setStyleName(nodeCaptionDiv, "i-tree-node-selected", selected);
}
protected boolean isSelected() {
return ITree.this.isSelected(this);
}
public void showContextMenu(Event event) {
if (!readonly && !disabled) {
if (actionKeys != null) {
int left = event.getClientX();
int top = event.getClientY();
top += Window.getScrollTop();
left += Window.getScrollLeft();
client.getContextMenu().showAt(this, left, top);
}
event.cancelBubble(true);
event.preventDefault();
}
}
/*
* We need to fix the width of TreeNodes so that the float in
* ie6compatNode does not wrap (see ticket #1245)
*/
private void fixWidth() {
nodeCaptionDiv.getStyle().setProperty("styleFloat", "left");
nodeCaptionDiv.getStyle().setProperty("display", "inline");
nodeCaptionDiv.getStyle().setProperty("marginLeft", "0");
final int captionWidth = ie6compatnode.getOffsetWidth()
+ nodeCaptionDiv.getOffsetWidth();
setWidth(captionWidth + "px");
}
@Override
public void onAttach() {
super.onAttach();
if (BrowserInfo.get().isIE6()) {
fixWidth();
}
}
}
}
|