<commit_before>/*******************************************************************************
 * 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.OPIBuilderPlugin;
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.swt.widgets.Display;
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 <NAME>
 * 
 */
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);
		}
	}
	<commit_msg>BOY: ignore nullpointer error on closeCurrentOPI<commit_after>/*******************************************************************************
 * 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.OPIBuilderPlugin;
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.swt.widgets.Display;
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 <NAME>
 * 
 */
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(){
		try {
			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);
			}
		} catch (NullPointerException e) {

		}
	}
	
{'fraction_non_alphanumeric': 0.13701067615658363, 'fraction_numerical': 0.005823358136525397, 'mean_word_length': 4.275597269624574, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5665263', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1761, 'n_words': 465}