All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.appshell.TabAppShellDelegate Maven / Gradle / Ivy

/*
 * @(#) $Id: TabAppShellDelegate.java 8593 2011-10-13 10:35:08Z laslovd $
 *
 * Copyright 2009/2010 by sIT Solutions,
 * A-1110 Wien, Geiselbergstr.21-25.
 * All rights reserved.
 *
 */
package at.spardat.xma.appshell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

import at.spardat.xma.component.ComponentClient;
import at.spardat.xma.page.EventAdapter;

/**
 * An AppShell implementing the menu tree with SWT's CTabs 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 s4709
 * @since 4.1.0
 */
public class TabAppShellDelegate extends CompositeMenuAppShellDelegate implements ITabItemCreator {

    private CTabFolder tabFolderW;
    private int alignment;
    private EventAdapter adapter;
    
    /**
     * Initializes the application shell.
     *
     * @param component the Component containing the DialogPage.
     * @param stateless indicating if this page is stateless on the server.
     * @param style The SWT-Style for the Shell of the DialogPage.
     * @param alignment SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT for aligning the tabs
     * @throws IllegalArgumentException if component is null.
     */
    public TabAppShellDelegate ( int alignment ) {
        this.alignment = alignment;
    }

    /**
     * Factory method for creating all the CTabFolders used for the menu.
     * This method can be overwritten to change or enhance the folder creation, 
     * e.g. to customize graphical design. 
     * @param parent The parent Composite (or CTabFolder what is a specialized Composite) 
     * @param menuItem The menu item related to the CTab related to the folder that has to be created here, 
     *                 or null for the root tab folder
     * @param globalAlignment the instance-wide defined alignment setting, see {@link #TabAppShellDelegate(ComponentClient, boolean, int, int)} 
     * @return the created CTabFolder instance
     */
    public CTabFolder createTabFolder ( Composite parent, IMenuItem menuItem, int globalAlignment ) {
        if ( appShell instanceof ITabItemCreator ) {
            CTabFolder res = ((ITabItemCreator) appShell).createTabFolder(parent,menuItem,globalAlignment);
            if ( res != null )
                return res;
        }
        return new CTabFolder(parent, SWT.NONE | globalAlignment); 
    }
    
    /**
     * Factory method for creating all the CTabItems used for the menu.
     * This method can be overwritten to change or enhance the folder creation, 
     * e.g. to customize graphical design. 
     * @param parent The parent CTabFolder
     * @param item related IMenuItem instance
     * @return the created CTabItem instance
     */
    public CTabItem createTabItem ( CTabFolder parent, IMenuItem item ) {
        if ( appShell instanceof ITabItemCreator ) {
            CTabItem res = ((ITabItemCreator) appShell).createTabItem(parent,item);
            if ( res != null )
                return res;
        }
        int flags = item.getStyle() & ~SWT.CLOSE; // CASCADE has unfortunately the same bit-mask as CLOSE
        return new CTabItem(parent,flags,item.getIndex());
    }
    
