# HG changeset patch # User Omair Majid # Date 1395088807 14400 # Node ID 1c4f192bf2e2a759a0bb537f5f15677496a02f0c # Parent 07602eec599d21d0bcb775e8e9f39fcf8d537172 Share code for thermostat-plugin.xml creation diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Messages.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Messages.java Fri Mar 14 15:24:53 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Messages.java Mon Mar 17 16:40:07 2014 -0400 @@ -62,12 +62,12 @@ public static String PluginModelReaderWriter_errorLoadingModel; public static String PluginModelReaderWriter_errorSavingFile; public static String PluginXmlCreationWizard_confirmOverwriteMessage; - public static String PluginXmlCreationWizard_confirmOverwriteTitle; - public static String PluginXmlCreationWizard_containerDoesNotExist; - public static String PluginXmlCreationWizard_creatingFile; - public static String PluginXmlCreationWizard_error; - public static String PluginXmlCreationWizard_errorWritingFile; - public static String PluginXmlCreationWizard_openingFileForEditing; + public static String GuiPluginXmlCreator_confirmOverwriteTitle; + public static String PluginXmlCreator_containerDoesNotExist; + public static String GuiPluginXmlCreator_creatingFile; + public static String GuiPluginXmlCreator_error; + public static String GuiPluginXmlCreator_errorWritingFile; + public static String GuiPluginXmlCreator_openingFileForEditing; public static String PluginXmlCreationWizard_windowTitle; public static String PluginXmlProjectSelectionPage_selectFileContainer; public static String PluginXmlProjectSelectionPage_browse; diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties Fri Mar 14 15:24:53 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties Mon Mar 17 16:40:07 2014 -0400 @@ -54,12 +54,12 @@ PluginModelReaderWriter_errorLoadingModel=Unable to load model PluginModelReaderWriter_errorSavingFile=Error saving file PluginXmlCreationWizard_confirmOverwriteMessage=A thermostat-plugin.xml file already exists. Overwrite it? -PluginXmlCreationWizard_confirmOverwriteTitle=Overwrite existing file? -PluginXmlCreationWizard_containerDoesNotExist=Container {0} does not exist. -PluginXmlCreationWizard_creatingFile=Creating {0} -PluginXmlCreationWizard_error=Error -PluginXmlCreationWizard_errorWritingFile=Error writing file -PluginXmlCreationWizard_openingFileForEditing=Opening file for editing... +GuiPluginXmlCreator_confirmOverwriteTitle=Overwrite existing file? +PluginXmlCreator_containerDoesNotExist=Container {0} does not exist. +GuiPluginXmlCreator_creatingFile=Creating plugin descriptor file +GuiPluginXmlCreator_error=Error +GuiPluginXmlCreator_errorWritingFile=Error writing file +GuiPluginXmlCreator_openingFileForEditing=Opening file for editing... PluginXmlCreationWizard_windowTitle=Create Themrostat Plugin PluginXmlProjectSelectionPage_selectFileContainer=Select new file container PluginXmlProjectSelectionPage_browse=Browse... diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/GuiPluginXmlCreator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/GuiPluginXmlCreator.java Mon Mar 17 16:40:07 2014 -0400 @@ -0,0 +1,99 @@ +package com.redhat.thermostat.tools.eclipse.plugin.wizards; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +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.wizards.PluginXmlCreator.Decisions; + +/** + * Wraps a {@link PluginXmlCreator} with GUI logic (including + * progress notification and prompts). + */ +public class GuiPluginXmlCreator { + + public boolean createPluginWithFeedback(final String containerName, final Wizard wizard) { + IRunnableWithProgress op = new IRunnableWithProgress() { + public void run(IProgressMonitor monitor) throws InvocationTargetException { + try { + create(containerName, monitor, wizard.getShell()); + } catch (CoreException e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); + } + } + }; + try { + wizard.getContainer().run(true, false, op); + } catch (InterruptedException e) { + return false; + } catch (InvocationTargetException e) { + Throwable realException = e.getTargetException(); + MessageDialog.openError(wizard.getShell(), Messages.GuiPluginXmlCreator_error, + realException.getMessage()); + return false; + } + return true; + } + + private void create(String containerName, IProgressMonitor monitor, final Shell shell) throws CoreException { + + monitor.beginTask(Messages.GuiPluginXmlCreator_creatingFile, 2); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + PluginXmlCreator creator = new PluginXmlCreator(root); + + try { + final IFile file = creator.create(containerName, monitor, new Decisions() { + @Override + public boolean overwriteExistingFile() { + final boolean[] overwrite = new boolean [1]; + shell.getDisplay().syncExec(new Runnable() { + @Override + public void run() { + overwrite[0] = MessageDialog.openConfirm(shell, Messages.GuiPluginXmlCreator_confirmOverwriteTitle, Messages.PluginXmlCreationWizard_confirmOverwriteMessage); + } + }); + return overwrite[0]; + } + }); + monitor.worked(1); + monitor.setTaskName(Messages.GuiPluginXmlCreator_openingFileForEditing); + shell.getDisplay().asyncExec(new Runnable() { + public void run() { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + try { + IDE.openEditor(page, file, true); + } catch (PartInitException e) { + e.printStackTrace(); + } + } + }); + monitor.worked(1); + } catch (IOException e) { + throwCoreException(Messages.GuiPluginXmlCreator_errorWritingFile, e); + } + } + + private void throwCoreException(String message, Throwable cause) throws CoreException { + IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, cause); + throw new CoreException(status); + } +} diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreationWizard.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreationWizard.java Fri Mar 14 15:24:53 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreationWizard.java Mon Mar 17 16:40:07 2014 -0400 @@ -1,28 +1,12 @@ package com.redhat.thermostat.tools.eclipse.plugin.wizards; +import org.eclipse.jface.viewers.ISelection; 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 { @@ -35,114 +19,23 @@ } @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + this.selection = selection; + + setWindowTitle(Messages.PluginXmlCreationWizard_windowTitle); +} + + @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; + GuiPluginXmlCreator creator = new GuiPluginXmlCreator(); + return creator.createPluginWithFeedback(containerName, this); } - 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()) { - final boolean[] overwrite = new boolean [1]; - getShell().getDisplay().syncExec(new Runnable() { - @Override - public void run() { - overwrite[0] = MessageDialog.openConfirm(getShell(), Messages.PluginXmlCreationWizard_confirmOverwriteTitle, Messages.PluginXmlCreationWizard_confirmOverwriteMessage); - } - }); - if (overwrite[0]) { - file.setContents(stream, true, true, monitor); - } - } else { - file.create(stream, true, monitor); - } - stream.close(); - } catch (IOException e) { - throwCoreException(Messages.PluginXmlCreationWizard_errorWritingFile, 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) { - e.printStackTrace(); - } - } - }); - monitor.worked(1); - } - - private InputStream getInitialContentStream() { - String contents = "\n" //$NON-NLS-1$ - + "\n" //$NON-NLS-1$ $NON-NLS-2$ - + ""; //$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); - } - - private void throwCoreException(String message, Throwable cause) throws CoreException { - IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, cause); - throw new CoreException(status); - } - - @Override - public void init(IWorkbench workbench, IStructuredSelection selection) { - this.selection = selection; - } } diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlCreator.java Mon Mar 17 16:40:07 2014 -0400 @@ -0,0 +1,77 @@ +package com.redhat.thermostat.tools.eclipse.plugin.wizards; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.osgi.util.NLS; + +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 PluginXmlCreator { + + private static final String FILE_NAME = "thermostat-plugin.xml"; //$NON-NLS-1$ + + public interface Decisions { + /** Invoked if a file already exists */ + public boolean overwriteExistingFile(); + } + + private IWorkspaceRoot root; + + public PluginXmlCreator(IWorkspaceRoot root) { + this.root = root; + } + + public IFile create(String projectName, IProgressMonitor monitor, Decisions decisions) throws CoreException, IOException { + final IFile file = getFileFromProject(projectName); + try (InputStream stream = getInitialContentStream()) { + if (file.exists()) { + boolean overwrite = decisions.overwriteExistingFile(); + if (overwrite) { + file.setContents(stream, true, true, monitor); + } + } else { + file.create(stream, true, monitor); + } + } + return file; + } + + private IFile getFileFromProject(String projectName) throws CoreException { + IResource resource = root.findMember(new Path(projectName)); + if (!resource.exists() || !(resource instanceof IContainer)) { + throwCoreException(NLS.bind(Messages.PluginXmlCreator_containerDoesNotExist, projectName)); + } + IContainer container = (IContainer) resource; + + return container.getFile(new Path(FILE_NAME)); + } + + private InputStream getInitialContentStream() { + String contents = "\n" //$NON-NLS-1$ + + "\n" //$NON-NLS-1$ $NON-NLS-2$ + + ""; //$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); + } + +} diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlProjectSelectionPage.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlProjectSelectionPage.java Fri Mar 14 15:24:53 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/PluginXmlProjectSelectionPage.java Mon Mar 17 16:40:07 2014 -0400 @@ -8,7 +8,6 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.Path; -import org.eclipse.jface.bindings.keys.KeyStroke; import org.eclipse.jface.fieldassist.ContentProposalAdapter; import org.eclipse.jface.fieldassist.SimpleContentProposalProvider; import org.eclipse.jface.fieldassist.TextContentAdapter; diff -r 07602eec599d -r 1c4f192bf2e2 com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java Fri Mar 14 15:24:53 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizard.java Mon Mar 17 16:40:07 2014 -0400 @@ -1,14 +1,10 @@ package com.redhat.thermostat.tools.eclipse.plugin.wizards; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; -import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; @@ -24,15 +20,13 @@ import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.launching.JavaRuntime; import org.eclipse.jdt.launching.LibraryLocation; -import org.eclipse.jface.viewers.ISelection; 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.ui.IWorkbenchWizard; -public class ThermostatProjectCreationWizard extends Wizard implements - INewWizard { +public class ThermostatProjectCreationWizard extends Wizard implements INewWizard { + private ThermostatProjectCreationWizardPage page; private ThermostatProjectCreationWizardPageTwo pageTwo; private HashMap selectedComponents; @@ -42,8 +36,11 @@ setNeedsProgressMonitor(true); } + public void init(IWorkbench workbench, IStructuredSelection selection) { + setWindowTitle("Create New Thermostat Plugin"); + } + public void addPages() { - setWindowTitle("Create New Thermostat Plugin"); page = new ThermostatProjectCreationWizardPage(); addPage(page); pageTwo = new ThermostatProjectCreationWizardPageTwo(); @@ -148,27 +145,14 @@ ICompilationUnit cu = pack.createCompilationUnit( "ThermostatPlugin.java", buffer.toString(), false, null); - // Create thermostat-plugin.xml in the main project - if (page.getProjectName().equals(projectTitle)) { - final IFile file = project.getFile("thermostat-plugin.xml"); - try { - InputStream stream = openContentStream(); - if (file.exists()) { - file.setContents(stream, true, true, null); - } else { - file.create(stream, true, null); - } - stream.close(); - } catch (IOException e) { - e.printStackTrace(); - } + createThermostatPluginXml(projectTitle); + } + + private void createThermostatPluginXml(String projectName) { + if (page.getProjectName().equals(projectName)) { + GuiPluginXmlCreator creator = new GuiPluginXmlCreator(); + creator.createPluginWithFeedback(projectName, this); } } - private InputStream openContentStream() { - String contents = "<--!Welcome to Thermostat-->"; // Sample content - return new ByteArrayInputStream(contents.getBytes()); - } - public void init(IWorkbench workbench, IStructuredSelection selection) { - } }