Bug 508101 - Script context menus do not work correctly

  fixed 'edit'
  removed 'rename'
  removed dead code

Change-Id: Ic9ba5c1a5f9c45d9dfc4fd05f992a6f813152187
diff --git a/plugins/org.eclipse.ease.ui.scripts/plugin.xml b/plugins/org.eclipse.ease.ui.scripts/plugin.xml
index f9fe4d6..274d8a1 100644
--- a/plugins/org.eclipse.ease.ui.scripts/plugin.xml
+++ b/plugins/org.eclipse.ease.ui.scripts/plugin.xml
@@ -72,17 +72,6 @@
       </command>
       <command
             categoryId="org.eclipse.ease.commands.category.script"
-            defaultHandler="org.eclipse.ease.ui.scripts.handler.RenameScript"
-            id="org.eclipse.ease.commands.script.rename"
-            name="Rename Script">
-         <commandParameter
-               id="org.eclipse.ease.commands.script.rename.name"
-               name="Script Name"
-               optional="false">
-         </commandParameter>
-      </command>
-      <command
-            categoryId="org.eclipse.ease.commands.category.script"
             defaultHandler="org.eclipse.ease.ui.scripts.handler.EditScript"
             id="org.eclipse.ease.commands.script.edit"
             name="Edit Script">
@@ -148,7 +137,7 @@
       </menuContribution>
       <menuContribution
             allPopups="false"
-            locationURI="popup:org.eclipse.ease.scripts.properties.allKeywords">
+            locationURI="popup:org.eclipse.ease.scripts.properties.allKeywords?after=additions">
          <command
                commandId="org.eclipse.ease.commands.script.addKeyword"
                icon="icons/elcl16/new_keyword.png"
diff --git a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/EditScript.java b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/EditScript.java
index f3fdca9..147410b 100644
--- a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/EditScript.java
+++ b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/EditScript.java
@@ -25,15 +25,12 @@
 import org.eclipse.ease.ui.scripts.ScriptEditorInput;
 import org.eclipse.ease.ui.scripts.repository.IRepositoryService;
 import org.eclipse.ease.ui.scripts.repository.IScript;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorDescriptor;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IPropertyListener;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.ide.IDE;
 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
 import org.eclipse.ui.texteditor.AbstractTextEditor;
