package org.concord.datagraph.state;

import java.util.Vector;

import org.concord.data.state.OTDataStoreRealObject;
import org.concord.datagraph.engine.ControllableDataGraphable;
import org.concord.datagraph.engine.ControllableDataGraphableDrawing;
import org.concord.datagraph.engine.DataGraphable;
import org.concord.datagraph.ui.DataPointLabel;
import org.concord.framework.data.stream.DataChannelDescription;
import org.concord.framework.data.stream.DataStore;
import org.concord.framework.data.stream.DefaultDataStore;
import org.concord.framework.otrunk.DefaultOTController;
import org.concord.framework.otrunk.OTChangeEvent;
import org.concord.framework.otrunk.OTChangeListener;
import org.concord.framework.otrunk.otcore.OTClassProperty;

public class OTDataCollectorDataStoreController extends DefaultOTController implements OTChangeListener
{
	public static Class [] realObjectClasses =  { DefaultDataStore.class };
	public static Class otObjectClass = OTDataCollectorDataStore.class;
	private DefaultDataStore dataStore;
	private OTDataCollectorDataStore otDCDS;
	private OTDataCollector dataCollector; 
	
	private static int DATA_LABEL_STORE = 0;

	public void loadRealObject(Object realObject)
	{
		dataStore = (DefaultDataStore) realObject;
		otDCDS = (OTDataCollectorDataStore) otObject;
		dataCollector = otDCDS.getDataCollector();
		
		int dataType = otDCDS.getDataType();
		if (dataType == DATA_LABEL_STORE){
			loadDataLabelStore();
		}
		
		dataCollector.addOTChangeListener(this);
		
	}
	
	private void loadDataLabelStore(){
		dataStore.clearValues();
		
		String xAxisString = dataCollector.getXDataAxis().getLabel();
		String yAxisString = dataCollector.getYDataAxis().getLabel();
		dataStore.setDataChannelDescription(0, new DataChannelDescription("Label"));
		dataStore.setDataChannelDescription(1, new DataChannelDescription(xAxisString, 1));
		dataStore.setDataChannelDescription(2, new DataChannelDescription(yAxisString, 1));
		
		// There is some hacking going on here to remove extraneous labels (0,0) and duplicates.
		// This should be fixed in the dataCollector.
		Vector labels = dataCollector.getLabels().getVector();
		int subtract = 0;
		for (int i = 0; i < labels.size(); i++) {
			OTDataPointLabel label = (OTDataPointLabel) labels.get(i);
			OTClassProperty property = label.otClass().getProperty("xData");
			if (!label.otIsSet(property)){
				continue;
			}
			
			Float xValue = (Float) dataStore.getValueAt((i-subtract)-1, 1);
			if (((i-subtract) > 0) && xValue != null && (xValue.equals(new Float(label.getXData()))) &&
					(dataStore.getValueAt((i-subtract)-1, 2).equals(new Float(label.getYData())))){
				subtract++;
				continue;
			}
			
	        dataStore.setValueAt(i-subtract, 0, label.getText());
	        dataStore.setValueAt(i-subtract, 1, new Float(label.getXData()));
	        dataStore.setValueAt(i-subtract, 2, new Float(label.getYData()));
        }
	}

	public void registerRealObject(Object realObject)
	{
		
	}

	public void saveRealObject(Object realObject)
	{
		// TODO Auto-generated method stub

	}

	public void stateChanged(OTChangeEvent e)
    {
		System.out.println("state changed");
        loadDataLabelStore();
    }

}

@@ -2158,20 +2158,52 @@
 int 
-subtract = 0
+index = 0;%0A%09%09Vector allLabels = new Vector()
 ;%0A%09%09
@@ -2438,267 +2438,39 @@
 %0A%09%09%09
-Float xValue = (Float) dataStore.getValueAt((i-subtract)-1, 1);%0A%09%09%09if (((i-subtract) %3E 0) && xValue != null && (xValue.equals(new Float(label.getXData()))) &&%0A%09%09%09%09%09(dataStore.getValueAt((i-subtract)-1, 2).equals(new Float(label.getYData()))))%7B%0A%09%09%09%09subtract++;
+if (allLabels.contains(label))%7B
 %0A%09%09%09
@@ -2520,25 +2520,20 @@
 alueAt(i
--subtract
+ndex
 , 0, lab
@@ -2578,25 +2578,20 @@
 alueAt(i
--subtract
+ndex
 , 1, new
@@ -2652,17 +2652,12 @@
 At(i
--subtract
+ndex
 , 2,
@@ -2687,16 +2687,65 @@
 ta()));%0A
+%09        allLabels.add(label);%0A%09        index++;%0A
         

Correct handling of duplicates, correct indices of values