<commit_before>/*************************************************************************\
* Copyright (c) 2010  UChicago Argonne, LLC
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
/*************************************************************************/

package org.csstudio.opibuilder.adl2boy.translator;

import org.csstudio.opibuilder.model.AbstractContainerModel;
import org.csstudio.opibuilder.model.AbstractWidgetModel;
import org.csstudio.opibuilder.util.MacrosInput;
import org.csstudio.opibuilder.widgetActions.ActionsInput;
import org.csstudio.opibuilder.widgetActions.OpenDisplayAction;
import org.csstudio.opibuilder.widgets.model.MenuButtonModel;
import org.csstudio.utility.adlparser.fileParser.ADLWidget;
import org.csstudio.utility.adlparser.fileParser.widgetParts.RelatedDisplayItem;
import org.csstudio.utility.adlparser.fileParser.widgets.RelatedDisplay;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.graphics.RGB;

/**
 * Convert MEDMs related display to BOYs MenuButton 
 * 
 * @author <NAME>, Argonne National Laboratory
 *
 */
public class RelatedDisplay2Model extends AbstractADL2Model {
	MenuButtonModel menuModel = new MenuButtonModel();

	public RelatedDisplay2Model(ADLWidget adlWidget, RGB[] colorMap,
			AbstractContainerModel parentModel) {
		super(adlWidget, colorMap, parentModel);
		parentModel.addChild(menuModel, true);
		processWidget(adlWidget);
	}

	public RelatedDisplay2Model(RGB[] colorMap) {
		super(colorMap);
	}

	/**
	 * @param adlWidget
	 */
	public void processWidget(ADLWidget adlWidget) {
		RelatedDisplay rdWidget = new RelatedDisplay(adlWidget);
		if (rdWidget != null) {
			setADLObjectProps(rdWidget, menuModel);
			setADLDynamicAttributeProps(rdWidget, menuModel);
			if (rdWidget.isForeColorDefined()) {
				menuModel.setForegroundColor(this.colorMap[rdWidget
						.getForegroundColor()]);
			}
			if (rdWidget.isBackColorDefined()) {
				menuModel.setBackgroundColor(this.colorMap[rdWidget
						.getBackgroundColor()]);
			}
			RelatedDisplayItem[] rdDisplays = rdWidget.getRelatedDisplayItems();
			if (rdDisplays.length > 0) {
				ActionsInput ai = menuModel.getActionsInput();
				for (int ii = 0; ii < rdDisplays.length; ii++) {
					if (!(rdDisplays[ii].getFileName().replaceAll("\"", "")
							.equals(""))) {
						OpenDisplayAction odAction = createOpenDisplayAction(rdDisplays[ii]);
						ai.addAction(odAction);
					}
				}
			}
		}
		String label = rdWidget.getLabel();
		if (label != null) {
			if (label.startsWith("-")) { // leading "-" was used to flag not
											// using the icon. Just don't use
											// the icon and throw this away
				label = label.substring(1);
			}
		}
		menuModel.setPropertyValue(MenuButtonModel.PROP_LABEL, label);
	}

