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