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

at.spardat.xma.page.Notebook 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
 *******************************************************************************/

package at.spardat.xma.page;

import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;

import at.spardat.xma.boot.component.*;

/**
 * Manages a Collection of NotebookPages and the corresponding SWT-TabFolder.
 *
 * @author s2877
 */
public class Notebook implements IEmbeddable, SelectionListener, HelpListener {

    /** The PageClient containing this Notebook */
    PageClient parent;

    /** The SWT-TabFolder */
    TabFolder tabFolder;

    /** Collection of all NotebookPages contained in this Notebook */
    ArrayList pages = new ArrayList();

    /** The NotebookPage with is currently visible on the active Tab of the TabFolder */
    NotebookPage activePage;

    /** true after enter() was executed on the initially active NotebookPage */
    boolean fullInitialized;


    /**
     * Initializes the Notebook inside the given PageClient.
     *
     * @param parent the PageClient containing this Notebook.
     */
    public Notebook(PageClient parent) {
        this.parent = parent;
        parent.addChild(this);
    }

    /**
     * Gets the TabFolder of the Notebook. The TabFolder is the corresponding
     * Control in SWT.
     *
     * @return the TabFolder of the Notebook.
     */
    public TabFolder getTabFolder() {
        if(tabFolder==null && parent.getComposite()!=null) createTabFolder(parent.getComposite());
        return tabFolder;
    }

    /**
     * Creates the SWT-Composite of the PageClient which is the TabFolder of the Notebook.
     *
     * @param parentComp the SWT-Composite which shall be the parent of the created SWT-Composite.
     * @return the TabFolder of the Notebook.
     */
    public Composite createComposite(Composite parentComp) {
        createTabFolder(parentComp);
        return tabFolder;
    }

    /**
     * Gets the SWT-Composite of the PageClient which is the TabFolder of the Notebook.
     *
     * @return the SWT-Composite corresponding to this PageClient
     */
    public Composite getComposite() {
        return getTabFolder();
    }

    /**
     * Gets the PageClient containig this Notebook.
     *
     * @return the PageClient containing this Notebook.
     */
    public PageClient getParent() {
        return parent;
    }

    /**
     * Gets the NotebookPage with the given index.
     * This NotebookPage belongs to the Tab on the TabFolder with the
     * same index.
     *
     * @param index the index for the NotebookPage.
     * @return the NotebookPage for that index.
     * @throws IndexOutOfBoundsException if the index is out of range.
     */
    public NotebookPage getPage(int index) {
        return (NotebookPage) pages.get(index);
    }

    /**
     * Adds a NotebookPage to the existing NotebookPages of this Notebook.
     * The new NotebookPage is appended at the end of the list.
     *
     * @param page the NotebookPage to add.
     */
    public void addPage(NotebookPage page) {
        addPage(pages.size(),page);
    }

    /**
     * Adds a NotebookPage to the existing NotebookPages at the given index.
     * The new NotebookPage is insertet at the given index position.
     *
     * @param index the index at which the new NotebookPage is to be inserted.
     * @param page the NotebookPage to add.
     */
    public void addPage(int index, NotebookPage page) {
        pages.add(index, page);
        page.setDialog(parent.getDialog());
        if(parent.isEventsEnabled()) {
            page.setEventsEnabled(true);
        }
        if(parent.getWidgets()!=null) {
            page.createTabItem(tabFolder,index);
            page.initGUI();
        }
        if(getActivePage()==page && fullInitialized) {
            if(!activePage.hasModels()) {
                activePage.createModels();
            }
            if(!activePage.isUIAttached()) {
                activePage.attachUI();
            }
            page.enterBase();
            page.stateChangedBase();
        }
    }

    /**
     * Removes a NotebookPage from the exisiting NotebookPages of this Notebook.
     * @param page the NotebookPage to remove.
     */
    public void removePage(NotebookPage page) {
        int index = pages.indexOf(page);
        if(pages.remove(page)) {
            if(fullInitialized && getActivePage()==page) {
                page.leaveBase();
                activePage=null;
            }
            if(parent.getWidgets()!=null) {
                Composite comp = page.getComposite();
                page.removeWidgetsBase();
                tabFolder.getItem(index).dispose(); // triggers SelectionEvent on Tabfolder
                comp.dispose();
            }
            page.setDialog(null);
            if(parent.isEventsEnabled()) {
                page.setEventsEnabled(false);
            } else if(activePage==null&&!pages.isEmpty()) {
                // if events are disabled, the SelectionEvent was ignored
                // and we have to switch pages manually
                changeToPage((NotebookPage) pages.get(tabFolder.getSelectionIndex()));
            }
        }
    }

