Debian Codegraph @

src/dr/app/bfe/XMLTreeModel.java

raw symbols SCIP
1 /*
2 * XMLTreeModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.bfe;
27
28 import dr.xml.XMLModelFile;
29 import org.jdom.Attribute;
30 import org.jdom.Document;
31 import org.jdom.Element;
32 import org.jdom.JDOMException;
33 import org.jdom.input.SAXBuilder;
34
35 import javax.swing.event.TreeModelListener;
36 import javax.swing.tree.TreeModel;
37 import javax.swing.tree.TreePath;
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.LinkedList;
41
42 /**
43 * Package: XMLTreeModel
44 * Description:
45 * <p/>
46 * <p/>
47 * Created by
48 *
49 * @author Alexander V. Alekseyenko ([email protected])
50 * Date: Apr 14, 2009
51 * Time: 11:07:19 PM
52 */
53 public class XMLTreeModel implements TreeModel {
54 protected class ElementObject {
55 public Element element;
56 protected LinkedList<ElementObject> children;
57 protected String id;
58
59 ElementObject(Element element) {
60 this.element = element;
61 id = "";
62 Attribute idAttribute = element.getAttribute("id");
63 if (idAttribute == null) {
64 idAttribute = element.getAttribute("idref");
65 }
66 if (idAttribute != null) {
67 id = idAttribute.getValue();
68 }
69 children = null;
70 }
71
72 public String toString() {
73 if (element == null)
74 return "<<Empty>>";
75 return element.getName() + ":" + id;
76 }
77
78 public String getId() {
79 return id;
80 }
81
82 public LinkedList<ElementObject> getChildren() {
83 if (children != null) {
84 return children;
85 }
86 children = new LinkedList<ElementObject>();
87 if (element == null)
88 return children;
89 for (Object child : element.getChildren()) {
90 if (child instanceof Element) {
91 children.add(new ElementObject((Element) child));
92 }
93 }
94 return children;
95 }
96
97 }
98
99 public ElementObject getRootElement() {
100 return rootElement;
101 }
102
103 public XMLModelFile getXmlModel() {
104 return xmlModel;
105 }
106
107 private ElementObject rootElement;
108 XMLModelFile xmlModel;
109
110 XMLTreeModel(String filename) {
111
112 SAXBuilder parser = new SAXBuilder();
113
114 Document doc;
115 try {
116 doc = parser.build(new File(filename));
117 rootElement = new ElementObject(doc.getRootElement());
118 xmlModel = new XMLModelFile(doc.getRootElement());
119 }
120 catch (IOException e) {
121 System.err.println("Error opening file " + filename);
122 rootElement = new ElementObject(null);
123 }
124 catch (JDOMException e) {
125 }
126 }
127
128 // TreeModel method implementation
129 public Object getRoot() {
130 return rootElement; //AUTOGENERATED METHOD IMPLEMENTATION
131 }
132
133 public Object getChild(Object o, int i) {
134 if (!(o instanceof ElementObject)) {
135 return null;
136 }
137
138 if (i < ((ElementObject) o).getChildren().size()) {
139 return ((ElementObject) o).getChildren().get(i);
140 }
141 return null;
142 }
143
144 public int getChildCount(Object o) {
145 return ((ElementObject) o).getChildren().size();
146 }
147
148 public boolean isLeaf(Object o) {
149 return ((ElementObject) o).getChildren().size() == 0; //AUTOGENERATED METHOD IMPLEMENTATION
150 }
151
152 public void valueForPathChanged(TreePath treePath, Object o) {
153 //AUTOGENERATED METHOD IMPLEMENTATION
154 }
155
156 public int getIndexOfChild(Object o, Object o1) {
157 LinkedList<ElementObject> children = ((ElementObject) o).getChildren();
158 for (int i = 0; i < children.size(); ++i) {
159 if (o1 == children.get(i))
160 return i;
161 }
162 return -1;
163 }
164
165 public void addTreeModelListener(TreeModelListener treeModelListener) {
166 //AUTOGENERATED METHOD IMPLEMENTATION
167 }
168
169 public void removeTreeModelListener(TreeModelListener treeModelListener) {
170 //AUTOGENERATED METHOD IMPLEMENTATION
171 }
172 }