// Example code for new properties

import rsdesigner.uiextension.*;
import rsdesigner.component.*;
import rsdesigner.design.*;

public class ExampleValidateProperties extends Object implements rsdesigner.uiextension.ValidateProperties {

    // Constructor must be public
    public ExampleValidateProperties() {}

    // Check if a property on a particular dataset can be set to a given value,
    // Throws an error if one or more values are illegal
    public boolean canSet(Properties properties) throws RSDException
    {
        boolean res = true;
        PropertySet pset = properties.getPropertySet();

        // Get the RSD Class string
        String pset_class = pset.getClassString();
        System.out.println("*** Validating " + properties.size() + " properties for " + pset_class);

        // Print a list of the properties
        for (int i = 0; i < properties.size(); i++)
            {
                String pname = properties.getName(i);
                String type = properties.getType(pname);
                Object value = properties.getValue(pname);
                if (value == null)
                    value = "UNSET";

                System.out.println("Property name = " + pname + " ; type = " + type + " ; value = " + value.toString());
            }

        // Now for an example - ensure that the min_bend_radius on a fibre of type wiring_wire is >= 2
        if (pset instanceof Fibre)
            {
                if (properties.exists("min_bend_radius") &&
                    properties.exists("type") &&
                    properties.getValue("type").toString().equals("CONNECTION!WIRE!WIRING_WIRE"))
                    {
                        Object value = properties.getValue("min_bend_radius");
                        double d_pval = 0.0;

                        if (value == null)
                            d_pval = 0.0;
                        else
                            d_pval = Double.parseDouble(properties.getValue("min_bend_radius").toString());

                        if (d_pval < 2.0)
                            {
                                properties.setMessage("Cannot set this min_bend_radius");
                                res = false;
                            }
                    }
            }

        // Send a message
        return res;
    }

}
