Skip to content

Commit 63309cb

Browse files
Pekka HyvönenIlia Motornyi
authored andcommitted
Fix Table and TreeTable with ItemClickListener
Fixes #29
1 parent 7c779b8 commit 63309cb

4 files changed

Lines changed: 175 additions & 8 deletions

File tree

vaadin-context-menu-addon/src/main/java/com/vaadin/addon/contextmenu/ContextMenu.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
import com.vaadin.addon.contextmenu.client.MenuSharedState;
1414
import com.vaadin.addon.contextmenu.client.MenuSharedState.MenuItemState;
1515
import com.vaadin.event.ContextClickEvent;
16+
import com.vaadin.event.ItemClickEvent;
17+
import com.vaadin.event.ItemClickEvent.ItemClickListener;
1618
import com.vaadin.event.ContextClickEvent.ContextClickListener;
1719
import com.vaadin.event.ContextClickEvent.ContextClickNotifier;
1820
import com.vaadin.server.AbstractExtension;
1921
import com.vaadin.server.Resource;
2022
import com.vaadin.server.ResourceReference;
23+
import com.vaadin.shared.MouseEventDetails;
24+
import com.vaadin.shared.MouseEventDetails.MouseButton;
2125
import com.vaadin.ui.AbstractComponent;
2226
import com.vaadin.ui.Component;
27+
import com.vaadin.ui.Table;
2328
import com.vaadin.util.ReflectTools;
2429

2530
@SuppressWarnings("serial")
@@ -63,11 +68,50 @@ public void itemClicked(int itemId, boolean menuClosed) {
6368
* many components as you wish.
6469
*
6570
* @param component
71+
* the component to set the context menu to
6672
*/
6773
public void setAsContextMenuOf(ContextClickNotifier component) {
74+
/*
75+
* Workaround for VScrollTable click handling, which prevents context
76+
* clicks from rows when ItemClickListener has been added. (#29)
77+
*/
78+
if (component instanceof Table) {
79+
useTableSpecificContextClickListener((Table) component);
80+
// For context clicks outside rows (header, footer, body) we still
81+
// need the context click listener.
82+
}
6883
component.addContextClickListener(contextClickListener);
6984
}
7085

86+
private void useTableSpecificContextClickListener(final Table table) {
87+
table.addItemClickListener(new ItemClickListener() {
88+
89+
@Override
90+
public void itemClick(ItemClickEvent event) {
91+
if (event.getButton() == MouseButton.RIGHT) {
92+
MouseEventDetails mouseEventDetails = new MouseEventDetails();
93+
mouseEventDetails.setAltKey(event.isAltKey());
94+
mouseEventDetails.setButton(event.getButton());
95+
mouseEventDetails.setClientX(event.getClientX());
96+
mouseEventDetails.setClientY(event.getClientY());
97+
mouseEventDetails.setCtrlKey(event.isCtrlKey());
98+
mouseEventDetails.setMetaKey(event.isMetaKey());
99+
mouseEventDetails.setRelativeX(event.getRelativeX());
100+
mouseEventDetails.setRelativeY(event.getRelativeY());
101+
mouseEventDetails.setShiftKey(event.isShiftKey());
102+
if (event.isDoubleClick()) {
103+
mouseEventDetails.setType(0x00002);
104+
} else {
105+
mouseEventDetails.setType(0x00001);
106+
}
107+
108+
contextClickListener.contextClick(
109+
new ContextClickEvent(table, mouseEventDetails));
110+
}
111+
}
112+
});
113+
}
114+
71115
public void addContextMenuOpenListener(
72116
ContextMenuOpenListener contextMenuComponentListener) {
73117
addListener(ContextMenuOpenEvent.class, contextMenuComponentListener,

vaadin-context-menu-demo/pom.xml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<vaadin.version>7.7.0</vaadin.version>
13+
<vaadin.version>7.7.7</vaadin.version>
1414
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
15+
<jetty.plugin.version>9.3.9.v20160517</jetty.plugin.version>
1516
</properties>
1617

1718
<organization>
@@ -184,16 +185,12 @@
184185
</execution>
185186
</executions>
186187
</plugin>
187-
188188
<plugin>
189-
<groupId>org.mortbay.jetty</groupId>
189+
<groupId>org.eclipse.jetty</groupId>
190190
<artifactId>jetty-maven-plugin</artifactId>
191-
<version>8.1.16.v20140903</version>
191+
<version>${jetty.plugin.version}</version>
192192
<configuration>
193-
<webApp>
194-
<contextPath>/context-menu</contextPath>
195-
</webApp>
196-
<scanIntervalSeconds>5</scanIntervalSeconds>
193+
<scanIntervalSeconds>3</scanIntervalSeconds>
197194
</configuration>
198195
</plugin>
199196

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

vaadin-context-menu-demo/src/main/java/com/vaadin/addon/contextmenu/ContextmenuUI.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public static class Servlet extends VaadinServlet {
2929

3030
@Override
3131
protected void init(VaadinRequest request) {
32+
if (request.getParameter("treetable") != null) {
33+
setContent(new ContextMenuTreeTable());
34+
} else {
35+
initUI();
36+
}
37+
}
38+
39+
private void initUI() {
3240
final VerticalLayout layout = new VerticalLayout();
3341
layout.setMargin(true);
3442
setContent(layout);

0 commit comments

Comments
 (0)