1 package sourceforge.org.qmc2.options.editor.model;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.w3c.dom.Node;
9 import org.w3c.dom.NodeList;
10 
11 public class ComboOption extends Option {
12 
13 	private final static String TAG_CHOICE = "choice";
14 
15 	private List<String> choices = new ArrayList<String>();
16 
ComboOption(String name, String type, String defaultValue)17 	public ComboOption(String name, String type, String defaultValue) {
18 		super(name, type, defaultValue);
19 	}
20 
parseChoices(Node optionNode)21 	public void parseChoices(Node optionNode) {
22 		NodeList children = optionNode.getChildNodes();
23 
24 		for (int i = 0; i < children.getLength(); i++) {
25 			Node node = children.item(i);
26 			if (TAG_CHOICE.equals(node.getNodeName())) {
27 				String choice = node.getAttributes()
28 						.getNamedItem(ATTRIBUTE_NAME).getNodeValue();
29 				choices.add(choice);
30 			}
31 		}
32 	}
33 
34 	@Override
parseData(Node optionNode)35 	protected void parseData(Node optionNode) {
36 		super.parseData(optionNode);
37 		parseChoices(optionNode);
38 	}
39 
40 	@Override
toXML(Document document)41 	public Element toXML(Document document) {
42 
43 		Element option = super.toXML(document);
44 
45 		for (String choice : choices) {
46 			Element choiceElement = document.createElement(TAG_CHOICE);
47 			choiceElement.setAttribute(ATTRIBUTE_NAME, choice);
48 			option.appendChild(choiceElement);
49 		}
50 		return option;
51 	}
52 
setChoices(List<String> choices)53 	public void setChoices(List<String> choices) {
54 		this.choices = choices;
55 	}
56 
getChoices()57 	public List<String> getChoices() {
58 		return choices;
59 	}
60 
addChoice(String choice)61 	public void addChoice(String choice) {
62 		if (!choices.contains(choice)) {
63 			choices.add(choice);
64 		}
65 	}
66 
67 }
68