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;

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);
		RelatedDisplay rdWidget = new RelatedDisplay(adlWidget);
		if (rdWidget != null) {
			setADLObjectProps(rdWidget, menuModel);
			if (rdWidget.isForeColorDefined() ){
				menuModel.setForegroundColor( colorMap[rdWidget.getForegroundColor()] );
			}
			if (rdWidget.isBackColorDefined()){
				menuModel.setBackgroundColor( 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].getName().replaceAll("\"", "").equals(""))){
						OpenDisplayAction odAction = new OpenDisplayAction();
	
						//Try to add the filename to the PROP_PATH
						IPath fPath = new Path(rdDisplays[ii].getName().replaceAll("\"", "").replace(".adl", ".opi"));
						System.out.println("Related display file: " + rdDisplays[ii].getName().replace(".adl", ".opi"));
						System.out.println("Related display file from IPath: " + fPath.toString() + ", " + fPath.getFileExtension());
						odAction.setPropertyValue(OpenDisplayAction.PROP_PATH, fPath);
	
						//Try to add macros
						System.out.println ("args " + rdDisplays[ii].getArgs());
						if (rdDisplays[ii].getArgs() != null){
							String argsIn = "true, " + rdDisplays[ii].getArgs().replaceAll("\"", "");
							MacrosInput macIn;
							try {
								macIn = MacrosInput.recoverFromString( argsIn);
								odAction.setPropertyValue(OpenDisplayAction.PROP_MACROS, macIn );
							} catch (Exception e) {
								e.printStackTrace();
							}
							
						}
						if (rdDisplays[ii].getLabel() != null){
							odAction.setPropertyValue(OpenDisplayAction.PROP_DESCRIPTION, rdDisplays[ii].getLabel().replaceAll("\"", ""));
						}
						ai.addAction(odAction);
					}
				}
			}
		}
		menuModel.setPropertyValue(MenuButtonModel.PROP_LABEL, rdWidget.getLabel());
		//TODO Add Visual property to RelatedDisplay2Model
		//TODO Add remove parent display to RelatedDisplay2Model
	}

	@Override
	public	AbstractWidgetModel getWidgetModel() {
		return menuModel;
	}

}

@@ -2816,24 +2816,308 @@
 ));%0A%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09if ((rdDisplays%5Bii%5D.getPolicy() == null) %7C%7C (!(rdDisplays%5Bii%5D.getPolicy().equals(%22replace display%22))) )%7B%0A%09%09%09%09%09%09%09odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, false);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09else%7B%0A%09%09%09%09%09%09%09odAction.setPropertyValue(OpenDisplayAction.PROP_REPLACE, true);%0A%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09ai.add

Add in the catch to have a related display open another window. MEDMs default is yes and BOY is no. If the option is present equal to "replace display" the display is replaced. Otherwise it is not.