From 2c4983f78c985b8ff6df7a2031681c0a45680373 Mon Sep 17 00:00:00 2001 From: John Alhroos Date: Wed, 27 Apr 2011 07:34:43 +0000 Subject: [PATCH] Adds context menu to empty space in Table. #5992 svn changeset:18482/svn branch:6.6 --- .../terminal/gwt/client/ui/VScrollTable.java | 54 ++++++++++++++++++- src/com/vaadin/ui/Table.java | 33 +++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java index e544c7abf6..94896c645f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java @@ -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; + } } diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 94c3bab896..171096b5b2 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -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 i = actionHandlers.iterator(); i .hasNext();) { (i.next()).handleAction(action, this, itemId); } + } else if (action != null && actionHandlers != null + && itemId == null) { + // Body action + for (final Iterator 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 actionSet = new LinkedHashSet(); + if (actionHandlers != null) { + final ArrayList keys = new ArrayList(); + for (final Iterator 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 visibleColOrder = new ArrayList(); @@ -2390,7 +2422,6 @@ public class Table extends AbstractSelect implements Action.Container, target.addAttribute("vcolorder", visibleColOrder.toArray()); // Rows - final Set actionSet = new LinkedHashSet(); final boolean selectable = isSelectable(); final boolean[] iscomponent = new boolean[visibleColumns.size()]; int iscomponentIndex = 0; -- 2.39.5