at.spardat.xma.appshell.MenuAppShellDelegate 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: MenuAppShellDelegate.java 8548 2011-09-28 11:41:26Z laslovd $
*
*
*
*
*
*/
package at.spardat.xma.appshell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import at.spardat.xma.component.ComponentClient;
import at.spardat.xma.event.swt.XMASelectionAdapter;
import at.spardat.xma.page.PageClient;
/**
* An AppShell implementing the menu as SWT menu at the shellbar (Windows standard menu).
* This class is to be thought to be subclassed by an application specific implementation.
*
* @author s3460
* @since version_number
*/
public class MenuAppShellDelegate extends ContextAppShellDelegate implements IMenuItemCreator {
/**
* This variable holds the root menu.
*/
protected Menu menuW;
private MenuItemSelectionListener selectionListener;
/**
* Creates the Menu which serves as the root menu widget and is set at: menuW.
* In the MenuAppShell's implementation a Menu of the style SWT.BAR at the Shell's top is created.
* This method can be overritten if some other kind of menu should be used as root
* for the MenuItems (like a pop-up menu).
* Altough this method can be overritten it is only to call by the XMA framework.
* @return the Menu used as root for the MenuItems.
*/
protected Menu getMenu(){
Shell shell = appShell.getShell();
Menu aMenu = new Menu(shell,SWT.BAR);
shell.setMenuBar(aMenu);
return aMenu;
}
// TODO: change comment
/**
* Creates the Widgets of the PageClient and all Subpages by calling
* {@link #createWidgets()} on the PageClient and all Subpages.
*
*/
void prepare() {
super.prepare();
menuW = getMenu();
selectionListener = new MenuItemSelectionListener(appShell);
}
/**
* Notify the PageClient and all Subpages, that the Widgets are disposed
* by calling {@link #removeWidgets()} on all Subpages and the PageClient.
*/
public void removeWidgets() {
super.removeWidgets();
menuW=null;
selectionListener = null;
}
/**
* Completely enable/disable the menu.
* @param enabled true: enable, false: disable.
*/
protected void setMenuEnabled(boolean enabled) {
if(menuW==null) return;
boolean wasEnabled=menuW.isEnabled();
menuW.setEnabled(enabled);
MenuItem[] items = menuW.getItems();
//disable the items of the shell bar menu
for (int i = 0; i < items.length; i++) {
MenuItem item = items[i];
at.spardat.xma.appshell.MenuItem xmaItem = (at.spardat.xma.appshell.MenuItem) item.getData();
if(wasEnabled) { xmaItem.localEnabled=item.getEnabled(); }
item.setEnabled(enabled&&xmaItem.localEnabled);
}
}
/**
* Enables the given menu item if the argument enabled 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
*/
protected void setMenuItemEnabled(IMenuItem item,boolean enabled) {
((at.spardat.xma.appshell.MenuItem)item).localEnabled=enabled;
MenuItem itemW = (MenuItem) item.getAttached();
if(itemW!=null) {
if(menuW.isEnabled()||item.getParentAttached()!=null) {
itemW.setEnabled(enabled);
}
}
}
/**
* Returns true
if the given 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
*/
protected boolean isMenuItemEnabled(IMenuItem item) {
if(!menuW.isEnabled()) return false;
MenuItem itemW = (MenuItem) item.getAttached();
if(itemW!=null&&menuW.isEnabled()) {
((at.spardat.xma.appshell.MenuItem)item).localEnabled=itemW.isEnabled();
}
if(item.isLocalEnabled()&&item.getParent()!=null) {
return isMenuItemEnabled(item.getParent());
}
return item.isLocalEnabled();
}
/**
* Attach the given MenuItem to its viusal representation.
* The visual representation is implemented by a SWT menu at the shellbar.
* The change is propagated to the GUI.
* If the item has no parent then it is atached to the bar menu.
* If the item has a parent (which is a item itself) then it is atached to the parent's menu.
* If the parent has no menu then there is one created.
*
* @param item to attach
*/
public void attachMenu(IMenuItem item) {
if(menuW==null) return;
Menu parentMenu = null;
MenuItem menuItemParentW = (MenuItem) item.getParentAttached();
if(menuItemParentW == null){//if item has no parent then attach to bar menu
parentMenu = menuW;
} else{
parentMenu = menuItemParentW.getMenu();
if (parentMenu == null){//if parent item has no menu then create one
parentMenu = createMenu(appShell.getShell(),SWT.DROP_DOWN);
menuItemParentW.setMenu(parentMenu);
}
}
MenuItem menuItemW = createMenuItem(parentMenu,item.getStyle(),item.getIndex());
menuItemW.setText(getMenuResource().getString(item.getName()));
menuItemW.setData(item);
if(item.getImage() != null){
menuItemW.setImage(item.getImage());
}
if(item.getAccelerator() != 0){
menuItemW.setAccelerator(item.getAccelerator());
}
menuItemW.addSelectionListener(selectionListener);
item.setAttached(menuItemW);
setMenuItemEnabled(item, item.isLocalEnabled());
}
/**
* Detach the MenuItem from its visual representation.
* The change must be propagated to the GUI.
* @param item to remove
*/
public void detachMenu(IMenuItem item) {
if(menuW==null) return;
MenuItem itemW = (MenuItem) item.getAttached();
if (itemW != null) {
if(menuW.isEnabled()) {
((at.spardat.xma.appshell.MenuItem)item).localEnabled=itemW.isEnabled();
}
itemW.dispose();
}
}
/**
* Calls the clientevent configured for the widget of the event by
* calling {@link #clientEvent(SelectionEvent,int)}. Handles the selection
* events of the menu-tree, too.
*
* @param event the SWT-Event that happened.
* @param type the type of the Event, as defined by the event type constants in class SWT.
*/
protected void clientEvent(SelectionEvent event,int type) {
super.clientEvent(event,type);
if(event.widget instanceof MenuItem && type==SWT.Selection) {
menuWSelected((MenuItem)event.widget);
}
}
/**
* Event-method called, when an item in the menu is selected. Calls the
* Task of the corresponding MenuItem.
*
*/
protected void menuWSelected(MenuItem aMenuItem) {
IMenuItem xmaMenuItem = (IMenuItem) aMenuItem.getData();
if(xmaMenuItem != null){
callMenu(xmaMenuItem);
}
}
/**
* Selects the named MenuItem. This method behaves like
* the user has selected the MenuItem via the GUI.
* @param menuId the name of the MenuItem to select.
*/
public void selectMenu(String menuId) {
if(menuW==null) return;
MenuItem menuItem = (MenuItem) getMenu(menuId).getAttached();
menuItem.setSelection(true);
callMenu(menuId);
}
/**
* Marks the named MenuItem as selected.
* When the receiver is of type CHECK
or RADIO
,
* it is selected when it is checked.
* @param menuId the name of the MenuItem to mark as selected.
*/
public void markMenu(String menuId) {
if(menuW==null) return;
MenuItem menuItem = (MenuItem) getMenu(menuId).getAttached();
menuItem.setSelection(true);
}
private class MenuItemSelectionListener extends XMASelectionAdapter {
MenuItemSelectionListener(PageClient page) {
super(page);
}
public void widgetSelectedImpl(SelectionEvent e) {
appShell.clientEventBase(e,SWT.Selection);
}
}
// TODO: comment me
public MenuItem createMenuItem ( Menu parentMenu, int style, int index ) {
if ( appShell instanceof IMenuItemCreator ) {
MenuItem res = ((IMenuItemCreator)appShell).createMenuItem(parentMenu,style,index);
if ( res != null )
return res;
}
return new MenuItem(parentMenu,style,index);
}
// TODO: comment me
public Menu createMenu ( Shell shell, int dropDown ) {
if ( appShell instanceof IMenuItemCreator ) {
Menu res = ((IMenuItemCreator)appShell).createMenu(shell,dropDown);
if ( res != null )
return res;
}
return new Menu(shell,dropDown);
}
}