com.htmlhifive.tools.wizard.ui.ProjectCreationWizard.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.wizard.ui.ProjectCreationWizard.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.htmlhifive.tools.wizard.ui;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.wst.jsdt.internal.ui.wizards.JavaProjectWizard;

import com.htmlhifive.tools.wizard.H5WizardPlugin;
import com.htmlhifive.tools.wizard.download.DownloadModule;
import com.htmlhifive.tools.wizard.library.xml.BaseProject;
import com.htmlhifive.tools.wizard.library.xml.Nature;
import com.htmlhifive.tools.wizard.log.PluginLogger;
import com.htmlhifive.tools.wizard.log.PluginLoggerFactory;
import com.htmlhifive.tools.wizard.log.ResultStatus;
import com.htmlhifive.tools.wizard.log.messages.Messages;
import com.htmlhifive.tools.wizard.ui.page.ConfirmLicensePage;
import com.htmlhifive.tools.wizard.ui.page.LibraryImportPage;
import com.htmlhifive.tools.wizard.ui.page.StructureSelectPage;
import com.htmlhifive.tools.wizard.utils.H5IOUtils;
import com.htmlhifive.tools.wizard.utils.H5LogUtils;

/**
 * hifive ?.
 * 
 * @author fkubo
 */
// public class ProjectCreationWizard extends BasicNewResourceWizard {
public class ProjectCreationWizard extends JavaProjectWizard {
    /** . */
    private static PluginLogger logger = PluginLoggerFactory.getLogger(ProjectCreationWizard.class);

    /**
     * .
     */
    // private WizardNewProjectCreationPage mainPage;

    /** ?. */
    private LibraryImportPage libraryImportPage;

    /** . */
    private StructureSelectPage structureSelectPage;

    /** ?. */
    private ConfirmLicensePage confirmLicensePage;

    // /** ?. */
    // private ResultPage resultPage;

    /** . */
    private final DownloadModule downloadModule;

    /**
     * .
     */
    public ProjectCreationWizard() {

        super();

        logger.log(Messages.TR0031, getClass().getSimpleName(), "<init>");

        downloadModule = new DownloadModule();
    }

