Package com.iizix.swt

Class IzMenu

java.lang.Object
com.iizix.swt.IzMenu

public class IzMenu extends 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 Details

    • IzMenu

      public IzMenu(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 Details

    • fromControl

      public static IzMenu fromControl(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 Control getControl()
      Gets the control for which the menu was created for.
      Returns:
      The control, or null for cascaded menus.
    • setMenu

      public IzMenu setMenu(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:
      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:
      IndexOutOfBoundsException - If range is wrong.
    • getItems

      public IzMenuItem[] getItems()
      Returns a (possibly empty) array of MenuItems 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 receiver
      y - the new y coordinate for the receiver
      Throws:
      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(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:
      NullPointerException - If location is null.
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables the receiver if the argument is true, 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:
      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()
      Returns true if the receiver is enabled and all of the receiver's ancestors are enabled, and false 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 is true, 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:
      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(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(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(String text, 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:
      IllegalArgumentException - For invalid styles.
    • add

      public IzMenu add(String text, Image image, 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:
      IllegalArgumentException - For invalid styles.
    • add

      public IzMenu add(int style, String text, 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:
      IllegalArgumentException - For invalid styles.
    • add

      public IzMenu add(int style, String text, Image image, 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:
      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(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.