corpus-services  1.0
ExbMergerGUI.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package de.uni_hamburg.corpora.swing;
7 
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.MalformedURLException;
15 import java.net.URL;
16 import java.nio.file.Paths;
17 import java.util.logging.Level;
18 import java.util.logging.Logger;
19 import javax.swing.JFileChooser;
20 import javax.swing.JFrame;
21 import javax.swing.JOptionPane;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.xpath.XPathExpressionException;
27 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
28 import org.w3c.dom.Document;
29 import org.xml.sax.SAXException;
30 
35 public class ExbMergerGUI extends javax.swing.JFrame {
36 
37  ExbMerger instance;
38  String transcriptName;
39 
43  public ExbMergerGUI() {
44  initComponents();
45  }
46 
52  @SuppressWarnings("unchecked")
53  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
54  private void initComponents() {
55 
56  jPopupMenu1 = new javax.swing.JPopupMenu();
57  jPopupMenu2 = new javax.swing.JPopupMenu();
58  jPanel1 = new javax.swing.JPanel();
59  jPanel2 = new javax.swing.JPanel();
60  jLabel1 = new javax.swing.JLabel();
61  firstVersion = new javax.swing.JComboBox();
62  browseButton = new javax.swing.JButton();
63  jPanel3 = new javax.swing.JPanel();
64  jLabel2 = new javax.swing.JLabel();
65  secondVersion = new javax.swing.JComboBox();
66  browseButton2 = new javax.swing.JButton();
67  jPanel4 = new javax.swing.JPanel();
68  jScrollPane2 = new javax.swing.JScrollPane();
69  firstVersionPane = new javax.swing.JTextPane();
70  jLabel3 = new javax.swing.JLabel();
71  compareButton = new javax.swing.JButton();
72  mergeButton = new javax.swing.JButton();
73 
74  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
75 
76  jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("SansSerif", 1, 24))); // NOI18N
77  jPanel1.setName(""); // NOI18N
78 
79  jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "First Version of The Exb File", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("SansSerif", 1, 14))); // NOI18N
80 
81  jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
82  jLabel1.setText("File Path:");
83 
84  firstVersion.setEditable(true);
85  firstVersion.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
86  firstVersion.addActionListener(new java.awt.event.ActionListener() {
87  public void actionPerformed(java.awt.event.ActionEvent evt) {
88  firstVersionActionPerformed(evt);
89  }
90  });
91 
92  browseButton.setText("Browse");
93  browseButton.addActionListener(new java.awt.event.ActionListener() {
94  public void actionPerformed(java.awt.event.ActionEvent evt) {
95  browseButtonActionPerformed(evt);
96  }
97  });
98 
99  javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
100  jPanel2.setLayout(jPanel2Layout);
101  jPanel2Layout.setHorizontalGroup(
102  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
103  .addGroup(jPanel2Layout.createSequentialGroup()
104  .addContainerGap()
105  .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
106  .addGroup(jPanel2Layout.createSequentialGroup()
107  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
108  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
109  .addComponent(firstVersion, 0, 702, Short.MAX_VALUE))
110  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
111  .addGap(0, 0, Short.MAX_VALUE)
112  .addComponent(browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
113  .addContainerGap())
114  );
115  jPanel2Layout.setVerticalGroup(
116  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
117  .addGroup(jPanel2Layout.createSequentialGroup()
118  .addContainerGap()
119  .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
120  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
121  .addComponent(firstVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
122  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
123  .addComponent(browseButton)
124  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
125  );
126 
127  jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Second Version of The Exb File", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("SansSerif", 1, 14))); // NOI18N
128 
129  jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
130  jLabel2.setText("File Path:");
131 
132  secondVersion.setEditable(true);
133  secondVersion.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
134 
135  browseButton2.setText("Browse");
136  browseButton2.addActionListener(new java.awt.event.ActionListener() {
137  public void actionPerformed(java.awt.event.ActionEvent evt) {
138  browseButton2ActionPerformed(evt);
139  }
140  });
141 
142  javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
143  jPanel3.setLayout(jPanel3Layout);
144  jPanel3Layout.setHorizontalGroup(
145  jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
146  .addGroup(jPanel3Layout.createSequentialGroup()
147  .addContainerGap()
148  .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
149  .addGroup(jPanel3Layout.createSequentialGroup()
150  .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
151  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
152  .addComponent(secondVersion, 0, 702, Short.MAX_VALUE))
153  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
154  .addGap(0, 0, Short.MAX_VALUE)
155  .addComponent(browseButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
156  .addContainerGap())
157  );
158  jPanel3Layout.setVerticalGroup(
159  jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
160  .addGroup(jPanel3Layout.createSequentialGroup()
161  .addContainerGap()
162  .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
163  .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
164  .addComponent(secondVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
165  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
166  .addComponent(browseButton2)
167  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
168  );
169 
170  jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Differences Between Two Versions", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("SansSerif", 1, 14))); // NOI18N
171 
172  jScrollPane2.setViewportView(firstVersionPane);
173 
174  javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
175  jPanel4.setLayout(jPanel4Layout);
176  jPanel4Layout.setHorizontalGroup(
177  jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178  .addGroup(jPanel4Layout.createSequentialGroup()
179  .addContainerGap()
180  .addComponent(jScrollPane2)
181  .addContainerGap())
182  );
183  jPanel4Layout.setVerticalGroup(
184  jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
185  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
186  .addContainerGap()
187  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
188  .addContainerGap())
189  );
190 
191  jLabel3.setFont(new java.awt.Font("SansSerif", 1, 24)); // NOI18N
192  jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
193  jLabel3.setText("Merging of Exb Files");
194 
195  compareButton.setText("Compare");
196  compareButton.addActionListener(new java.awt.event.ActionListener() {
197  public void actionPerformed(java.awt.event.ActionEvent evt) {
198  compareButtonActionPerformed(evt);
199  }
200  });
201 
202  mergeButton.setText("Merge");
203  mergeButton.addActionListener(new java.awt.event.ActionListener() {
204  public void actionPerformed(java.awt.event.ActionEvent evt) {
205  mergeButtonActionPerformed(evt);
206  }
207  });
208 
209  javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
210  jPanel1.setLayout(jPanel1Layout);
211  jPanel1Layout.setHorizontalGroup(
212  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
213  .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
214  .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
215  .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
216  .addGroup(jPanel1Layout.createSequentialGroup()
217  .addContainerGap()
218  .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
219  .addContainerGap())
220  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
221  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
222  .addComponent(compareButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
223  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
224  .addComponent(mergeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
225  .addGap(8, 8, 8))
226  );
227  jPanel1Layout.setVerticalGroup(
228  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229  .addGroup(jPanel1Layout.createSequentialGroup()
230  .addContainerGap()
231  .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
232  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
233  .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
234  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
235  .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
236  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
237  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
238  .addComponent(compareButton)
239  .addComponent(mergeButton))
240  .addGap(6, 6, 6)
241  .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
242  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
243  );
244 
245  compareButton.getAccessibleContext().setAccessibleDescription("");
246 
247  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
248  getContentPane().setLayout(layout);
249  layout.setHorizontalGroup(
250  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251  .addGroup(layout.createSequentialGroup()
252  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
253  .addContainerGap())
254  );
255  layout.setVerticalGroup(
256  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
257  .addGroup(layout.createSequentialGroup()
258  .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
259  .addGap(0, 0, Short.MAX_VALUE))
260  );
261 
262  pack();
263  }// </editor-fold>//GEN-END:initComponents
264 
265  private void compareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compareButtonActionPerformed
266  String firstFilePath = (String) firstVersion.getSelectedItem();
267  String secondFilePath = (String) secondVersion.getSelectedItem();
268  instance = new ExbMerger();
269  URL firstURL = null;
270  URL secondURL = null;
271  try {
272  firstURL = Paths.get(firstFilePath).toUri().toURL();
273  } catch (MalformedURLException ex) {
274  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
275  }
276  try {
277  secondURL = Paths.get(secondFilePath).toUri().toURL();
278  } catch (MalformedURLException ex) {
279  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
280  }
281  BasicTranscriptionData btFirst = new BasicTranscriptionData(firstURL);
282  BasicTranscriptionData btSecond = new BasicTranscriptionData(secondURL);
283  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
284  DocumentBuilder db = null;
285  try {
286  db = dbf.newDocumentBuilder();
287  } catch (ParserConfigurationException ex) {
288  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
289  }
290  Document doc = null;
291  try {
292  doc = db.parse(TypeConverter.String2InputStream(btFirst.toSaveableString())); // get the file as a document
293  } catch (SAXException ex) {
294  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
295  } catch (IOException ex) {
296  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
297  } catch (TransformerException ex) {
298  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
299  } catch (ParserConfigurationException ex) {
300  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
301  } catch (XPathExpressionException ex) {
302  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
303  }
304  if (doc.getElementsByTagName("transcription-name").getLength() > 0) { // check if transcript name exists for the exb file
305  transcriptName = doc.getElementsByTagName("transcription-name").item(0).getTextContent(); // get transcript name
306  } else {
307  transcriptName = "No Name Transcript";
308  }
309  Report mergeReport = null;
310  try {
311  instance.check(btFirst);
312  } catch (JexmaraldaException ex) {
313  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
314  }
315  try {
316  mergeReport = instance.check(btSecond);
317  } catch (JexmaraldaException ex) {
318  Logger.getLogger(ExbMergerGUI.class.getName()).log(Level.SEVERE, null, ex);
319  }
320  firstVersionPane.setText(mergeReport.getFullReports());
321  }//GEN-LAST:event_compareButtonActionPerformed
322 
323  private void firstVersionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_firstVersionActionPerformed
324  // TODO add your handling code here:
325  }//GEN-LAST:event_firstVersionActionPerformed
326 
327  private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
328  JFileChooser chooser = new JFileChooser();
329  String filePath;
330  if (evt.getSource() == browseButton) {
331  chooser = new JFileChooser(new File(System.getProperty("user.home"))); //Home Directory as default
332  chooser.setDialogTitle("Select File Location");
333  chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
334 
335  if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
336  filePath = chooser.getSelectedFile().getPath();
337  firstVersion.setSelectedItem(filePath);
338  }
339  }
340  }//GEN-LAST:event_browseButtonActionPerformed
341 
342  private void browseButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton2ActionPerformed
343  JFileChooser chooser = new JFileChooser();
344  String filePath;
345  if (evt.getSource() == browseButton2) {
346  chooser = new JFileChooser(new File(System.getProperty("user.home"))); //Home Directory as default
347  chooser.setDialogTitle("Select File Location");
348  chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
349 
350  if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
351  filePath = chooser.getSelectedFile().getPath();
352  secondVersion.setSelectedItem(filePath);
353  }
354  }
355  }//GEN-LAST:event_browseButton2ActionPerformed
356 
357  private void mergeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mergeButtonActionPerformed
358  if (transcriptName != null) {
359  MergeFrame merge = new MergeFrame(ExbMergerGUI.this);
360  merge.setVisible(true);
361  setVisible(false);
362  } else {
363  JOptionPane.showMessageDialog(null, "Please compare two versions before merging them!");
364  }
365  }//GEN-LAST:event_mergeButtonActionPerformed
366 
370  public static void main(String args[]) {
371  /* Set the Nimbus look and feel */
372  //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
373  /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
374  * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
375  */
376  try {
377  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
378  if ("Nimbus".equals(info.getName())) {
379  javax.swing.UIManager.setLookAndFeel(info.getClassName());
380  break;
381  }
382  }
383  } catch (ClassNotFoundException ex) {
384  java.util.logging.Logger.getLogger(ExbMergerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
385  } catch (InstantiationException ex) {
386  java.util.logging.Logger.getLogger(ExbMergerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
387  } catch (IllegalAccessException ex) {
388  java.util.logging.Logger.getLogger(ExbMergerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
389  } catch (javax.swing.UnsupportedLookAndFeelException ex) {
390  java.util.logging.Logger.getLogger(ExbMergerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
391  }
392  //</editor-fold>
393 
394  /* Create and display the form */
395  java.awt.EventQueue.invokeLater(new Runnable() {
396  public void run() {
397  new ExbMergerGUI().setVisible(true);
398  }
399  });
400  }
401 
402  // Variables declaration - do not modify//GEN-BEGIN:variables
403  private javax.swing.JButton browseButton;
404  private javax.swing.JButton browseButton2;
405  private javax.swing.JButton compareButton;
406  private javax.swing.JComboBox firstVersion;
407  private javax.swing.JTextPane firstVersionPane;
408  private javax.swing.JLabel jLabel1;
409  private javax.swing.JLabel jLabel2;
410  private javax.swing.JLabel jLabel3;
411  private javax.swing.JPanel jPanel1;
412  private javax.swing.JPanel jPanel2;
413  private javax.swing.JPanel jPanel3;
414  private javax.swing.JPanel jPanel4;
415  private javax.swing.JPopupMenu jPopupMenu1;
416  private javax.swing.JPopupMenu jPopupMenu2;
417  private javax.swing.JScrollPane jScrollPane2;
418  private javax.swing.JButton mergeButton;
419  private javax.swing.JComboBox secondVersion;
420  // End of variables declaration//GEN-END:variables
421 }
static InputStream String2InputStream(String s)