/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager.graphene;

import org.epics.graphene.Point2DDataset;
import org.epics.graphene.Point2DDatasets;
import org.epics.util.array.ListNumber;
import org.epics.util.array.ListNumbers;
import org.epics.vtype.VTable;
import org.epics.vtype.ValueUtil;

/**
 * Utility class to convert VTypes into graphene datasets.
 * 
 * @author carcassi
 */
public class DatasetConversions {
    
    /**
     * Converts a VTable into a Point2DDataset.
     * 
     * @param vTable the table containing the data
     * @param xColumn the column for the x values
     * @param yColumn the column for the y values
     * @return the dataset
     */
    public static Point2DDataset point2DDatasetFromVTable(VTable vTable,
            String xColumn, String yColumn) {
        // Extract x and y using column names
        ListNumber xValues = ValueUtil.numericColumnOf(vTable, xColumn);
        ListNumber yValues = ValueUtil.numericColumnOf(vTable, yColumn);

        // If one column name was null, fill it with the index
        if (xValues == null && yValues != null) {
            xValues = ListNumbers.linearList(0, 1, yValues.size());
        }
        if (yValues == null && xValues != null) {
            yValues = ListNumbers.linearList(0, 1, xValues.size());
        }

        // If both column names were null, use the first two numeric columns
        if (xValues == null && yValues == null) {
            for (int i = 0; i < vTable.getColumnCount(); i++) {
                if (vTable.getColumnType(i).isPrimitive()) {
                    if (xValues == null) {
                        xValues = (ListNumber) vTable.getColumnData(i);
                    } else if (yValues == null) {
                        yValues = (ListNumber) vTable.getColumnData(i);
                    }
                }
            }
            
            if (yValues == null) {
                throw new IllegalArgumentException("Couldn't find two numeric columns");
            }
        }
        
        return Point2DDatasets.lineData(xValues, yValues);
    }
}

@@ -1076,16 +1076,24 @@
 olumn);%0A
+        
 %0A       
@@ -1100,305 +1100,182 @@
  // 
-If one column name was null, fill it with the index%0A        if (xValues == null && yValues != null) %7B%0A            xValues = ListNumbers.linearList(0, 1, yValues.size());%0A        %7D%0A        if (yValues == null && xValues != null) %7B%0A            yValues = ListNumbers.linearList(0, 1, xValues.siz
+Fill the missing columns with the first available columns%0A        for (int i = 0; i %3C vTable.getColumnCount(); i++) %7B%0A            if (vTable.getColumnType(i).isPrimitiv
 e())
-;
+ %7B
 %0A   
@@ -1283,11 +1283,8 @@
     
-%7D%0A%0A
     
@@ -1294,73 +1294,35 @@
  // 
-If both column names were null, use the first two numeric
+Don't reuse the same
  column
-s
 %0A   
@@ -1330,113 +1330,63 @@
     
-if (xValues == null && yValues == null) %7B%0A            for (int i = 0; i %3C vTable.getColumnCount(); i++) %7B
+        if (!vTable.getColumnName(i).equals(xColumn) &&
 %0A   
@@ -1390,36 +1390,41 @@
                 
-if (
+        !
 vTable.getColumn
@@ -1423,36 +1423,38 @@
 etColumn
-Typ
+Nam
 e(i).
-isPrimitive(
+equals(yColumn
 )) %7B%0A   
@@ -1733,32 +1733,42 @@
 %7D%0A            %7D%0A
+        %7D%0A
             %0A   
@@ -1772,24 +1772,39 @@
         
-    
 if (
+xValues == null %7C%7C 
 yValues 
@@ -1806,36 +1806,32 @@
 lues == null) %7B%0A
-    
             thro
@@ -1899,30 +1899,16 @@
 umns%22);%0A
-            %7D%0A
         

update to SNAPSHOT