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 interface
Listener interface for widgets that offer context menus.static interface
Provider interface for classes that are responsible for supplying the contents of the context menu.protected class
Manage registrations of listener widgets, their context menu providers and the corresponding event handlers.class
Timer for handling touch events triggering the context menu.Nested classes/interfaces inherited from class UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled
Nested classes/interfaces inherited from interface HasWidgets
HasWidgets.ForIsWidget
Nested classes/interfaces inherited from interface IndexedPanel
IndexedPanel.ForIsWidget
Nested classes/interfaces inherited from interface InsertPanel
InsertPanel.ForIsWidget
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
true
to close sub-menus automatically when pointer exited the sub-menu.private boolean
true
to automatically set timer for hiding context menu after pointer exited context menu.private boolean
true
to open sub-menus automatically on hovering over the corresponding parent menu item.protected ContextMenu
Reference to current child menu ornull
if no child menu is visible.protected static ContextMenu
Shared instance of context menu.private static final int
Default horizontal offset of context menu relative to the coordinates of the context menu triggering event.private static final int
Default vertical offset of context menu relative to the coordinates of the context menu triggering event.private static final int
Default delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.private int
Delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context (sub)menu.(package private) HandlerRegistration
Reference to fullscreen change handler.private Timer
Timer for closing the context menu after a fixed delay.private boolean
true
if pointer is currently over context (sub)menu.static final int
Delay in milliseconds before the touch event is considered a 'long touch' instead of a 'tap'.private int
Delay before two finger touch triggers context menu, provided that no other touch event fired duringdelayTap
milliseconds.private int
Horizontal offset of context menu relative to coordinates of context menu triggering event.private int
Vertical offset of context menu relative to coordinates of context menu triggering event.protected ContextMenu
Reference 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 Style
Reference to style of context menu.protected ContextMenu.TouchTimer
Timer for handling touch events that are scheduled to trigger context menu.Fields inherited from class UIObject
DEBUG_ID_PREFIX
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Create new context menu.ContextMenu
(ContextMenu parent) Create new context submenu forparent
menu. -
Method Summary
Modifier and TypeMethodDescriptionadd
(String name, ContextMenu submenu) Add new submenu to context menu.void
add
(ContextMenu.Listener listener, ContextMenu.Provider provider) Register a newlistener
widget for context menu requests and associate with theprovider
of the context menu.Adds aFullscreenChangeEvent
handler.void
Add new separator to context menu.protected void
Cancel the timer for hiding the context menu.void
close()
Close submenu or context menu if this is the top level.void
closeAll()
Recursively close hierarchy of context submenus, including the top level context menu.void
Close submenu, if one is showing.Get the parent menu.boolean
Checks if submenus close automatically when pointer stops hovering over corresponding menu item in parent menu.boolean
Checks if context menu hides automatically (with some delay) after pointer exited.boolean
Checks if submenus open automatically when pointer hovers over corresponding menu item in parent menu.static boolean
Check if context menu is visible.boolean
Check if this is a submenu.void
onContextMenu
(ContextMenuEvent event) void
Called when aFullscreenChangeEvent
is fired.void
onMouseOut
(MouseOutEvent event) void
onMouseOver
(MouseOverEvent event) void
onTouchCancel
(TouchCancelEvent event) void
onTouchEnd
(TouchEndEvent event) void
onTouchMove
(TouchMoveEvent event) void
onTouchStart
(TouchStartEvent event) protected void
open()
Open submenu.void
openChildMenu
(ContextMenu child) Open submenuchild
.void
setAutoClose
(boolean auto) Sets whether submenus should close automatically when pointer stops hovering over corresponding menu item in parent menu.void
setAutoHide
(boolean auto) Sets whether context menu hides automatically (with delay) after pointer exits context menu.void
setAutoOpen
(boolean auto) Sets whether submenus should open automatically when pointer hovers over corresponding menu item in parent menu.void
setDelayHide
(int delay) Set delay before hiding the context (sub)menu (in milliseconds) after the pointer has exited the context menu, provided thatautoHide
is set totrue
.protected void
Set timer for delayed hiding of context menu ifautoClose
istrue
.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
.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.static ContextMenu
The context menu is added to theRootPanel
of the GWT application and shared among all elements that sport a context menu.protected void
showAt
(int x, int y) Show context menu (or submenu) at position(x, y)
relative to browser window.void
showAt
(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, remove
Methods inherited from class Panel
add, adopt, doAttachChildren, doDetachChildren, orphan, remove
Methods 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, unsinkEvents
Methods 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, toString
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface HasHandlers
fireEvent
Methods inherited from interface IndexedPanel
getWidget, getWidgetCount, getWidgetIndex, remove
Methods inherited from interface IndexedPanel.ForIsWidget
getWidgetIndex
Methods inherited from interface InsertPanel.ForIsWidget
add
Methods 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 duringdelayTap
milliseconds.- See Also:
-
style
Reference to style of context menu. Most important to control positioning of context menu. -
autoHide
private boolean autoHidetrue
to automatically set timer for hiding context menu after pointer exited context menu. -
autoOpen
private boolean autoOpentrue
to open sub-menus automatically on hovering over the corresponding parent menu item. -
autoClose
private boolean autoClosetrue
to close sub-menus automatically when pointer exited the sub-menu. -
isMouseOver
private boolean isMouseOvertrue
if 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 theRootPanel
to 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 isnull
for the shared, top level context menu. -
childMenu
Reference to current child menu ornull
if 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 forparent
menu. 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:
true
if context menu is showing
-
add
Register a newlistener
widget for context menu requests and associate with theprovider
of 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 thelistener
widget and asks the provider to supply context menu for coordinates(x', y')
relative tolistener
widget. No context menu is shown if noprovider
found or ifprovider
did 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 ifautoClose
istrue
. 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
-true
to 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:
true
if 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
-true
to 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:
true
if 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
-true
to 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:
true
if 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 thatautoHide
is 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 ornull
for the top level context menu.- Returns:
- the parent (sub)menu
-
onContextMenu
- Specified by:
onContextMenu
in interfaceContextMenuHandler
-
onMouseOver
Pointer (re-)entered context (sub)menu. Cancel timer to hide (sub)menu, in case it was running.
- Specified by:
onMouseOver
in interfaceMouseOverHandler
-
onMouseOut
Pointer exited context (sub)menu. Start timer to hide (sub)menu.
- Specified by:
onMouseOut
in 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:
onTouchStart
in 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:
onTouchMove
in 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:
onTouchEnd
in 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:
onTouchCancel
in interfaceTouchCancelHandler
-
addFullscreenChangeHandler
Description copied from interface:HasFullscreenChangeHandlers
Adds aFullscreenChangeEvent
handler.- Specified by:
addFullscreenChangeHandler
in interfaceHasFullscreenChangeHandlers
- Parameters:
handler
- the fullscreenchange handler- Returns:
HandlerRegistration
used to remove this handler
-
onFullscreenChange
Called when aFullscreenChangeEvent
is fired.Placement of shared context menu element in the DOM needs to be adjusted when entering or exiting fullscreen mode.
- Specified by:
onFullscreenChange
in interfaceFullscreenChangeHandler
- Parameters:
event
- theFullscreenChangeEvent
that was fired