	/**
	 * @param rdDisplays
	 * @param ii
	 * @return
	 */
	public OpenDisplayAction createOpenDisplayAction(
			RelatedDisplayItem rdDisplay) {
		OpenDisplayAction odAction = new OpenDisplayAction();

		// Try to add the filename to the PROP_PATH
		IPath fPath = new Path(rdDisplay.getFileName().replaceAll("\"", "")
				.replace(".adl", ".opi"));
		System.out.println("Related display file: "
				+ rdDisplay.getFileName().replace(".adl", ".opi"));
		odAction.setPropertyValue(OpenDisplayAction.PROP_PATH, fPath);

		// Try to add macros
		addMacrosToOpenDisplayAction(rdDisplay, odAction);
		if (rdDisplay.getLabel() != null) {
			odAction.setPropertyValue(OpenDisplayAction.PROP_DESCRIPTION,
					rdDisplay.getLabel().replaceAll("\"", ""));
		}
		if ((rdDisplay.getPolicy() != null)) { // policy is present
			if (rdDisplay.getPolicy().equals("replace display")) { // replace
																	// the
																	// display
				odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, true);
			} else { // don't replace the display
				odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, false);
			}
		} else { // policy not present go to default
			odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, false); // don't
																				// replace
																				// the
																				// display
		}
		return odAction;
	}

	/**
	 * @param rdDisplays
	 * @param ii
	 * @param odAction
	 */
	public void addMacrosToOpenDisplayAction(RelatedDisplayItem rdDisplay,
			OpenDisplayAction odAction) {
		if (rdDisplay.getArgs() != null) {
			String args = rdDisplay.getArgs().replaceAll("\"", "");
			MacrosInput macIn = makeMacros(args);
			odAction.setPropertyValue(OpenDisplayAction.PROP_MACROS, macIn);
		}
	}

	/**
	 * @param args
	 * @return
	 */
	public MacrosInput makeMacros(String args) {
		String resArgs = removeParentMacros(args);
		String argsIn = "true, " + resArgs;<commit_msg>o.c.opibuilder.adl2boy: Fix case that Empty/missing macros cause problems.<commit_after>/*************************************************************************\
* Copyright (c) 2010  UChicago Argonne, LLC
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
/*************************************************************************/

package org.csstudio.opibuilder.adl2boy.translator;

import org.csstudio.opibuilder.model.AbstractContainerModel;
import org.csstudio.opibuilder.model.AbstractWidgetModel;
import org.csstudio.opibuilder.util.MacrosInput;
import org.csstudio.opibuilder.widgetActions.ActionsInput;
import org.csstudio.opibuilder.widgetActions.OpenDisplayAction;
import org.csstudio.opibuilder.widgets.model.MenuButtonModel;
import org.csstudio.utility.adlparser.fileParser.ADLWidget;
import org.csstudio.utility.adlparser.fileParser.widgetParts.RelatedDisplayItem;
import org.csstudio.utility.adlparser.fileParser.widgets.RelatedDisplay;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.graphics.RGB;

/**
 * Convert MEDMs related display to BOYs MenuButton 
 * 
 * @author <NAME>, Argonne National Laboratory
 *
 */
public class RelatedDisplay2Model extends AbstractADL2Model {
	MenuButtonModel menuModel = new MenuButtonModel();

	public RelatedDisplay2Model(ADLWidget adlWidget, RGB[] colorMap,
			AbstractContainerModel parentModel) {
		super(adlWidget, colorMap, parentModel);
		parentModel.addChild(menuModel, true);
		processWidget(adlWidget);
	}

	public RelatedDisplay2Model(RGB[] colorMap) {
		super(colorMap);
	}

	/**
	 * @param adlWidget
	 */
	public void processWidget(ADLWidget adlWidget) {
		RelatedDisplay rdWidget = new RelatedDisplay(adlWidget);
		if (rdWidget != null) {
			setADLObjectProps(rdWidget, menuModel);
			setADLDynamicAttributeProps(rdWidget, menuModel);
			if (rdWidget.isForeColorDefined()) {
				menuModel.setForegroundColor(this.colorMap[rdWidget
						.getForegroundColor()]);
			}
			if (rdWidget.isBackColorDefined()) {
				menuModel.setBackgroundColor(this.colorMap[rdWidget
						.getBackgroundColor()]);
			}
			RelatedDisplayItem[] rdDisplays = rdWidget.getRelatedDisplayItems();
			if (rdDisplays.length > 0) {
				ActionsInput ai = menuModel.getActionsInput();
				for (int ii = 0; ii < rdDisplays.length; ii++) {
					if (!(rdDisplays[ii].getFileName().replaceAll("\"", "")
							.equals(""))) {
						OpenDisplayAction odAction = createOpenDisplayAction(rdDisplays[ii]);
						ai.addAction(odAction);
					}
				}
			}
		}
		String label = rdWidget.getLabel();
		if (label != null) {
			if (label.startsWith("-")) { // leading "-" was used to flag not
											// using the icon. Just don't use
											// the icon and throw this away
				label = label.substring(1);
			}
		}
		menuModel.setPropertyValue(MenuButtonModel.PROP_LABEL, label);
	}

	/**
	 * @param rdDisplays
	 * @param ii
	 * @return
	 */
	public OpenDisplayAction createOpenDisplayAction(
			RelatedDisplayItem rdDisplay) {
		OpenDisplayAction odAction = new OpenDisplayAction();

		// Try to add the filename to the PROP_PATH
		IPath fPath = new Path(rdDisplay.getFileName().replaceAll("\"", "")
				.replace(".adl", ".opi"));
		System.out.println("Related display file: "
				+ rdDisplay.getFileName().replace(".adl", ".opi"));
		odAction.setPropertyValue(OpenDisplayAction.PROP_PATH, fPath);

		// Try to add macros
		addMacrosToOpenDisplayAction(rdDisplay, odAction);
		if (rdDisplay.getLabel() != null) {
			odAction.setPropertyValue(OpenDisplayAction.PROP_DESCRIPTION,
					rdDisplay.getLabel().replaceAll("\"", ""));
		}
		if ((rdDisplay.getPolicy() != null)) { // policy is present
			if (rdDisplay.getPolicy().equals("replace display")) { // replace
																	// the
																	// display
				odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, true);
			} else { // don't replace the display
				odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, false);
			}
		} else { // policy not present go to default
			odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, false); // don't
																				// replace
																				// the
																				// display
		}
		return odAction;
	}

	/**
	 * @param rdDisplays
	 * @param ii
	 * @param odAction
	 */
	public void addMacrosToOpenDisplayAction(RelatedDisplayItem rdDisplay,
			OpenDisplayAction odAction) {
		if (rdDisplay.getArgs() != null && !rdDisplay.getArgs().isEmpty()) {
			String args = rdDisplay.getArgs().replaceAll("\"", "");
			MacrosInput macIn = makeMacros(args);
			odAction.setPropertyValue(OpenDisplayAction.PROP_MACROS, macIn);
		}
	}

	/**
	 * @param args
	 * @return
	 */
	public MacrosInput makeMacros(String args) {
		String resArgs = removeParentMacros(args);
		String argsIn = "true, " + resArgs;
{'fraction_non_alphanumeric': 0.14621814054277166, 'fraction_numerical': 0.002579713135899288, 'mean_word_length': 4.061096605744125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3879296', 'n_tokens_mistral': 3375, 'n_tokens_neox': 2870, 'n_words': 585}