
package uk.co.wingpath.modbusgui;

import javax.swing.*;
import java.awt.event.*;
import uk.co.wingpath.gui.*;
import uk.co.wingpath.modbus.*;

public class ExceptionSelector
    extends WComboBox<ModbusException>
{
    private static String [] names =
        {
            "OK",
            "Timed out",
            "Invalid response",
            "Error response 1: Illegal function",
            "Error response 2: Illegal data address",
            "Error response 3: Illegal data value",
            "Error response 4: Server failure",
            "Error response 5: Acknowledge",
            "Error response 6: Server busy",
            "Error response 8: Memory parity error",
            "Error response 10: No path available to target",
            "Error response 11: Target device failed to respond",
        };

    private static ModbusException timedOutException =
        new ModbusException (Modbus.ERROR_TIMED_OUT, null, "Timed out", false);
    private static ModbusException invalidResponseException = 
        new ModbusException (Modbus.ERROR_ILLEGAL_DATA_VALUE,
            null, "Invalid response", false);

    private static ModbusException [] exceptions =
        {
            ModbusException.NO_ERROR,
            timedOutException,
            invalidResponseException,
            new ModbusException (Modbus.ERROR_ILLEGAL_FUNCTION,
                null, null, true),
            new ModbusException (Modbus.ERROR_ILLEGAL_DATA_ADDRESS,
                null, null, true),
            new ModbusException (Modbus.ERROR_ILLEGAL_DATA_VALUE,
                null, null, true),
            new ModbusException (Modbus.ERROR_SERVER_FAILURE,
                null, null, true),
            new ModbusException (Modbus.ERROR_ACKNOWLEDGE,
                null, null, true),
            new ModbusException (Modbus.ERROR_SERVER_BUSY,
                null, null, true),
            new ModbusException (Modbus.ERROR_MEMORY_PARITY,
                null, null, true),
            new ModbusException (Modbus.ERROR_PATH_UNAVAILABLE,
                null, null, true),
            new ModbusException (Modbus.ERROR_TIMED_OUT,
                null, null, true),
        };

    public ExceptionSelector (String label)
    {
        super (label, exceptions, names);
    }

    @Override
    public ModbusException getValue ()
    {
        ModbusException exception = super.getValue ();
        if (exception.getErrorCode () == Modbus.ERROR_NONE)
            exception = null;
        return exception;
    }

    @Override
    public void setValue (ModbusException exception)
    {
        // Map the exception to one that is supported by the selector.
        if (exception == null)
        {
            exception = ModbusException.NO_ERROR;
        }
        else if (!exception.isResponse ())
        {
            int error = exception.getErrorCode ();
            if (error == Modbus.ERROR_NONE)
            {
                exception = ModbusException.NO_ERROR;
            }
            else if (error == Modbus.ERROR_TIMED_OUT)
            {
                exception = timedOutException;
            }
            else
            {
                exception = invalidResponseException;
            }
        }

        try
        {
            super.setValue (exception);
        }
        catch (IllegalArgumentException e)
        {
        }
    }
}

