view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreationWizard.java @ 102:e8210c93bfa9

Fix a message with parameter
author Omair Majid <omajid@redhat.com>
date Fri, 07 Feb 2014 16:07:53 -0500
parents 146a0704ba68
children 9cb641d3e236
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.wizards;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.*;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;

import java.io.*;

import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;

import com.redhat.thermostat.tools.eclipse.plugin.Activator;
import com.redhat.thermostat.tools.eclipse.plugin.Messages;
import com.redhat.thermostat.tools.eclipse.plugin.model.SchemaConstants;

public class PluginXmlCreationWizard extends Wizard implements INewWizard {

    private PluginXmlProjectSelectionPage page;
    private ISelection selection;

    public PluginXmlCreationWizard() {
        super();
        setNeedsProgressMonitor(true);
    }

    @Override
    public void addPages() {
        setWindowTitle(Messages.PluginXmlCreationWizard_windowTitle);
        page = new PluginXmlProjectSelectionPage(selection);
        addPage(page);
    }

    /**
     * This method is called when 'Finish' button is pressed in the wizard. We
     * will create an operation and run it using wizard as execution context.
     */
    @Override
    public boolean performFinish() {
        final String containerName = page.getContainerName();
        final String fileName = "thermostat-plugin.xml"; //$NON-NLS-1$
        IRunnableWithProgress op = new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor)
                    throws InvocationTargetException {
                try {
                    doFinish(containerName, fileName, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                } finally {
                    monitor.done();
                }
            }
        };
        try {
            getContainer().run(true, false, op);
        } catch (InterruptedException e) {
            return false;
        } catch (InvocationTargetException e) {
            Throwable realException = e.getTargetException();
            MessageDialog.openError(getShell(), Messages.PluginXmlCreationWizard_error,
                    realException.getMessage());
            return false;
        }
        return true;
    }

    private void doFinish(String containerName, String fileName,
            IProgressMonitor monitor) throws CoreException {
        // create a sample file
        monitor.beginTask(NLS.bind(Messages.PluginXmlCreationWizard_creatingFile, fileName), 2);
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource resource = root.findMember(new Path(containerName));
        if (!resource.exists() || !(resource instanceof IContainer)) {
            throwCoreException(NLS.bind(Messages.PluginXmlCreationWizard_containerDoesNotExist, containerName));
        }
        IContainer container = (IContainer) resource;
        final IFile file = container.getFile(new Path(fileName));
        try {
            InputStream stream = getInitialContentStream();
            if (file.exists()) {
                // TODO dont replace file without confirming
                file.setContents(stream, true, true, monitor);
            } else {
                file.create(stream, true, monitor);
            }
            stream.close();
        } catch (IOException e) {
        }
        monitor.worked(1);
        monitor.setTaskName(Messages.PluginXmlCreationWizard_openingFileForEditing);
        getShell().getDisplay().asyncExec(new Runnable() {
            public void run() {
                IWorkbenchPage page = PlatformUI.getWorkbench()
                        .getActiveWorkbenchWindow().getActivePage();
                try {
                    IDE.openEditor(page, file, true);
                } catch (PartInitException e) {
                }
            }
        });
        monitor.worked(1);
    }

    private InputStream getInitialContentStream() {
        String contents = "<?xml version=\"1.0\"?>\n" //$NON-NLS-1$
                + "<plugin xmlns=\"" + SchemaConstants.SCHEMA_NAMESPACE + "\"\n" //$NON-NLS-1$ $NON-NLS-2$
                + "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" //$NON-NLS-1$
                + "        xsi:schemaLocation=\"" + SchemaConstants.getSchemaLocation() + "\">\n" //$NON-NLS-1$ $NON-NLS-2$
                + "</plugin>"; //$NON-NLS-1$

        return new ByteArrayInputStream(contents.getBytes());
    }

    private void throwCoreException(String message) throws CoreException {
        IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                IStatus.OK, message, null);
        throw new CoreException(status);
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        this.selection = selection;
    }
}