NXvalidate  1
 All Classes Namespaces Files Functions Variables
TreeUtils.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Stephen Rankin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * TreeUtils.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import javax.swing.JTree;
32 import javax.swing.tree.TreePath;
33 import org.w3c.dom.Document;
34 
39 public class TreeUtils {
40 
41 
42 
43 
44  public void hideGoodNodes(JTree tree){
45 
46  if (tree.isSelectionEmpty()) {
47  return;
48  }
49 
50  ArrayList<NXNodeMapper> nodes = new ArrayList<NXNodeMapper>();
51  ArrayList<TreePath> paths = new ArrayList<TreePath>();
52  NXNodeMapper baseNode = getBaseNode(tree);
53 
54  if (baseNode==null) {
55  return;
56  }
57 
58  Enumeration children = baseNode.children();
59  NXNodeMapper tmpNode = null;
60 
61  NXNodeMapper parent = null;
62  TreePath path = null;
63  ArrayList<NXNodeMapper> tmpPath = null;
64  ArrayList<NXNodeMapper> tmpPath2 = null;
65  nodes.add(baseNode);
66 
67  while (children.hasMoreElements()) {
68 
69  tmpNode = (NXNodeMapper)children.nextElement();
70  nodes.add(tmpNode);
71  nodes.addAll(getSubNodes(tmpNode));
72 
73  }
74 
75  for (int i = 0; i < nodes.size(); ++i) {
76 
77  nodes.get(i).checkBadNode();
78 
79  if(nodes.get(i).getBadNode()){
80 
81  tmpPath = new ArrayList<NXNodeMapper>();
82  //tmpPath.add(nodes.get(i));
83 
84  parent = (NXNodeMapper)nodes.get(i).getParent();
85 
86  while(!parent.toString().equals(baseNode.toString()) && parent!=null){
87  tmpPath.add(parent);
88  parent = (NXNodeMapper)parent.getParent();
89  }
90  tmpPath.add(baseNode);
91  tmpPath.add((NXNodeMapper)baseNode.getParent());
92 
93  tmpPath2 = new ArrayList<NXNodeMapper>();
94 
95  int number = tmpPath.size() -1;
96  for(int j = 0; j < tmpPath.size();++j){
97  tmpPath2.add(tmpPath.get(number -j));
98  }
99 
100  paths.add(new TreePath(tmpPath2.toArray()));
101  tree.expandPath(new TreePath(tmpPath2.toArray()));
102  }
103  }
104 
105  }
106 
107  private ArrayList<NXNodeMapper> getSubNodes(NXNodeMapper node){
108 
109  ArrayList<NXNodeMapper> nodes = new ArrayList<NXNodeMapper>();
110  NXNodeMapper tmpNode = null;
111 
112  Enumeration children = node.children();
113 
114  while (children.hasMoreElements()) {
115 
116  tmpNode = (NXNodeMapper)children.nextElement();
117  nodes.add(tmpNode);
118  nodes.addAll(getSubNodes(tmpNode));
119 
120  }
121 
122  return nodes;
123 
124  }
125 
126  public boolean hasBadChildren(NXNodeMapper node){
127 
128  ArrayList<NXNodeMapper> subNodes = getSubNodes(node);
129  boolean bad = false;
130  for(int i = 0;i<subNodes.size();++i){
131  if(subNodes.get(i).getBadNode()){
132  bad = true;
133  break;
134  }
135  }
136  return bad;
137  }
138 
139  public void showGoodNodes(JTree tree){
140 
141  if (tree.isSelectionEmpty()) {
142  return;
143  }
144 
145  NXNodeMapper baseNode = getBaseNode(tree);
146  NXNodeMapper node = null;
147 
148  int rows = tree.getRowCount();
149 
150  for (int i = 0; i < rows; ++i) {
151  node = (NXNodeMapper) tree.getPathForRow(i).getLastPathComponent();
152  System.out.println("Node Name: " + node.toString());
153  }
154 
155  }
156 
157  public String getTreePath(JTree tree) {
158 
159  if (tree.isSelectionEmpty()) {
160  return null;
161  }
162 
163  TreePath treePath = tree.getSelectionPath().getParentPath();
164  NXNodeMapper node =
165  (NXNodeMapper) tree.getSelectionPath().getLastPathComponent();
166 
167  String path = "";
168  NXNodeMapper tmpNode = null;
169  Object[] nodes = treePath.getPath();
170 
171  for (int i = 0; i < treePath.getPathCount(); ++i) {
172  tmpNode = (NXNodeMapper) nodes[i];
173  path = path + "/" + tmpNode.toString();
174  }
175 
176  path = path + "/" + node.toString();
177 
178  return path;
179 
180  }
181 
182  public NXNodeMapper getBaseNode(JTree tree) {
183  if (getRootNode(tree) != null) {
184  return getRootNode(tree);
185  } else {
186  return null;
187  }
188  }
189 
190  public File getNXDLFile(JTree tree) {
191  if (getRootNode(tree) != null) {
192  return getRootNode(tree).getNXDLFile();
193  } else {
194  return null;
195  }
196  }
197 
198  public File getReducedFile(JTree tree) {
199  if (getRootNode(tree) != null) {
200  return getRootNode(tree).getReducedFile();
201  } else {
202  return null;
203  }
204  }
205 
206  public Document getReducedDoc(JTree tree) {
207  if (getRootNode(tree) != null) {
208  return getRootNode(tree).getReducedDoc();
209  } else {
210  return null;
211  }
212  }
213 
214  public Document getResultsDoc(JTree tree) {
215  if (getRootNode(tree) != null) {
216  return getRootNode(tree).getResultsDoc();
217  } else {
218  return null;
219  }
220  }
221 
222  public File getResultsFile(JTree tree) {
223  if (getRootNode(tree) != null) {
224  return getRootNode(tree).getResultsFile();
225  } else {
226  return null;
227  }
228  }
229 
230  public void setNXDLFile(JTree tree, File file) {
231  if (getRootNode(tree) != null) {
232  getRootNode(tree).setNXDLFile(file);
233  }
234  }
235 
236  public void setReducedFile(JTree tree, File file) {
237  if (getRootNode(tree) != null) {
238  getRootNode(tree).setReducedFile(file);
239  }
240  }
241 
242  public void setReducedDoc(JTree tree, Document doc) {
243  if (getRootNode(tree) != null) {
244  getRootNode(tree).setReducedDoc(doc);
245  }
246  }
247 
248  public void setResultsDoc(JTree tree, Document doc) {
249  if (getRootNode(tree) != null) {
250  getRootNode(tree).setResultsDoc(doc);
251  }
252  }
253 
254  public void setResultsFile(JTree tree, File file) {
255  if (getRootNode(tree) != null) {
256  getRootNode(tree).setResultsFile(file);
257  }
258  }
259 
260  public void setValidated(JTree tree, File file) {
261  NXNodeMapper tmpNode = getRootNode(tree);
262  if (tmpNode != null) {
263  if(tmpNode.getResultsFile().equals(file)){
264  tmpNode.setValidatedNode(true);
265  }
266  }
267  }
268 
269  private NXNodeMapper getRootNode(JTree tree) {
270  if (tree.isSelectionEmpty()) {
271  return null;
272  }
273 
274  TreePath treePath = tree.getSelectionPath().getParentPath();
275 
276  if(treePath==null){
277  return null;
278  }
279 
280  NXNodeMapper tmpNode = null;
281  Object[] nodes = treePath.getPath();
282  if (nodes.length > 1) {
283  tmpNode = (NXNodeMapper) nodes[1];
284  } else if (nodes.length == 1) {
285  tmpNode =
286  (NXNodeMapper) tree.getSelectionPath().getLastPathComponent();
287  } else {
288  return null;
289  }
290  return tmpNode;
291  }
292 
293 }