Search in sources :

Example 1 with Pair

use of net.sourceforge.usbdm.deviceEditor.information.Variable.Pair in project usbdm-eclipse-plugins by podonoghue.

the class ParseMenuXML method parseChoices.

/**
 * Parses the children of this element
 *
 * @param  parentModel  Model to attach children to
 * @param  menuElement  Menu element to parse
 *
 * @throws Exception
 */
void parseChoices(Variable variable, Element menuElement) throws Exception {
    ArrayList<Pair> entries = new ArrayList<Pair>();
    String defaultValue = null;
    NodeList choiceNodes = menuElement.getElementsByTagName("choice");
    for (int index = 0; index < choiceNodes.getLength(); index++) {
        Node node = choiceNodes.item(index);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element element = (Element) node;
        Pair entry = new Pair(element.getAttribute("name"), element.getAttribute("value"));
        entries.add(entry);
        if (defaultValue == null) {
            defaultValue = entry.name;
        }
        if (element.getAttribute("isDefault").equalsIgnoreCase("true")) {
            defaultValue = entry.name;
        }
    }
    if (entries.size() == 0) {
        /**
         * Should be another variable of the same type to copy from
         */
        Variable otherVariable = getDerived(menuElement);
        if (otherVariable == null) {
            throw new Exception("No choices found in <" + menuElement.getTagName() + " name=\"" + variable.getName() + "\">");
        }
        if (otherVariable.getClass() != variable.getClass()) {
            throw new Exception("Referenced variable of wrong type <" + menuElement.getTagName() + " derivedFrom=\"" + variable.getName() + "\">");
        }
        if (variable instanceof BooleanVariable) {
            BooleanVariable otherVar = (BooleanVariable) otherVariable;
            BooleanVariable var = (BooleanVariable) variable;
            var.setFalseValue(otherVar.getFalseValue());
            var.setTrueValue(otherVar.getTrueValue());
            var.setDefault(otherVar.getDefault());
            var.setValue(otherVar.getDefault());
        } else if (variable instanceof ChoiceVariable) {
            ChoiceVariable otherVar = (ChoiceVariable) otherVariable;
            ChoiceVariable var = (ChoiceVariable) variable;
            var.setData(otherVar.getData());
            var.setDefault(otherVar.getDefault());
            var.setValue(otherVar.getDefault());
        }
    } else {
        if (variable instanceof BooleanVariable) {
            if (entries.size() > 2) {
                throw new Exception("Wrong number of choices in <" + menuElement.getTagName() + " name=\"" + variable.getName() + "\">");
            }
            BooleanVariable var = (BooleanVariable) variable;
            var.setFalseValue(entries.get(0));
            var.setTrueValue(entries.get(1));
        } else if (variable instanceof ChoiceVariable) {
            Pair[] theEntries = entries.toArray(new Pair[entries.size()]);
            ChoiceVariable var = (ChoiceVariable) variable;
            var.setData(theEntries);
        }
        variable.setDefault(defaultValue);
        variable.setValue(defaultValue);
    }
}
Also used : BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) PinListVariable(net.sourceforge.usbdm.deviceEditor.information.PinListVariable) DoubleVariable(net.sourceforge.usbdm.deviceEditor.information.DoubleVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable) IrqVariable(net.sourceforge.usbdm.deviceEditor.information.IrqVariable) StringVariable(net.sourceforge.usbdm.deviceEditor.information.StringVariable) IndexedCategoryVariable(net.sourceforge.usbdm.deviceEditor.information.IndexedCategoryVariable) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable) BitmaskVariable(net.sourceforge.usbdm.deviceEditor.information.BitmaskVariable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) NumericListVariable(net.sourceforge.usbdm.deviceEditor.information.NumericListVariable) BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) FileNotFoundException(java.io.FileNotFoundException) Pair(net.sourceforge.usbdm.deviceEditor.information.Variable.Pair)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 BitmaskVariable (net.sourceforge.usbdm.deviceEditor.information.BitmaskVariable)1 BooleanVariable (net.sourceforge.usbdm.deviceEditor.information.BooleanVariable)1 ChoiceVariable (net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable)1 DoubleVariable (net.sourceforge.usbdm.deviceEditor.information.DoubleVariable)1 IndexedCategoryVariable (net.sourceforge.usbdm.deviceEditor.information.IndexedCategoryVariable)1 IrqVariable (net.sourceforge.usbdm.deviceEditor.information.IrqVariable)1 LongVariable (net.sourceforge.usbdm.deviceEditor.information.LongVariable)1 NumericListVariable (net.sourceforge.usbdm.deviceEditor.information.NumericListVariable)1 PinListVariable (net.sourceforge.usbdm.deviceEditor.information.PinListVariable)1 StringVariable (net.sourceforge.usbdm.deviceEditor.information.StringVariable)1 Variable (net.sourceforge.usbdm.deviceEditor.information.Variable)1 Pair (net.sourceforge.usbdm.deviceEditor.information.Variable.Pair)1 UsbdmException (net.sourceforge.usbdm.jni.UsbdmException)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1