public MenuButton(SafeHtml content, ItemsProvider itemsProvider) { super(); this.itemsProvider = itemsProvider; addStyleName(RESOURCES.css().menuButton()); showMenuTimer = new Timer() { @Override public void run() { showMenu(); } }; final FocusWidget mainButton = new MainButton(content); final FocusWidget dropButton = new DropButton(); add(mainButton); add(dropButton); attachMouseEventHandlers(mainButton); attachMouseEventHandlers(dropButton); }
private void showMenu() { final List<MenuItem> items = itemsProvider.getItems(); if (!items.isEmpty()) { menu = new ItemsList(items, itemsProvider, RESOURCES, null); getActionHandler().ifPresent(handler -> menu.setActionHandler(handler)); menu.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop() + getOffsetHeight()); menu.show(); menu.getElement().setId("commandsPopup"); } }
private void createChildPopup(Pair<List<MenuItem>, String> children) { childList = new ItemsList(children.first, dataProvider, resources, "Execute on:"); getActionHandler().ifPresent(actionHandler -> childList.setActionHandler(actionHandler)); childList.setPopupPosition(getAbsoluteLeft() + getOffsetWidth(), getAbsoluteTop()); childList.show(); childList.setAutoHideEnabled(false); childList.setAnimationEnabled(false); childList.addCloseHandler( event -> { if (event.isAutoClosed()) { hide(); } }); } }
private void attachEventHandlers() { addDomHandler( event -> { if (overItem != null) { overItem.removeStyleName(resources.css().popupItemOver()); } overItem = ItemWidget.this; addStyleName(resources.css().popupItemOver()); if (childList != null) { childList.hide(); } if (dataProvider.isGroup(item)) { Pair<List<MenuItem>, String> children = dataProvider.getChildren(item); createChildPopup(children); } }, MouseOverEvent.getType()); addDomHandler( event -> { if (dataProvider.isGroup(item)) { return; } hide(true); getActionHandler().ifPresent(actionHandler -> actionHandler.onAction(item)); }, ClickEvent.getType()); }
MainButton(SafeHtml content) { super(Document.get().createDivElement()); getElement().setInnerSafeHtml(content); addStyleName(RESOURCES.css().button()); addStyleName(RESOURCES.css().mainButton()); addClickHandler( event -> { if (menu != null && menu.isShowing()) { return; } final Optional<MenuItem> defaultItem = itemsProvider.getDefaultItem(); if (defaultItem.isPresent()) { getActionHandler() .ifPresent(actionHandler -> actionHandler.onAction(defaultItem.get())); } else { showMenu(); } }); } }
ItemsList( List<MenuItem> children, ItemsProvider dataProvider, MenuButton.Resources resources, @Nullable String title) { super(true, false); this.dataProvider = dataProvider; this.resources = resources; setAnimationEnabled(true); setAnimationType(AnimationType.ROLL_DOWN); addStyleName(resources.css().popupPanel()); final FlowPanel content = new FlowPanel(); add(content); if (title != null) { Label label = new Label(title); label.setStyleName(resources.css().label()); content.add(label); } children.forEach(child -> content.add(new ItemWidget(child))); addCloseHandler( event -> { if (childList != null && childList.isShowing()) { childList.hide(false); } }); }
ItemWidget(MenuItem item) { this.item = item; addStyleName(resources.css().popupItem()); final Element itemLabel = Document.get().createDivElement(); itemLabel.setInnerText(item.getName()); itemLabel.getStyle().setFloat(Style.Float.LEFT); getElement().appendChild(itemLabel); if (dataProvider.isGroup(item)) { DivElement arrow = Document.get().createDivElement(); arrow.setInnerHTML(FontAwesome.PLAY); arrow.addClassName(resources.css().arrow()); getElement().appendChild(arrow); } attachEventHandlers(); }
DropButton() { super(Document.get().createDivElement()); addStyleName(RESOURCES.css().button()); addStyleName(RESOURCES.css().dropButton()); final FlowPanel marker = new FlowPanel(); marker.getElement().appendChild(RESOURCES.arrowIcon().getSvg().getElement()); marker.addStyleName(RESOURCES.css().expandedImage()); getElement().appendChild(marker.getElement()); addClickHandler( event -> { if (menu == null || !menu.isShowing()) { showMenu(); } }); } }
@Override public void actionPerformed(ActionEvent e) { itemsProvider .getDefaultItem() .ifPresent( defaultItem -> getActionHandler().ifPresent(handler -> handler.onAction(defaultItem))); } }
/** Updates button's tooltip depending on it's state (what child elements it contains). */ public void updateTooltip() { final Optional<MenuItem> defaultItem = itemsProvider.getDefaultItem(); if (defaultItem.isPresent()) { MenuItem menuItem = defaultItem.get(); String message = ""; if (menuItem instanceof CommandItem) { message = messages.goalButtonTooltipExecute(menuItem.getName()); } else if (menuItem instanceof MachineItem) { MachineItem machineMenuItem = (MachineItem) menuItem; message = messages.goalButtonTooltipExecuteOnMachine( machineMenuItem.getCommand().getName(), machineMenuItem.getName()); } setTooltip(message, keyBinding); } else if (getItemsProvider().containsGuideItemOnly()) { setTooltip(messages.goalButtonTooltipNoCommand(goal.getId()), null); } else { setTooltip(messages.goalButtonTooltipChooseCommand(goal.getId()), null); } }
/** Adds the given {@code command} to the provider. */ public void addCommand(CommandImpl command) { commands.add(command); if (dataChangedHandler != null) { dataChangedHandler.onDataChanged(); } }
/** Removes the given {@code command} from the provider. */ public void removeCommand(CommandImpl command) { commands.remove(command); // reset the defaultItem if (defaultItem instanceof CommandItem) { final CommandImpl cmd = ((CommandItem) defaultItem).getCommand(); if (cmd.equals(command)) { defaultItem = null; } } if (dataChangedHandler != null) { dataChangedHandler.onDataChanged(); } }