Vojtech Szocs has uploaded a new change for review.

Change subject: webadmin,userportal: Fix table context menu
......................................................................

webadmin,userportal: Fix table context menu

Before this patch, CellTable's CellPreviewHandler was used to capture
'mousedown/right-click' events to select the row where mouse was
pointing. After that, CellTable's ContextMenuHandler was used to
capture 'contextmenu' events to prevent the default browser-specific
context menu, and to show application-specific table context menu.

In some cases, CellPreviewHandler prevented ContextMenuHandler to be
invoked properly, which caused the default browser-specific context
menu to appear (instead of application-specific one).

This patch fixes the above mentioned problem by moving
CellPreviewHandler logic into ContextMenuHandler, and using
deferred command when showing application-specific context menu.

This patch also fixes a problem when clicking "outside" the table
(within its parent DIV element that fills up remaining space
vertically) doesn't cause the application-specific context menu
to appear.

Change-Id: Ic699cb5880be54cb0402cd3f1d176b635d188bf0
Signed-off-by: Vojtech Szocs <[email protected]>
---
M 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/action/AbstractActionPanel.java
M 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/AbstractActionTable.java
2 files changed, 68 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/20/7820/1

diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/action/AbstractActionPanel.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/action/AbstractActionPanel.java
index 5142d4f..b9f59a7 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/action/AbstractActionPanel.java
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/action/AbstractActionPanel.java
@@ -15,6 +15,8 @@
 import org.ovirt.engine.ui.common.widget.PopupPanel;
 import org.ovirt.engine.ui.common.widget.TitleMenuItemSeparator;
 
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.ContextMenuEvent;
@@ -199,19 +201,31 @@
         widget.addDomHandler(new ContextMenuHandler() {
             @Override
             public void onContextMenu(ContextMenuEvent event) {
-                event.preventDefault();
-                event.stopPropagation();
+                AbstractActionPanel.this.onContextMenu(event);
+            }
+        }, ContextMenuEvent.getType());
+    }
 
-                // Show context menu only when not empty
+    protected void onContextMenu(final ContextMenuEvent event) {
+        final int eventX = event.getNativeEvent().getClientX();
+        final int eventY = event.getNativeEvent().getClientY();
+
+        // Suppress default browser context menu
+        event.preventDefault();
+        event.stopPropagation();
+
+        // Use deferred command to ensure that the context menu
+        // is shown only after other event handlers do their job
+        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
+            @Override
+            public void execute() {
+                // Avoid showing empty context menu
                 if (hasActionButtons()) {
-                    int eventX = event.getNativeEvent().getClientX();
-                    int eventY = event.getNativeEvent().getClientY();
-
                     updateContextMenu(contextMenuBar, actionButtonList, 
contextPopupPanel);
                     contextPopupPanel.showAndFitToScreen(eventX, eventY);
                 }
             }
-        }, ContextMenuEvent.getType());
+        });
     }
 
     MenuBar updateContextMenu(MenuBar menuBar, List<ActionButtonDefinition<T>> 
actions, final PopupPanel popupPanel) {
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/AbstractActionTable.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/AbstractActionTable.java
index a18a29c..1728c0c 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/AbstractActionTable.java
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/AbstractActionTable.java
@@ -10,8 +10,11 @@
 
 import com.google.gwt.core.client.Scheduler;
 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
-import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.TableCellElement;
+import com.google.gwt.dom.client.TableRowElement;
 import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ContextMenuEvent;
 import com.google.gwt.event.dom.client.KeyCodes;
 import com.google.gwt.event.dom.client.KeyDownEvent;
 import com.google.gwt.event.dom.client.KeyDownHandler;
@@ -171,7 +174,8 @@
                 }
                 tooltip = new PopupPanel(true);
 
-                tooltip.setWidget(new 
Label(selectionModel.getSelectedList().size() + " " + 
constants.selectedActionTable())); //$NON-NLS-1$
+                tooltip.setWidget(new 
Label(selectionModel.getSelectedList().size()
+                        + " " + constants.selectedActionTable())); 
//$NON-NLS-1$
                 if (mousePosition[0] == 0 && mousePosition[1] == 0) {
                     mousePosition[0] = Window.getClientWidth() / 2;
                     mousePosition[1] = Window.getClientHeight() * 1 / 3;
@@ -218,9 +222,6 @@
         // Enable keyboard selection
         table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
 
-        // Add context menu handler for table widget
-        addContextMenuHandler(table);
-
         // Add arrow key handler
         table.addDomHandler(new KeyDownHandler() {
             @Override
@@ -257,24 +258,8 @@
             }
         }, KeyDownEvent.getType());
 
-        // Add right click handler
-        table.addCellPreviewHandler(new CellPreviewEvent.Handler<T>() {
-            @Override
-            public void onCellPreview(CellPreviewEvent<T> event) {
-                if (event.getNativeEvent().getButton() != 
NativeEvent.BUTTON_RIGHT
-                        || 
!"mousedown".equals(event.getNativeEvent().getType())) { //$NON-NLS-1$
-                    return;
-                }
-
-                final T value = event.getValue();
-
-                if (!selectionModel.isSelected(value)) {
-                    selectionModel.setMultiSelectEnabled(false);
-                    selectionModel.setMultiRangeSelectEnabled(false);
-                    selectionModel.setSelected(value, true);
-                }
-            }
-        });
+        // Add context menu handler for table widget
+        addContextMenuHandler(tableContainer);
 
         // Use fixed table layout
         table.setWidth("100%", true); //$NON-NLS-1$
@@ -284,6 +269,44 @@
         tableContainer.setWidget(table);
         tableHeaderContainer.setWidget(tableHeader);
         tableHeaderContainer.setVisible(!showDefaultHeader);
+    }
+
+    @Override
+    protected void onContextMenu(ContextMenuEvent event) {
+        super.onContextMenu(event);
+
+        Element target = event.getNativeEvent().getEventTarget().cast();
+        T value = getValueFromElement(target);
+
+        if (value != null && !selectionModel.isSelected(value)) {
+            selectionModel.setMultiSelectEnabled(false);
+            selectionModel.setMultiRangeSelectEnabled(false);
+            selectionModel.setSelected(value, true);
+        }
+    }
+
+    private T getValueFromElement(Element target) {
+        TableCellElement tableCell = findNearestParentCell(target);
+
+        if (tableCell != null) {
+            Element trElem = tableCell.getParentElement();
+            TableRowElement tr = TableRowElement.as(trElem);
+            int row = tr.getSectionRowIndex();
+            return table.getVisibleItem(row);
+        } else {
+            return null;
+        }
+    }
+
+    private TableCellElement findNearestParentCell(Element elem) {
+        while ((elem != null) && (elem != table.getElement())) {
+            String tagName = elem.getTagName();
+            if ("td".equalsIgnoreCase(tagName) || 
"th".equalsIgnoreCase(tagName)) { //$NON-NLS-1$ //$NON-NLS-2$
+                return elem.cast();
+            }
+            elem = elem.getParentElement();
+        }
+        return null;
     }
 
     @UiHandler("prevPageButton")
@@ -378,8 +401,7 @@
 
             if (width == null) {
                 addColumn(column, headerText);
-            }
-            else {
+            } else {
                 addColumn(column, headerText, width);
             }
         } else if (!present && table.getColumnIndex(column) != -1) {


--
To view, visit http://gerrit.ovirt.org/7820
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic699cb5880be54cb0402cd3f1d176b635d188bf0
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Vojtech Szocs <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to