JComparator - JComparator.java
Cerca
 











JComparator.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JComparator.java
  8.  *
  9.  * Created on 3-giu-2010, 16.23.22
  10.  */
  11. package jcomparator.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.awt.Dimension;
  15. import java.awt.Toolkit;
  16. import java.awt.datatransfer.DataFlavor;
  17. import java.awt.datatransfer.Transferable;
  18. import java.awt.datatransfer.UnsupportedFlavorException;
  19. import java.awt.dnd.DnDConstants;
  20. import java.awt.dnd.DropTarget;
  21. import java.awt.dnd.DropTargetDragEvent;
  22. import java.awt.dnd.DropTargetDropEvent;
  23. import java.awt.dnd.DropTargetEvent;
  24. import java.awt.dnd.DropTargetListener;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import javax.swing.JFileChooser;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.UIManager;
  31. import javax.swing.UnsupportedLookAndFeelException;
  32. import jcomparator.Comparator;
  33.  
  34. /**
  35.  *
  36.  * @author Paolo
  37.  */
  38. public class JComparator extends javax.swing.JFrame {
  39.  
  40.     private DropTarget frame;
  41.  
  42.     private class Drop implements DropTargetListener {
  43.  
  44.         public void dragEnter(DropTargetDragEvent dtde) {
  45.         }
  46.  
  47.         public void dragOver(DropTargetDragEvent dtde) {
  48.         }
  49.  
  50.         public void dropActionChanged(DropTargetDragEvent dtde) {
  51.         }
  52.  
  53.         public void dragExit(DropTargetEvent dte) {
  54.         }
  55.  
  56.         public void drop(DropTargetDropEvent dtde) {
  57.             Transferable obj = dtde.getTransferable();
  58.             DataFlavor[] data = obj.getTransferDataFlavors();
  59.             for (int i = 0; i < data.length; i++) {
  60.                 if (data[i].isFlavorJavaFileListType()) {
  61.                     dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
  62.                     try {
  63.                         List<File> list = (List<File>) obj.getTransferData(data[i]);
  64.                         if (list.size() <= 2) {
  65.                             if (list.size() == 1) {
  66.                                 if (primoField.getText().equals("") && !secondoField.getText().equals("")) {
  67.                                     if (!list.get(0).getAbsolutePath().equals(secondoField.getText())) {
  68.                                         primoField.setText(list.get(0).getAbsolutePath());
  69.                                     } else {
  70.                                         JOptionPane.showMessageDialog(getContentPane(), "Errore, i due file coincidono");
  71.                                     }
  72.                                 } else if (!primoField.getText().equals("") && secondoField.getText().equals("")) {
  73.                                     if (!list.get(0).getAbsolutePath().equals(primoField.getText())) {
  74.                                         secondoField.setText(list.get(0).getAbsolutePath());
  75.                                     } else {
  76.                                         JOptionPane.showMessageDialog(getContentPane(), "Errore, i due file coincidono");
  77.                                     }
  78.                                 } else {
  79.                                     primoField.setText(list.get(0).getAbsolutePath());
  80.                                 }
  81.                             } else if (list.size() == 2) {
  82.                                 primoField.setText(list.get(0).getAbsolutePath());
  83.                                 secondoField.setText(list.get(1).getAbsolutePath());
  84.                             }
  85.                         }
  86.                     } catch (UnsupportedFlavorException ex) {
  87.                     } catch (IOException ex) {
  88.                     }
  89.                     dtde.dropComplete(true);
  90.                     return;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     private class Compara implements Runnable {
  97.  
  98.         private boolean alive;
  99.         private Thread startThread;
  100.  
  101.         public Compara() {
  102.             this.alive = true;
  103.         }
  104.  
  105.         public void stop() {
  106.             this.alive = false;
  107.         }
  108.  
  109.         public void start() {
  110.             this.startThread = new Thread(this);
  111.             this.startThread.start();
  112.         }
  113.  
  114.         public void run() {
  115.             File primo = new File(primoField.getText());
  116.             File secondo = new File(secondoField.getText());
  117.             if (primo.exists() && secondo.exists()) {
  118.                 Comparator compara = new Comparator(primo, secondo);
  119.                 compara.start();
  120.                 while (this.alive) {
  121.                     numeroByte.setText(compara.getEqualByte() + "");
  122.                     percentualeByte.setValue((int) Double.parseDouble(compara.getPercentEqual()));
  123.                     percentualeByte.setString(compara.getPercentEqual() + "%");
  124.                     percentualeLetti.setValue((int) Double.parseDouble(compara.getPercentCharging()));
  125.                     percentualeLetti.setString(compara.getPercentCharging() + "%");
  126.                     try {
  127.                         Thread.sleep(1);
  128.                     } catch (InterruptedException ex) {
  129.                     }
  130.                     if (!compara.isAlive()) {
  131.                         numeroByte.setText(compara.getEqualByte() + "");
  132.                         percentualeByte.setValue((int) Double.parseDouble(compara.getPercentEqual()));
  133.                         percentualeByte.setString(compara.getPercentEqual() + "%");
  134.                         percentualeLetti.setValue((int) Double.parseDouble(compara.getPercentCharging()));
  135.                         percentualeLetti.setString(compara.getPercentCharging() + "%");
  136.                         JOptionPane.showMessageDialog(rootPane, "Comparamento utlimato!");
  137.                         comparaButton.setEnabled(true);
  138.                         this.stop();
  139.                     }
  140.                 }
  141.             } else {
  142.                 JOptionPane.showMessageDialog(rootPane, "Errore, uno dei due file non esiste");
  143.                 this.stop();
  144.                 comparaButton.setEnabled(true);
  145.             }
  146.         }
  147.     }
  148.  
  149.     /** Creates new form JComparator */
  150.     public JComparator() {
  151.         this.frame = new DropTarget(this, new Drop());
  152.         initComponents();
  153.     }
  154.  
  155.     /** This method is called from within the constructor to
  156.      * initialize the form.
  157.      * WARNING: Do NOT modify this code. The content of this method is
  158.      * always regenerated by the Form Editor.
  159.      */
  160.     @SuppressWarnings("unchecked")
  161.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  162.     private void initComponents() {
  163.  
  164.         sfogliaFile = new javax.swing.JFileChooser();
  165.         primoLabel = new javax.swing.JLabel();
  166.         secondoLabel = new javax.swing.JLabel();
  167.         primoField = new javax.swing.JTextField();
  168.         secondoField = new javax.swing.JTextField();
  169.         sfogliaButton = new javax.swing.JButton();
  170.         sfogliaButton1 = new javax.swing.JButton();
  171.         comparaButton = new javax.swing.JButton();
  172.         risultatoLabel = new javax.swing.JLabel();
  173.         risultatoLabel2 = new javax.swing.JLabel();
  174.         percentualeByte = new javax.swing.JProgressBar();
  175.         numeroByte = new javax.swing.JLabel();
  176.         lettiLabel = new javax.swing.JLabel();
  177.         percentualeLetti = new javax.swing.JProgressBar();
  178.  
  179.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  180.         setTitle("JComparator - by Paolo Ricciuti (C)");
  181.  
  182.         primoLabel.setText("Primo file:");
  183.  
  184.         secondoLabel.setText("Secondo file:");
  185.  
  186.         sfogliaButton.setText("Sfoglia..");
  187.         sfogliaButton.addActionListener(new java.awt.event.ActionListener() {
  188.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  189.                 sfogliaButtonActionPerformed(evt);
  190.             }
  191.         });
  192.  
  193.         sfogliaButton1.setText("Sfoglia..");
  194.         sfogliaButton1.addActionListener(new java.awt.event.ActionListener() {
  195.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  196.                 sfogliaButton1ActionPerformed(evt);
  197.             }
  198.         });
  199.  
  200.         comparaButton.setText("Compara");
  201.         comparaButton.addActionListener(new java.awt.event.ActionListener() {
  202.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  203.                 comparaButtonActionPerformed(evt);
  204.             }
  205.         });
  206.  
  207.         risultatoLabel.setText("Numero di byte uguali: ");
  208.  
  209.         risultatoLabel2.setText("Percentuale di byte uguali: ");
  210.  
  211.         percentualeByte.setStringPainted(true);
  212.  
  213.         numeroByte.setText("0");
  214.  
  215.         lettiLabel.setText("Percentuale di byte comparati: ");
  216.  
  217.         percentualeLetti.setStringPainted(true);
  218.  
  219.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  220.         getContentPane().setLayout(layout);
  221.         layout.setHorizontalGroup(
  222.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  223.             .addGroup(layout.createSequentialGroup()
  224.                 .addContainerGap()
  225.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  226.                     .addGroup(layout.createSequentialGroup()
  227.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  228.                             .addGroup(layout.createSequentialGroup()
  229.                                 .addComponent(primoLabel)
  230.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  231.                                 .addComponent(primoField, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
  232.                             .addGroup(layout.createSequentialGroup()
  233.                                 .addComponent(secondoLabel)
  234.                                 .addGap(18, 18, 18)
  235.                                 .addComponent(secondoField, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
  236.                         .addGap(18, 18, 18)
  237.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  238.                             .addComponent(sfogliaButton)
  239.                             .addComponent(sfogliaButton1)))
  240.                     .addComponent(comparaButton)
  241.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  242.                         .addGroup(layout.createSequentialGroup()
  243.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  244.                                 .addComponent(risultatoLabel)
  245.                                 .addComponent(risultatoLabel2))
  246.                             .addGap(29, 29, 29)
  247.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  248.                                 .addComponent(numeroByte, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
  249.                                 .addComponent(percentualeByte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  250.                         .addGroup(layout.createSequentialGroup()
  251.                             .addComponent(lettiLabel)
  252.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  253.                             .addComponent(percentualeLetti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
  254.                 .addContainerGap(44, Short.MAX_VALUE))
  255.         );
  256.         layout.setVerticalGroup(
  257.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  258.             .addGroup(layout.createSequentialGroup()
  259.                 .addGap(28, 28, 28)
  260.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  261.                     .addComponent(primoLabel)
  262.                     .addComponent(primoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  263.                     .addComponent(sfogliaButton))
  264.                 .addGap(18, 18, 18)
  265.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  266.                     .addComponent(secondoLabel)
  267.                     .addComponent(secondoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  268.                     .addComponent(sfogliaButton1))
  269.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  270.                 .addComponent(comparaButton)
  271.                 .addGap(18, 18, 18)
  272.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  273.                     .addComponent(risultatoLabel)
  274.                     .addComponent(numeroByte))
  275.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  276.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  277.                     .addComponent(percentualeByte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  278.                     .addComponent(risultatoLabel2))
  279.                 .addGap(9, 9, 9)
  280.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  281.                     .addComponent(lettiLabel)
  282.                     .addComponent(percentualeLetti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  283.                 .addContainerGap(93, Short.MAX_VALUE))
  284.         );
  285.  
  286.         pack();
  287.     }// </editor-fold>//GEN-END:initComponents
  288.  
  289.     private void sfogliaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sfogliaButtonActionPerformed
  290.         int retval = this.sfogliaFile.showOpenDialog(this);
  291.         if (retval == JFileChooser.APPROVE_OPTION) {
  292.             String path = this.sfogliaFile.getSelectedFile().getAbsolutePath();
  293.             if (!path.equalsIgnoreCase(this.secondoField.getText())) {
  294.                 this.primoField.setText(path);
  295.             } else {
  296.                 JOptionPane.showMessageDialog(rootPane, "Errore, i due file coincidono");
  297.             }
  298.         }
  299.     }//GEN-LAST:event_sfogliaButtonActionPerformed
  300.  
  301.     private void sfogliaButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sfogliaButton1ActionPerformed
  302.         int retval = this.sfogliaFile.showOpenDialog(this);
  303.         if (retval == JFileChooser.APPROVE_OPTION) {
  304.             String path = this.sfogliaFile.getSelectedFile().getAbsolutePath();
  305.             if (!path.equalsIgnoreCase(this.primoField.getText())) {
  306.                 this.secondoField.setText(path);
  307.             } else {
  308.                 JOptionPane.showMessageDialog(rootPane, "Errore, i due file coincidono");
  309.             }
  310.         }
  311.     }//GEN-LAST:event_sfogliaButton1ActionPerformed
  312.  
  313.     private void comparaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comparaButtonActionPerformed
  314.         new Compara().start();
  315.         this.comparaButton.setEnabled(false);
  316.  
  317.     }//GEN-LAST:event_comparaButtonActionPerformed
  318.  
  319.     /**
  320.      * @param args the command line arguments
  321.      */
  322.     public static void main(String args[]) {
  323.         java.awt.EventQueue.invokeLater(new Runnable() {
  324.  
  325.             public void run() {
  326.                 try {
  327.                     UIManager.setLookAndFeel(new NimbusLookAndFeel());
  328.                 } catch (UnsupportedLookAndFeelException ex) {
  329.                 }
  330.                 JComparator jc = new JComparator();
  331.                 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  332.                 int x = (screen.width - jc.getSize().width) / 2;
  333.                 int y = (screen.height - jc.getSize().height) / 2;
  334.                 jc.setLocation(x, y);
  335.                 jc.setVisible(true);
  336.             }
  337.         });
  338.     }
  339.     // Variables declaration - do not modify//GEN-BEGIN:variables
  340.     private javax.swing.JButton comparaButton;
  341.     private javax.swing.JLabel lettiLabel;
  342.     private javax.swing.JLabel numeroByte;
  343.     private javax.swing.JProgressBar percentualeByte;
  344.     private javax.swing.JProgressBar percentualeLetti;
  345.     private javax.swing.JTextField primoField;
  346.     private javax.swing.JLabel primoLabel;
  347.     private javax.swing.JLabel risultatoLabel;
  348.     private javax.swing.JLabel risultatoLabel2;
  349.     private javax.swing.JTextField secondoField;
  350.     private javax.swing.JLabel secondoLabel;
  351.     private javax.swing.JButton sfogliaButton;
  352.     private javax.swing.JButton sfogliaButton1;
  353.     private javax.swing.JFileChooser sfogliaFile;
  354.     // End of variables declaration//GEN-END:variables
  355. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.