Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* 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: TreeMenuAppShellDelegate.java 9562 2012-05-15 14:44:36Z hoenninger $
*
*
*
*
*/
package at.spardat.xma.appshell;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import at.spardat.xma.page.EventAdapter;
/**
* An AppShell implementing the menu tree with a SWT-Tree and the
* visualization of the context strings with a SWT-List.
* This class is to be thought to be subclassed by an application specific implementation.
* @author s2877
* @since 1.4.0
*/
public class TreeMenuAppShellDelegate extends CompositeMenuAppShellDelegate implements ITreeItemCreator {
/**
* swt-tree containing the menu items.
*/
protected Tree menuW=null;
/**
* contains all disabled icons
* key:original icon
* value:disabled icon
*/
private Map disabledIcons;
/**
* contains the last selection to be able to undo selection of disabled nodes
*/
private TreeItem[] lastSelection = new TreeItem[0];
/*
* (non-Javadoc)
* @see at.spardat.xma.appshell.CompositeMenuAppShell#createRootMenu(at.spardat.xma.page.EventAdapter, org.eclipse.swt.layout.FormData, org.eclipse.swt.widgets.Composite)
*/
protected Control createRootMenu(EventAdapter adapter, Composite menuCompositeW) {
menuW = new Tree(menuCompositeW,SWT.SINGLE);
menuW.addSelectionListener(adapter);
return menuW;
}
/**
* 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;
contextW=null;
}
/**
* 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==menuW && type==SWT.Selection) {
menuWSelected();
}
}
/**
* Event-method called, when an item in the menu-tree is selected. Calls the
* Task of the corresponding MenuItem.
*/
protected void menuWSelected() {
if(menuW.getSelectionCount()==1) {
TreeItem node = menuW.getSelection()[0];
MenuItem menuItem = (MenuItem) node.getData();
if(isMenuItemEnabled(menuItem)) {
lastSelection=menuW.getSelection();
callMenu(menuItem);
} else {
menuW.setSelection(lastSelection);
}
} else {
menuW.setSelection(lastSelection);
}
}
/**
* Completely enable/disable the menu.
* @param enabled true: enable, false: disable.
*/
protected void setMenuEnabled(boolean enabled) {
if(menuW==null) return;
menuW.setEnabled(enabled);
}
/**
* 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) {
((MenuItem)item).localEnabled=enabled;
setItemEnabledRec(((MenuItem)item),enabled);
}
/**
* Recursively mark all submenues as enabled/disabled
*/
private void setItemEnabledRec(MenuItem item, boolean enabled) {
TreeItem node = (TreeItem) item.getAttached();
if(enabled&&isMenuItemEnabled(item)) {
node.setForeground(item.foreground);
item.foreground=null;
node.setImage(item.image);
for(Iterator it=item.getItems().iterator();it.hasNext();) {
setItemEnabledRec((MenuItem) it.next(), enabled);
}
} else if(!enabled) {
if(item.foreground==null) { item.foreground=node.getForeground(); }
node.setForeground(appShell.getComponent().getDisplay().getSystemColor(SWT.COLOR_GRAY));
node.setImage(getDisabledIcon(item.image));
for(Iterator it=item.getItems().iterator();it.hasNext();) {
setItemEnabledRec((MenuItem) it.next(), enabled);
}
}
}
/**
* Get the grayed version of icon. All grayed versions are
* stored in disabledIcons for automatic disposing in {@link #detachMenu(IMenuItem)}
*/
private Image getDisabledIcon(Image icon) {
if(disabledIcons==null) disabledIcons=new HashMap();
Image disabled = (Image) disabledIcons.get(icon);
if(disabled==null) {
disabled = new Image(appShell.getComponent().getDisplay(),icon,SWT.IMAGE_DISABLE);
disabledIcons.put(icon, disabled);
}
return disabled;
}
/**
* 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;
if(item.isLocalEnabled()&&item.getParent()!=null) {
return isMenuItemEnabled(item.getParent());
}
return item.isLocalEnabled();
}
/**
* Attach the given MenuItem to its visual representation.
* The visual representation is implemented by a SWT TreeItem.
* The change is propagated to the GUI.
* @param item to attach
*/
public void attachMenu(IMenuItem item) {
if(menuW==null) return;
TreeItem parentNode = (TreeItem) item.getParentAttached();
TreeItem node;
if (parentNode == null) { // direct child of rootMenu
node = createTreeItem(menuW,item.getStyle(),item.getIndex());
} else {
node = createTreeItem(parentNode,item.getStyle(),item.getIndex());
parentNode.setExpanded(true);
}
node.setText(removeMnemonics(getMenuResource().getString(item.getName())));
node.setData(item);
if(item.getImage() != null){
node.setImage(item.getImage());
}
item.setAttached(node);
setMenuEnabled(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;
TreeItem node = (TreeItem) item.getAttached();
if (node != null) {
node.dispose();
}
((MenuItem)item).foreground=null;
if(disabledIcons!=null) {
for(Iterator it=disabledIcons.values().iterator();it.hasNext();) {
((Image)it.next()).dispose();
}
disabledIcons=null;
}
}
/**
* 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;
TreeItem node = (TreeItem) getMenu(menuId).getAttached();
menuW.setSelection(new TreeItem[]{node});
callMenu(menuId);
}
/**
* Visibly marks the named MenuItem as selected.
* @param menuId the name of the MenuItem to mark as selected.
*/
public void markMenu(String menuId) {
if(menuW==null) return;
TreeItem node = (TreeItem) getMenu(menuId).getAttached();
menuW.setSelection(new TreeItem[]{node});
}
// TODO: comment me
public TreeItem createTreeItem ( TreeItem parent, int style, int index ) {
if ( appShell instanceof ITreeItemCreator ) {
TreeItem res = ((ITreeItemCreator)appShell).createTreeItem(parent,style,index);
if ( res != null )
return res;
}
return new TreeItem(parent,style,index);
}
// TODO: comment me
public TreeItem createTreeItem ( Tree tree, int style, int index ) {
if ( appShell instanceof ITreeItemCreator ) {
TreeItem res = ((ITreeItemCreator)appShell).createTreeItem(tree,style,index);
if ( res != null )
return res;
}
return new TreeItem(tree,style,index);
}
private static String removeMnemonics ( String text ) {
final String TMP = "@#@##@#@";
text = text.replaceAll("&&",TMP);
text = text.replaceAll("&","");
text = text.replaceAll(TMP,"&");
return text;
}
}