
package uk.co.wingpath.modbusgui;

import java.io.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import uk.co.wingpath.event.*;
import uk.co.wingpath.event.Event;
import uk.co.wingpath.util.*;
import uk.co.wingpath.modbus.*;
import uk.co.wingpath.gui.*;
import uk.co.wingpath.xml.*;

public class DeviceIdSettings
    implements Xml.Savable
{
    private DeviceId deviceId;
    private Numeric.Value [] data;
    private final ValueEventSource listeners;

    public DeviceIdSettings (Product product)
    {
        deviceId = new DeviceId ();
        data = new Numeric.Value [0];
        deviceId.put (Modbus.DEVICE_VENDOR_NAME, "Wingpath Limited");
        deviceId.put (Modbus.DEVICE_PRODUCT_CODE, product.getName ());
        deviceId.put (Modbus.DEVICE_MAJOR_MINOR_REVISION,
            product.getVersion ());
        listeners = new ValueEventSource ();

    }

    public DeviceId getDeviceId ()
    {
        return deviceId.clone ();
    }

    public void setDeviceId (DeviceId deviceId)
    {
        if (!deviceId.equals (this.deviceId))
        {
            this.deviceId = deviceId.clone ();
            listeners.fireValueChanged (this);
        }
    }

    public Numeric.Value [] getCmd17Data ()
    {
        return data.clone ();
    }

    public void setCmd17Data (Numeric.Value [] data)
    {
        this.data = data.clone ();
    }

    public void save (Xml.Saver saver)
        throws IOException
    {
        saver.startTag ("objects");
        DeviceIdXml.save (saver, deviceId);
        saver.endTag ("objects");
        saver.saveValue ("cmd17", data, 16);
    }

    public Xml.Loader getXmlLoader ()
    {
        deviceId = new DeviceId ();
        data = new Numeric.Value [0];

        return new Xml.AbstractLoader ()
        {
            @Override
            public Xml.Loader startChild (String tag)
            {

                if (tag.equalsIgnoreCase ("objects"))
                {
                    return DeviceIdXml.getXmlLoader (
                        new Xml.Receiver<DeviceId> ()
                        {
                            public void receive (DeviceId value)
                            {
                                deviceId = value;
                            }
                        });
                }
                else if (tag.equalsIgnoreCase ("cmd17"))
                {
                    return new Xml.NumericArrayLoader (
                        Numeric.Type.uint8, 16, true,
                        new Xml.Receiver<Numeric.Value []> ()
                        {
                            public void receive (Numeric.Value [] value)
                            {
                                data = value;
                            }
                        });
                }
                else
                {
                    return null;
                }
            }

            @Override
            public void cleanup ()
            {
                listeners.fireValueChanged (this);
            }
        };
    }

    public void addValueListener (ValueListener l)
    {
        listeners.addListener (l);
    }

    public void removeValueListener (ValueListener l)
    {
        listeners.removeListener (l);
    }
}


