at.spardat.xma.guidesign.presentation.dialog.XMADialog.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.guidesign.presentation.dialog.XMADialog.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
 *******************************************************************************/

/*
 * @(#)
 *
 *
 *
 *
 *
 */
package at.spardat.xma.guidesign.presentation.dialog;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import at.spardat.xma.guidesign.plugin.GUIDesignerPlugin;
import at.spardat.xma.guidesign.plugin.GUIDesignerPlugin.Implementation;
import at.spardat.xma.guidesign.presentation.GuidesignEditor;

/**
 *
 *
 * @author s1462
 * @since guidesign.editor 0.9
 * @modified
 */
public abstract class XMADialog extends TitleAreaDialog implements IValidCompositeListener, SelectionListener {

    protected ILabelProvider lblProvider;
    protected IContentProvider cntProvider;
    protected EObject object;
    protected EReference feature;
    protected String name;
    protected EObject result;
    /**
      * the composite with formlayout, that hold all content of the dialog
     */
    protected Composite content;

    /**
     * This keeps track of the editing domain that is used to track all changes to the model.
     */
    protected AdapterFactoryEditingDomain editingDomain;

    /**
     * This is the one adapter factory used for providing views of the model.
     */
    protected ComposedAdapterFactory adapterFactory;

    /**
     * Holds a reference to the active editor
     */
    protected GuidesignEditor editor;

    /**
     *
     * @param _parent
     * @param _lblProvider
     * @param _obj
     * @param _feature
     * @param _name
     */
    public XMADialog(Shell shell, EObject _object, EReference _feature, String _name, GuidesignEditor _editor) {
        super(shell);
        object = _object;
        feature = _feature;
        name = _name;
        this.editor = _editor;
        if (editor != null) {
            editingDomain = (AdapterFactoryEditingDomain) editor.getEditingDomain();
            adapterFactory = (ComposedAdapterFactory) editor.getAdapterFactory();
        }
        lblProvider = new AdapterFactoryLabelProvider(adapterFactory);
    }

    /**
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    protected void configureShell(Shell shell, int width, int height) {
        super.configureShell(shell);
        if (width != 0 && height != 0)
            shell.setSize(width, height);
    }

    /**
     * This method supplies the container, where the dialog controls has to
     * be placed with a form layout.
     * @param parent Composite from the Dialog.createDialogArea
     * @return the container
     */
    private void initDialogArea(Composite parent) {
        Composite outerContent = (Composite) super.createDialogArea(parent);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;

        content = new Composite(outerContent, SWT.NONE);
        content.setLayoutData(gridData);

        FormLayout layout = new FormLayout();
        layout.marginWidth = 3;
        layout.marginHeight = 3;
        content.setLayout(layout);
    }

    /**
      * This method should be overriden with call super.createDialogArea on 1. place
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    protected Control createDialogArea(Composite parent) {
        initDialogArea(parent);
        return content;
    }

    private void setDialogSettings() {
        // save location (on close)
        Implementation plugin = GUIDesignerPlugin.getPlugin();
        Preferences preferences = plugin.getPluginPreferences();
        preferences.setValue(this.getClass().getName() + ".location.x", getShell().getLocation().x);
        preferences.setValue(this.getClass().getName() + ".location.y", getShell().getLocation().y);
        plugin.savePluginPreferences();
    }

    /**
     * @see org.eclipse.jface.window.Window#close()
     */
    public boolean close() {
        setDialogSettings();
        return super.close();
    }

    /**
     * Has to be overriden!
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    protected void okPressed() {
        super.okPressed();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.window.Window#constrainShellSize()
     */
    protected void constrainShellSize() {
        super.constrainShellSize();
        // retrieve location settings
        Implementation plugin = GUIDesignerPlugin.getPlugin();
        Preferences preferences = plugin.getPluginPreferences();
        int x = Integer.MIN_VALUE;
        int y = Integer.MIN_VALUE;
        try {
            x = preferences.getInt(this.getClass().getName() + ".location.x");
            y = preferences.getInt(this.getClass().getName() + ".location.y");
        } catch (Exception ex) {
            GUIDesignerPlugin.INSTANCE.log(ex);
        }
        ;
        if (x != Integer.MIN_VALUE && y != Integer.MIN_VALUE) {
            // set location
            getShell().setLocation(x, y);
        }
    }

    /**
     * If all neccassary input fields contains valid content returns true and
     * the ok Button is enabled.
     * @return true, if all neccassary input fields contains valid content
     */
    abstract protected boolean isDialogComplete();

    /**
     * Implements the modify listener which has to handle all modify events of the dialogs content.
     */
    public void modifyText(ModifyEvent event) {
        handleDialogAreaEvents();
    }

    /**
     * Implements the selection listener which has to handle all selection events of the dialogs content.
     */
    public void widgetSelected(SelectionEvent event) {
        handleDialogAreaEvents();
    }

    /**
     * Implements the selection listener which has to handle all selection events of the dialogs content.
     */
    public void widgetDefaultSelected(SelectionEvent event) {
        handleDialogAreaEvents();
    }

    /**
     * Central method that handles all events occured in the DialogArea
     *
     */
    private void handleDialogAreaEvents() {
        setDialogButtons();
    }

    /**
     * set the dialog buttons dependend of the dialog complete state
     *
     */
    protected void setDialogButtons() {
        if (isDialogComplete()) {
            if (getButton(IDialogConstants.OK_ID) != null)
                getButton(IDialogConstants.OK_ID).setEnabled(true);
        } else {
            if (getButton(IDialogConstants.OK_ID) != null)
                getButton(IDialogConstants.OK_ID).setEnabled(false);
        }
    }

    /**
     * Get Strings from plugin.properties
     * @param propKey property key
     * @return the string from the resource bundle
     */
    protected String getText(String propKey) {
        return GUIDesignerPlugin.INSTANCE.getString(propKey);
    }

    /**
     * Get Strings from plugin.properties
     * @param propKey property key
     * @param substitution for the protperty value
     * @return the string from the resource bundle
     */
    protected String getText(String propKey, Object[] substitution) {
        return GUIDesignerPlugin.INSTANCE.getString(propKey, substitution);
    }
}