NXvalidate  1
 All Classes Namespaces Files Functions Variables
BulkLoadFilesFrame.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  * BulkLoadFilesFrame.java
24  *
25  * Created on 10-Aug-2010, 15:32:56
26  */
27 
28 package org.nexusformat.nxvalidate;
29 
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.ResourceBundle;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import javax.swing.table.DefaultTableModel;
38 
43 public class BulkLoadFilesFrame extends javax.swing.JFrame {
44 
45  private File nxdlFile = null;
46  private ArrayList<String> dataFileList = null;
47  private FileActions fileLoadingActions = null;
48  private boolean badFiles = false;
49  private ResourceBundle bundle = null;
50  private ArrayList<String> dataFileSelectedList = null;
51  private CheckNexusFileType check = null;
52 
54  public BulkLoadFilesFrame(FileActions fileLoadingActions) {
55  initComponents();
56  this.fileLoadingActions = fileLoadingActions;
57  bundle = ResourceBundle.getBundle(
58  "org/nexusformat/nxvalidate/resources/nxvalidate");
59  check = new CheckNexusFileType();
60 
61  }
62 
63  public BulkLoadFilesFrame() {
64  initComponents();
65  bundle = ResourceBundle.getBundle(
66  "org/nexusformat/nxvalidate/resources/nxvalidate");
67  check = new CheckNexusFileType();
68  }
69 
75  @SuppressWarnings("unchecked")
76  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
77  private void initComponents() {
78 
79  jFileChooser1 = new javax.swing.JFileChooser();
80  problemOptionPane = new javax.swing.JOptionPane();
81  jSplitPane1 = new javax.swing.JSplitPane();
82  jPanel1 = new javax.swing.JPanel();
83  loadNXDCButton = new javax.swing.JButton();
84  loadDataFilesButton = new javax.swing.JButton();
85  nxdcFileNameTextField = new javax.swing.JTextField();
86  validateButton = new javax.swing.JButton();
87  cancelButton = new javax.swing.JButton();
88  jCheckBox1 = new javax.swing.JCheckBox();
89  jScrollPane1 = new javax.swing.JScrollPane();
90  jTable1 = new javax.swing.JTable();
91 
92  jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
93 
94  jPanel1.setVerifyInputWhenFocusTarget(false);
95 
96  loadNXDCButton.setText("Load NXDL");
97  loadNXDCButton.setActionCommand("Load NXDL");
98  loadNXDCButton.addActionListener(new java.awt.event.ActionListener() {
99  public void actionPerformed(java.awt.event.ActionEvent evt) {
100  loadNXDCButtonActionPerformed(evt);
101  }
102  });
103 
104  loadDataFilesButton.setText("Load Data Files");
105  loadDataFilesButton.addActionListener(new java.awt.event.ActionListener() {
106  public void actionPerformed(java.awt.event.ActionEvent evt) {
107  loadDataFilesButtonActionPerformed(evt);
108  }
109  });
110 
111  validateButton.setLabel("Validate");
112  validateButton.addActionListener(new java.awt.event.ActionListener() {
113  public void actionPerformed(java.awt.event.ActionEvent evt) {
114  validateButtonActionPerformed(evt);
115  }
116  });
117 
118  cancelButton.setText("Cancel");
119  cancelButton.addActionListener(new java.awt.event.ActionListener() {
120  public void actionPerformed(java.awt.event.ActionEvent evt) {
121  cancelButtonActionPerformed(evt);
122  }
123  });
124 
125  jCheckBox1.setText("Load files only.");
126  jCheckBox1.setToolTipText("Load files only, do not validate.");
127  jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
128  public void actionPerformed(java.awt.event.ActionEvent evt) {
129  jCheckBox1ActionPerformed(evt);
130  }
131  });
132 
133  javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
134  jPanel1.setLayout(jPanel1Layout);
135  jPanel1Layout.setHorizontalGroup(
136  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
137  .addGroup(jPanel1Layout.createSequentialGroup()
138  .addContainerGap()
139  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
140  .addGroup(jPanel1Layout.createSequentialGroup()
141  .addComponent(loadNXDCButton)
142  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
143  .addComponent(nxdcFileNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE))
144  .addGroup(jPanel1Layout.createSequentialGroup()
145  .addComponent(loadDataFilesButton)
146  .addGap(92, 92, 92)
147  .addComponent(jCheckBox1)
148  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
149  .addComponent(cancelButton)
150  .addGap(33, 33, 33)
151  .addComponent(validateButton)))
152  .addContainerGap(17, Short.MAX_VALUE))
153  );
154  jPanel1Layout.setVerticalGroup(
155  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
156  .addGroup(jPanel1Layout.createSequentialGroup()
157  .addContainerGap()
158  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
159  .addComponent(loadNXDCButton)
160  .addComponent(nxdcFileNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
161  .addGap(18, 18, 18)
162  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
163  .addComponent(loadDataFilesButton)
164  .addComponent(validateButton)
165  .addComponent(cancelButton)
166  .addComponent(jCheckBox1))
167  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
168  );
169 
170  jSplitPane1.setTopComponent(jPanel1);
171 
172  jTable1.setModel(new javax.swing.table.DefaultTableModel(
173  new Object [][] {
174  {null, null}
175  },
176  new String [] {
177  "Include", "File Name"
178  }
179  ) {
180  Class[] types = new Class [] {
181  java.lang.Boolean.class, java.lang.String.class
182  };
183 
184  public Class getColumnClass(int columnIndex) {
185  return types [columnIndex];
186  }
187  });
188  jScrollPane1.setViewportView(jTable1);
189  jTable1.getColumnModel().getColumn(0).setMinWidth(60);
190  jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
191  jTable1.getColumnModel().getColumn(0).setMaxWidth(60);
192 
193  jSplitPane1.setRightComponent(jScrollPane1);
194 
195  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
196  getContentPane().setLayout(layout);
197  layout.setHorizontalGroup(
198  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199  .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
200  );
201  layout.setVerticalGroup(
202  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
203  .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
204  );
205 
206  pack();
207  }// </editor-fold>//GEN-END:initComponents
208 
209  private void loadNXDCButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadNXDCButtonActionPerformed
210  if (evt.getSource() == loadNXDCButton) {
211 
212  jFileChooser1.setMultiSelectionEnabled(false);
213  jFileChooser1.setFileSelectionMode(jFileChooser1.FILES_ONLY);
214  int returnVal = jFileChooser1.showOpenDialog(this);
215 
216  if (returnVal == jFileChooser1.APPROVE_OPTION) {
217  nxdlFile = jFileChooser1.getSelectedFile();
218 
219  if(check.checkNXDLFile(nxdlFile)){
220 
221  nxdcFileNameTextField.setText(nxdlFile.getAbsolutePath());
222  nxdcFileNameTextField.setToolTipText(nxdlFile.getAbsolutePath());
223 
224  } else{
225  nxdlFile = null;
226  problemOptionPane.showMessageDialog(this,
227  bundle.getString("notNXDLFileMessage"));
228  }
229 
230 
231  } else {
232  nxdlFile = null;
233  }
234 
235  }
236  }//GEN-LAST:event_loadNXDCButtonActionPerformed
237 
238  private void loadDataFilesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDataFilesButtonActionPerformed
239  if (evt.getSource() == loadDataFilesButton) {
240 
241  dataFileList = new ArrayList<String>();
242 
243  File[] dataFiles = null;
244  jFileChooser1.setMultiSelectionEnabled(true);
245  jFileChooser1.setFileSelectionMode(jFileChooser1.FILES_AND_DIRECTORIES);
246  int returnVal = jFileChooser1.showOpenDialog(this);
247 
248  if (returnVal == jFileChooser1.APPROVE_OPTION) {
249  try {
250  dataFiles = jFileChooser1.getSelectedFiles();
251  fileLoadingActions.setDataFiles(dataFiles);
252  fileLoadingActions.setWhich(3);
253  fileLoadingActions.bulkCheck();
254  dataFileList = fileLoadingActions.getDataFileList();
255  updateTable();
256  } catch (FileNotFoundException ex) {
257  Logger.getLogger(BulkLoadFilesFrame.class.getName()).log(Level.SEVERE, null, ex);
258  } catch (IOException ex) {
259  Logger.getLogger(BulkLoadFilesFrame.class.getName()).log(Level.SEVERE, null, ex);
260  }
261  } else {
262  dataFiles = null;
263  }
264 
265  }
266  }//GEN-LAST:event_loadDataFilesButtonActionPerformed
267 
268  private void updateTable() throws FileNotFoundException, IOException {
269 
270  DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{
271  {null, null}
272  },
273  new String[]{
274  "Include", "File Name"
275  }) {
276 
277  Class[] types = new Class[]{
278  java.lang.Boolean.class, java.lang.String.class
279  };
280 
281  public Class getColumnClass(int columnIndex) {
282  return types[columnIndex];
283  }
284  };
285 
286  tableModel.removeRow(0);
287 
288  Boolean useIt = new Boolean(true);
289  Object[] row = new Object[2];
290  for (int i = 0; i < dataFileList.size(); ++i) {
291  row[1] = dataFileList.get(i);
292  if (check.checkNexusFile(new File(dataFileList.get(i)))) {
293  row[0] = new Boolean(true);
294  } else {
295  row[0] = new Boolean(false);
296  badFiles = true;
297  }
298 
299  tableModel.addRow(row);
300  }
301 
302  jTable1.setModel(tableModel);
303  jTable1.getColumnModel().getColumn(0).setMinWidth(60);
304  jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
305  jTable1.getColumnModel().getColumn(0).setMaxWidth(60);
306 
307  if (badFiles) {
308  problemOptionPane.showMessageDialog(this,
309  bundle.getString("filesNotNexusError"));
310  }
311 
312  }
313 
314  private void validateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validateButtonActionPerformed
315 
316  if (evt.getSource() == validateButton) {
317 
318  if (nxdlFile == null) {
319  problemOptionPane.showMessageDialog(this,
320  bundle.getString("openSchemaFileMessage"));
321  return;
322  }
323 
324  if (fileLoadingActions.getNXConvertFile() == null) {
325  problemOptionPane.showMessageDialog(this,
326  bundle.getString("noConvertCommandFound"));
327  return;
328  }
329 
330 
331  Boolean checked = null;
332  dataFileSelectedList = new ArrayList<String>();
333 
334  int rows = jTable1.getModel().getRowCount();
335 
336  for (int i = 0; i < rows; ++i) {
337  checked = (Boolean) jTable1.getModel().getValueAt(i, 0);
338 
339  if (checked) {
340  dataFileSelectedList.add((String) jTable1.getModel().getValueAt(i, 1));
341  }
342 
343  }
344 
345  if (jCheckBox1.isSelected()) {
346 
347  fileLoadingActions.setWhich(4);
348  fileLoadingActions.setNXDLFile(nxdlFile);
349  fileLoadingActions.setDataFileList(dataFileSelectedList);
350 
351  (new Thread(fileLoadingActions)).start();
352 
353  } else {
354 
355  fileLoadingActions.setWhich(5);
356  fileLoadingActions.setNXDLFile(nxdlFile);
357  fileLoadingActions.setDataFileList(dataFileSelectedList);
358 
359  (new Thread(fileLoadingActions)).start();
360 
361  }
362  }
363  }//GEN-LAST:event_validateButtonActionPerformed
364 
365  private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
366 
367  if (evt.getSource() == jCheckBox1) {
368 
369  if (jCheckBox1.isSelected()) {
370  validateButton.setText("Load");
371  } else {
372  validateButton.setText("Validate");
373  }
374  }
375 
376  }//GEN-LAST:event_jCheckBox1ActionPerformed
377 
378  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
379  if (evt.getSource() == cancelButton) {
380  this.setVisible(false);
381  }
382  }//GEN-LAST:event_cancelButtonActionPerformed
383 
387  public static void main(String args[]) {
388  java.awt.EventQueue.invokeLater(new Runnable() {
389 
390  public void run() {
391  new BulkLoadFilesFrame().setVisible(true);
392  }
393  });
394  }
395  // Variables declaration - do not modify//GEN-BEGIN:variables
396  private javax.swing.JButton cancelButton;
397  private javax.swing.JCheckBox jCheckBox1;
398  private javax.swing.JFileChooser jFileChooser1;
399  private javax.swing.JPanel jPanel1;
400  private javax.swing.JScrollPane jScrollPane1;
401  private javax.swing.JSplitPane jSplitPane1;
402  private javax.swing.JTable jTable1;
403  private javax.swing.JButton loadDataFilesButton;
404  private javax.swing.JButton loadNXDCButton;
405  private javax.swing.JTextField nxdcFileNameTextField;
406  private javax.swing.JOptionPane problemOptionPane;
407  private javax.swing.JButton validateButton;
408  // End of variables declaration//GEN-END:variables
409 }