package de.prob2.ui.menu;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ResourceBundle;

import com.google.inject.Inject;

import de.prob.animator.command.GetPreferenceCommand;
import de.prob.statespace.StateSpace;

import de.prob2.ui.internal.ProB2Module;
import de.prob2.ui.internal.StageManager;
import de.prob2.ui.project.MachineLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EditPreferencesProvider {
	private static final Logger LOGGER = LoggerFactory.getLogger(EditPreferencesProvider.class);

	private final MachineLoader machineLoader;
	private final StageManager stageManager;
	private final ResourceBundle bundle;

	@Inject
	private EditPreferencesProvider(final StageManager stageManager, final MachineLoader machineLoader, final ResourceBundle bundle) {
		this.machineLoader = machineLoader;
		this.stageManager = stageManager;
		this.bundle = bundle;
	}

	public void showExternalEditor(Path path) {
		final StateSpace stateSpace = machineLoader.getEmptyStateSpace();
		final GetPreferenceCommand cmd = new GetPreferenceCommand("EDITOR_GUI");
		stateSpace.execute(cmd);
		final File editor = new File(cmd.getValue());
		final String[] cmdline;
		if (ProB2Module.IS_MAC && editor.isDirectory()) {
			// On Mac, use the open tool to start app bundles
			cmdline = new String[] { "/usr/bin/open", "-a", editor.getAbsolutePath(), path.toString() };
		} else {
			// Run normal executables directly
			cmdline = new String[] { editor.getAbsolutePath(), path.toString() };
		}
		final ProcessBuilder processBuilder = new ProcessBuilder(cmdline);
		try {
			processBuilder.start();
		} catch (IOException e) {
			LOGGER.error("Failed to start external editor", e);
			stageManager.makeExceptionAlert(bundle.getString("menu.editPreferencesProvider.alerts.couldNotStartEditor.message"), e).showAndWait();
		}
	}
}

@@ -35,20 +35,55 @@
 java.io.
+IOException;%0Aimport java.nio.file.
 File
+s
 ;%0Aimport
@@ -84,38 +84,37 @@
 import java.
+n
 io.
-IOException
+file.Path
 ;%0Aimport jav
@@ -120,32 +120,109 @@
 va.nio.file.Path
+s;%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List
 ;%0Aimport java.ut
@@ -1060,23 +1060,23 @@
 %0A%0A%09p
-ublic void show
+rivate Path get
 Exte
@@ -1089,18 +1089,13 @@
 itor
-(
 Path
- path
+(
 ) %7B%0A
@@ -1270,78 +1270,192 @@
 ;%0A%09%09
-final File editor = new File(cmd.getValue());%0A%09%09final String%5B%5D cmdline
+return Paths.get(cmd.getValue());%0A%09%7D%0A%0A%09private static List%3CString%3E getCommandLine(final Path executable, final List%3CString%3E args) %7B%0A%09%09final List%3CString%3E commandLine = new ArrayList%3C%3E()
 ;%0A%09%09
@@ -1480,22 +1480,21 @@
 _MAC && 
-editor
+Files
 .isDirec
@@ -1498,16 +1498,26 @@
 rectory(
+executable
 )) %7B%0A%09%09%09
@@ -1574,85 +1574,99 @@
 %09%09%09c
-mdline = new String%5B%5D %7B %22/usr/bin/open%22, %22-a%22, editor.getAbsolutePath(), path
+ommandLine.add(%22/usr/bin/open%22);%0A%09%09%09commandLine.add(%22-a%22);%0A%09%09%7D%0A%09%09commandLine.add(executable
 .toS
@@ -1676,137 +1676,262 @@
 ng()
- %7D
+)
 ;%0A%09%09
-%7D else %7B%0A%09%09%09// Run normal executables directly%0A%09%09%09cmdline = new String%5B%5D %7B editor.getAbsolute
+commandLine.addAll(args);%0A%09%09return commandLine;%0A%09%7D%0A%0A%09private static List%3CString%3E getCommandLine(final Path executable, final String... args) %7B%0A%09%09return getCommandLine(executable, Arrays.asList(args));%0A%09%7D%0A%0A%09public void showExternalEditor(
 Path
-(),
  path
-.toString() %7D;%0A%09%09%7D
+) %7B
 %0A%09%09f
@@ -1990,15 +1990,86 @@
 der(
-cmdline
+getCommandLine(this.getExternalEditorPath().toAbsolutePath(), path.toString())
 );%0A%09

Refactor EditPreferencesProvider.showExternalEditor