at.spardat.xma.appshell.MenuItem Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* @(#) $Id: MenuItem.java 9060 2012-03-06 10:06:47Z hoenninger $
*
*
*
*
*/
package at.spardat.xma.appshell;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
/**
* Logical representation of a menu item. It contains a {@link Task} which is called
* every time the menu item is selected. It may contain a list of MenuItems representing
* its submenu. Internally it maintains references to its parent MenuItem, the {@link AppShell}
* the visual representation of the menu item.
*
* @author s2877
* @since 1.4.0
*/
public class MenuItem implements IMenuItem {
IAppShell appShell;
IMenuItem parent;
String name;
Object attached;
ITask task;
List subMenu = new ArrayList();
Image image;
int accelerator;
int style = SWT.NONE;
boolean localEnabled=true;
boolean preserveTasks;
// to store the foregroundColor during disabling
Color foreground;
IMenuSelectionListener menuSelectionListener;
/**
* creates a MenuItem
* @param appShell the AppShell containing the MenuItem
*/
public MenuItem(IAppShell appShell) {
this.appShell=appShell;
}
/**
* creates a MenuItem
* @param name the name of the MenuItem
*/
public MenuItem(String name) {
this.name=name;
}
/**
* creates a MenuItem
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
*/
public MenuItem(String name, Task task) {
this.name=name;
this.task=task;
}
/**
* Note: For a MenuItem used at a MenuAppShell and so representing an org.eclipse.swt.widgets.MenuItem
* only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR may be specified.
* If this MenuItem has childs then only the style CASCADE is valid.
* @param name the name of the MenuItem
* @param style the SWT style
*/
public MenuItem(String name, int style) {
this.name=name;
this.style = style;
}
/**
*
* @param name the name of the MenuItem
* @param image an image shown at the MenuItem
*/
public MenuItem(String name, Image image) {
this.name=name;
this.image = image;
}
/**
* Note: For a MenuItem used at a MenuAppShell and so representing an org.eclipse.swt.widgets.MenuItem
* only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR may be specified.
* If this MenuItem has childs then only the style CASCADE is valid.
* @param name the name of the MenuItem
* @param style the SWT style
* @param image an image shown at the MenuItem
*/
public MenuItem(String name, int style, Image image) {
this.name=name;
this.style = style;
this.image = image;
}
/**
* Note: For a MenuItem used at a ShellMenuAppShell (i.e. at an SWT menu) and so representing an org.eclipse.swt.widgets.MenuItem
* only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR may be specified.
* If this MenuItem has childs then only the style CASCADE is valid.
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
* @param style the SWT style
*/
public MenuItem(String name, Task task, int style) {
this.name=name;
this.task=task;
this.style = style;
}
/***
*
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
* @param image an image shown at the MenuItem
*/
public MenuItem(String name, Task task, Image image) {
this.name=name;
this.task=task;
this.image = image;
}
/***
*
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
* @param image an image shown at the MenuItem
* @param accelerator hotkey code, does not work on TreeMenu and TabMenu
*/
public MenuItem(String name, Task task, Image image, int accelerator) {
this.name=name;
this.task=task;
this.image = image;
this.accelerator=accelerator;
}
/**
* Note: For a MenuItem used at a ShellMenuAppShell (i.e. at an SWT menu) and so representing an org.eclipse.swt.widgets.MenuItem
* only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR may be specified.
* If this MenuItem has childs then only the style CASCADE is valid.
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
* @param style the SWT style
* @param image an image shown at the MenuItem
*/
public MenuItem(String name, Task task, int style, Image image) {
this.name=name;
this.task=task;
this.style = style;
this.image = image;
}
/**
* Note: For a MenuItem used at a ShellMenuAppShell (i.e. at an SWT menu) and so representing an org.eclipse.swt.widgets.MenuItem
* only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR may be specified.
* If this MenuItem has childs then only the style CASCADE is valid.
* @param name the name of the MenuItem
* @param task the task to run when the MenuItem is selected
* @param style the SWT style
* @param image an image shown at the MenuItem
* @param accelerator hotkey code, does not work on TreeMenu and TabMenu
*/
public MenuItem(String name, Task task, int style, Image image, int accelerator) {
this.name=name;
this.task=task;
this.style = style;
this.image = image;
this.accelerator=accelerator;
}
/**
* Get the name of the MenuItem. The name must be unique within the
* client side application.
* @return the name of the MenuItem
*/
public String getName() {
return name;
}
/**
* Set the name of the MenuItem
* @param name the unique name of this item.
*/
public void setName(String name) {
// if(appShell!=null && appShell.menuItems.get(name)!=null) {
// throw new IllegalStateException("menuItem allready registered");
// }
this.name=name;
}
// /**
// * Get the full name of the MenuItem containing the
// * full name of the parent. The names of all MenuItems
// * in the hirarchy are concatenated seperated by "/".
// * @return
// */
// public String getFullName() {
// if(parent!=null) return parent.getFullName()+"/"+name;
// else return name;
// }
/**
* Set the task to run when the MenuItem is selected
* @param task the task of this item.
*/
public void setTask(ITask task) {
this.task=task;
}
/**
* Get the Task than will be called whenever this MenuItem is selected.
* @return the Task of this item.
*/
public ITask getTask() {
return task;
}
/**
* Get the visual representation of the MenuItem.
* e.g.: a {@link at.spardat.xma.mdl.tree.ITreeWMClient} or org.eclipse.swt.widget.MenuItem.
* @return the GUI-Object corresponding to this MenuItem.
*/
public Object getAttached() {
return attached;
}
/**
* Set the visual representation of the MenuItem
* e.g.: a {@link at.spardat.xma.mdl.tree.ITreeWMClient} or org.eclipse.swt.widget.MenuItem.
* @param attached the GUI-Object corresponding to this MenuItem.
*/
public void setAttached(Object attached) {
this.attached=attached;
}
/**
* @return Returns the image which is shown at this item. Can be null
*/
public Image getImage() {
return image;
}
/**
* @param image Sets the image to show at this item.
*/
public void setImage(Image image) {
this.image = image;
}
// TODO: comment me
public int getAccelerator () {
return accelerator;
}
// TODO: comment me
public void setAccelerator ( int accelerator ) {
this.accelerator = accelerator;
}
/**
* If no style was set then SWT.NONE is returned.
* @return Returns the SWT style set at this item.
*/
public int getStyle() {
return style;
}
/**
* For internal XMA use.
* @param style The style to set.
*/
void setStyle(int style) {
this.style = style;
}
/**
* Get the visual representation of the parent MenuItem
* @return the GUI-Object corresponding to the parent of this MenuItem.
*/
public Object getParentAttached() {
if(parent!=null) return parent.getAttached();
else return attached;
}
/**
* Get the MenuItem containing this MenuItem in its submenu.
* @return the parent MenuItem.
*/
public IMenuItem getParent() {
return parent;
}
/**
* Get the index of the MenuItem in the list of
* subitems in its parent.
* @return the index of this.
*/
public int getIndex() {
if(parent!=null) {
return parent.getIndexOf(this);
} else {
return -1;
}
}
/**
* Get the index of the given MenuItem in the submenu of this.
* @param item to determine the index of
* @return the index of the given item in the submenu
*/
public int getIndexOf(IMenuItem item) {
return subMenu.indexOf(item);
}
/**
* Select the MenuItem. This runs its contained Task.
* If there is no Task and no MenuSelectionListener attached to this MenuItem, nothing happens.
*/
public void select() {
if(task==null && menuSelectionListener==null) return; // nothing to do
if(appShell==null) throw new IllegalStateException("MenuItem '"+name+"' not attached");
if (menuSelectionListener == null) {
menuSelectionListener = new IMenuSelectionListener() {
public void menuItemSelected ( IMenuItem menuItem, ITask topTask, ITask task ) {
task.setMenu(MenuItem.this);
topTask.call(task);
}
};
}
menuSelectionListener.menuItemSelected(this,appShell.getTopTask(),task);
}
/**
* Add the given MenuItem to the list of subitems.
* The new item is registered at the AppShell and
* becomes visible.
* Note: A MenuItem used at a MenuAppShell (i.e. at an SWT menu) must be of the style CASCADE to have children.
* @param item to add.
*/
public void addItem(MenuItem item) {
if(item.parent!=null) throw new IllegalStateException("MenuItem '"+item.getName()+"' allready attached");
if(appShell instanceof MenuAppShell && (this.getStyle()& SWT.CASCADE) == 0){
throw new IllegalArgumentException("Can't add item '"+item.getName()+"'. The parent MenuItem '"+this.getName()+"' is not of style CASCADE");
}
item.parent=this;
subMenu.add(item);
appShell.registerMenu(item);
item.appShell=appShell;
}
/**
* Add the given MenuItem to the list of subitems at the given index.
* The new item is registered at the AppShell and becomes visible.
* Note: A MenuItem used at a MenuAppShell (i.e. at an SWT menu) must be of the style CASCADE to have children.
*
* @param index where to add the new item.
* @param item to add.
*/
public void addItem(int index, MenuItem item) {
if(item.parent!=null) throw new IllegalStateException("MenuItem '"+item.getName()+"' allready attached");
if(appShell instanceof MenuAppShell && (this.getStyle()& SWT.CASCADE) == 0){
throw new IllegalArgumentException("Can't add item '"+item.getName()+"'. The parent MenuItem '"+this.getName()+"' is not of style CASCADE");
}
item.parent=this;
subMenu.add(index,item);
appShell.registerMenu(item);
item.appShell=appShell;
}
/**
* Get the list of all MenuItems of the submenu of this MenuItem.
* @return the list of all MenuItems of the submenu.
*/
public List getItems() {
return subMenu;
}
/**
* Remove all MenuItems from the list of subitems.
* The items are unregisterd from the AppShell and
* are no longer visible.
*/
public void clearItems() {
for(Iterator it=subMenu.iterator();it.hasNext();) {
MenuItem item = (MenuItem)it.next();
item.appShell=null;
appShell.unregisterMenu(item);
item.parent=null;
}
subMenu.clear();
}
/**
* Remove the given MenuItem from the list of subitems.
* This item is unregisterd from the AppShell and
* is no longer visible.
* @param item to remove
*/
public void removeItem(MenuItem item) {
item.appShell=null;
appShell.unregisterMenu(item);
subMenu.remove(item);
item.parent=null;
}
/**
* Determine if the given item is contained in the submenu of this menu.
* @param item the MenuItem to search for in the submenu.
* @return true if the given item is contained in the list of subitems.
*/
public boolean containsItem(IMenuItem item) {
return containsItem(item, false);
}
/**
* Determine if the given item is contained in the submenu of this menu.
* @param item the MenuItem to search for in the submenu.
* @param recurisvely true if the item should be searched deeply
* @return true if the given item is contained in the list of subitems.
*/
public boolean containsItem(IMenuItem item, boolean recurisvely) {
for ( Iterator iterator = subMenu.iterator(); iterator.hasNext(); ) {
IMenuItem subItem = (IMenuItem) iterator.next();
if ( subItem != null ) {
if ( subItem.equals(item) )
return true;
else if ( recurisvely ) {
boolean deepInside = containsItem(subItem,true);
if ( deepInside )
return true;
}
}
}
return false;
}
/**
* Enables this menu item if the argument is true
,
* and disables it otherwise. A disabled menu item is
* not selectable from the user interface and
* draws with an inactive or "grayed" look.
* Disabling a menu item makes all its children not selectable, too.
* @since 2.2.0
*/
public void setEnabled(boolean enabled) {
((AppShellDelegate)appShell).setMenuItemEnabled(this,enabled);
}
/**
* Returns true
if this menu item is enabled and false
* otherwise. A disabled menu item is not selectable from the user interface
* and draws with an inactive or "grayed" look. A menu item is disabled, if
* it is either directly disabled or any of its parents is disabled.
* @since 2.2.0
*/
public boolean isEnabled() {
return ((AppShellDelegate)appShell).isMenuItemEnabled(this);
}
/**
* Returns false
if this menu item is directly disabled by calling
* setEnabled(false)
on its instance. It does not take the enabled
* state of its parent into account.
* @since 2.2.0
*/
public boolean isLocalEnabled() {
return localEnabled;
}
/**
* Returns the value of the preservedTasks-flag
*/
public boolean isPreserveTasks() {
return preserveTasks;
}
/**
* Set the preservedTasks-flag
*/
public void setPreserveTasks(boolean keepExistingTasks) {
this.preserveTasks = keepExistingTasks;
}
/**
* Set the menu selection listener.
*/
public void setMenuSelectionListener(IMenuSelectionListener menuSelectionListener) {
this.menuSelectionListener = menuSelectionListener;
}
/**
* Removes the menu selection listener.
*/
public void removeMenuSelectionListener() {
menuSelectionListener = null;
}
/**
* Returns true when the menu item has a menu selection listener.
*/
public boolean hasMenuSelectionListener() {
return menuSelectionListener != null;
}
}