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

Java tutorial

Introduction

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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.wizards.ClassPathDetector;
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;

import at.spardat.xma.gui.projectw.cp.ProjectClassPathHandler;

/**
 * As addition to the JavaCapabilityConfigurationPage, the wizard does an
 * early project creation (so that linked folders can be defined) and, if an
 * existing external location was specified, offers to do a classpath detection
 */
public class NewXMAJavaProjectCreationWizardPage extends JavaCapabilityConfigurationPage {

    private WizardNewProjectCreationPage fMainPage;
    //    private NewJavaProjectWizardPage fJavaPage2;

    private IPath fCurrProjectLocation;
    protected IProject fCurrProject;

    protected boolean fCanRemoveContent;

    /**
     * Constructor for NewProjectCreationWizardPage.
     */
    public NewXMAJavaProjectCreationWizardPage(WizardNewProjectCreationPage mainPage) {
        super();
        setPageComplete(false);

        fMainPage = mainPage;
        fCurrProjectLocation = null;
        fCurrProject = null;
        fCanRemoveContent = false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
     */
    public void setVisible(boolean visible) {
        if (visible) {
            changeToNewProject();
        } else {
            removeProject();
        }
        super.setVisible(visible);
    }

    private void changeToNewProject() {
        IProject newProjectHandle = fMainPage.getProjectHandle();
        IPath newProjectLocation = fMainPage.getLocationPath();

        if (fMainPage.useDefaults()) {
            fCanRemoveContent = !newProjectLocation.append(fMainPage.getProjectName()).toFile().exists();
        } else {
            fCanRemoveContent = !newProjectLocation.toFile().exists();
        }

        final boolean initialize = !(newProjectHandle.equals(fCurrProject)
                && newProjectLocation.equals(fCurrProjectLocation));

        IRunnableWithProgress op = new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    updateProject(initialize, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };

        try {
            getContainer().run(false, true, op);
        } catch (InvocationTargetException e) {
            String title = "Error";
            String message = "Problems invoking the wizard";
            ExceptionHandler.handle(e, getShell(), title, message);
        } catch (InterruptedException e) {
            // cancel pressed
        }
    }

    protected void updateProject(boolean initialize, IProgressMonitor monitor)
            throws CoreException, InterruptedException {
        fCurrProject = fMainPage.getProjectHandle();
        fCurrProjectLocation = fMainPage.getLocationPath();
        boolean noProgressMonitor = !initialize && fCanRemoveContent;

        if (monitor == null || noProgressMonitor) {
            monitor = new NullProgressMonitor();
        }
        try {
            monitor.beginTask("create Project", 2); //$NON-NLS-1$

            createProject(fCurrProject, fCurrProjectLocation, new SubProgressMonitor(monitor, 1));
            if (initialize) {
                IClasspathEntry[] entries = null;
                IPath outputLocation = null;
                ClassPathDetector detector = null;

                if (fCurrProjectLocation.toFile().exists()
                        && !Platform.getLocation().equals(fCurrProjectLocation)) {
                    // detect classpath
                    if (!fCurrProject.getFile(".classpath").exists()) { //$NON-NLS-1$
                        // if .classpath exists noneed to look for files
                        detector = new ClassPathDetector(fCurrProject, monitor);
                        outputLocation = detector.getOutputLocation();
                    }
                }

                IJavaProject prj = JavaCore.create(fCurrProject);
                entries = ProjectClassPathHandler.createClassPath(detector, prj, fMainPage.getProjectName());

                init(prj, outputLocation, entries, false);
                prj.makeConsistent(new SubProgressMonitor(monitor, 2));
            }
            monitor.worked(2);
        } finally {
            monitor.done();
        }
    }

    /**
     * Called from the wizard on finish.
     */
    public void performFinish(IProgressMonitor monitor) throws CoreException, InterruptedException {
        try {
            monitor.beginTask("project creation finished", 3); //$NON-NLS-1$
            if (fCurrProject == null) {
                updateProject(true, new SubProgressMonitor(monitor, 1));
            }

            configureJavaProject(new SubProgressMonitor(monitor, 2));

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            monitor.done();
            fCurrProject = null;
        }
    }

    private void removeProject() {
        if (fCurrProject == null || !fCurrProject.exists()) {
            return;
        }

        IRunnableWithProgress op = new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                boolean noProgressMonitor = Platform.getLocation().equals(fCurrProjectLocation);
                if (monitor == null || noProgressMonitor) {
                    monitor = new NullProgressMonitor();
                }
                monitor.beginTask("remove project", 3); //$NON-NLS-1$

                try {
                    fCurrProject.delete(fCanRemoveContent, false, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                } finally {
                    monitor.done();
                    fCurrProject = null;
                    fCanRemoveContent = false;
                }
            }
        };

        try {
            getContainer().run(false, true, op);
        } catch (InvocationTargetException e) {
            String title = "Error";
            String message = "Problems invoking the wizard"; //$NON-NLS-1$
            ExceptionHandler.handle(e, getShell(), title, message);
        } catch (InterruptedException e) {
            // cancel pressed
        }
    }

    /**
     * Called from the wizard on cancel.
     */
    public void performCancel() {
        removeProject();
    }
}