Package com.iizix.swt

Class 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 
      ConstructorDescription
      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 TypeMethodDescription
      IzMenuadd​(int style, java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
      Adds a menu item.
      IzMenuadd​(int style, java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
      Adds a menu item.
      IzMenuadd​(IzSubMenu subMenu)
      Adds a submenu as a "contribution".
      IzMenuadd​(java.lang.String text, org.eclipse.swt.graphics.Image image, org.eclipse.swt.widgets.Listener selectionListener)
      Adds a menu item as a "push button".
      IzMenuadd​(java.lang.String text, org.eclipse.swt.widgets.Listener selectionListener)
      Adds a menu item as a "push button".
      IzMenuadd​(org.eclipse.jface.action.IAction action)
      Adds an action as a menu item to this menu.
      IzMenuaddItems​(java.lang.Object... items)
      Adds all items that must be either instance of IAction of IzSubMenu, or null entries for separators.
      IzMenuaddMenuListener​(java.lang.Runnable menuListener)
      Sets the listener when the menu is about to be shown.
      IzMenuaddSeparator()
      Adds a separator to the menu.
      IzMenuclear()
      Removes all menu items so as to clear the menu.
      voiddispose()
      Disposes of the menu.
      static IzMenufromControl​(org.eclipse.swt.widgets.Control control)
      Gets the pop-up menu associated with a component.
      org.eclipse.swt.widgets.ControlgetControl()
      Gets the control for which the menu was created for.
      IzMenuItemgetItem​(int index)
      Gets a menu item at index.
      intgetItemCount()
      Returns the number of items contained in the receiver.
      IzMenuItem[]getItems()
      Returns a (possibly empty) array of MenuItems which are the items in the receiver.
      booleanhasEnabledItems()
      Returns if there are any enabled menu items to show.
      booleanisDisposed()
      Returns if the menu is disposed.
      booleanisEnabled()
      Returns true if the receiver is enabled and all of the receiver's ancestors are enabled, and false otherwise.
      booleanisVisible()
      Returns if the menu is currently displaying.
      IzMenuomitDisabled()
      Set the disabled menu item omitting when displaying the menu.
      voidsetEnabled​(boolean enabled)
      Enables the receiver if the argument is true, and disables it otherwise.
      voidsetLocation​(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.
      voidsetLocation​(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.
      IzMenusetMenu​(org.eclipse.swt.widgets.Control parent, boolean alignBottomLeft)
      Assigns the menu to the control.
      voidsetVisible​(boolean visible)
      Marks the receiver as visible if the argument is true, and marks it invisible otherwise.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 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:
        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 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:
        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()
        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:
        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.