View Javadoc
1   /*
2    * Copyright 2000-2018 Vaadin Ltd.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  package com.vaadin.v7.contextmenu;
18  
19  import com.vaadin.contextmenu.ContextMenu;
20  import com.vaadin.event.ContextClickEvent;
21  import com.vaadin.event.ContextClickEvent.ContextClickNotifier;
22  import com.vaadin.shared.MouseEventDetails;
23  import com.vaadin.shared.MouseEventDetails.MouseButton;
24  import com.vaadin.v7.event.ItemClickEvent;
25  import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
26  import com.vaadin.v7.ui.Table;
27  import com.vaadin.v7.ui.TreeTable;
28  
29  /**
30   * Compatibility version of ContextMenu to use with v7.Table and v7.TreeTable in
31   * the Framework 8.0 compatibility package.
32   * <p>
33   * This compatibility version exists to fix #29 using context menu in table &
34   * tree table with item click listener.
35   *
36   * @deprecated To use only for compatibility v7.Table
37   */
38  @Deprecated
39  public class TableContextMenu extends ContextMenu {
40  
41      /**
42       * Constructs a context menu and sets it for the given table.
43       *
44       * @param table
45       *            the table to set the context menu to
46       */
47      public TableContextMenu(Table table) {
48          super(table, true);
49      }
50  
51      /**
52       * Constructs a context menu and sets it for the given tree table.
53       *
54       * @param treeTable
55       *            the tree table to set the context menu to
56       */
57      public TableContextMenu(TreeTable treeTable) {
58          super(treeTable, true);
59      }
60  
61      /**
62       * Sets this as a context menu of the component. You can set one menu to as
63       * many components as you wish.
64       *
65       * @param component
66       *            the component to set the context menu to
67       */
68      @Override
69      public void setAsContextMenuOf(ContextClickNotifier component) {
70          /*
71           * Workaround for VScrollTable click handling, which prevents context
72           * clicks from rows when ItemClickListener has been added. (#29)
73           */
74          if (component instanceof Table) {
75              useTableSpecificContextClickListener((Table) component);
76              // For context clicks outside rows (header, footer, body) we still
77              // need the context click listener.
78          }
79          super.setAsContextMenuOf(component);
80      }
81  
82      private void useTableSpecificContextClickListener(final Table table) {
83          table.addItemClickListener(new ItemClickListener() {
84  
85              @Override
86              public void itemClick(ItemClickEvent event) {
87                  if (event.getButton() == MouseButton.RIGHT) {
88                      MouseEventDetails mouseEventDetails = new MouseEventDetails();
89                      mouseEventDetails.setAltKey(event.isAltKey());
90                      mouseEventDetails.setButton(event.getButton());
91                      mouseEventDetails.setClientX(event.getClientX());
92                      mouseEventDetails.setClientY(event.getClientY());
93                      mouseEventDetails.setCtrlKey(event.isCtrlKey());
94                      mouseEventDetails.setMetaKey(event.isMetaKey());
95                      mouseEventDetails.setRelativeX(event.getRelativeX());
96                      mouseEventDetails.setRelativeY(event.getRelativeY());
97                      mouseEventDetails.setShiftKey(event.isShiftKey());
98                      if (event.isDoubleClick()) {
99                          mouseEventDetails.setType(0x00002);
100                     } else {
101                         mouseEventDetails.setType(0x00001);
102                     }
103 
104                     getContextClickListener().contextClick(
105                             new ContextClickEvent(table, mouseEventDetails));
106                 }
107             }
108         });
109     }
110 
111 }