    /**
     * Activates the NotebookPage with the given index. The new active
     * PageClient will become visible inside the Notebook. This implies selecting
     * the corresponding Tab on the TabFolder.
     *
     * @param index the index of the NotebookPage to activate.
     * @throws IndexOutOfBoundsException if the index is out of range.
     */
    public void setActivePage(int index) {
        NotebookPage page = (NotebookPage) pages.get(index);
        tabFolder.setSelection(index); // does not cause the SWT-Event
        changeToPage(page);
    }

    /**
     * Activates the  given NotebookPage. The new active
     * PageClient will become visible inside the Notebook. This implies selecting
     * the corresponding Tab on the TabFolder.
     *
     * @param page the NotebookPage to activate.
     * @throws IllegalArgumentException if the NotebookPage is not part
     *      of the Notebook.
     */
    public void setActivePage(NotebookPage page) {
        int index = pages.indexOf(page);
        if (index >= 0) {
            tabFolder.setSelection(index); // does not cause the SWT-Event
            changeToPage(page);
        } else {
            throw new IllegalArgumentException(
                "NotebookPage not in Notebook: " + page);
        }
    }

    /**
     * Gets the active NotebookPage. The active NotebookPage is the NotebookPage
     * that is visible inside the Notebook.
     *
     * @return the currently active NotebookPage.
     */
    public NotebookPage getActivePage() {
        if (activePage == null && pages.size()>0) activePage = (NotebookPage) pages.get(0);
        return activePage;
    }

    /**
     * Gets the active NotebookPage. The active NotebookPage is the NotebookPage
     * that is visible inside the Notebook.
     *
     * @return the currently active NotebookPage.
     */
    public int getActivePageIndex() {
        return pages.indexOf(getActivePage());
    }


    /**
     * Eventhandler called by SWT every time a Tab in the TabFolder is selected by the GUI.
     * It is not called if Tabfolder.setSelection() is called programatically.
     * This causes a switch of the active NotebookPage. The currently
     * active NotebookPage is deactivated. The NotebookPage corresponding
     * to the selected Tab is activated.
     *
     * @param event the SWT-Event that happended.
     */
    public void widgetSelected(SelectionEvent event) {
        if(!parent.isEventsEnabled()) return;
        if(fullInitialized){//only call close() if fullInitialized is true
            if(activePage!=null&&!activePage.close()){
                int index = pages.indexOf(activePage);  //only change the page if close() returns true (default)
                tabFolder.setSelection(index); // does not cause the SWT-Event
                return;
            }
        }

        IDialogPage dialog = parent.getDialogPage();
        try {
            dialog.setEventsEnabled(false);
            changeToPage((NotebookPage)event.item.getData());
            if(fullInitialized) {
                parent.getDialog().stateChangedBase();
                parent.getDialog().updateErrorStatus(event.widget);
            }
        } catch(Exception exc) {
            parent.showException(exc);
        } finally {
            dialog.setEventsEnabled(true);
        }
    }

    /**
     * Does the actual switch of the active NotebookPage. It calls leave(),
     * enter() and the other necessary methods.
     * @param page the new active page.
     */
    private void changeToPage(NotebookPage page) {
        if(fullInitialized) {
            if(activePage!=null) activePage.leaveBase();
            activePage = page;
            if(activePage!=null) {
                if(!activePage.hasModels()) {
                    activePage.createModels();
                }
                if(!activePage.isUIAttached()) {
                    activePage.attachUI();
                }
                activePage.enterBase();
            }
        } else {
            activePage = page;
        }
    }

    /**
     * Only needed because of the implementet Interface SelectionListener. Not used.
     */
    public void widgetDefaultSelected(SelectionEvent event) {
    }

    /**
     * Eventhandler called by SWT every time F1 is pressed inside the tabfolder.
     * Shows the help-uri of the active NotebookPage in a browser window.
     *
     * @param event the SWT-Event that happened.
     */
    public void helpRequested(HelpEvent event) {
        if(fullInitialized) {
            if(activePage!=null) {
                activePage.helpEvent(event,SWT.Help);
            } else {
                parent.helpEvent(event,SWT.Help);
            }
        }
    }

    /**
     * Sets the containing dialog of the PageClient by calling
     * {@link NotebookPage#setDialog(IDialog)} on all NotebookPages.
     */
    public void setDialog(IDialog dialog) {
        for (Iterator it = pages.iterator(); it.hasNext();) {
            ((NotebookPage) it.next()).setDialog(dialog);
        }
    }

