Class IzMenu
- java.lang.Object
- com.iizix.swt.IzMenu
public class IzMenu extends java.lang.Object
The Menu drop-in replacement in iiziGo that does not consume handles, i.e. creates the SWT menu only when required. It accepts menu items as well as action contributions. It can display all menus or just the ones enabled. It also handles omitting of separators to avoid duplicates, typically common when disabled actions or menu items are omitted.- Author:
- Christopher Mindus
Constructor Summary
Constructors Constructor Description IzMenu(IzMenuItem parent)
Constructor for cascaded menu.IzMenu(org.eclipse.swt.widgets.Control parent)
Constructor for menu on a Control.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IzMenu
add(int style, java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item.IzMenu
add(int style, java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item.IzMenu
add(IzSubMenu subMenu)
Adds a submenu as a "contribution".IzMenu
add(java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item as a "push button".IzMenu
add(java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item as a "push button".IzMenu
add(org.eclipse.jface.action.IAction action)
Adds an action as a menu item to this menu.IzMenu
addItems(java.lang.Object... items)
Adds all items that must be either instance of IAction of IzSubMenu, or null entries for separators.IzMenu
addMenuListener(java.lang.Runnable menuListener)
Sets the listener when the menu is about to be shown.IzMenu
addSeparator()
Adds a separator to the menu.IzMenu
clear()
Removes all menu items so as to clear the menu.void
dispose()
Disposes of the menu.static IzMenu
fromControl(org.eclipse.swt.widgets.Control control)
Gets the pop-up menu associated with a component.org.eclipse.swt.widgets.Control
getControl()
Gets the control for which the menu was created for.IzMenuItem
getItem(int index)
Gets a menu item at index.int
getItemCount()
Returns the number of items contained in the receiver.IzMenuItem[]
getItems()
Returns a (possibly empty) array ofMenuItem
s which are the items in the receiver.boolean
hasEnabledItems()
Returns if there are any enabled menu items to show.boolean
isDisposed()
Returns if the menu is disposed.boolean
isEnabled()
Returnstrue
if the receiver is enabled and all of the receiver's ancestors are enabled, andfalse
otherwise.boolean
isVisible()
Returns if the menu is currently displaying.IzMenu
omitDisabled()
Set the disabled menu item omitting when displaying the menu.void
setEnabled(boolean enabled)
Enables the receiver if the argument istrue
, and disables it otherwise.void
setLocation(int x, int y)
Sets the location of the receiver, which must be a popup, to the point specified by the arguments which are relative to the display.void
setLocation(org.eclipse.swt.graphics.Point location)
Sets the location of the receiver, which must be a popup, to the point specified by the argument which is relative to the display.IzMenu
setMenu(org.eclipse.swt.widgets.Control parent, boolean alignBottomLeft)
Assigns the menu to the control.void
setVisible(boolean visible)
Marks the receiver as visible if the argument istrue
, and marks it invisible otherwise.
Constructor Detail
IzMenu
public IzMenu(org.eclipse.swt.widgets.Control parent)
Constructor for menu on a Control.- Parameters:
parent
- The control parent.
IzMenu
public IzMenu(IzMenuItem parent)
Constructor for cascaded menu.- Parameters:
parent
- The menu item parent.
Method Detail
fromControl
public static IzMenu fromControl(org.eclipse.swt.widgets.Control control)
Gets the pop-up menu associated with a component. The parents are searched for the menu if not found.- Parameters:
control
- The control.- Returns:
- The IzMenu associated with the control (to display), null if not found.
clear
public IzMenu clear()
Removes all menu items so as to clear the menu. This call can be done during the call to the menu listener fired before the menu is shown.- Returns:
this
in order to be able to concatenate calls.
getControl
public org.eclipse.swt.widgets.Control getControl()
Gets the control for which the menu was created for.- Returns:
- The control, or null for cascaded menus.
setMenu
public IzMenu setMenu(org.eclipse.swt.widgets.Control parent, boolean alignBottomLeft)
Assigns the menu to the control. It must be the same control as the parent. When the right button (SWT.MenuDetect) is pressed, the menu is shown.- Parameters:
parent
- The control parent.alignBottomLeft
- Flag to align bottom left on control when mouse button 2 is pressed.- Returns:
this
in order to be able to concatenate calls.- Throws:
java.lang.IllegalArgumentException
- If the menu is set to another control than it's parent.
dispose
public void dispose()
Disposes of the menu.
isDisposed
public boolean isDisposed()
Returns if the menu is disposed.
getItemCount
public int getItemCount()
Returns the number of items contained in the receiver.- Returns:
- the number of items.
getItem
public IzMenuItem getItem(int index)
Gets a menu item at index.- Parameters:
index
- The index.- Returns:
- The menu item.
- Throws:
java.lang.IndexOutOfBoundsException
- If range is wrong.
getItems
public IzMenuItem[] getItems()
Returns a (possibly empty) array ofMenuItem
s which are the items in the receiver.Note: This is not the actual structure used by the receiver to maintain its list of items, so modifying the array will not affect the receiver.
- Returns:
- the items in the receiver.
setLocation
public void setLocation(int x, int y)
Sets the location of the receiver, which must be a popup, to the point specified by the arguments which are relative to the display.Note that this is different from most widgets where the location of the widget is relative to the parent.
Note that the platform window manager ultimately has control over the location of popup menus.
- Parameters:
x
- the new x coordinate for the receivery
- the new y coordinate for the receiver- Throws:
org.eclipse.swt.SWTException
-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
setLocation
public void setLocation(org.eclipse.swt.graphics.Point location)
Sets the location of the receiver, which must be a popup, to the point specified by the argument which is relative to the display.Note that this is different from most widgets where the location of the widget is relative to the parent.
Note that the platform window manager ultimately has control over the location of popup menus.
- Parameters:
location
- the new location for the receiver- Throws:
java.lang.NullPointerException
- If location is null.
setEnabled
public void setEnabled(boolean enabled)
Enables the receiver if the argument istrue
, and disables it otherwise. A disabled menu is typically not selectable from the user interface and draws with an inactive or "grayed" look.- Parameters:
enabled
- the new enabled state- Throws:
org.eclipse.swt.SWTException
-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
isEnabled
public boolean isEnabled()
Returnstrue
if the receiver is enabled and all of the receiver's ancestors are enabled, andfalse
otherwise. A disabled menu is typically not selectable from the user interface and draws with an inactive or "grayed" look.- Returns:
- the receiver's enabled state
setVisible
public void setVisible(boolean visible)
Marks the receiver as visible if the argument istrue
, and marks it invisible otherwise.If one of the receiver's ancestors is not visible or some other condition makes the receiver not visible, marking it visible may not actually cause it to be displayed.
- Parameters:
visible
- the new visibility state- Throws:
org.eclipse.swt.SWTException
-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
isVisible
public boolean isVisible()
Returns if the menu is currently displaying.- Returns:
- true when visible, false when not visible or a cascaded menu.
omitDisabled
public IzMenu omitDisabled()
Set the disabled menu item omitting when displaying the menu.- Returns:
this
in order to be able to concatenate calls.
hasEnabledItems
public boolean hasEnabledItems()
Returns if there are any enabled menu items to show.- Returns:
- false for separator, disabled menu item. Cascaded menu are also checked.
addMenuListener
public IzMenu addMenuListener(java.lang.Runnable menuListener)
Sets the listener when the menu is about to be shown. Menu states can then be processed.- Parameters:
menuListener
- The runnable that will be called prior to displaying the menu.- Returns:
this
in order to be able to concatenate calls.
addSeparator
public IzMenu addSeparator()
Adds a separator to the menu.- Returns:
this
in order to be able to concatenate calls.
add
public IzMenu add(org.eclipse.jface.action.IAction action)
Adds an action as a menu item to this menu.- Parameters:
action
- The action to add.- Returns:
this
in order to be able to concatenate calls.
add
public IzMenu add(java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item as a "push button".NOTE: supported styles: SWT.SEPARATOR, SWT.PUSH, SWT.CHECK, SWT.RADIO and SWT.CASCADE: NO OTHER!
- Parameters:
text
- The text, null for none.selectionListener
- The selection listener or null for none.- Returns:
this
in order to be able to concatenate calls.- Throws:
java.lang.IllegalArgumentException
- For invalid styles.
add
public IzMenu add(java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item as a "push button".NOTE: supported styles: SWT.SEPARATOR, SWT.PUSH, SWT.CHECK, SWT.RADIO and SWT.CASCADE: NO OTHER!
- Parameters:
text
- The text, null for none.image
- The image, null for none.selectionListener
- The selection listener or null for none.- Returns:
this
in order to be able to concatenate calls.- Throws:
java.lang.IllegalArgumentException
- For invalid styles.
add
public IzMenu add(int style, java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item.NOTE: supported styles: SWT.SEPARATOR, SWT.PUSH, SWT.CHECK, SWT.RADIO and SWT.CASCADE: NO OTHER!
- Parameters:
style
- The menu item style.text
- The text, null for none.- Throws:
java.lang.IllegalArgumentException
- For invalid styles.
add
public IzMenu add(int style, java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
Adds a menu item.NOTE: supported styles: SWT.SEPARATOR, SWT.PUSH, SWT.CHECK, SWT.RADIO and SWT.CASCADE: NO OTHER!
- Parameters:
style
- The menu item style.text
- The text, null for none.image
- The image, null for none.- Throws:
java.lang.IllegalArgumentException
- For invalid styles.
add
public IzMenu add(IzSubMenu subMenu)
Adds a submenu as a "contribution".- Parameters:
subMenu
- The submenu.- Returns:
- The
this
instance for concatenation.
addItems
public IzMenu addItems(java.lang.Object... items)
Adds all items that must be either instance of IAction of IzSubMenu, or null entries for separators. An item can also be an Object [] in which case the addItems(objectArray) will be called.- Parameters:
items
- The items.- Returns:
- The
this
instance for concatenation.