Class ContextMenu
- All Implemented Interfaces:
ContextMenuHandler,MouseOutHandler,MouseOverHandler,TouchCancelHandler,TouchEndHandler,TouchMoveHandler,TouchStartHandler,HasAttachHandlers,EventHandler,HasHandlers,EventListener,HasVisibility,HasWidgets,HasWidgets.ForIsWidget,IndexedPanel,IndexedPanel.ForIsWidget,InsertPanel,InsertPanel.ForIsWidget,IsWidget,Iterable<Widget>,FullscreenChangeHandler,HasFullscreenChangeHandlers
Main class for implementing context menus in GWT applications. Context menus
work through a shared instance (like Tooltip's). Each widget that
sports context menus (the listener widget) must be associated
with a context menu provider.
The context menu opens when the ContextMenuHandler fires (typically
by a right click on mouse as a pointing device and a two-finger tap/click on
a touch pad). For devices without a pointer the context menu is triggered by
a two-finger touch (after some configurable delay). The context menu is
automatically closed for one of the following events:
- an entry has been selected
- scrolling of the window
- on devices with pointers: once the pointer exited the menu (after some delay)
- on devices without pointers: by tapping anywhere else on the screen
CSS Style Rules
- .gwt-ContextMenu
- the context menu element.
- Author:
- Christoph Hauert
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceListener interface for widgets that offer context menus.static interfaceProvider interface for classes that are responsible for supplying the contents of the context menu.protected classManage registrations of listener widgets, their context menu providers and the corresponding event handlers.classTimer for handling touch events triggering the context menu.Nested classes/interfaces inherited from class UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabledNested classes/interfaces inherited from interface HasWidgets
HasWidgets.ForIsWidgetNested classes/interfaces inherited from interface IndexedPanel
IndexedPanel.ForIsWidgetNested classes/interfaces inherited from interface InsertPanel
InsertPanel.ForIsWidget -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleantrueto close sub-menus automatically when pointer exited the sub-menu.private booleantrueto automatically set timer for hiding context menu after pointer exited context menu.private booleantrueto open sub-menus automatically on hovering over the corresponding parent menu item.protected ContextMenuReference to current child menu ornullif no child menu is visible.protected static ContextMenuShared instance of context menu.private static final intDefault horizontal offset of context menu relative to the coordinates of the context menu triggering event.private static final intDefault vertical offset of context menu relative to the coordinates of the context menu triggering event.private static final intDefault delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.private intDelay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.(package private) HandlerRegistrationReference to fullscreen change handler.private TimerTimer for closing the context menu after a fixed delay.private booleantrueif pointer is currently over context (sub)menu.static final intDelay in milliseconds before the touch event is considered a 'long touch' instead of a 'tap'.private intDelay before two finger touch triggers context menu, provided that no other touch event fired duringdelayTapmilliseconds.private intHorizontal offset of context menu relative to coordinates of context menu triggering event.private intVertical offset of context menu relative to coordinates of context menu triggering event.protected ContextMenuReference to the parent menu.protected HashMap<ContextMenu.Listener, ContextMenu.Registration> Lookup table of listener widgets sporting context menus and their corresponding providers of the context menu contents.protected StyleReference to style of context menu.protected ContextMenu.TouchTimerTimer for handling touch events that are scheduled to trigger context menu.Fields inherited from class UIObject
DEBUG_ID_PREFIX -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate new context menu.ContextMenu(ContextMenu parent) Create new context submenu forparentmenu. -
Method Summary
Modifier and TypeMethodDescriptionadd(String name, ContextMenu submenu) Add new submenu to context menu.voidadd(ContextMenu.Listener listener, ContextMenu.Provider provider) Register a newlistenerwidget for context menu requests and associate with theproviderof the context menu.Adds aFullscreenChangeEventhandler.voidAdd new separator to context menu.protected voidCancel the timer for hiding the context menu.voidclose()Close submenu or context menu if this is the top level.voidcloseAll()Recursively close hierarchy of context submenus, including the top level context menu.voidClose submenu, if one is showing.Get the parent menu.booleanChecks if submenus close automatically when pointer stops hovering over corresponding menu item in parent menu.booleanChecks if context menu hides automatically (with some delay) after pointer exited.booleanChecks if submenus open automatically when pointer hovers over corresponding menu item in parent menu.static booleanCheck if context menu is visible.booleanCheck if this is a submenu.voidonContextMenu(ContextMenuEvent event) voidCalled when aFullscreenChangeEventis fired.voidonMouseOut(MouseOutEvent event) voidonMouseOver(MouseOverEvent event) voidonTouchCancel(TouchCancelEvent event) voidonTouchEnd(TouchEndEvent event) voidonTouchMove(TouchMoveEvent event) voidonTouchStart(TouchStartEvent event) protected voidopen()Open submenu.voidopenChildMenu(ContextMenu child) Open submenuchild.voidsetAutoClose(boolean auto) Sets whether submenus should close automatically when pointer stops hovering over corresponding menu item in parent menu.voidsetAutoHide(boolean auto) Sets whether context menu hides automatically (with delay) after pointer exits context menu.voidsetAutoOpen(boolean auto) Sets whether submenus should open automatically when pointer hovers over corresponding menu item in parent menu.voidsetDelayHide(int delay) Set delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context menu, provided thatautoHideis set totrue.protected voidSet timer for delayed hiding of context menu ifautoCloseistrue.voidsetLongTouch(int delay) Set delay (in milliseconds) before showing the context menu after a two finger touch, provided that no other touch events fired duringdelay.voidsetOffset(int ox, int oy) Sets the horizontal and vertical offset between the location where the context menu was triggered and the position of the top left corner of the context menu.static ContextMenuThe context menu is added to theRootPanelof the GWT application and shared among all elements that sport a context menu.protected voidshowAt(int x, int y) Show context menu (or submenu) at position(x, y)relative to browser window.voidshowAt(ContextMenu.Listener listener, int x, int y) Prepares context menu (or submenu) for showing at position(x, y)relative to browser window.Methods inherited from class ComplexPanel
add, add, adjustIndex, checkIndexBoundsForAccess, checkIndexBoundsForInsertion, getChildren, getWidget, getWidgetCount, getWidgetIndex, getWidgetIndex, insert, insert, iterator, remove, removeMethods inherited from class Panel
add, adopt, doAttachChildren, doDetachChildren, orphan, removeMethods inherited from class Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, fireEvent, getHandlerCount, getLayoutData, getParent, isAttached, isOrWasAttached, onAttach, onBrowserEvent, onDetach, onLoad, onUnload, removeFromParent, setLayoutData, sinkEvents, unsinkEventsMethods inherited from class UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, onEnsureDebugId, removeStyleDependentName, removeStyleName, resolvePotentialElement, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface HasHandlers
fireEventMethods inherited from interface IndexedPanel
getWidget, getWidgetCount, getWidgetIndex, removeMethods inherited from interface IndexedPanel.ForIsWidget
getWidgetIndexMethods inherited from interface InsertPanel.ForIsWidget
addMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
DEFAULT_OFFSET_X
private static final int DEFAULT_OFFSET_XDefault horizontal offset of context menu relative to the coordinates of the context menu triggering event.- See Also:
-
DEFAULT_OFFSET_Y
private static final int DEFAULT_OFFSET_YDefault vertical offset of context menu relative to the coordinates of the context menu triggering event.- See Also:
-
offsetX
private int offsetXHorizontal offset of context menu relative to coordinates of context menu triggering event. In general, this should be a negative number such that any pointing device actually ends up on top of the context menu.- See Also:
-
offsetY
private int offsetYVertical offset of context menu relative to coordinates of context menu triggering event. In general, this should be a negative number such that any pointing device actually ends up on top of the context menu.- See Also:
-
DELAY_HIDE
private static final int DELAY_HIDEDefault delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.- See Also:
-
delayHide
private int delayHideDelay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.- See Also:
-
LONG_TOUCH_TIME
public static final int LONG_TOUCH_TIMEDelay in milliseconds before the touch event is considered a 'long touch' instead of a 'tap'.- See Also:
-
longTouch
private int longTouchDelay before two finger touch triggers context menu, provided that no other touch event fired duringdelayTapmilliseconds.- See Also:
-
style
Reference to style of context menu. Most important to control positioning of context menu. -
autoHide
private boolean autoHidetrueto automatically set timer for hiding context menu after pointer exited context menu. -
autoOpen
private boolean autoOpentrueto open sub-menus automatically on hovering over the corresponding parent menu item. -
autoClose
private boolean autoClosetrueto close sub-menus automatically when pointer exited the sub-menu. -
isMouseOver
private boolean isMouseOvertrueif pointer is currently over context (sub)menu. -
fullscreenChangeHandler
HandlerRegistration fullscreenChangeHandlerReference to fullscreen change handler. When entering fullscreen the context menu needs to be moved from theRootPanelto the fullscreen element and vice versa. -
participants
Lookup table of listener widgets sporting context menus and their corresponding providers of the context menu contents. -
parentMenu
Reference to the parent menu. This isnullfor the shared, top level context menu. -
childMenu
Reference to current child menu ornullif no child menu is visible. -
contextMenu
Shared instance of context menu. -
hideContextMenuTimer
Timer for closing the context menu after a fixed delay.- See Also:
-
touchTimer
Timer for handling touch events that are scheduled to trigger context menu.
-
-
Constructor Details
-
ContextMenu
protected ContextMenu()Create new context menu. Use shared instance to create top level context menu,sharedContextMenu(). -
ContextMenu
Create new context submenu forparentmenu. Cannot be used to create top level context menu. Use shared instance instead,sharedContextMenu().Note: for submenus only.
- Parameters:
parent- menu (cannot benull)
-
-
Method Details
-
isShowing
public static boolean isShowing()Check if context menu is visible.- Returns:
trueif context menu is showing
-
add
Register a newlistenerwidget for context menu requests and associate with theproviderof the context menu.- Parameters:
listener- widget sporting a context menuprovider- provides context menu entries for this listener widget
-
addSeparator
public void addSeparator()Add new separator to context menu. -
add
Add new submenu to context menu.- Parameters:
name- of menu item with submenusubmenu- to add- Returns:
- context menu item that controls submenu
-
showAt
protected void showAt(int x, int y) Show context menu (or submenu) at position(x, y)relative to browser window.- Parameters:
x- horizontal position of context menuy- vertical position of context menu
-
showAt
Prepares context menu (or submenu) for showing at position(x, y)relative to browser window. Retrieves the context menu provider for thelistenerwidget and asks the provider to supply context menu for coordinates(x', y')relative tolistenerwidget. No context menu is shown if noproviderfound or ifproviderdid not supply any context menu entries.- Parameters:
listener- widget that requested the context menux- horizontal position of context menuy- vertical position of context menu- See Also:
-
openChildMenu
Open submenuchild. If needed, first close any other currently open submenu.- Parameters:
child- new child menu to open
-
closeChildMenu
public void closeChildMenu()Close submenu, if one is showing. -
open
protected void open()Open submenu. -
close
public void close()Close submenu or context menu if this is the top level. -
closeAll
public void closeAll()Recursively close hierarchy of context submenus, including the top level context menu. -
setHideTimer
protected void setHideTimer()Set timer for delayed hiding of context menu ifautoCloseistrue. This timer is used when pointer exits context (sub)menu.- See Also:
-
cancelHideTimer
protected void cancelHideTimer()Cancel the timer for hiding the context menu. Triggered when pointer re-enters context (sub)menu before the timer for hiding the menu runs out. This cascades through the entire (sub)menu hierarchy. -
setAutoHide
public void setAutoHide(boolean auto) Sets whether context menu hides automatically (with delay) after pointer exits context menu.- Parameters:
auto-trueto automatically hide context menu after delay- See Also:
-
isAutoHide
public boolean isAutoHide()Checks if context menu hides automatically (with some delay) after pointer exited.- Returns:
trueif context menu hides automatically- See Also:
-
setAutoOpen
public void setAutoOpen(boolean auto) Sets whether submenus should open automatically when pointer hovers over corresponding menu item in parent menu.- Parameters:
auto-trueto open submenus automatically- See Also:
-
isAutoOpen
public boolean isAutoOpen()Checks if submenus open automatically when pointer hovers over corresponding menu item in parent menu.- Returns:
trueif context menu opens automatically- See Also:
-
setAutoClose
public void setAutoClose(boolean auto) Sets whether submenus should close automatically when pointer stops hovering over corresponding menu item in parent menu.- Parameters:
auto-trueto close submenus automatically- See Also:
-
isAutoClose
public boolean isAutoClose()Checks if submenus close automatically when pointer stops hovering over corresponding menu item in parent menu.- Returns:
trueif context menu closes automatically- See Also:
-
setOffset
public void setOffset(int ox, int oy) Sets the horizontal and vertical offset between the location where the context menu was triggered and the position of the top left corner of the context menu. Typically the offset should be negative in both directions to ensure that once the context menu is visible, the pointer is located inside the context menu.- Parameters:
ox- the horizontal offsetoy- the vertical offset- See Also:
-
setDelayHide
public void setDelayHide(int delay) Set delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context menu, provided thatautoHideis set totrue.- Parameters:
delay- the delay for hiding context (sub)menu- See Also:
-
setLongTouch
public void setLongTouch(int delay) Set delay (in milliseconds) before showing the context menu after a two finger touch, provided that no other touch events fired duringdelay.- Parameters:
delay- the delay for showing context menu after two-finger touch- See Also:
-
getParentMenu
Get the parent menu. The parent can be a submenu ornullfor the top level context menu.- Returns:
- the parent (sub)menu
-
onContextMenu
- Specified by:
onContextMenuin interfaceContextMenuHandler
-
onMouseOver
Pointer (re-)entered context (sub)menu. Cancel timer to hide (sub)menu, in case it was running.
- Specified by:
onMouseOverin interfaceMouseOverHandler
-
onMouseOut
Pointer exited context (sub)menu. Start timer to hide (sub)menu.
- Specified by:
onMouseOutin interfaceMouseOutHandler
-
onTouchStart
Note: gesture events are proprietary implementations by Apple. Let's try to do without and stick to more primitive touch events.
- Specified by:
onTouchStartin interfaceTouchStartHandler
-
onTouchMove
One or more touches moved. Cancel request to show context menu (if scheduled).
Note: gesture events are proprietary implementations by Apple. Let's try to do without and stick to more primitive touch events.
- Specified by:
onTouchMovein interfaceTouchMoveHandler
-
onTouchEnd
One or more touches ended. Cancel request to show context menu (if scheduled).
Note: gesture events are proprietary implementations by Apple. Let's try to do without and stick to more primitive touch events.
- Specified by:
onTouchEndin interfaceTouchEndHandler
-
onTouchCancel
Touch canceled. Cancel request to show context menu (if scheduled).
Note: gesture events are proprietary implementations by Apple. Let's try to do without and stick to more primitive touch events.
- Specified by:
onTouchCancelin interfaceTouchCancelHandler
-
addFullscreenChangeHandler
Description copied from interface:HasFullscreenChangeHandlersAdds aFullscreenChangeEventhandler.- Specified by:
addFullscreenChangeHandlerin interfaceHasFullscreenChangeHandlers- Parameters:
handler- the fullscreenchange handler- Returns:
HandlerRegistrationused to remove this handler
-
onFullscreenChange
Called when aFullscreenChangeEventis fired.Placement of shared context menu element in the DOM needs to be adjusted when entering or exiting fullscreen mode.
- Specified by:
onFullscreenChangein interfaceFullscreenChangeHandler- Parameters:
event- theFullscreenChangeEventthat was fired