package org.csstudio.opibuilder.actions;

import java.net.URL;
import java.util.LinkedHashMap;

import org.csstudio.csdata.ProcessVariable;
import org.csstudio.opibuilder.OPIBuilderPlugin;
import org.csstudio.opibuilder.preferences.PreferencesHelper;
import org.csstudio.opibuilder.runmode.OPIRunnerPerspective.Position;
import org.csstudio.opibuilder.runmode.RunModeService;
import org.csstudio.opibuilder.util.MacrosInput;
import org.csstudio.opibuilder.util.ResourceUtil;
import org.csstudio.ui.util.AdapterUtil;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;


public class OpenOPIProbeHandler extends AbstractHandler {
	private static final String MACRO_NAME = "probe_pv"; //$NON-NLS-1$


	public Object execute(ExecutionEvent event) throws ExecutionException {
		final ISelection selection =
			HandlerUtil.getActiveMenuSelection(event);
		final ProcessVariable[] pvs =
			AdapterUtil.convert(selection, ProcessVariable.class);
		final Shell shell = HandlerUtil.getActiveShell(event);
		IPath probeOPIPath = PreferencesHelper.getProbeOPIPath();

		// When not defined, try built-in probe opi example
		if(probeOPIPath == null || probeOPIPath.isEmpty()){
			URL url = FileLocator.find(OPIBuilderPlugin.getDefault().getBundle(),
					new Path("opi/probe.opi"), null); //$NON-NLS-1$
			try {
				url = FileLocator.toFileURL(url);
			} catch (Throwable e) {
				MessageDialog.openError(shell, "No Probe OPI",
						"Cannot open probe OPI.\nPlease define your probe OPI on BOY preference page.");
			}
			probeOPIPath = ResourceUtil.getPathFromString(url.getPath());
		}

		LinkedHashMap<String, String> macros = new LinkedHashMap<String, String>();
		if(pvs.length >0)
			macros.put(MACRO_NAME, pvs[0].getName());

		int i=0;
		for(ProcessVariable pv : pvs){
			macros.put(MACRO_NAME + "_" + Integer.toString(i), pv.getName()); //$NON-NLS-1$
			i++;
		}

		MacrosInput macrosInput = new MacrosInput(macros, true);

		// Errors in here will show in dialog and error log
		RunModeService.runOPIInView(probeOPIPath, null,macrosInput, Position.DETACHED);
		return null;
	}

	
}

@@ -41,30 +41,8 @@
 %0D%0A%0D%0A
-import java.net.URL;%0D%0A
 impo
@@ -121,58 +121,8 @@
 e;%0D%0A
-import org.csstudio.opibuilder.OPIBuilderPlugin;%0D%0A
 impo
@@ -641,147 +641,13 @@
 ime.
-FileLocator;%0D%0Aimport org.eclipse.core.runtime.IPath;%0D%0Aimport org.eclipse.core.runtime.Path;%0D%0Aimport org.eclipse.jface.dialogs.MessageDialog
+IPath
 ;%0D%0Ai
@@ -1388,16 +1388,18 @@
 ty())%7B%0D%0A
+//
 %09%09%09URL u
@@ -1464,16 +1464,18 @@
 dle(),%0D%0A
+//
 %09%09%09%09%09new
@@ -1520,16 +1520,18 @@
 NLS-1$%0D%0A
+//
 %09%09%09try %7B
@@ -1528,24 +1528,26 @@
 //%09%09%09try %7B%0D%0A
+//
 %09%09%09%09url = Fi
@@ -1569,24 +1569,26 @@
 eURL(url);%0D%0A
+//
 %09%09%09%7D catch (
@@ -1603,16 +1603,18 @@
 e e) %7B%0D%0A
+//
 %09%09%09%09Mess
@@ -1657,16 +1657,18 @@
  OPI%22,%0D%0A
+//
 %09%09%09%09%09%09%22C
@@ -1747,16 +1747,18 @@
 ge.%22);%0D%0A
+//
 %09%09%09%7D%0D%0A%09%09
@@ -1808,23 +1808,79 @@
 ing(
-url.getPath());
+%22platform:/plugin/org.csstudio.opibuilder/opi/probe.opi%22);//$NON-NLS-1$
 %0D%0A%09%09

fix a bug on probe opi that cannot find images