    /*
     * (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 ) {
        this.adapter = adapter;
        tabFolderW = createTabFolder(menuCompositeW,null,alignment);
        tabFolderW.addSelectionListener(adapter);
        return tabFolderW;
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#setMenuEnabled(boolean)
     */
    protected void setMenuEnabled ( boolean enabled ) {
        tabFolderW.setEnabled(enabled);
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#setMenuItemEnabled(at.spardat.xma.appshell.IMenuItem, boolean)
     */
    protected void setMenuItemEnabled ( IMenuItem item, boolean enabled ) {
        Widget widget = (Widget) item.getAttached();
        if ( widget == null )
            return;
        if ( widget instanceof CTabItem ) {
            Control ctrl = ((CTabItem) widget).getControl();
            if ( ctrl != null )
                ctrl.setEnabled(enabled);
        }
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#isMenuItemEnabled(at.spardat.xma.appshell.IMenuItem)
     */
    protected boolean isMenuItemEnabled ( IMenuItem item ) {
        Widget widget = (Widget) item.getAttached();
        if ( widget == null )
            return true;
        if ( widget instanceof CTabItem ) {
            Control ctrl = ((CTabItem) widget).getControl();
            return ctrl == null || ctrl.isEnabled();
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#attachMenu(at.spardat.xma.appshell.IMenuItem)
     */
    public void attachMenu ( IMenuItem item ) {
        if ( tabFolderW == null ) 
            return;
        
        CTabItem parentTab = (CTabItem) item.getParentAttached();
        CTabItem tabItem = null;
        if ( parentTab == null ) { // direct child of rootMenu
            tabItem = createTabItem(tabFolderW,item);
        } else {
            CTabItem grandParentTab = (CTabItem) item.getParent().getParentAttached();
            Composite parentFolder = (Composite) parentTab.getControl();
            Composite grandParentFolder = grandParentTab == null 
                                        ? tabFolderW
                                        : (Composite) grandParentTab.getControl();
            if ( parentFolder == null ) {
                parentFolder = createTabFolder(grandParentFolder,item.getParent(),alignment);
                ((CTabFolder) parentFolder).addSelectionListener(adapter);
                parentTab.setControl(parentFolder);
            }
            tabItem = createTabItem((CTabFolder) parentFolder,item);
        }
        if ( tabItem != null ) {
            tabItem.setText(getMenuResource().getString(item.getName()));
            tabItem.setData(item);
            if ( item.getImage() != null ) {
                tabItem.setImage(item.getImage());
            }
            item.setAttached(tabItem);
        }
        setMenuEnabled(item.isLocalEnabled());
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#detachMenu(at.spardat.xma.appshell.IMenuItem)
     */
    public void detachMenu ( IMenuItem item ) {
        if ( tabFolderW == null ) 
            return;

        CTabItem tab = (CTabItem) item.getAttached();
        if ( tab != null )
            tab.dispose();
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#selectMenu(java.lang.String)
     */
    public void selectMenu ( String menuId ) {
        if ( tabFolderW == null )
            return;
        
        markMenu(menuId);
        callMenu(menuId);
    }

    /*
     * (non-Javadoc)
     * @see at.spardat.xma.appshell.AppShell#markMenu(java.lang.String)
     */
    public void markMenu ( String menuId ) {
        if ( tabFolderW == null )
            return;
        
        IMenuItem menuItem = getMenu(menuId);
        markMenu(menuItem);
    }

    /**
     * Recursively selects the CTabItem related to the given menu item and it's parent items. 
     * @param menuItem
     */
    private void markMenu ( IMenuItem menuItem ) {
        Widget o = (Widget) menuItem.getAttached();
        if ( o == null )
            return;
        if ( o instanceof CTabItem ) {
            CTabItem tabItem = (CTabItem) o;
            tabItem.getParent().setSelection(tabItem);
            if ( menuItem.getParent() != null )
                markMenu(menuItem.getParent());
        }
    }

    
    /**
     * Calls the client event configured for the widget of the event by
     * calling {@link #clientEvent(SelectionEvent,int)}. Handles the selection
     * events of the menu-tabs, 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 ( type == SWT.Selection && event.widget instanceof CTabFolder ) {
            tabFolderSelected((CTabFolder)event.widget);
        }
    }

    /**
     * Event-method called, when an item in the given tab folder is selected. Calls the
     * Task of the corresponding MenuItem, witch could possible be a child leave under 
     * the selected one.  
     *
     * @param folder which got the selection event
     */
    public void tabFolderSelected ( CTabFolder folder ) {
        CTabItem tabItem = folder.getSelection();
        if ( tabItem == null )
            return;
        if ( tabItem.getControl() instanceof CTabFolder )
            tabFolderSelected((CTabFolder) tabItem.getControl());
        else {
            IMenuItem xmaMenuItem = (IMenuItem) tabItem.getData();
            if ( xmaMenuItem != null ) {
                callMenu(xmaMenuItem);
            }
        }
        if ( appShell instanceof ITabItemCreator ) {
            ((ITabItemCreator) appShell).tabFolderSelected(folder);            
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy