/*******************************************************************************
 * 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.converter.writer;

import org.apache.log4j.Logger;
import org.csstudio.opibuilder.converter.model.Edm_TextupdateClass;

/**
 * XML conversion class for Edm_TextupdateClass.
 * @author Matevz, Xihui Chen
 */
public class Opi_TextupdateClass extends OpiWidget {

	private static Logger log = Logger.getLogger("org.csstudio.opibuilder.converter.writer.Opi_TextupdateClass");
	private static final String typeId = "TextUpdate";	
	private static final String name = "EDM Text Update";
	private static final String version = "1.0";

	/**
	 * Converts the Edm_TextupdateClass to OPI TextUpdate widget XML.  
	 */
	public Opi_TextupdateClass(Context con, Edm_TextupdateClass t) {
		super(con,t);
		setTypeId(typeId);
		setName(name);
		setVersion(version);

		new OpiString(widgetContext, "pv_name", convertPVName(t.getControlPv()));		
		new OpiBoolean(widgetContext, "transparent", !t.isFill());
		
		int align = 0;
		if (t.getFontAlign()!=null){			
			if(t.getFontAlign().equals("right"))
				align = 2;
			else if(t.getFontAlign().equals("center"))
				align = 1;			
		}
		new OpiInt(widgetContext, "horizontal_alignment", align);

			
		
		new OpiInt(widgetContext, "border_width", t.getLineWidth());
		new OpiInt(widgetContext, "border_style", t.isLineAlarm()?0:1);
		new OpiColor(widgetContext, "border_color", t.getFgColor(), t);
		new OpiBoolean(widgetContext, "border_alarm_sensitive", t.isLineAlarm());
		new OpiBoolean(widgetContext, "precision_from_pv", true);
		if(t.getDisplayMode()!=null){
			int mode = 0;
			boolean showUnits = false;
			if(t.getDisplayMode().equals("decimal")){
				mode =1;
				new OpiBoolean(widgetContext, "precision_from_pv", false);
			}
			else if(t.getDisplayMode().equals("hex"))
				mode = 3;
			else if(t.getDisplayMode().equals("engineer") || t.getDisplayMode().equals("exp"))
				mode = 2;
			else{
				mode=0;
				showUnits = true;
			}
			new OpiBoolean(widgetContext, "show_units", showUnits);
			new OpiInt(widgetContext, "format_type", mode);
		}
		
		
		new OpiInt(widgetContext, "precision", t.getPrecision());
		
		new OpiColor(widgetContext, "foreground_color", t.getFgColor(), t);
		
		log.debug("Edm_TextupdateClass written.");
	}
	
}

@@ -1617,15 +1617,8 @@
 );%0A%0A
-%09%09%09%0A%09%09%0A
 %09%09ne
@@ -1888,41 +1888,48 @@
 ));%0A
+%0A
 %09%09
-new OpiBoolean(widgetContext, %22
+// Handle format and units%0A%09%09boolean 
 prec
@@ -1937,26 +1937,91 @@
 sion
-_from_pv%22,
+FromPV = true;%0A%09%09boolean showUnits =
  true
-)
 ;%0A
+%09%09int mode = 0; // show default display%0A
 %09%09if
@@ -2043,45 +2043,126 @@
 de()
-!=
+ == 
 null)%7B
-%0A%09%09%09int mode = 0;%0A%09%09%09boolean 
+ // default mode in EDM; ignores precision field and shows units%0A%09%09%09mode = 0;%0A%09%09%09precisionFromPV = true;%0A%09%09%09
 show
@@ -2173,108 +2173,105 @@
 s = 
-fals
+tru
 e;%0A%09%09
-%09if(t.getDisplayMode().equals(%22decimal%22))%7B%0A%09%09%09%09mode =1;%0A%09%09%09%09new OpiBoolean(widgetContext, %22
+%7D else %7B // not default mode in EDM%0A%09%09%09// these modes specify precision and not units%0A%09%09%09
 prec
@@ -2279,40 +2279,49 @@
 sion
-_from_pv%22,
+FromPV =
  false
-)
 ;%0A%09%09%09
-%7D%0A%09%09%09else 
+showUnits = false;%0A%09%09%09
 if(t
@@ -2350,14 +2350,19 @@
 ls(%22
-hex
+decimal
 %22))
+%7B
 %0A%09%09%09
@@ -2373,14 +2373,16 @@
 e = 
-3
+1
 ;%0A%09%09%09
+%7D 
 else
@@ -2459,16 +2459,18 @@
 (%22exp%22))
+ %7B
 %0A%09%09%09%09mod
@@ -2479,20 +2479,60 @@
 = 2;%0A%09%09%09
+%7D 
 else
+ if(t.getDisplayMode().equals(%22hex%22)) 
 %7B%0A%09%09%09%09mo
@@ -2537,40 +2537,97 @@
 mode
-=0
+ = 3
 ;%0A%09%09%09
-%09showUnits = true;%0A%09%09%09%7D%0A%09
+%7D%0A%09%09%7D%0A%09%09%0A%09%09new OpiBoolean(widgetContext, %22precision_from_pv%22, precisionFromPV);%0A
 %09%09ne
@@ -2682,17 +2682,16 @@
 its);%0A%09%09
-%09
 new OpiI
@@ -2734,18 +2734,8 @@
 e);%0A
-%09%09%7D%0A%09%09%0A%09%09%0A
 %09%09ne

improve handling of precision