// Empty the menu every time it receives new information
if (!getItems().isEmpty()) {
clearItems();
}
UIDL options = uidl.getChildUIDL(0);
// FIXME remove in version 7
if (options.hasAttribute("submenuIcon")) {
submenuIcon = client.translateVaadinUri(uidl.getChildUIDL(0)
.getStringAttribute("submenuIcon"));
} else {
submenuIcon = null;
}
if (uidl.hasAttribute("width")) {
UIDL moreItemUIDL = options.getChildUIDL(0);
StringBuffer itemHTML = new StringBuffer();
if (moreItemUIDL.hasAttribute("icon")) {
itemHTML.append("<img src=\""
+ client.translateVaadinUri(moreItemUIDL
.getStringAttribute("icon")) + "\" class=\""
+ Icon.CLASSNAME + "\" alt=\"\" />");
}
String moreItemText = moreItemUIDL.getStringAttribute("text");
if ("".equals(moreItemText)) {
moreItemText = "►";
}
itemHTML.append(moreItemText);
moreItem = new CustomMenuItem(itemHTML.toString(), emptyCommand);
collapsedRootItems = new VMenuBar(true);
moreItem.setSubMenu(collapsedRootItems);
moreItem.addStyleName(CLASSNAME + "-more-menuitem");
}
UIDL uidlItems = uidl.getChildUIDL(1);
Iterator<Object> itr = uidlItems.getChildIterator();
Stack<Iterator<Object>> iteratorStack = new Stack<Iterator<Object>>();
Stack<VMenuBar> menuStack = new Stack<VMenuBar>();
VMenuBar currentMenu = this;
while (itr.hasNext()) {
UIDL item = (UIDL) itr.next();
CustomMenuItem currentItem = null;
String itemText = item.getStringAttribute("text");
final int itemId = item.getIntAttribute("id");
boolean itemHasCommand = item.hasAttribute("command");
// Construct html from the text and the optional icon
StringBuffer itemHTML = new StringBuffer();
Command cmd = null;
if (item.hasAttribute("separator")) {
itemHTML.append("<span>---</span>");
} else {
// Add submenu indicator
if (item.getChildCount() > 0) {
// FIXME For compatibility reasons: remove in version 7
String bgStyle = "";
if (submenuIcon != null) {
bgStyle = " style=\"background-image: url("
+ submenuIcon
+ "); text-indent: -999px; width: 1em;\"";
}
itemHTML.append("<span class=\"" + CLASSNAME
+ "-submenu-indicator\"" + bgStyle
+ ">►</span>");
}
itemHTML.append("<span class=\"" + CLASSNAME
+ "-menuitem-caption\">");
if (item.hasAttribute("icon")) {
itemHTML.append("<img src=\""
+ client.translateVaadinUri(item
.getStringAttribute("icon"))
+ "\" class=\"" + Icon.CLASSNAME + "\" alt=\"\" />");
}
itemHTML.append(Util.escapeHTML(itemText) + "</span>");
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);
currentItem.setSeparator(item.hasAttribute("separator"));
currentItem.setEnabled(!item.hasAttribute("disabled"));
if (item.hasAttribute("style")) {
String itemStyle = item.getStringAttribute("style");
currentItem.addStyleDependentName(itemStyle);
}
if (item.getChildCount() > 0) {
menuStack.push(currentMenu);
iteratorStack.push(itr);
itr = item.getChildIterator();
currentMenu = new VMenuBar(true);
if (uidl.hasAttribute("style")) {
for (String style : uidl.getStringAttribute("style").split(
" ")) {
currentMenu.addStyleDependentName(style);