at.spardat.xma.gui.mapper.WidgetCreationWiz.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.gui.mapper.WidgetCreationWiz.java

Source

/*******************************************************************************
 * 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: WidgetCreationWiz.java 2106 2007-11-28 16:57:42Z s3460 $
package at.spardat.xma.gui.mapper;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;

import at.spardat.xma.guidesign.GuidesignFactory;
import at.spardat.xma.guidesign.HiddenWidget;
import at.spardat.xma.guidesign.IBDAttachable;
import at.spardat.xma.guidesign.IWidgetWithLabel;
import at.spardat.xma.guidesign.XMAComposite;
import at.spardat.xma.guidesign.XMALabel;
import at.spardat.xma.guidesign.XMASeperator;
import at.spardat.xma.guidesign.XMATable;
import at.spardat.xma.guidesign.XMATableColumn;
import at.spardat.xma.guidesign.XMAWidget;
import at.spardat.xma.guidesign.types.SeperatorType;

/**
 * Wizard to create widgets from BusinessData attributes.
 *
 * @author YSD, 15.07.2003 12:46:59
 */
public class WidgetCreationWiz extends Wizard {

    /**
     * The attributes from which to create widgets.
     */
    private MdlAttribute[] attributes_;
    private boolean createTableCols_;
    private MapperDialog.InsertionPoint insPoint_;
    private MdlRelationships mdlRels_;

    /**
     * Constructor.
     */
    public WidgetCreationWiz(MdlAttribute[] attributes, MapperDialog.InsertionPoint insertionPoint,
            MdlRelationships mdlRels) {
        attributes_ = attributes;
        insPoint_ = insertionPoint;
        createTableCols_ = insertionPoint.parent_ instanceof XMATable;
        mdlRels_ = mdlRels;
        setWindowTitle("Create Widgets From Attributes");
    }

    /**
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    public void addPages() {
        addPage(new WidgetCreationWizPSort());
        addPage(new WidgetCreationWizPProps(createTableCols_));
        super.addPages();
    }

    /**
     * @see org.eclipse.jface.wizard.Wizard#getStartingPage()
     */
    public IWizardPage getStartingPage() {
        if (attributes_.length == 1)
            return getPage("props");
        else
            return super.getStartingPage();
    }

    /**
     * Returns attribute array.
     */
    public MdlAttribute[] getAttributes() {
        return attributes_;
    }

    /**
     * Returns createTableCols_ property
     */
    public boolean getCreateTableCols() {
        return createTableCols_;
    }

    /**
     * Notification from the sort page that attributes_ sort order did change
     */
    public void attributesChanged() {
        WidgetCreationWizPProps page = (WidgetCreationWizPProps) getPage("props");
        page.attributesChanged();
    }

    /**
     * @see org.eclipse.jface.wizard.IWizard#performFinish()
     */
    public boolean performFinish() {
        if (createTableCols_) {
            finishWithTableCols();
        } else {
            finishWithWidgets();
        }
        return true;
    }

    /**
     * Inserts table columns in the XMA model
     */
    private void finishWithTableCols() {
        for (int i = 0, j = insPoint_.childIndex_; i < attributes_.length; i++, j++) {
            XMATable table = (XMATable) insPoint_.parent_;
            XMATableColumn newCol = attributes_[i].getWidgetInitData(true).createXMATableColumn();
            // add column to table
            table.getColumn().add(j, newCol);
            // create relationship
            mdlRels_.add(mdlRels_.new Relationship(newCol, attributes_[i]));
        }
    }

