/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * 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
 ******************************************************************************/
package org.csstudio.opibuilder.scriptUtil;

import org.csstudio.opibuilder.editparts.AbstractBaseEditPart;
import org.csstudio.opibuilder.util.ErrorHandlerUtil;
import org.csstudio.opibuilder.util.MacrosInput;
import org.csstudio.opibuilder.widgetActions.AbstractOpenOPIAction;
import org.csstudio.opibuilder.widgetActions.ExecuteCommandAction;
import org.csstudio.opibuilder.widgetActions.OpenDisplayAction;
import org.csstudio.opibuilder.widgetActions.OpenOPIInViewAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * The utility class to facilitate BOY script programming.
 * 
 * @author Xihui Chen
 * 
 */
public class ScriptUtil {

	/**
	 * Open an OPI.
	 * 
	 * @param widget
	 *            the widget to which the script is attached.
	 * @param opiPath
	 *            the path of the OPI. It can be either an absolute path or a
	 *            relative path to the Display file of the widget.
	 * @param target
	 *            target place of the new OPI. 0: new tab; 1: replace current
	 *            one; 2: new window; 3: view on left; 4: view on right; 5: view
	 *            on top; 6: view on bottom; 7: detached view
	 * @param macrosInput
	 *            the macrosInput. null if no macros needed.
	 */
	public final static void openOPI(AbstractBaseEditPart widget,
			String opiPath, int target, MacrosInput macrosInput) {
		AbstractOpenOPIAction action;
		if (target < 3) {
			action = new OpenDisplayAction();
			action.setPropertyValue(OpenDisplayAction.PROP_REPLACE, target);
		} else {
			action = new OpenOPIInViewAction();
			action.setPropertyValue(OpenOPIInViewAction.PROP_POSITION,
					target - 3);
		}
		action.setWidgetModel(widget.getWidgetModel());
		action.setPropertyValue(OpenDisplayAction.PROP_PATH, opiPath);
		action.setPropertyValue(OpenDisplayAction.PROP_MACROS, macrosInput);
		action.run();
	}
	
	/**
	 * Close current active OPI.
	 */
	public static void closeCurrentOPI(){
		IWorkbenchPage activePage = 
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart activePart = activePage.getActivePart();
		
		if(activePart instanceof IEditorPart){
			activePage.closeEditor((IEditorPart) activePart, false);
		}else if(activePart instanceof IViewPart){
			activePage.hideView((IViewPart) activePart);
		}
	}
	

	/**
	 * Pop up an Elog dialog to make an Elog entry.
	 * 
	 * @param filePath
	 *            path of a file to attach or null. It could be either a local
	 *            file system file path or a workspace file path. File types
	 *            that the logbook support depend on implementation but should
	 *            include *.gif, *.jpg: File will be attached as image.
	 */
	public final static void makeElogEntry(final String filePath) {
		if (ScriptUtilSSHelper.getIMPL() != null)
			ScriptUtilSSHelper.getIMPL().makeElogEntry(filePath);
		else
			throw new RuntimeException("This method is not implemented!");
	}

	/**
	 * Execute an Eclipse command.
	 * 
	 * @param commandId
	 *            the command id.
	 */
	public final static void executeEclipseCommand(String commandId) {
		IHandlerService handlerService = (IHandlerService) PlatformUI
				.getWorkbench().getActiveWorkbenchWindow()
				.getService(IHandlerService.class);

		try {
			handlerService.executeCommand(commandId, null);
		} catch (Exception e) {
			ErrorHandlerUtil.handleError("Failed to execute eclipse command: "
					+ commandId, e);
		}

	}
	
	/** Executing a system or shell command.
	 *  On Unix, that could be anything in the PATH.
	 *  <p>
	 *  Several things can happen:
	 *  <ul>
	 *  <li>Command finishes OK right away
	 *  <li>Command gives error right away
	 *  <li>Command runs for a long time, eventually giving error or OK.
	 *  </ul>
	 *  The command executor waits a little time to see if the command
	 *  finishes, and calls back in case of an error.
	 *  When the command finishes right away OK or runs longer,
	 *  we leave it be. Command output will be printed on BOY console.
	 *  
	 *  @param command Command to run. Format depends on OS.
     *  @param wait Time to wait for completion in seconds
     */
	public final static void executeSystemCommand(String command, int wait){
		ExecuteCommandAction action = new ExecuteCommandAction();
		action.setPropertyValue(ExecuteCommandAction.PROP_COMMAND, command);
		action.setPropertyValue(ExecuteCommandAction.PROP_WAIT_TIME, wait);
		action.run();
	}

}

@@ -499,16 +499,65 @@
 tUtil;%0A%0A
+import org.csstudio.opibuilder.OPIBuilderPlugin;%0A
 import o
@@ -5080,12 +5080,156 @@
 un();%0A%09%7D
+%0A%09%0A%09/**%0A%09 * @return true if it the OPI is running in WebOPI.%0A%09 */%0A%09public final static boolean isWebOPI()%7B%0A%09%09return OPIBuilderPlugin.isRAP();%0A%09%7D
 %0A%0A%7D%0A

add ScriptUtil.isWebOPI().