View Javadoc
1   package com.vaadin.v7.contextmenu;
2   
3   import com.vaadin.contextmenu.ContextMenu;
4   import com.vaadin.event.ContextClickEvent;
5   import com.vaadin.event.ContextClickEvent.ContextClickNotifier;
6   import com.vaadin.shared.MouseEventDetails;
7   import com.vaadin.shared.MouseEventDetails.MouseButton;
8   import com.vaadin.v7.event.ItemClickEvent;
9   import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
10  import com.vaadin.v7.ui.Table;
11  import com.vaadin.v7.ui.TreeTable;
12  
13  /**
14   * Compatibility version of ContextMenu to use with v7.Table and v7.TreeTable in
15   * the Framework 8.0 compatibility package.
16   * <p>
17   * This compatibility version exists to fix #29 using context menu in table &
18   * tree table with item click listener.
19   */
20  public class TableContextMenu extends ContextMenu {
21  
22      /**
23       * Constructs a context menu and sets it for the given table.
24       *
25       * @param table
26       *            the table to set the context menu to
27       */
28      public TableContextMenu(Table table) {
29          super(table, true);
30      }
31  
32      /**
33       * Constructs a context menu and sets it for the given tree table.
34       *
35       * @param treeTable
36       *            the tree table to set the context menu to
37       */
38      public TableContextMenu(TreeTable treeTable) {
39          super(treeTable, true);
40      }
41  
42      /**
43       * Sets this as a context menu of the component. You can set one menu to as
44       * many components as you wish.
45       *
46       * @param component
47       *            the component to set the context menu to
48       */
49      @Override
50      public void setAsContextMenuOf(ContextClickNotifier component) {
51          /*
52           * Workaround for VScrollTable click handling, which prevents context
53           * clicks from rows when ItemClickListener has been added. (#29)
54           */
55          if (component instanceof Table) {
56              useTableSpecificContextClickListener((Table) component);
57              // For context clicks outside rows (header, footer, body) we still
58              // need the context click listener.
59          }
60          super.setAsContextMenuOf(component);
61      }
62  
63      private void useTableSpecificContextClickListener(final Table table) {
64          table.addItemClickListener(new ItemClickListener() {
65  
66              @Override
67              public void itemClick(ItemClickEvent event) {
68                  if (event.getButton() == MouseButton.RIGHT) {
69                      MouseEventDetails mouseEventDetails = new MouseEventDetails();
70                      mouseEventDetails.setAltKey(event.isAltKey());
71                      mouseEventDetails.setButton(event.getButton());
72                      mouseEventDetails.setClientX(event.getClientX());
73                      mouseEventDetails.setClientY(event.getClientY());
74                      mouseEventDetails.setCtrlKey(event.isCtrlKey());
75                      mouseEventDetails.setMetaKey(event.isMetaKey());
76                      mouseEventDetails.setRelativeX(event.getRelativeX());
77                      mouseEventDetails.setRelativeY(event.getRelativeY());
78                      mouseEventDetails.setShiftKey(event.isShiftKey());
79                      if (event.isDoubleClick()) {
80                          mouseEventDetails.setType(0x00002);
81                      } else {
82                          mouseEventDetails.setType(0x00001);
83                      }
84  
85                      getContextClickListener().contextClick(
86                              new ContextClickEvent(table, mouseEventDetails));
87                  }
88              }
89          });
90      }
91  
92  }