at.spardat.xma.gui.projectw.XMAProjectWizzardPage.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.gui.projectw.XMAProjectWizzardPage.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
 *******************************************************************************/

/*
 * Created on 19.09.2003
 *
 *
 *
 */
package at.spardat.xma.gui.projectw;

import java.io.File;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.wizard.WizardPage;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import at.spardat.xma.gui.projectw.template.ProjectTemplateHandler;

/**
 * @author s3595
 *
 * To change the template for this generated type comment go to
 *
 */
public class XMAProjectWizzardPage extends WizardPage {

    private Text webappName;
    private Text pluginName;
    private XMAProjectCreationWizzard wizard;
    private NewXMAJavaProjectCreationWizardPage fJavaPage;

    /**
     * @param pageName
     * @param title
     * @param titleImage
     */
    protected XMAProjectWizzardPage(XMAProjectCreationWizzard wizardInp, String pageName) {
        super(pageName);
        this.wizard = wizardInp;

        fJavaPage = wizard.getFJavaPage();

        setPageComplete(true);

    }

    // wizard.getFMainPage().getProjectName();

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 3;
        layout.verticalSpacing = 9;

        /*
         *  select webappplication name
         */
        Label label = new Label(container, SWT.NULL);
        label.setText("&Web Application Name:");

        webappName = new Text(container, SWT.BORDER | SWT.SINGLE);
        webappName.setText("");
        webappName.setToolTipText("The name of your web application, is also the name of the XMA-Application!");

        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        webappName.setLayoutData(gd);
        webappName.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                dialogChanged();
            }
        });

        label = new Label(container, SWT.NULL);

        /*
         *  select plugin to use
         */
        String defaultPlugin = getDefaultPlugin();
        if (defaultPlugin != null && defaultPlugin.length() > 0) {
            label = new Label(container, SWT.NULL);
            label.setText("&Plugins to use:");

            pluginName = new Text(container, SWT.BORDER | SWT.SINGLE);
            pluginName.setText(defaultPlugin);
            pluginName.setToolTipText("select the plugins web application that should be imported (as a startup)");
            gd = new GridData(GridData.FILL_HORIZONTAL);
            pluginName.setLayoutData(gd);
            pluginName.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialogChanged();
                }
            });

            Button button2 = new Button(container, SWT.PUSH);
            button2.setText("Browse...");
            button2.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    String strFile = handleBrowseFile("C:/spardat/imc/");
                    if (strFile != null)
                        pluginName.setText(strFile);
                }
            });
        }

        dialogChanged();
        setControl(container);
    }

    private final static File IMC_PLUGIN_DIR = new File("C:/spardat/imc/xma");
    private final static File DEFAULT_IMC_PLUGIN = new File(IMC_PLUGIN_DIR,
            "xma_plugins_dummy/99_99_99/plugins.war");
    // TODO: remove all that shit asap!
    private final static File TRAINING_IMC_PLUGIN_DIR = new File("C:/xma_training/spardat/imc/xma");
    private final static File TRAINING_DEFAULT_IMC_PLUGIN = new File(TRAINING_IMC_PLUGIN_DIR,
            "xma_plugins_dummy/99_99_99/plugins.war");

    private String getDefaultPlugin() {
        if (IMC_PLUGIN_DIR.exists())
            return DEFAULT_IMC_PLUGIN.getAbsolutePath();
        if (TRAINING_IMC_PLUGIN_DIR.exists())
            return TRAINING_DEFAULT_IMC_PLUGIN.getAbsolutePath();
        return "";
    }

    private String handleBrowseFile(String strDefault) {

        FileDialog dialog = new FileDialog(getShell());

        dialog.setFileName(strDefault);
        String strSelection = dialog.open();
        return strSelection;
    }

    private void dialogChanged() {

        if (webappName.getText().length() == 0) {
            updateStatus("XMA Webapplication name must be specified");
            return;
        }

        if (pluginName != null) {
            String strPN = pluginName.getText();
            if (strPN.length() == 0) {
                updateStatus("Import Plugins must be specified");
                return;
            }

            if (!(strPN.endsWith(".war") || strPN.endsWith(".WAR"))) {
                updateStatus("Plugins has to be delivered within a WAR-File");
                return;
            }
        }

        updateStatus(null);
    }

    private void updateStatus(String message) {
        setErrorMessage(message);
        setPageComplete(message == null);
    }

    public void performFinish(IProgressMonitor monitor) throws CoreException, InterruptedException {
        try {
            monitor.beginTask("create xma specifics", 3); //$NON-NLS-1$

            IJavaProject prj = fJavaPage.getJavaProject();
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IResource resource = root.findMember(prj.getPath());
            IContainer container = (IContainer) resource;

            IPath location = container.getLocation();

            ProjectTemplateHandler.createProject(location.toString(), webappName.getText(),
                    pluginName != null ? pluginName.getText() : "dummyPlugin.war", container.getName());

            container.refreshLocal(IResource.DEPTH_INFINITE, monitor);

        } finally {
            monitor.done();
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
     */
    public void setVisible(boolean visible) {

        webappName.setText(wizard.getFMainPage().getProjectName());

        super.setVisible(visible);
    }

}