diff options
Diffstat (limited to 'client/src/main/java/com/vaadin/client/ui/VMenuBar.java')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VMenuBar.java | 75 |
1 files changed, 67 insertions, 8 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VMenuBar.java b/client/src/main/java/com/vaadin/client/ui/VMenuBar.java index d95954b26d..9dedb78205 100644 --- a/client/src/main/java/com/vaadin/client/ui/VMenuBar.java +++ b/client/src/main/java/com/vaadin/client/ui/VMenuBar.java @@ -16,8 +16,10 @@ package com.vaadin.client.ui; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Queue; import com.google.gwt.core.client.GWT; @@ -46,6 +48,7 @@ import com.google.gwt.user.client.ui.HasHTML; import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; +import com.google.web.bindery.event.shared.HandlerRegistration; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.LayoutManager; @@ -53,12 +56,13 @@ import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; import com.vaadin.client.Util; import com.vaadin.client.WidgetUtil; +import com.vaadin.client.extensions.EventTrigger; import com.vaadin.shared.ui.ContentMode; import com.vaadin.shared.ui.menubar.MenuBarConstants; public class VMenuBar extends FocusableFlowPanel -implements CloseHandler<PopupPanel>, KeyPressHandler, KeyDownHandler, -FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { + implements CloseHandler<PopupPanel>, KeyPressHandler, KeyDownHandler, + FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler, EventTrigger { // The hierarchy of VMenuBar is a bit weird as VMenuBar is the Paintable, // used for the root menu but also used for the sub menus. @@ -117,6 +121,8 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { /** For internal use only. May be removed or replaced in the future. */ public boolean htmlContentAllowed; + private Map<String, List<Command>> triggers = new HashMap<>(); + public VMenuBar() { // Create an empty horizontal menubar this(false, null); @@ -414,9 +420,12 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { * @param item */ public void itemClick(CustomMenuItem item) { - if (item.getCommand() != null) { + boolean triggered = triggerEventIfNeeded(item); + if (item.getCommand() != null || triggered) { try { - item.getCommand().execute(); + if (item.getCommand() != null) { + item.getCommand().execute(); + } } finally { setSelected(null); if (visibleChildMenu != null) { @@ -808,6 +817,7 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { protected ContentMode descriptionContentMode = null; private String styleName; + private String id; /** * Default menu item {@link Widget} constructor for GWT.create(). @@ -1166,6 +1176,14 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { return null; } + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + } /** @@ -1642,6 +1660,7 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { openMenuAndFocusFirstIfPossible(getSelected()); } else { try { + triggerEventIfNeeded(getSelected()); final Command command = getSelected().getCommand(); if (command != null) { command.execute(); @@ -1654,10 +1673,7 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { // not leave menu to visible ("hover open") mode menuVisible = false; - VMenuBar root = this; - while (root.getParentMenu() != null) { - root = root.getParentMenu(); - } + VMenuBar root = getRoot(); root.ignoreFocus = true; root.getElement().focus(); root.ignoreFocus = false; @@ -1669,6 +1685,17 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { return false; } + private boolean triggerEventIfNeeded(CustomMenuItem item) { + List<Command> commands = getTriggers().get(item.getId()); + if (commands != null) { + for (Command command : commands) { + command.execute(); + } + return true; + } + return false; + } + private void selectFirstItem() { for (int i = 0; i < items.size(); i++) { CustomMenuItem item = items.get(i); @@ -1877,4 +1904,36 @@ FocusHandler, SubPartAware, MouseOutHandler, MouseOverHandler { public void onMouseOut(MouseOutEvent event) { LazyCloser.schedule(); } + + private VMenuBar getRoot() { + VMenuBar root = this; + + while (root.getParentMenu() != null) { + root = root.getParentMenu(); + } + + return root; + } + + @Override + public HandlerRegistration addTrigger(Command command, + String partInformation) { + if (partInformation == null || partInformation.isEmpty()) { + throw new IllegalArgumentException( + "The 'partInformation' parameter must contain the menu item id"); + } + + getTriggers().computeIfAbsent(partInformation, s-> new ArrayList<>()).add(command); + return () -> { + List<Command> commands = getTriggers().get(partInformation); + if (commands != null) { + commands.remove(command); + } + }; + } + + private Map<String, List<Command>> getTriggers() { + return getRoot().triggers; + } + } |