    /**
     * Gets the containing dialog of the PageClient by calling
     * {@link NotebookPage#getDialog()} on the active NotebookPage.
     */
    public IDialog getDialog() {
        return getActivePage().getDialog();
    }


    /**
     * creates the corresponding TabFolder
     */
    private void createTabFolder(Composite parentComp) {
        tabFolder = new TabFolder(parentComp, SWT.NONE);
        tabFolder.setData(this);
        tabFolder.addSelectionListener(this);
    }

    /**
     * Creates the Widgets of all NotebookPages by calling
     * {@link NotebookPage#initGUI()} on all NotebookPages.
     */
    public void initGUI() {
        if(tabFolder==null) { createTabFolder(parent.getComposite()); }
        if(getActivePage()!=null && !getActivePage().hasModels()) {
            activePage.createModels();
        }
        for (Iterator it = pages.iterator(); it.hasNext();) {
            ((NotebookPage) it.next()).initGUI();
        }
        if(activePage!=null && !activePage.isUIAttached()) {
            activePage.attachUI();
        }
    }

    /**
     * Notify the active NotebookPage, that the PageClient is becoming visible
     * by calling {@link PageClient#enterBase()} on the active NotebookPage.
     */
    public void enterBase() {
        if(activePage!=null) {
            if(!activePage.hasModels()) {
                activePage.createModels();
            }
            if(!activePage.isUIAttached()) {
                activePage.attachUI();
            }
            activePage.enterBase();
        }
        fullInitialized=true;
    }

    /**
     * Notify the active NotebookPage of a possible Change in the PageModels
     * by calling {@link PageClient#stateChangedBase()} the active NotebookPage.
     */
    public void stateChangedBase() {
        if(activePage!=null) activePage.stateChangedBase();
    }

    /**
     * Delegates the call of {@link at.spardat.xma.page.PageClient#determineStateBase()} to the active page.
     */
    public void determineStateBase() {
        if(activePage!=null) activePage.determineStateBase();
    }

    /**
     * Delegates the call of {@link at.spardat.xma.page.PageClient#stateChangedExtend()} to the active page.
     */
    public void stateChangedExtend() {
        if(activePage!=null) activePage.stateChangedExtend();
    }

    /**
     * Delegates the call of {@link at.spardat.xma.page.PageClient#stateChangedBaseImpl()} to the active page.
     */
    public void stateChangedBaseImpl() {
        if(activePage!=null) activePage.stateChangedBaseImpl();
    }
    
    public void initializePageEffectsBaseImpl () {
        if(activePage!=null) activePage.initializePageEffectsBaseImpl();        
    }

    public void layoutStateChangedBaseImpl () {
        if(activePage!=null) activePage.layoutStateChangedBaseImpl();        
    }

    public void applyPageEffectsBaseImpl () {
        if(activePage!=null) activePage.applyPageEffectsBaseImpl();        
    }

    public void resetLayoutStateBaseImpl () {
        if(activePage!=null) activePage.resetLayoutStateBaseImpl();        
    }    

    /**
     * Notify the active NotebookPage, that the PageClient no longer is visible
     * by calling {@link PageClient#leaveBase()} on the active NotebookPage.
     */
    public void leaveBase() {
        if(activePage!=null) activePage.leaveBase();
    }

    /**
     * Notify all NotebookPages, that the Widgets are disposed
     * by calling {@link NotebookPage#removeWidgets()} on all NotebookPages.
     */
    public void removeWidgetsBase() {
//        if(!tabFolder.isDisposed()) { tabFolder.dispose(); }
        for (Iterator it = pages.iterator(); it.hasNext();) {
            ((NotebookPage) it.next()).removeWidgetsBase();
        }
        tabFolder=null;
        activePage=null;
        fullInitialized=false;
    }

    /**
     * Removes the WidgetModels of all NotebookPages by calling
     * {@link NotebookPage#removeWidgetModels()} on all NotebookPages.
     */
    public void removeModel() {
        for (Iterator it = pages.iterator(); it.hasNext();) {
            ((NotebookPage) it.next()).removeModel();
        }
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.component.IXMAControl#getContextString()
     */
    public String getContextString() {
        return null;
    }

    /**
     * Enables/disables GUI-events all NotebookPages.
     * @param enabled
     */
    public void setEventsEnabled(boolean enabled) {
        for (Iterator it = pages.iterator(); it.hasNext();) {
            ((NotebookPage) it.next()).setEventsEnabled(enabled);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy