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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
package com.vaadin.terminal.gwt.client.ui;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;
public class VMenuBar extends Widget implements Paintable, PopupListener {
/** Set the CSS class name to allow styling. */
public static final String CLASSNAME = "v-menubar";
/** For server connections **/
protected String uidlId;
protected ApplicationConnection client;
protected final VMenuBar hostReference = this;
protected String submenuIcon = null;
protected boolean collapseItems = true;
protected CustomMenuItem moreItem = null;
// Construct an empty command to be used when the item has no command
// associated
protected static final Command emptyCommand = null;
/** Widget fields **/
protected boolean subMenu;
protected ArrayList<CustomMenuItem> items;
protected Element containerElement;
protected VOverlay popup;
protected VMenuBar visibleChildMenu;
protected VMenuBar parentMenu;
protected CustomMenuItem selected;
public VMenuBar() {
// Create an empty horizontal menubar
this(false);
}
public VMenuBar(boolean subMenu) {
super();
setElement(DOM.createDiv());
items = new ArrayList<CustomMenuItem>();
popup = null;
visibleChildMenu = null;
Element table = DOM.createTable();
Element tbody = DOM.createTBody();
DOM.appendChild(getElement(), table);
DOM.appendChild(table, tbody);
if (!subMenu) {
setStyleName(CLASSNAME);
Element tr = DOM.createTR();
DOM.appendChild(tbody, tr);
containerElement = tr;
} else {
setStyleName(CLASSNAME + "-submenu");
containerElement = tbody;
}
this.subMenu = subMenu;
sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
}
/**
* This method must be implemented to update the client-side component from
* UIDL data received from server.
*
* This method is called when the page is loaded for the first time, and
* every time UI changes in the component are received from the server.
*/
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
// This call should be made first. Ensure correct implementation,
// and let the containing layout manage caption, etc.
if (client.updateComponent(this, uidl, true)) {
return;
}
// For future connections
this.client = client;
uidlId = uidl.getId();
// Empty the menu every time it receives new information
if (!getItems().isEmpty()) {
clearItems();
}
UIDL options = uidl.getChildUIDL(0);
if (options.hasAttribute("submenuIcon")) {
submenuIcon = client.translateVaadinUri(uidl.getChildUIDL(0)
.getStringAttribute("submenuIcon"));
} else {
submenuIcon = null;
}
collapseItems = options.getBooleanAttribute("collapseItems");
if (collapseItems) {
UIDL moreItemUIDL = options.getChildUIDL(0);
StringBuffer itemHTML = new StringBuffer();
if (moreItemUIDL.hasAttribute("icon")) {
itemHTML.append("<img src=\""
+ client.translateToolkitUri(moreItemUIDL
.getStringAttribute("icon"))
+ "\" align=\"left\" />");
}
itemHTML.append(moreItemUIDL.getStringAttribute("text"));
moreItem = new CustomMenuItem(itemHTML.toString(), emptyCommand);
}
UIDL uidlItems = uidl.getChildUIDL(1);
Iterator<UIDL> itr = uidlItems.getChildIterator();
Stack<Iterator<UIDL>> iteratorStack = new Stack<Iterator<UIDL>>();
Stack<VMenuBar> menuStack = new Stack<VMenuBar>();
VMenuBar currentMenu = this;
while (itr.hasNext()) {
UIDL item = itr.next();
CustomMenuItem currentItem = null;
String itemText = item.getStringAttribute("text");
final int itemId = item.getIntAttribute("id");
boolean itemHasCommand = item.getBooleanAttribute("command");
// Construct html from the text and the optional icon
StringBuffer itemHTML = new StringBuffer();
if (item.hasAttribute("icon")) {
itemHTML.append("<img src=\""
+ client.translateToolkitUri(item
.getStringAttribute("icon"))
+ "\" align=\"left\" />");
}
itemHTML.append(itemText);
if (currentMenu != this && item.getChildCount() > 0
&& submenuIcon != null) {
itemHTML.append("<img src=\"" + submenuIcon
+ "\" align=\"right\" />");
}
Command cmd = null;
if (itemHasCommand) {
// Construct a command that fires onMenuClick(int) with the
// item's id-number
cmd = new Command() {
public void execute() {
hostReference.onMenuClick(itemId);
}
};
}
currentItem = currentMenu.addItem(itemHTML.toString(), cmd);
if (item.getChildCount() > 0) {
menuStack.push(currentMenu);
iteratorStack.push(itr);
itr = item.getChildIterator();
currentMenu = new VMenuBar(true);
currentItem.setSubMenu(currentMenu);
}
while (!itr.hasNext() && !iteratorStack.empty()) {
itr = iteratorStack.pop();
currentMenu = menuStack.pop();
}
}// while
// we might need to collapse the top-level menu
if (collapseItems) {
int topLevelWidth = 0;
int ourWidth = getOffsetWidth();
int i = 0;
for (; i < getItems().size() && topLevelWidth < ourWidth; i++) {
CustomMenuItem item = getItems().get(i);
topLevelWidth += item.getOffsetWidth();
}
if (topLevelWidth > getOffsetWidth()) {
ArrayList<CustomMenuItem> toBeCollapsed = new ArrayList<CustomMenuItem>();
VMenuBar collapsed = new VMenuBar(true);
for (int j = i - 2; j < getItems().size(); j++) {
toBeCollapsed.add(getItems().get(j));
}
for (int j = 0; j < toBeCollapsed.size(); j++) {
CustomMenuItem item = toBeCollapsed.get(j);
removeItem(item);
// it's ugly, but we have to insert the submenu icon
if (item.getSubMenu() != null && submenuIcon != null) {
StringBuffer itemText = new StringBuffer(item.getHTML());
itemText.append("<img src=\"");
itemText.append(submenuIcon);
itemText.append("\" align=\"right\" />");
item.setHTML(itemText.toString());
}
collapsed.addItem(item);
}
moreItem.setSubMenu(collapsed);
addItem(moreItem);
}
}
}// updateFromUIDL
/**
* This is called by the items in the menu and it communicates the
* information to the server
*
* @param clickedItemId
* id of the item that was clicked
*/
public void onMenuClick(int clickedItemId) {
// Updating the state to the server can not be done before
// the server connection is known, i.e., before updateFromUIDL()
// has been called.
if (uidlId != null && client != null) {
// Communicate the user interaction parameters to server. This call
// will initiate an AJAX request to the server.
client.updateVariable(uidlId, "clickedId", clickedItemId, true);
}
}
/** Widget methods **/
/**
* Returns a list of items in this menu
*/
public List<CustomMenuItem> getItems() {
return items;
}
/**
* Remove all the items in this menu
*/
public void clearItems() {
Element e = getContainingElement();
while (DOM.getChildCount(e) > 0) {
DOM.removeChild(e, DOM.getChild(e, 0));
}
items.clear();
}
/**
* Returns the containing element of the menu
*
* @return
*/
public Element getContainingElement() {
return containerElement;
}
/**
* Returns a new child element to add an item to
*
* @return
*/
public Element getNewChildElement() {
if (subMenu) {
Element tr = DOM.createTR();
DOM.appendChild(getContainingElement(), tr);
return tr;
} else {
return getContainingElement();
}
}
/**
* Add a new item to this menu
*
* @param html
* items text
* @param cmd
* items command
* @return the item created
*/
public CustomMenuItem addItem(String html, Command cmd) {
CustomMenuItem item = new CustomMenuItem(html, cmd);
addItem(item);
return item;
}
/**
* Add a new item to this menu
*
* @param item
*/
public void addItem(CustomMenuItem item) {
DOM.appendChild(getNewChildElement(), item.getElement());
item.setParentMenu(this);
item.setSelected(false);
items.add(item);
}
/**
* Remove the given item from this menu
*
* @param item
*/
public void removeItem(CustomMenuItem item) {
if (items.contains(item)) {
int index = items.indexOf(item);
Element container = getContainingElement();
DOM.removeChild(container, DOM.getChild(container, index));
items.remove(index);
}
}
/*
* @see
* com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user
* .client.Event)
*/
@Override
public void onBrowserEvent(Event e) {
super.onBrowserEvent(e);
Element targetElement = DOM.eventGetTarget(e);
CustomMenuItem targetItem = null;
for (int i = 0; i < items.size(); i++) {
CustomMenuItem item = items.get(i);
if (DOM.isOrHasChild(item.getElement(), targetElement)) {
targetItem = item;
}
}
if (targetItem != null) {
switch (DOM.eventGetType(e)) {
case Event.ONCLICK:
itemClick(targetItem);
break;
case Event.ONMOUSEOVER:
itemOver(targetItem);
break;
case Event.ONMOUSEOUT:
itemOut(targetItem);
break;
}
}
}
/**
* When an item is clicked
*
* @param item
*/
public void itemClick(CustomMenuItem item) {
if (item.getCommand() != null) {
setSelected(null);
if (visibleChildMenu != null) {
visibleChildMenu.hideChildren();
}
hideParents();
DeferredCommand.addCommand(item.getCommand());
} else {
if (item.getSubMenu() != null
&& item.getSubMenu() != visibleChildMenu) {
setSelected(item);
showChildMenu(item);
}
}
}
/**
* When the user hovers the mouse over the item
*
* @param item
*/
public void itemOver(CustomMenuItem item) {
setSelected(item);
boolean menuWasVisible = visibleChildMenu != null;
if (menuWasVisible && visibleChildMenu != item.getSubMenu()) {
popup.hide();
visibleChildMenu = null;
}
if (item.getSubMenu() != null && (parentMenu != null || menuWasVisible)
&& visibleChildMenu != item.getSubMenu()) {
showChildMenu(item);
}
}
/**
* When the mouse is moved away from an item
*
* @param item
*/
public void itemOut(CustomMenuItem item) {
if (visibleChildMenu != item.getSubMenu() || visibleChildMenu == null) {
hideChildMenu(item);
setSelected(null);
}
}
/**
* Shows the child menu of an item. The caller must ensure that the item has
* a submenu.
*
* @param item
*/
public void showChildMenu(CustomMenuItem item) {
popup = new VOverlay(true, false, true);
popup.setWidget(item.getSubMenu());
popup.addPopupListener(this);
if (subMenu) {
popup.setPopupPosition(item.getParentMenu().getAbsoluteLeft()
+ item.getParentMenu().getOffsetWidth(), item
.getAbsoluteTop());
} else {
popup.setPopupPosition(item.getAbsoluteLeft(), item.getParentMenu()
.getAbsoluteTop()
+ item.getParentMenu().getOffsetHeight());
}
item.getSubMenu().onShow();
visibleChildMenu = item.getSubMenu();
item.getSubMenu().setParentMenu(this);
popup.show();
}
/**
* Hides the submenu of an item
*
* @param item
*/
public void hideChildMenu(CustomMenuItem item) {
if (visibleChildMenu != null
&& !(visibleChildMenu == item.getSubMenu())) {
popup.hide();
}
}
/**
* When the menu is shown.
*/
public void onShow() {
if (!items.isEmpty()) {
(items.get(0)).setSelected(true);
}
}
/**
* Recursively hide all child menus
*/
public void hideChildren() {
if (visibleChildMenu != null) {
visibleChildMenu.hideChildren();
popup.hide();
}
}
/**
* Recursively hide all parent menus
*/
public void hideParents() {
if (visibleChildMenu != null) {
popup.hide();
setSelected(null);
}
if (getParentMenu() != null) {
getParentMenu().hideParents();
}
}
/**
* Returns the parent menu of this menu, or null if this is the top-level
* menu
*
* @return
*/
public VMenuBar getParentMenu() {
return parentMenu;
}
/**
* Set the parent menu of this menu
*
* @param parent
*/
public void setParentMenu(VMenuBar parent) {
parentMenu = parent;
}
/**
* Returns the currently selected item of this menu, or null if nothing is
* selected
*
* @return
*/
public CustomMenuItem getSelected() {
return selected;
}
/**
* Set the currently selected item of this menu
*
* @param item
*/
public void setSelected(CustomMenuItem item) {
// If we had something selected, unselect
if (item != selected && selected != null) {
selected.setSelected(false);
}
// If we have a valid selection, select it
if (item != null) {
item.setSelected(true);
}
selected = item;
}
/**
* Listener method, fired when this menu is closed
*/
public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
hideChildren();
if (autoClosed) {
hideParents();
}
// setSelected(null);
visibleChildMenu = null;
popup = null;
}
/**
*
* A class to hold information on menu items
*
*/
private class CustomMenuItem extends UIObject implements HasHTML {
protected String html = null;
protected Command command = null;
protected VMenuBar subMenu = null;
protected VMenuBar parentMenu = null;
public CustomMenuItem(String html, Command cmd) {
setElement(DOM.createTD());
setHTML(html);
setCommand(cmd);
setSelected(false);
addStyleName("menuitem");
}
public void setSelected(boolean selected) {
if (selected) {
addStyleDependentName("selected");
} else {
removeStyleDependentName("selected");
}
}
/*
* setters and getters for the fields
*/
public void setSubMenu(VMenuBar subMenu) {
this.subMenu = subMenu;
}
public VMenuBar getSubMenu() {
return subMenu;
}
public void setParentMenu(VMenuBar parentMenu) {
this.parentMenu = parentMenu;
}
public VMenuBar getParentMenu() {
return parentMenu;
}
public void setCommand(Command command) {
this.command = command;
}
public Command getCommand() {
return command;
}
public String getHTML() {
return html;
}
public void setHTML(String html) {
this.html = html;
DOM.setInnerHTML(getElement(), html);
}
public String getText() {
return html;
}
public void setText(String text) {
setHTML(text);
}
}
}// class VMenuBar
|