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

Java tutorial

Introduction

Here is the source code for at.spardat.xma.gui.mapper.BDInstanceSelectionDialog.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: BDInstanceSelectionDialog.java 6339 2010-07-29 15:06:02Z laslovd $
package at.spardat.xma.gui.mapper;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionDialog;

import at.spardat.xma.guidesign.XMAComponent;

/**
 *
 * @author YSD, 11.07.2003 11:08:00
 */
public class BDInstanceSelectionDialog extends TitleAreaDialog {

    private XMAComponent xmaC_;
    private IJavaProject jp_;
    private Validator validator_;

    private String resultName_;
    private String resultType_;

    Label separator_;
    Label instanceNameL_;
    Text instanceName_;
    Label classNameL_;
    Text className_;
    Button browse_;

    private int mode_;

    public static final int MODE_NEW = 0;

    public static final int MODE_EDIT = 1;

    public BDInstanceSelectionDialog(Shell parent, int mode, XMAComponent xmaC, IJavaProject jp, Validator v) {
        super(parent);
        mode_ = mode;
        xmaC_ = xmaC;
        jp_ = jp;
        validator_ = v;
    }

    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        if (mode_ == MODE_NEW)
            shell.setText("Create new BusinessData Instance");
        else
            shell.setText("Edit BusinessData Instance");
    }

    /**
     * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    protected Control createDialogArea(Composite parent) {

        Composite myComposite = new Composite(parent, SWT.NULL);
        GridData gd = new GridData(GridData.FILL_BOTH);
        myComposite.setLayoutData(gd);

        FormLayout layout = new FormLayout();
        layout.marginHeight = 10;
        layout.marginWidth = 10;
        myComposite.setLayout(layout);

        FormData data;
        Control[] tabOrder;
        int SPLIT = 80;

        instanceName_ = new Text(myComposite, SWT.BORDER);
        instanceName_.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                stateChanged(true);
            }
        });
        instanceNameL_ = new Label(myComposite, SWT.NULL);
        instanceNameL_.setText("Name:");

        className_ = new Text(myComposite, SWT.BORDER);
        className_.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                stateChanged(true);
            }
        });
        classNameL_ = new Label(myComposite, SWT.NULL);
        classNameL_.setText("Class:");

        browse_ = new Button(myComposite, SWT.PUSH);
        browse_.setText("Browse...");
        browse_.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                browseButtonSelected();
            }
        });

        separator_ = new Label(myComposite, SWT.SEPARATOR | SWT.HORIZONTAL);

        // instanceName_
        data = new FormData();
        data.left = new FormAttachment(0, SPLIT);
        data.right = new FormAttachment(100, 0);
        data.width = 300;
        instanceName_.setLayoutData(data);

        // instanceNameL_
        data = new FormData();
        data.top = new FormAttachment(instanceName_, 0, SWT.CENTER);
        instanceNameL_.setLayoutData(data);

        // className_
        data = new FormData();
        data.left = new FormAttachment(0, SPLIT);
        data.right = new FormAttachment(browse_, -5, SWT.LEFT);
        data.top = new FormAttachment(instanceName_, 3);
        className_.setLayoutData(data);

        // classNameL_
        data = new FormData();
        data.top = new FormAttachment(className_, 0, SWT.CENTER);
        classNameL_.setLayoutData(data);

        // browse_
        data = new FormData();
        data.top = new FormAttachment(instanceName_, 3);
        data.right = new FormAttachment(100, 0);
        browse_.setLayoutData(data);

        // separator_
        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        separator_.setLayoutData(data);

        //        tabOrder = new Control[3];
        //        tabOrder[1] = newBDW;
        //        tabOrder[2] = bdDeleteW;
        //        myComposite.setTabList (tabOrder);

        setMessage("Select a class derived from BusinessData and give it a unique name.");

        return myComposite;
    }

    /**
     * Adds buttons to this dialog's button bar.
     */
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
    }

    /**
     * @see org.eclipse.jface.window.Window#setShellStyle(int)
     */
    protected void setShellStyle(int newShellStyle) {
        super.setShellStyle(SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.RESIZE | SWT.TITLE);
    }

    public int open() {
        create();
        Shell shell = getShell();
        stateChanged(false);
        return super.open();
    }

    private void browseButtonSelected() {
        ProgressMonitorDialog pmd = new ProgressMonitorDialog(getShell());
        SelectionDialog selDialog = null;
        try {
            selDialog = JavaUI.createTypeDialog(getShell(), pmd, SearchEngine.createWorkspaceScope(),
                    IJavaElementSearchConstants.CONSIDER_CLASSES, false);
            selDialog.setTitle("Select BusinessData");
        } catch (Exception ex) {
            setErrorMessage(ex.toString());
        }
        if (selDialog != null) {
            if (selDialog.open() == IDialogConstants.CANCEL_ID)
                return;
            Object[] types = selDialog.getResult();
            if (types == null || types.length == 0)
                return;
            IType selectedType = (IType) types[0];
            className_.setText(selectedType.getFullyQualifiedName());
        }
    }

    /**
     * Checks for valid input on every change on this UI;
     */
    private void stateChanged(boolean updateMessage) {
        try {
            String error = validator_.validateState(this, instanceName_.getText(), className_.getText());
            if (updateMessage)
                setErrorMessage(error);
            getOKButton().setEnabled(error == null);
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        }
    }

    protected void okPressed() {
        String error = validator_.validateResult(this, instanceName_.getText(), className_.getText());
        if (error == null) {
            resultName_ = instanceName_.getText();
            resultType_ = className_.getText();
            super.okPressed();
        } else {
            setErrorMessage(error);
        }
    }

    public String getResultName() {
        return resultName_;
    }

    public String getResultType() {
        return resultType_;
    }

    /**
     * Returns one of the <tt>MODE_</tt>-constants.
     */
    public int getMode() {
        return mode_;
    }

    /**
     * Client who calls this dialog must provide a validator to validate the input
     *
     * @author YSD, 12.07.2003 09:51:21
     */
    public interface Validator {

        /**
         * Called frequently on every state change. Must return non null
         * error message if input is illegal, or null if ok.
         */
        public String validateState(BDInstanceSelectionDialog caller, String name, String type);

        /**
         * This method is called if the user is about to press ok. It must return
         * a non null error string, if the input is not accepted. Otherwise,
         * null should be returned and the input will be accepted, i.e., the dialog
         * is closed.
         */
        public String validateResult(BDInstanceSelectionDialog caller, String name, String type);
    }

}