]> source.dussan.org Git - vaadin-framework.git/commitdiff
Adds context menu to empty space in Table. #5992
authorJohn Alhroos <john.ahlroos@itmill.com>
Wed, 27 Apr 2011 07:34:43 +0000 (07:34 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Wed, 27 Apr 2011 07:34:43 +0000 (07:34 +0000)
svn changeset:18482/svn branch:6.6

src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java
src/com/vaadin/ui/Table.java

index e544c7abf6b8214e2b0e57af66801c3a8d851139..94896c645f0b0ee19f94aa7c507eeb3507989800 100644 (file)
@@ -30,6 +30,8 @@ import com.google.gwt.dom.client.TableSectionElement;
 import com.google.gwt.dom.client.Touch;
 import com.google.gwt.event.dom.client.BlurEvent;
 import com.google.gwt.event.dom.client.BlurHandler;
+import com.google.gwt.event.dom.client.ContextMenuEvent;
+import com.google.gwt.event.dom.client.ContextMenuHandler;
 import com.google.gwt.event.dom.client.FocusEvent;
 import com.google.gwt.event.dom.client.FocusHandler;
 import com.google.gwt.event.dom.client.KeyCodes;
@@ -98,7 +100,7 @@ import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
  * TODO implement unregistering for child components in Cells
  */
 public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
-        VHasDropHandler, FocusHandler, BlurHandler, Focusable {
+        VHasDropHandler, FocusHandler, BlurHandler, Focusable, ActionOwner {
 
     public static final String CLASSNAME = "v-table";
     public static final String CLASSNAME_SELECTION_FOCUS = CLASSNAME + "-focus";
@@ -193,7 +195,9 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
      */
     private int scrollingVelocity = 10;
 
-    private Timer scrollingVelocityTimer = null;;
+    private Timer scrollingVelocityTimer = null;
+
+    private String[] bodyActionKeys;
 
     /**
      * Represents a select range of rows
@@ -439,6 +443,13 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
             }
         }, TouchStartEvent.getType());
 
+        scrollBodyPanel.sinkEvents(Event.ONCONTEXTMENU);
+        scrollBodyPanel.addDomHandler(new ContextMenuHandler() {
+            public void onContextMenu(ContextMenuEvent event) {
+                handleBodyContextMenu(event);
+            }
+        }, ContextMenuEvent.getType());
+
         setStyleName(CLASSNAME);
 
         add(tHead);
@@ -457,6 +468,18 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
 
     }
 
+    private void handleBodyContextMenu(ContextMenuEvent event) {
+        if (enabled && bodyActionKeys != null) {
+            int left = Util.getTouchOrMouseClientX(event.getNativeEvent());
+            int top = Util.getTouchOrMouseClientY(event.getNativeEvent());
+            top += Window.getScrollTop();
+            left += Window.getScrollLeft();
+            client.getContextMenu().showAt(this, left, top);
+        }
+        event.stopPropagation();
+        event.preventDefault();
+    }
+
     /**
      * Fires a column resize event which sends the resize information to the
      * server.
@@ -787,6 +810,10 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
         multiselectmode = uidl.hasAttribute("multiselectmode") ? uidl
                 .getIntAttribute("multiselectmode") : MULTISELECT_MODE_DEFAULT;
 
+        if (uidl.hasAttribute("alb")) {
+            bodyActionKeys = uidl.getStringArrayAttribute("alb");
+        }
+
         setCacheRate(uidl.hasAttribute("cr") ? uidl.getDoubleAttribute("cr")
                 : CACHE_RATE_DEFAULT);
 
@@ -5886,4 +5913,27 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler,
             }
         });
     }
+
+    public Action[] getActions() {
+        if (bodyActionKeys == null) {
+            return new Action[] {};
+        }
+        final Action[] actions = new Action[bodyActionKeys.length];
+        for (int i = 0; i < actions.length; i++) {
+            final String actionKey = bodyActionKeys[i];
+            Action bodyAction = new TreeAction(this, null, actionKey);
+            bodyAction.setCaption(getActionCaption(actionKey));
+            bodyAction.setIconUrl(getActionIcon(actionKey));
+            actions[i] = bodyAction;
+        }
+        return actions;
+    }
+
+    public ApplicationConnection getClient() {
+        return client;
+    }
+
+    public String getPaintableId() {
+        return paintableId;
+    }
 }
index 94c3bab896acd89e9941f26d219231890b816e66..171096b5b2a4d40bb847a97273bc3815f5e5e783 100644 (file)
@@ -2140,10 +2140,18 @@ public class Table extends AbstractSelect implements Action.Container,
                 final Action action = (Action) actionMapper.get(st.nextToken());
                 if (action != null && containsId(itemId)
                         && actionHandlers != null) {
+                    // Item action
                     for (final Iterator<Handler> i = actionHandlers.iterator(); i
                             .hasNext();) {
                         (i.next()).handleAction(action, this, itemId);
                     }
+                } else if (action != null && actionHandlers != null
+                        && itemId == null) {
+                    // Body action
+                    for (final Iterator<Handler> i = actionHandlers.iterator(); i
+                            .hasNext();) {
+                        (i.next()).handleAction(action, this, null);
+                    }
                 }
             }
         }
@@ -2377,6 +2385,30 @@ public class Table extends AbstractSelect implements Action.Container,
 
         target.addAttribute("colfooters", columnFootersVisible);
 
+        /*
+         * Body actions - Actions which has the target null and can be invoked
+         * by right clicking on the table body.
+         */
+        final Set<Action> actionSet = new LinkedHashSet<Action>();
+        if (actionHandlers != null) {
+            final ArrayList<String> keys = new ArrayList<String>();
+            for (final Iterator<Handler> ahi = actionHandlers.iterator(); ahi
+                    .hasNext();) {
+
+                // Getting actions for the null item, which in this case means
+                // the body item
+                final Action[] aa = (ahi.next()).getActions(null, this);
+                if (aa != null) {
+                    for (int ai = 0; ai < aa.length; ai++) {
+                        final String key = actionMapper.key(aa[ai]);
+                        actionSet.add(aa[ai]);
+                        keys.add(key);
+                    }
+                }
+            }
+            target.addAttribute("alb", keys.toArray());
+        }
+
         // Visible column order
         final Collection<?> sortables = getSortableContainerPropertyIds();
         final ArrayList<String> visibleColOrder = new ArrayList<String>();
@@ -2390,7 +2422,6 @@ public class Table extends AbstractSelect implements Action.Container,
         target.addAttribute("vcolorder", visibleColOrder.toArray());
 
         // Rows
-        final Set<Action> actionSet = new LinkedHashSet<Action>();
         final boolean selectable = isSelectable();
         final boolean[] iscomponent = new boolean[visibleColumns.size()];
         int iscomponentIndex = 0;