    /**
     * Creates widgets and inserts them into the XMA model
     */
    private void finishWithWidgets() {
        AttachHelper attacher = new AttachHelper();
        XMAComposite comp = (XMAComposite) insPoint_.parent_;

        /**
         * first calculate seperators to use if we have new widget to attach
         */
        if (hasNewAttachableWidgets()) {
            boolean isNewSeperator = attacher.calculateSeperator(insPoint_);
            if (isNewSeperator) {
                /**
                 * inform user, that a separator has been created
                 */
                MessageDialog.openInformation(getShell(), "Separator created",
                        "An artificial, invisible vertical separator named '" + attacher.getSeparatorName()
                                + "' that separates labels from widgets has been created and added to your composite.");
            }
        }

        /**
         * create all widgets
         */
        XMAWidget[] newWidgets = new XMAWidget[attributes_.length];
        for (int i = 0; i < attributes_.length; i++) {
            newWidgets[i] = attributes_[i].getWidgetInitData(false).createWidget();
        }

        /**
         * calculate the index where these widgets must be inserted in the parent composite
         */
        int insertionIndex = 0;
        if (insPoint_.child_ != null) {
            // must be inserted relative to a child
            insertionIndex = getChildIndex(insPoint_.child_);
            if (insertionIndex == -1)
                insertionIndex = 0;
            else {
                if (!insPoint_.before_)
                    insertionIndex++;
                /**
                 * Labels usually appear one slot next to the widget. If this is the case, we increase
                 * insertionIndex to jump over the label.
                 */
                if (insertionIndex < comp.getControls().size()) {
                    if (insPoint_.child_ instanceof IWidgetWithLabel) {
                        if (((IWidgetWithLabel) insPoint_.child_).getLabel() == comp.getControls()
                                .get(insertionIndex)) {
                            insertionIndex++;
                        }
                    }
                }
            }
        }

        /**
         * insert the widgets in the parent composite
         */
        for (int i = newWidgets.length - 1; i >= 0; i--) {
            if (newWidgets[i] instanceof IWidgetWithLabel) {
                XMALabel label = ((IWidgetWithLabel) newWidgets[i]).getLabel();
                if (label != null) {
                    comp.getControls().add(insertionIndex, label);
                    // must call init on newly created widget
                    label.init();
                }

            }
            comp.getControls().add(insertionIndex, newWidgets[i]);
            newWidgets[i].init(); // must call init on newly created widget
        }

        /**
         * attach the widgets correctly
         */
        for (int i = 0; i < newWidgets.length; i++) {
            if (attributes_[i].getWidgetInitData(false).getWType() != MdlAttribute.WIDG_HIDDEN) {
                //only attach non hidden widgets
                attacher.attachFirstStage(newWidgets[i]);
            }
        }

        XMAWidget lastInserted = null;
        for (int i = 0; i < newWidgets.length; i++) {
            if (attributes_[i].getWidgetInitData(false).getWType() != MdlAttribute.WIDG_HIDDEN) {
                //only attach non hidden widgets
                if (lastInserted == null) {
                    // the first widget must be attached depending on the insertion position
                    if (insPoint_.child_ == null || insPoint_.child_ instanceof HiddenWidget) {
                        // widgets should be vertically attached starting at the top edge
                        attacher.attachAtTop(newWidgets[i]);
                    } else if (insPoint_.before_) {
                        // widgets should be vertically attached before an other one
                        attacher.attachVerticallyBefore(newWidgets[i], insPoint_.child_);
                    } else {
                        // widgets should be attached after a specified one
                        attacher.attachVerticallyAfter(newWidgets[i], insPoint_.child_);
                    }
                } else {
                    // all other widgets get attached below the last one attached
                    attacher.attachVerticallyAfter(newWidgets[i], lastInserted);
                }
                lastInserted = newWidgets[i];
            }
        }

        /**
         * finally, create relationships between widgets and attributes
         */
        for (int i = 0; i < newWidgets.length; i++) {
            // create relationship
            mdlRels_.add(mdlRels_.new Relationship((IBDAttachable) newWidgets[i], attributes_[i]));
        }
    }

    private boolean hasNewAttachableWidgets() {
        for (int i = 0; i < attributes_.length; i++) {
            if (attributes_[i].getWidgetInitData(false).getWType() != MdlAttribute.WIDG_HIDDEN) {
                return true;
            }
        }
        return false;
    }

    /**
     * Return the index of the given child within the list of childs of the enclosing composite.
     *
     * @return -1, if the index cannot be computed
     */
    private int getChildIndex(XMAWidget w) {
        XMAComposite c = w.getParentcomp();
        if (c == null)
            return -1;
        Iterator iter = c.getControls().iterator();
        int index = 0;
        while (iter.hasNext()) {
            XMAWidget child = (XMAWidget) iter.next();
            if (w == child)
                return index;
            index++;
        }
        return -1;
    }

}