
import java.awt.ComponentOrientation;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UTG.java
 *
 * Created on May 23, 2010, 2:34:44 PM
 */
/**
 *
 * @author sol
 */
public class UTG extends javax.swing.JFrame {

    /** Creates new form UTG */
    public UTG() {

        initComponents();
        applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        setTitle("Unique Tags Generator");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(3, 2));

        jLabel1.setText("الملف الأول");
        jPanel1.add(jLabel1);

        jButton1.setText("استعراض");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        jLabel2.setText("الملف الثاني");
        jPanel1.add(jLabel2);

        jButton2.setText("استعراض");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);

        jLabel3.setText("الملف النهائي");
        jPanel1.add(jLabel3);

        jButton3.setText("استعراض");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);

        getContentPane().add(jPanel1, java.awt.BorderLayout.EAST);

        jPanel2.setMinimumSize(new java.awt.Dimension(150, 75));
        jPanel2.setLayout(new java.awt.GridLayout(3, 1));
        jPanel2.add(jTextField1);
        jPanel2.add(jTextField2);
        jPanel2.add(jTextField3);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jButton4.setText("توليد الملف");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-444)/2, (screenSize.height-146)/2, 444, 146);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            JFileChooser c = new JFileChooser();
            int x = c.showOpenDialog(this);
            if (x == JFileChooser.APPROVE_OPTION) {
                file2 = c.getSelectedFile();
                jTextField2.setText(file2.getAbsolutePath());
            }

        } catch (Exception e) {
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            JFileChooser c = new JFileChooser();
            int x = c.showOpenDialog(this);
            if (x == JFileChooser.APPROVE_OPTION) {
                file1 = c.getSelectedFile();
                jTextField1.setText(file1.getAbsolutePath());
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            JFileChooser c = new JFileChooser();
            int x = c.showSaveDialog(this);
            if (x == JFileChooser.APPROVE_OPTION) {
                output = c.getSelectedFile();
                jTextField3.setText(output.getAbsolutePath());
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            br1 = new BufferedReader(new FileReader(file1));
            br2 = new BufferedReader(new FileReader(file2));
            bw = new BufferedWriter(new FileWriter(output));

            s1 = br1.readLine();
            while (s1 != null) {
                al.add(s1);
                s1 = br1.readLine();
            }

            s2 = br2.readLine();
            while (s2 != null) {
                al.add(s2);
                s2 = br2.readLine();
            }

            boolean found;
            for (int i = 0; i < al.size(); i++) {
                found = false;
                String s = al.get(i);
                for (int j = i + 1; j < al.size(); j++) {
                    if (al.get(j).equals(s)) {
                        found = true;
                        al.remove(j);
                        j--;
                    }
                }
            }

            for (String s : al) {
                bw.write(s);
                bw.newLine();
            }
            bw.close();
            String msg = "لقد تم توليد الملف بنجاح";
            String title = "مبروك !";
            JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
        } catch (FileNotFoundException ex1) {
        } catch (IOException ex2) {
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UTG().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
    File file1, file2, output;
    BufferedReader br1, br2;
    BufferedWriter bw;
    String s1, s2;
    ArrayList<String> al = new ArrayList<String>();
}