@@ -42,92 +39,90 @@
 public class EditScript extends AbstractHandler implements IHandler {
 
 	public static final String COMMAND_ID = "org.eclipse.ease.commands.script.edit";
+	public static final String PARAMETER_NAME = COMMAND_ID + ".name";
 
 	@Override
 	public Object execute(final ExecutionEvent event) throws ExecutionException {
-		ISelection selection = HandlerUtil.getCurrentSelection(event);
-		if (selection instanceof IStructuredSelection) {
-			for (final Object element : ((IStructuredSelection) selection).toList()) {
-				if (element instanceof IScript) {
-					final Object content = ((IScript) element).getResource();
-					if ((content instanceof IFile) && (((IFile) content).exists())) {
-						// open editor
-						IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-						try {
-							IDE.openEditor(page, (IFile) content);
-						} catch (PartInitException e) {
-							Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
-						}
+		final IScript script = RunScript.getScript(event, PARAMETER_NAME);
 
-					} else if ((content instanceof File) && (((File) content).exists())) {
-						ScriptType type = ((IScript) element).getType();
-						IEditorDescriptor descriptor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor("foo." + type.getDefaultExtension());
-						if (descriptor != null) {
-							IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-							try {
-								final ScriptEditorInput editorInput = new ScriptEditorInput((IScript) element);
-								final IEditorPart editor = page.openEditor(editorInput, descriptor.getId());
+		if (script != null) {
+			final Object content = script.getResource();
+			if ((content instanceof IFile) && (((IFile) content).exists())) {
+				// open editor
+				final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+				try {
+					IDE.openEditor(page, (IFile) content);
+				} catch (final PartInitException e) {
+					Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
+				}
 
-								// editor will not save external script files,
-								// we need to do this on our own
-								editor.addPropertyListener(new IPropertyListener() {
-									@Override
-									public void propertyChanged(final Object source, final int propId) {
+			} else if ((content instanceof File) && (((File) content).exists())) {
+				final ScriptType type = script.getType();
+				final IEditorDescriptor descriptor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor("foo." + type.getDefaultExtension());
+				if (descriptor != null) {
+					final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+					try {
+						final ScriptEditorInput editorInput = new ScriptEditorInput(script);
+						final IEditorPart editor = page.openEditor(editorInput, descriptor.getId());
 
-										// check for changes of PROP_DIRTY from
-										// true to false,
-										// meaning the editor tried to save data
-										if (IEditorPart.PROP_DIRTY == propId) {
-											if ((editor instanceof AbstractDecoratedTextEditor) && (!editor.isDirty())) {
-												final IDocumentProvider documentProvider = ((AbstractTextEditor) editor).getDocumentProvider();
-												final String newSource = documentProvider.getDocument(editorInput).get();
+						// editor will not save external script files,
+						// we need to do this on our own
+						editor.addPropertyListener(new IPropertyListener() {
+							@Override
+							public void propertyChanged(final Object source, final int propId) {
 
-												FileOutputStream outputStream = null;
+								// check for changes of PROP_DIRTY from
+								// true to false,
+								// meaning the editor tried to save data
+								if (IEditorPart.PROP_DIRTY == propId) {
+									if ((editor instanceof AbstractDecoratedTextEditor) && (!editor.isDirty())) {
+										final IDocumentProvider documentProvider = ((AbstractTextEditor) editor).getDocumentProvider();
+										final String newSource = documentProvider.getDocument(editorInput).get();
+
+										FileOutputStream outputStream = null;
+										try {
+											outputStream = new FileOutputStream((File) content);
+											outputStream.write(newSource.getBytes());
+
+										} catch (final Exception e) {
+											Logger.error(Activator.PLUGIN_ID, "Could not store recorded script.", e);
+										} finally {
+											if (outputStream != null) {
 												try {
-													outputStream = new FileOutputStream((File) content);
-													outputStream.write(newSource.getBytes());
-
-												} catch (Exception e) {
-													Logger.error(Activator.PLUGIN_ID, "Could not store recorded script.", e);
-												} finally {
-													if (outputStream != null) {
-														try {
-															outputStream.close();
-														} catch (IOException e) {
-															// giving up
-														}
-													}
+													outputStream.close();
+												} catch (final IOException e) {
+													// giving up
 												}
-
-												// refresh script in repository
-												final IRepositoryService repositoryService = PlatformUI.getWorkbench().getService(IRepositoryService.class);
-												// FIXME we should only update
-												// this one resource instead of
-												// all scripts
-												repositoryService.update(false);
 											}
 										}
+
+										// refresh script in repository
+										final IRepositoryService repositoryService = PlatformUI.getWorkbench().getService(IRepositoryService.class);
+										// FIXME we should only update
+										// this one resource instead of
+										// all scripts
+										repositoryService.update(false);
 									}
-								});
-
-							} catch (PartInitException e) {
-								Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
+								}
 							}
-						}
+						});
 
-					} else {
-						ScriptType type = ((IScript) element).getType();
-						IEditorDescriptor descriptor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor("foo." + type.getDefaultExtension());
-						if (descriptor != null) {
-							IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-							try {
-								final ScriptEditorInput editorInput = new ScriptEditorInput((IScript) element);
-								page.openEditor(editorInput, descriptor.getId());
+					} catch (final PartInitException e) {
+						Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
+					}
+				}
 
-							} catch (PartInitException e) {
-								Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
-							}
-						}
+			} else {
+				final ScriptType type = script.getType();
+				final IEditorDescriptor descriptor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor("foo." + type.getDefaultExtension());
+				if (descriptor != null) {
+					final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+					try {
+						final ScriptEditorInput editorInput = new ScriptEditorInput(script);
+						page.openEditor(editorInput, descriptor.getId());
+
+					} catch (final PartInitException e) {
+						Logger.error(Activator.PLUGIN_ID, "Could not open editor for file " + content, e);
 					}
 				}
 			}
diff --git a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RenameScript.java b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RenameScript.java
deleted file mode 100644
index 1daf73e..0000000
--- a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RenameScript.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2013 Christian Pontesegger and others.
- * 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:
- *     Christian Pontesegger - initial API and implementation
- *******************************************************************************/
-package org.eclipse.ease.ui.scripts.handler;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.IHandler;
-
-public class RenameScript extends AbstractHandler implements IHandler {
-
-	public static final String COMMAND_ID = "org.eclipse.ease.commands.script.rename";
-	public static final String PARAMETER_NAME = "org.eclipse.ease.commands.script.rename.name";
-
-	@Override
-	public final Object execute(final ExecutionEvent event) throws ExecutionException {
-
-		final String[] macros = event.getParameter(PARAMETER_NAME).split(";");
-
-		for (final String macroID : macros) {
-			// TODO implement dialog
-			// final Macro macro = MacroManager.getDefault().getMacro(macroID);
-			// MacroManager.getDefault().removeMacro(macro);
-		}
-
-		return null;
-	}
-}
diff --git a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RunScript.java b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RunScript.java
index b55244e..867b257 100644
--- a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RunScript.java
+++ b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/RunScript.java
@@ -30,27 +30,10 @@
 
 	@Override
 	public final Object execute(final ExecutionEvent event) throws ExecutionException {
-		IScript script = null;
-
-		final String scriptName = event.getParameter(PARAMETER_NAME);
-		if (scriptName != null) {
-			// script name provided as parameter
-			final IRepositoryService repositoryService = (IRepositoryService) PlatformUI.getWorkbench().getService(IRepositoryService.class);
-			script = repositoryService.getScript(scriptName);
-
-		} else {
-			// look for active script selection
-			ISelection selection = HandlerUtil.getCurrentSelection(event);
-			if (selection instanceof IStructuredSelection) {
-				Object element = ((IStructuredSelection) selection).getFirstElement();
-				if (element instanceof IScript)
-					script = (IScript) element;
-			}
-		}
-
+		final IScript script = getScript(event, PARAMETER_NAME);
 		if (script != null) {
 			// see if we may execute this in the current view or as stand-alone
-			IWorkbenchPart part = HandlerUtil.getActivePart(event);
+			final IWorkbenchPart part = HandlerUtil.getActivePart(event);
 
 			if (part instanceof IScriptEngineProvider)
 				// execute in current view
@@ -64,4 +47,26 @@
 
 		return null;
 	}
+
+	public static IScript getScript(final ExecutionEvent event, String parameterName) {
+		IScript script = null;
+
+		final String scriptName = event.getParameter(parameterName);
+		if (scriptName != null) {
+			// script name provided as parameter
+			final IRepositoryService repositoryService = PlatformUI.getWorkbench().getService(IRepositoryService.class);
+			script = repositoryService.getScript(scriptName);
+
+		} else {
+			// look for active script selection
+			final ISelection selection = HandlerUtil.getCurrentSelection(event);
+			if (selection instanceof IStructuredSelection) {
+				final Object element = ((IStructuredSelection) selection).getFirstElement();
+				if (element instanceof IScript)
+					script = (IScript) element;
+			}
+		}
+
+		return script;
+	}
 }
diff --git a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/ui/ScriptContextMenuEntries.java b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/ui/ScriptContextMenuEntries.java
index c4b0151..175f1aa 100644
--- a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/ui/ScriptContextMenuEntries.java
+++ b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/ui/ScriptContextMenuEntries.java
@@ -13,7 +13,6 @@
 import java.util.HashMap;
 
 import org.eclipse.ease.ui.scripts.handler.EditScript;
-import org.eclipse.ease.ui.scripts.handler.RenameScript;
 import org.eclipse.ease.ui.scripts.handler.RunScript;
 import org.eclipse.ease.ui.scripts.repository.IScript;
 import org.eclipse.jface.action.Separator;
@@ -56,7 +55,7 @@
 
 			if (names.length() > 0) {
 				names.deleteCharAt(names.length() - 1);
-				final HashMap<String, String> parameters = new HashMap<String, String>();
+				final HashMap<String, String> parameters = new HashMap<>();
 
 				// add "run" entry
 				parameters.put(RunScript.PARAMETER_NAME, names.toString());
@@ -73,27 +72,11 @@
 
 				// add "edit" entry
 				parameters.clear();
+				parameters.put(EditScript.PARAMETER_NAME, names.toString());
 				contributionParameter.commandId = EditScript.COMMAND_ID;
 				contributionParameter.label = "Edit";
 				contribution = new CommandContributionItem(contributionParameter);
 				additions.addContributionItem(contribution, null);
-
-				// add "rename" entry
-				parameters.clear();
-				parameters.put(RenameScript.PARAMETER_NAME, names.toString());
-				contributionParameter.commandId = RenameScript.COMMAND_ID;
-				contributionParameter.label = "Rename";
-				contribution = new CommandContributionItem(contributionParameter);
-				additions.addContributionItem(contribution, null);
-
-				// TODO re-implement
-				// add "delete" entry
-				// parameters.clear();
-				// parameters.put(Delete.PARAMETER_NAME, names.toString());
-				// contributionParameter.commandId = Delete.COMMAND_ID;
-				// contributionParameter.label = "Delete";
-				// contribution = new CommandContributionItem(contributionParameter);
-				// additions.addContributionItem(contribution, null);
 			}
 		}
 	}