NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXvalidateBasicGui.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 Nexus Team
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  * NXvalidateBasicGui.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import org.nexusformat.nxvalidate.filter.SchematronFilter;
29 import org.nexusformat.nxvalidate.filter.XmlFilter;
30 import org.nexusformat.nxvalidate.filter.NeXusFilter;
31 import org.nexusformat.nxvalidate.filter.HdfFilter;
32 import org.nexusformat.nxvalidate.filter.AllNeXusFilter;
33 import java.awt.GridBagConstraints;
34 import java.awt.GridBagLayout;
35 import java.awt.Insets;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.io.File;
39 import java.util.ArrayList;
40 import java.util.Iterator;
41 import java.util.Vector;
42 import javax.swing.JButton;
43 import javax.swing.JFileChooser;
44 import javax.swing.JFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTextArea;
49 import javax.swing.JTextField;
50 import javax.swing.JTree;
51 import javax.swing.SwingUtilities;
52 import javax.swing.ToolTipManager;
53 import javax.swing.UIManager;
54 import javax.swing.tree.DefaultTreeCellRenderer;
55 import javax.swing.tree.DefaultTreeModel;
56 import javax.swing.tree.TreeModel;
57 import javax.xml.parsers.SAXParser;
58 import javax.xml.parsers.SAXParserFactory;
59 
60 public class NXvalidateBasicGui extends JPanel implements ActionListener {
61 
65  private static final long serialVersionUID = -2935498610579762249L;
66  static private final String newline = "\n";
67  JButton browseFileButton;
68  JButton validateButton;
69  JLabel filenameLabel;
70  JTextField filenameText;
71  JLabel nxdlLabel;
72  JButton browseNxdlButton;
73  JTextField nxdlText;
74  JTextArea log;
75  JTree tree;
76  JFileChooser fc;
77  JFileChooser nxdlChooser;
78  String reducedNeXusFilename;
79  String schematronFilename = "schematron.sch";
80  File rawFile = null;
81  File schematronFile = null;
82  NXconvert converter;
83  NXschematron schematron;
84  ArrayList<Report> reports;
85  NXvalidate validator;
86 
87  public NXvalidateBasicGui() {
88  super(new GridBagLayout());
89 
90  GridBagConstraints c = new GridBagConstraints();
91 
92  // Create the log first, because the action listeners
93  // need to refer to it.
94  log = new JTextArea(5, 20);
95  log.setMargin(new Insets(5, 5, 5, 5));
96  log.setEditable(false);
97  JScrollPane logScrollPane = new JScrollPane(log);
98 
99  // Create a nexus file chooser
100  fc = new JFileChooser();
101 
102  fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
103  NeXusFilter nxsFilter = new NeXusFilter();
104  this.fc.addChoosableFileFilter(nxsFilter);
105  this.fc.addChoosableFileFilter(new AllNeXusFilter());
106  this.fc.addChoosableFileFilter(new HdfFilter());
107  this.fc.addChoosableFileFilter(new XmlFilter());
108  this.fc.setFileFilter(nxsFilter);
109 
110  // set the default definition filename
111  this.setDefaultDefinition();
112 
113  // create a definition file chooser
114  this.nxdlChooser = new JFileChooser();
115  nxdlChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
116  nxdlChooser.setSelectedFile(new File(this.schematronFilename));
117  this.nxdlChooser.addChoosableFileFilter(new SchematronFilter());
118 
119  // ----- nexus file loading
120  // Create the filename label
121  filenameLabel = new JLabel("NXS File:");
122 
123  // Create the filename text field
124  filenameText = new JTextField(30);
125  filenameText.setToolTipText("The NeXus file to validate");
126 
127  // Create the open button.
128  browseFileButton = new JButton("Browse...");
129  browseFileButton.addActionListener(this);
130 
131  // ----- nxdl file loading
132  // Create the filename label
133  nxdlLabel = new JLabel("NXDL File:");
134 
135  // Create the text field
136  nxdlText = new JTextField(30);
137  nxdlText.setToolTipText("The definition to validate against");
138  nxdlText.setText(this.schematronFilename);
139 
140  // create the browse button
141  browseNxdlButton = new JButton("Browse...");
142  browseNxdlButton.addActionListener(this);
143 
144  // ----- the big validate button
145  // Create the validation button.
146  validateButton = new JButton("Perform Validation");
147  validateButton.setToolTipText("Perform Validation");
148  validateButton.addActionListener(this);
149 
150  // Create the tree view
151  tree = new JTree(new NXSnode());
152 
153  DefaultTreeCellRenderer rend1 = new DefaultTreeCellRenderer();
154  // IconAndTipRenderer rend2 = new
155  // IconAndTipRenderer(rend1.getOpenIcon(),
156  // rend1.getClosedIcon(), rend1.getLeafIcon());
157  // tree.setCellRenderer(rend2);
158  ToolTipManager.sharedInstance().registerComponent(tree);
159 
160  // Add the buttons and the log to this panel.
161 
162  // nxs input
163  c.fill = GridBagConstraints.HORIZONTAL;
164  c.gridy = 0;
165  c.gridx = 0;
166  c.weightx = 0.5;
167  c.insets = new Insets(0, 10, 0, 0);
168  add(filenameLabel, c);
169 
170  c.fill = GridBagConstraints.HORIZONTAL;
171  c.insets = new Insets(0, 0, 0, 0);
172  c.gridy = 0;
173  c.gridx = 1;
174  c.weightx = 0.5;
175  add(filenameText, c);
176 
177  c.fill = GridBagConstraints.HORIZONTAL;
178  c.gridy = 0;
179  c.gridx = 2;
180  c.weightx = 0.5;
181  add(browseFileButton, c);
182 
183  // nxdl input
184  c.fill = GridBagConstraints.HORIZONTAL;
185  c.gridy = 1;
186  c.gridx = 0;
187  c.weightx = 0.5;
188  c.insets = new Insets(0, 10, 0, 0);
189  add(nxdlLabel, c);
190 
191  c.fill = GridBagConstraints.HORIZONTAL;
192  c.insets = new Insets(0, 0, 0, 0);
193  c.gridy = 1;
194  c.gridx = 1;
195  c.weightx = 0.5;
196  add(nxdlText, c);
197 
198  c.fill = GridBagConstraints.HORIZONTAL;
199  c.gridy = 1;
200  c.gridx = 2;
201  c.weightx = 0.5;
202  add(browseNxdlButton, c);
203 
204  // validate button
205  c.fill = GridBagConstraints.HORIZONTAL;
206  c.gridy = 2;
207  c.gridx = 0;
208  c.weightx = 1.0;
209  c.gridwidth = 3;
210  add(validateButton, c);
211 
212  c.fill = GridBagConstraints.BOTH;
213  c.weightx = 1.0;
214  c.weighty = 1.0;
215  c.gridy = 3;
216  add(new JScrollPane(tree), c);
217 
218  c.fill = GridBagConstraints.HORIZONTAL;
219  c.weightx = 0.0;
220  c.weighty = 0.0;
221  c.gridy = 4;
222  add(logScrollPane, c);
223  }
224 
225  private void setDefaultDefinition() {
226  schematronFile = new File(this.schematronFilename);
227  }
228 
229  public TreeModel parseXML(String filename) throws Exception {
230  SAXParserFactory factory = SAXParserFactory.newInstance();
231  XMLIconTreeHandler handler = new XMLIconTreeHandler();
232  SAXParser saxParser = factory.newSAXParser();
233  saxParser.parse(new File(filename), handler);
234  return new DefaultTreeModel(handler.getRoot());
235  }
236 
237  public void actionPerformed(ActionEvent e) {
238 
239  if (e.getSource() == browseFileButton) { // Handle open button action.
240  int returnVal = fc.showOpenDialog(NXvalidateBasicGui.this);
241 
242  if (returnVal == JFileChooser.APPROVE_OPTION) {
243  File file = fc.getSelectedFile();
244  // Set the text box value to show the selected filename.
245  rawFile = file;
246  log.append("Selected NeXus File: " + file.getName() + "." + newline);
247  // TODO move code to make the reduced file here (and also after
248  // the filename has been typed).
249  } else {
250  log.append("Browse nexus cancelled by user." + newline);
251  }
252  log.setCaretPosition(log.getDocument().getLength());
253 
254  } else if (e.getSource() == browseNxdlButton) {
255  int returnVal = nxdlChooser.showOpenDialog(NXvalidateBasicGui.this);
256  if (returnVal == JFileChooser.APPROVE_OPTION) {
257  File file = nxdlChooser.getSelectedFile();
258  nxdlText.setText(file.getAbsolutePath());
259  schematronFile = file;
260  log.append("Selected Definition File: " + file.getName() + "." + newline);
261  } else {
262  log.append("Browse definition cancelled by user." + newline);
263  }
264  } else if (e.getSource() == validateButton) { // Handle save button action.
265  // Do the validation
266  try {
267 
268  validator = new NXvalidate();
269  validator.setNXSFile(rawFile);
270  validator.setConvertNxs(true);
271  validator.setSchematron(schematronFile);
272  validator.setKeepTemp(true);
273  validator.process();
274 
275  // Get the reports
276  reports = validator.getReports();
277 
278  // As we only have the ability to pass 1 file at the
279  // moment through the GUI - we will throw an exception
280  // if we have more than one report.
281 
282  if (this.reports.size() > 1) {
283  throw new Exception(
284  "We only sent a single file to be processed, but have received multiple reports back.");
285  }
286  if (this.reports.size() < 1) {
287  throw new Exception(
288  "Failed to generate a report for " + filenameText.getText());
289  }
290 
291  // converter = new NXconvert(filenameText.getText(), true);
292  // log.append("Converting NeXus file into reduced format."
293  // + newline);
294  // reducedNeXusFilename = converter.convert();
295  // // log.append(filenameText.getText() + " ==> "
296  // // + reducedNeXusFilename + newline);
297  // log.append("Finished making reduced file." + newline);
298  //
299  // // Update tree
300  // tree.setModel(this.parseXML(reducedNeXusFilename));
301  //
302  // // create the validation setup
303  // log.append("Validating against " + schematronFile + "."
304  // + newline);
305  // schematron = new NXschematron(reducedNeXusFilename,
306  // schematronFile, true);
307  // String result = schematron.validate();
308  //
309  //
310  // // create the report
311  // report = new Report(reducedNeXusFilename, result);
312  // log.append("There were " + report.numErrors() +
313  // " errors found."
314  // + newline);
315  // // log.append(report.getReport() + newline);
316  //
317 
318  log.append("Finished Validating." + newline);
319  Report report = reports.get(0);
320  tree.setModel(report.getTree());
321 
322  log.append("There were " + report.numErrors() + " errors found."
323  + newline);
324 
325  ArrayList<SVRLitem> messages = report.getReport();
326 
327  Iterator i = messages.iterator();
328  while (i.hasNext()) {
329  log.append("->" + i.next() + newline);
330  }
331 
332  } catch (Throwable e1) {
333  log.append(e1.getMessage() + newline);
334  e1.printStackTrace();
335  }
336 
337  log.setCaretPosition(log.getDocument().getLength());
338  }
339  }
340 
341  // /** Returns an ImageIcon, or null if the path was invalid. */
342  // protected static ImageIcon createImageIcon(String path) {
343  // java.net.URL imgURL = NXvalidateBasicGui.class.getResource(path);
344  // if (imgURL != null) {
345  // return new ImageIcon(imgURL);
346  // } else {
347  // System.err.println("Couldn't find file: " + path);
348  // return null;
349  // }
350  // }
355  private static void createAndShowGUI() {
356  // Create and set up the window.
357  JFrame frame = new JFrame("NeXus Validation Tool");
358  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
359 
360  // Add content to the window.
361  frame.add(new NXvalidateBasicGui());
362 
363  // Display the window.
364  frame.pack();
365  frame.setVisible(true);
366  }
367 
368  public static void main(String[] args) {
369  // Schedule a job for the event dispatch thread:
370  // creating and showing this application's GUI.
371  SwingUtilities.invokeLater(new Runnable() {
372 
373  public void run() {
374  // Turn off metal's use of bold fonts
375  UIManager.put("swing.boldMetal", Boolean.FALSE);
376  createAndShowGUI();
377  }
378  });
379  }
380 }