Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/XMLHandler.java @ 3:9e67171477bc
PHANTOM Omni Heptic device release
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Wed, 25 Apr 2012 17:09:09 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:4b2f975e35fa | 3:9e67171477bc |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
5 | |
6 This program is free software: you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation, either version 3 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
19 package uk.ac.qmul.eecs.ccmi.checkboxtree; | |
20 | |
21 import java.util.Stack; | |
22 | |
23 import javax.swing.tree.DefaultTreeModel; | |
24 | |
25 import org.xml.sax.Attributes; | |
26 import org.xml.sax.SAXException; | |
27 import org.xml.sax.helpers.DefaultHandler; | |
28 | |
29 /* Build a tree out of an XML file. The hierarchical structure of the XML file reflects | |
30 * the structure of the tree. The XMl file must have the form specified in CheckBoxTree | |
31 * class comment. | |
32 * | |
33 */ | |
34 class XMLHandler extends DefaultHandler { | |
35 | |
36 @SuppressWarnings("serial") | |
37 public XMLHandler(DefaultTreeModel treeModel, SetProperties properties){ | |
38 this.properties = properties; | |
39 this.treeModel = treeModel; | |
40 /* path is used to keep track of the current node's path. If the path is present as a * | |
41 * properties entry, then the current node, that has jus been created, will be set as selected */ | |
42 path = new Stack<String>() { | |
43 @Override | |
44 public String toString(){ | |
45 StringBuilder builder = new StringBuilder(); | |
46 for(int i=0; i<size();i++){ | |
47 builder.append(get(i)); | |
48 if(i != size()-1) | |
49 builder.append(CheckBoxTreeNode.STRING_PATH_SEPARATOR); | |
50 } | |
51 return builder.toString(); | |
52 } | |
53 }; | |
54 } | |
55 | |
56 /* | |
57 * Create a CheckBoxTreeNode out of an xml tag. The tree node name is given by the value | |
58 * attribute. Whether the tree node is selectable or not, depends on the tag name, which can be | |
59 * selectable/unselectable. If the node is selectable, then it will be set as selected if its path | |
60 * is present in the properties | |
61 */ | |
62 @Override | |
63 public void startElement(String uri, | |
64 String localName, | |
65 String qName, | |
66 Attributes attributes) | |
67 throws SAXException { | |
68 String nodeName = attributes.getValue(VALUE_ATTR); | |
69 if(nodeName == null) | |
70 throw new SAXException("Value attribute missing"); | |
71 boolean isSelectable = SELECTABLE_NODE.equals(qName); | |
72 | |
73 CheckBoxTreeNode newNode = new CheckBoxTreeNode(nodeName,isSelectable); | |
74 if(currentNode == null){ | |
75 currentNode = newNode; | |
76 treeModel.setRoot(newNode); | |
77 }else{ | |
78 currentNode.add(newNode); | |
79 } | |
80 currentNode = newNode; | |
81 path.push(nodeName); | |
82 if(properties.contains(path.toString())) | |
83 newNode.setSelected(true); | |
84 } | |
85 | |
86 /* when an end tag is encountered, we carry on building the tree with the father as current node */ | |
87 @Override | |
88 public void endElement(String uri, | |
89 String localName, | |
90 String qName) | |
91 throws SAXException { | |
92 path.pop(); | |
93 currentNode = (CheckBoxTreeNode)currentNode.getParent(); | |
94 } | |
95 | |
96 private CheckBoxTreeNode currentNode; | |
97 private DefaultTreeModel treeModel; | |
98 private SetProperties properties; | |
99 private Stack<String> path; | |
100 /* attributes used in the XML file */ | |
101 public static final String VALUE_ATTR = "value"; | |
102 public static final String SELECTABLE_NODE = "selectable"; | |
103 public static final String UNSELECTABLE_NODE = "selectable"; | |
104 | |
105 | |
106 } |