/*******************************************************************************
 * 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 java.util.LinkedHashMap;

import org.csstudio.opibuilder.util.MacrosInput;
import org.mozilla.javascript.NativeArray;


/**Utility class to facilitate Javascript programming
 * for data operation. The basic data type such as int, double, boolean and string are 
 * exchangeable between JavaScript and Java, which means these types of JavaScript variables
 * can be directly used as parameters of Java methods, but <b>array</b> is not exchangeable between 
 * JavaScript and Java. This utility class provides methods to create Java array or convert
 * JavaScript array to Java array.  
 * @author Xihui Chen
 *
 */
public class DataUtil {

	/**Create a new int array with given size.
	 * @param size the size of the array
	 * @return an int array with given size.
	 */
	public final static int[] createIntArray(int size){
		int[] result = new int[size];	
		return result;
	}

	/**Create a new double array with given size.
	 * @param size the size of the array
	 * @return a double array with given size.
	 */
	public final static double[] createDoubleArray(int size){
		double[] result = new double[size];	
		return result;
	}
	
	/**Convert JavaScript array to Java int array.
	 * @param jsArray JavaScript array
	 * @return java int array.
	 */
	public final static int[] toJavaIntArray(Object array){
	    if (array instanceof NativeArray) {
	        NativeArray jsArray = (NativeArray) array;
	        int[] result = new int[(int) jsArray.getLength()];
	        int i=0;
	        for(Object id : jsArray.getIds()){
	            Object o = jsArray.get((Integer)id, null);
	            if(o instanceof Number)
	                result[i++]=((Number)o).intValue();
	            else
	                result[i++] = 0;				
	        }
	        return result;
	    } else {
	        return (int[]) array;
	    }
	}
	
	/**Convert JavaScript array to Java double array.
	 * @param jsArray JavaScript array
	 * @return java array.
	 */
	public final static double[] toJavaDoubleArray(Object array){
	    if (array instanceof NativeArray) {
            NativeArray jsArray = (NativeArray) array;
            double[] result = new double[(int) jsArray.getLength()];
            int i=0;
            for(Object id : jsArray.getIds()){
                Object o = jsArray.get((Integer)id, null);
                if(o instanceof Number)
                    result[i++]=((Number)o).doubleValue();
                else
                    result[i++] = 0;				
            }
            return result;
	    } else {
	        return (double[]) array;
	    }
	}
	
	/**Create a MacrosInput, which can be used as the macros input for a container widget or display.
	 * New macro can be added or replaced by 
	 * <code>MacrosInput.put(String macroName, String macroValue);</code> 
	 * @param include_parent_macros If parent macros should be included.
	 * @return a new created MacrosInput.
	 */
	public final static MacrosInput createMacrosInput(boolean include_parent_macros){
		return new MacrosInput(new LinkedHashMap<String, String>(), include_parent_macros);
	}	
}

@@ -530,16 +530,39 @@
 HashMap;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2311,28 +2311,386 @@
     
-return (int%5B%5D) array
+   Object%5B%5D objArray = ((List%3C?%3E) array).toArray();%0A%09            int%5B%5D result = new int%5BobjArray.length%5D;%0A%09            for (int i = 0; i %3C objArray.length; i++) %7B%0A%09                if(objArray%5Bi%5D instanceof Number)%0A%09                    result%5Bi%5D=((Number)objArray%5Bi%5D).intValue();%0A%09                else%0A%09                    result%5Bi%5D = 0;%0A%09            %7D%0A%09            return result
 ;%0A%09 
@@ -3326,20 +3326,16 @@
 ++%5D = 0;
-%09%09%09%09
 %0A       
@@ -3395,31 +3395,369 @@
     
-return (double%5B%5D) array
+Object%5B%5D objArray = ((List%3C?%3E) array).toArray();%0A%09        double%5B%5D result = new double%5BobjArray.length%5D;%0A%09        for (int i = 0; i %3C objArray.length; i++) %7B%0A%09            if(objArray%5Bi%5D instanceof Number)%0A                    result%5Bi%5D=((Number)objArray%5Bi%5D).doubleValue();%0A                else%0A                    result%5Bi%5D = 0;%0A%09        %7D%0A%09        return result
 ;%0A%09 

Fix Java<->JS array converters (#528).