    @Override
    public void dispose() {

        logger.log(Messages.TR0031, getClass().getSimpleName(), "dispose");

        downloadModule.close();
        super.dispose();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addPages() {

        logger.log(Messages.TR0031, getClass().getSimpleName(), "addPages");

        structureSelectPage = new StructureSelectPage("structureSelectPage");
        addPage(structureSelectPage);

        libraryImportPage = new LibraryImportPage("libraryImportPage");
        addPage(libraryImportPage);

        confirmLicensePage = new ConfirmLicensePage("confirmLicensePage");
        addPage(confirmLicensePage);

        // first, second .
        super.addPages();

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean performFinish() {

        logger.log(Messages.TR0031, getClass().getSimpleName(), "performFinish");

        // ??.
        // getContainer().showPage(resultPage);

        if (!super.performFinish()) {
            // ?.;
            return false;
        }

        final ResultStatus logger = new ResultStatus();

        try {
            // ZIP.
            final IRunnableWithProgress runnable = getExtractRunnnable(logger);
            getContainer().run(false, false, runnable);

        } catch (InvocationTargetException e) {
            final Throwable ex = e.getTargetException();
            // SE0023=ERROR,????????
            logger.log(ex, Messages.SE0023, "");

            // We were cancelled...
            removeProject(logger);
            return false;
        } catch (InterruptedException e) {
            logger.setInterrupted(true);

            // We were cancelled...
            removeProject(logger);
            return false;
        } finally {
            // ?.
            logger.showDialog(Messages.PI0137);
        }

        // Wizard????????.
        //return logger.isSuccess();
        return true;
    }

    /**
     * ???.
     * 
     * @param logger 
     */
    private void removeProject(ResultStatus logger) {

        logger.log(Messages.TR0031, getClass().getSimpleName(), "removeProject");

        final IProject proj = structureSelectPage.getProjectHandle();
        if (proj != null && proj.exists()) {
            try {
                logger.log(Messages.SE0105, proj.getName());
                proj.delete(true, true, null);
                logger.log(Messages.SE0106, proj.getName());
            } catch (CoreException ex) {
                // SE0100=INFO,({0})???????
                logger.log(ex, Messages.SE0100, proj.getName());
            }
        }
    }

    /**
     * Nature?.
     * 
     * @param project 
     * @param monitor ?
     * @param natureId NatureID
     * @throws CoreException 
     */
    public static void addNature(IProject project, IProgressMonitor monitor, String natureId) throws CoreException {

        if (monitor != null && monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        if (!project.hasNature(natureId)) {
            IProjectDescription description = project.getDescription();
            String[] prevNatures = description.getNatureIds();
            String[] newNatures = new String[prevNatures.length + 1];
            System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
            newNatures[prevNatures.length] = natureId;
            description.setNatureIds(newNatures);
            project.setDescription(description, monitor);
        }
    }

    /**
     * ???Runnable ?.
     * 
     * @return ???Runnable.
     */
    private IRunnableWithProgress getExtractRunnnable(final ResultStatus logger) {

        return new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {

                if (monitor == null) {
                    // ?.
                    monitor = new NullProgressMonitor();
                }

                // ??.
                BaseProject baseProject = structureSelectPage.getBaseProject();
                if (baseProject == null) {
                    // ??.
                    H5LogUtils.putLog(null, Messages.SE0048);
                    //H5LogUtils.showLog(null, Messages.SE0043, Messages.SE0048);
                    logger.setSuccess(false);
                    return;
                }

                // .
                monitor.beginTask(Messages.PI0101.format(), 10000);

                // nature, project-download, zip-extract, replace, reflesh

                // create the project
                try {
                    final IProject proj = structureSelectPage.getProjectHandle();

                    // .
                    proj.setDefaultCharset("UTF-8", monitor);

                    // ?(Core???).
                    downloadModule.downloadProject(monitor, 2000, logger, baseProject, proj); // 2000

                    // SE0061=INFO,?????
                    logger.log(Messages.SE0061);
                    monitor.worked(1000); //  1000

                    // ?.
                    for (com.htmlhifive.tools.wizard.library.xml.File file : baseProject.getReplace().getFile()) {
                        H5IOUtils.convertProjectName(getShell(), proj, file.getName());
                        // SE0069=INFO,({0})???????
                        logger.log(Messages.SE0069, file.getName());
                    }

                    // Nature.
                    if (baseProject.getNatures() != null) {
                        for (Nature nature : baseProject.getNatures().getNature()) {
                            try {
                                // SE0065=INFO,Nature{0}???
                                logger.log(Messages.SE0065, nature.getId());

                                addNature(proj, monitor, nature.getId());

                                // SE0066=INFO,Nature{0}????
                                logger.log(Messages.SE0066, nature.getId());
                            } catch (CoreException e) {

                                // ?????.
                                // SE0067=INFO,Nature{0}?????
                                logger.logIgnoreSetSuccess(e, Messages.SE0067, nature.getId());

                                // SE0031=ERROR,???????????name={0}, natureId={1}
                                //H5LogUtils.putLog(e, Messages.SE0031, nature.getName(), nature.getId());
                                H5LogUtils.showLog(e, Messages.SE0032, Messages.SE0031, nature.getName(),
                                        nature.getId());
                            }
                        }
                    }
                    monitor.worked(1000);

                    // SE0062=INFO,????????
                    logger.log(Messages.SE0062);

                    // ?.
                    final IProject project = structureSelectPage.getProjectHandle();

                    // ?
                    downloadModule.downloadLibrary(monitor, 5000, logger,
                            H5WizardPlugin.getInstance().getSelectedLibrarySortedSet(), project); // 5000

                    // ???.
                    project.refreshLocal(IResource.DEPTH_ONE, monitor);
                    // SE0104=INFO,????
                    logger.log(Messages.SE0104);
                    monitor.worked(1000);

                } catch (OperationCanceledException e) {
                    // ??.
                    throw new InterruptedException(e.getMessage());
                } catch (CoreException e) {
                    // SE0023=ERROR,????????
                    logger.log(e, Messages.SE0023, "");
                    throw new InvocationTargetException(e, Messages.SE0023.format());
                } finally {
                    monitor.done();
                }
            }
        };

    }
}