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);
}
}
Aggregations