|
| 1 | +package com.vaadin.addon.contextmenu; |
| 2 | + |
| 3 | +import java.util.EventObject; |
| 4 | + |
| 5 | +import javax.servlet.annotation.WebServlet; |
| 6 | + |
| 7 | +import com.vaadin.addon.contextmenu.ContextMenu; |
| 8 | +import com.vaadin.addon.contextmenu.ContextMenu.ContextMenuOpenListener; |
| 9 | +import com.vaadin.addon.contextmenu.ContextMenu.ContextMenuOpenListener.ContextMenuOpenEvent; |
| 10 | +import com.vaadin.annotations.Theme; |
| 11 | +import com.vaadin.annotations.VaadinServletConfiguration; |
| 12 | +import com.vaadin.data.Item; |
| 13 | +import com.vaadin.server.VaadinRequest; |
| 14 | +import com.vaadin.server.VaadinServlet; |
| 15 | +import com.vaadin.shared.MouseEventDetails.MouseButton; |
| 16 | +import com.vaadin.ui.Button; |
| 17 | +import com.vaadin.ui.Label; |
| 18 | +import com.vaadin.ui.Panel; |
| 19 | +import com.vaadin.ui.TextField; |
| 20 | +import com.vaadin.ui.TreeTable; |
| 21 | +import com.vaadin.ui.UI; |
| 22 | +import com.vaadin.ui.VerticalLayout; |
| 23 | +import com.vaadin.ui.Button.ClickEvent; |
| 24 | +import com.vaadin.ui.HorizontalLayout; |
| 25 | + |
| 26 | +/** |
| 27 | + * For testing workaround for #29. Open with ../context-menu/?treetable |
| 28 | + */ |
| 29 | +public class ContextMenuTreeTable extends VerticalLayout { |
| 30 | + |
| 31 | + private ContextMenu menu; |
| 32 | + private TreeTable table; |
| 33 | + private VerticalLayout logLayout; |
| 34 | + private int counter; |
| 35 | + |
| 36 | + public ContextMenuTreeTable() { |
| 37 | + createTreeTable(); |
| 38 | + logLayout = new VerticalLayout(); |
| 39 | + Panel logPanel = new Panel(); |
| 40 | + logPanel.setContent(logLayout); |
| 41 | + logPanel.setHeight("100px"); |
| 42 | + |
| 43 | + HorizontalLayout buttons = new HorizontalLayout(); |
| 44 | + buttons.addComponent(new Button("Add ItemClickListener to TreeTable", |
| 45 | + new Button.ClickListener() { |
| 46 | + |
| 47 | + @Override |
| 48 | + public void buttonClick(ClickEvent event) { |
| 49 | + addItemClickListener(table); |
| 50 | + } |
| 51 | + })); |
| 52 | + buttons.addComponent(new Button("Add ContextMenu to TreeTable", |
| 53 | + new Button.ClickListener() { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void buttonClick(ClickEvent event) { |
| 57 | + addContextMenu(table); |
| 58 | + } |
| 59 | + })); |
| 60 | + buttons.addComponent(new Button("Add ContextMenuOpenListener to Menu", |
| 61 | + new Button.ClickListener() { |
| 62 | + |
| 63 | + @Override |
| 64 | + public void buttonClick(ClickEvent event) { |
| 65 | + menu.addContextMenuOpenListener( |
| 66 | + new ContextMenuOpenListener() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onContextMenuOpen( |
| 70 | + ContextMenuOpenEvent event) { |
| 71 | + log(event, ""); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + })); |
| 76 | + |
| 77 | + setMargin(true); |
| 78 | + setSpacing(true); |
| 79 | + addComponents(logPanel, buttons, table); |
| 80 | + } |
| 81 | + |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + private TreeTable createTreeTable() { |
| 84 | + table = new TreeTable(); |
| 85 | + table.addContainerProperty("id", String.class, null); |
| 86 | + for (int i = 0; i < 10; ++i) { |
| 87 | + Item item = table.addItem(i); |
| 88 | + item.getItemProperty("id").setValue(i + " foobar"); |
| 89 | + } |
| 90 | + return table; |
| 91 | + } |
| 92 | + |
| 93 | + private void addContextMenu(TreeTable table) { |
| 94 | + menu = new ContextMenu(table, true); |
| 95 | + menu.addItem("foobar", null); |
| 96 | + menu.addSeparator(); |
| 97 | + menu.addItem("shazbot", null); |
| 98 | + } |
| 99 | + |
| 100 | + private void addItemClickListener(TreeTable table) { |
| 101 | + table.addItemClickListener(event -> { |
| 102 | + if (event.getButton() == MouseButton.RIGHT) { |
| 103 | + log(event, "context-click"); |
| 104 | + } else if (event.isDoubleClick()) { |
| 105 | + log(event, "double-click"); |
| 106 | + } else { |
| 107 | + log(event, "click!"); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + private void log(EventObject event, String message) { |
| 113 | + logLayout.addComponentAsFirst( |
| 114 | + new Label(counter++ + event.getClass().getSimpleName() + " " |
| 115 | + + event.getSource().getClass().getSimpleName() + " " |
| 116 | + + message)); |
| 117 | + } |
| 118 | +} |
0 commit comments