package holaatodos;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.text.DefaultEditorKit;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.io.*;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;


/**
 * <p>Title: Hola a todos</p>
 *
 * <p>Description: este es el tutorial hola a todos</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Mi empresa</p>
 *
 * @author Mi nombre
 * @version 1.0
 */
public class HolaATodosMarco extends JFrame {
    String fichero;
    JPanel contentPane;
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileExit = new JMenuItem();

    JMenuItem jMenuFileOpenFile =new JMenuItem();
    JMenuItem jMenuFileSaveFileWS = new JMenuItem();
    JMenuItem jMenuFileSaveFile = new JMenuItem();
    JMenu jMenuHelp = new JMenu();
    JMenuItem jMenuHelpAbout = new JMenuItem();
    JToolBar jToolBar = new JToolBar();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    ImageIcon image1 = new ImageIcon(holaatodos.HolaATodosMarco.class.
                                     getResource("openFile.png"));
    ImageIcon image2 = new ImageIcon(holaatodos.HolaATodosMarco.class.
                                     getResource("closeFile.png"));
    ImageIcon image3 = new ImageIcon(holaatodos.HolaATodosMarco.class.
                                     getResource("help.png"));
    JLabel statusBar = new JLabel();
    JPanel jPanel1 = new JPanel();
    Border border1 = BorderFactory.createLineBorder(SystemColor.controlShadow,
            2);
    JButton jButton4 = new JButton();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea jTextArea1 = new JTextArea();
    JLabel jLabel2 = new JLabel();
    TitledBorder titledBorder1 = new TitledBorder("");
    Border border2 = BorderFactory.createLineBorder(SystemColor.controlShadow,
            2);
    TitledBorder titledBorder2 = new TitledBorder("");
    JLabel jLabel3 = new JLabel();
    TitledBorder titledBorder3 = new TitledBorder("");
    TitledBorder titledBorder4 = new TitledBorder("");
    JScrollPane jScrollPane2 = new JScrollPane();
    JTextArea jTextArea2 = new JTextArea();
    JButton jButton5 = new JButton();
    public HolaATodosMarco() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();


        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(900, 600));
        setTitle("WS-CDL to XML");
        statusBar.setText(" ");
        statusBar.setBounds(new Rectangle(0, 585, 900, 15));
        jMenuFile.setText("File");
        jMenuFileExit.setText("Exit");
        jMenuFileExit.addActionListener(new
                                        HolaATodosMarco_jMenuFileExit_ActionAdapter(this));
        jMenuHelp.setText("Help");
        jMenuHelpAbout.setText("About...");
        jMenuHelpAbout.addActionListener(new
                                         HolaATodosMarco_jMenuHelpAbout_ActionAdapter(this));
        jPanel1.setBackground(SystemColor.activeCaptionBorder);
        jPanel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 20));
        jPanel1.setBorder(titledBorder4);
        jPanel1.setBounds(new Rectangle(1, 30, 898, 563));
        jPanel1.setLayout(null);
        jButton4.setBounds(new Rectangle(413, 129, 79, 56));
        jButton4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
        jButton4.setHorizontalTextPosition(SwingConstants.LEFT);
        jButton4.setText("Transform");
        jButton4.addActionListener(new HolaATodosMarco_jButton4_actionAdapter(this));
        jButton1.addActionListener(new HolaATodosMarco_jButton1_actionAdapter(this));
        jToolBar.setBounds(new Rectangle(0, 0, 900, 25));
        jScrollPane1.getViewport().setBackground(SystemColor.controlHighlight);
        jScrollPane1.setBorder(BorderFactory.createRaisedBevelBorder());
        jScrollPane1.setBounds(new Rectangle(13, 56, 397, 452));
        jTextArea1.setAlignmentX((float) 1.5);
        jLabel2.setBackground(UIManager.getColor("Button.background"));
        jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 17));
        jLabel2.setAlignmentX((float) 0.6);
        jLabel2.setBorder(titledBorder2);
        jLabel2.setOpaque(true);
        jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel2.setText("WS-CDL");
        jLabel2.setBounds(new Rectangle(13, 11, 397, 42));
        jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 17));
        jLabel3.setBorder(titledBorder3);
        jLabel3.setOpaque(true);
        jLabel3.setDisplayedMnemonic('0');
        jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel3.setText("TIMED AUTOMATA");
        jLabel3.setBounds(new Rectangle(494, 12, 384, 42));
        jScrollPane2.getViewport().setBackground(SystemColor.controlHighlight);
        jScrollPane2.setBorder(BorderFactory.createRaisedBevelBorder());
        jScrollPane2.setBounds(new Rectangle(494, 57, 384, 450));
        jButton5.setBounds(new Rectangle(413, 217, 79, 30));
        jButton5.setText("Clear");
        jButton5.addActionListener(new HolaATodosMarco_jButton5_actionAdapter(this));
        jButton2.addActionListener(new HolaATodosMarco_jButton2_actionAdapter(this));

        jMenuBar1.add(jMenuFile);
        jMenuFile.add(jMenuFileOpenFile);
        jMenuFileOpenFile.setText("Open File...");
        jMenuFileOpenFile.addActionListener(new
                                         HolaATodosMarco_jMenuFileOpen_ActionAdapter(this));

        jMenuFile.add(jMenuFileSaveFileWS);
        jMenuFileSaveFileWS.setText("Save WS-CDL File...");
        jMenuFileSaveFileWS.addActionListener(new HolaATodosMarco_jMenuFileSaveFileWS_ActionAdapter(this));
        jMenuFileSaveFile.setText("Save XML File...");

        jMenuFile.add(jMenuFileSaveFile);
        jMenuFileSaveFile.addActionListener(new HolaATodosMarco_jMenuFileSave_ActionAdapter(this));
        jMenuFile.add(jMenuFileExit);



        jMenuBar1.add(jMenuHelp);
        jMenuHelp.add(jMenuHelpAbout);
        setJMenuBar(jMenuBar1);
        jButton1.setIcon(image1);
        jButton1.setToolTipText("Open File");
        jButton2.setIcon(image2);
        jButton2.setToolTipText("Close File");
        jToolBar.add(jButton1);
        jToolBar.add(jButton2);
        jPanel1.add(jScrollPane1);
        jPanel1.add(jLabel2);
        jPanel1.add(jButton4);
        jPanel1.add(jLabel3);
        jPanel1.add(jScrollPane2);
        jPanel1.add(jButton5);
        jScrollPane2.getViewport().add(jTextArea2);
        jScrollPane1.getViewport().add(jTextArea1);
        jScrollPane1.getViewport().add(jTextArea1);

        jScrollPane1.getViewport().add(jTextArea1);

        jPanel1.add(jLabel2);
        jScrollPane1.getViewport().add(jTextArea1);

        contentPane.add(jToolBar, null);
        contentPane.add(statusBar, null);
        contentPane.add(jPanel1, null);
    }

    /**
     * File | Exit action performed.
     *
     * @param actionEvent ActionEvent

     */
    void jMenuFileSaveWS_actionPerformed(ActionEvent actionEvent){
       FileDialog fd = new FileDialog(new Frame(), "Save WS-CDL file...", FileDialog.SAVE);
      fd.setFile("*.xml");
      fd.show();
      if ( fd.getDirectory() != null )
      if ( fd.getFile() != null ){
        String stt =fd.getDirectory();
        String st = fd.getFile();

        try{
          FileWriter fw = new FileWriter(stt + st);
          BufferedWriter br =new BufferedWriter(fw);
          DefaultEditorKit dek = new DefaultEditorKit();
          dek.write(br, jTextArea1.getDocument(), 0,jTextArea1.getDocument().getLength());
        fw.close();
        }
        catch(Exception e){
            System.err.println("Error: " +e);

        }
           System.out.println(fd.getDirectory() + fd.getFile());
      }else
        System.out.println("Save cancelled !");


   }

    void jMenuFileSave_actionPerformed(ActionEvent actionEvent){
        FileDialog fd = new FileDialog(new Frame(), "Save XML file...", FileDialog.SAVE);
       fd.setFile("*.xml");
       fd.show();
       if ( fd.getDirectory() != null )
       if ( fd.getFile() != null ){
         String stt =fd.getDirectory();
         String st = fd.getFile();

         try{
           FileWriter fw = new FileWriter(stt + st);
           BufferedWriter br =new BufferedWriter(fw);
           DefaultEditorKit dek = new DefaultEditorKit();
           dek.write(br, jTextArea2.getDocument(), 0,jTextArea2.getDocument().getLength());
         fw.close();
         }
         catch(Exception e){
             System.err.println("Error: " +e);

         }
            System.out.println(fd.getDirectory() + fd.getFile());
       }else
         System.out.println("Save cancelled !");


    }
    void jMenuFileOpen_actionPerformed(ActionEvent actionEvent){
        FileDialog fd=new FileDialog(new JFrame("prueba"),"Open File",FileDialog.LOAD);
        fd.show();

       if ( fd.getFile() != null )
       {

         String path=fd.getDirectory() + fd.getFile();
         fichero = path;
         try
         {
           FileInputStream fr=new FileInputStream(path);
           BufferedInputStream br=new BufferedInputStream(fr);
           int str;
           while((str=br.read())!=-1)
           {
           char c=(char)str;
           String s=""+c;
           jTextArea1.append(s);
                }


         }

         catch(Exception exc)
         {
           System.out.println(exc);
         }
            System.out.println(fd.getDirectory() + fd.getFile());


    }


    }
    void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
        System.exit(0);
    }

    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
        HolaATodosMarco_AboutBox dlg = new HolaATodosMarco_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.pack();
        dlg.show();
    }

    public void jButton4_actionPerformed(ActionEvent e) {

        try{
     // if ( fichero != null)
    //  {

            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer1 =
            tFactory.newTransformer(new StreamSource("prueba28.xsl"));
          /**  transformer1.transform(new StreamSource(fichero),
                                  new StreamResult(new FileOutputStream("salida.xml")));
          */
            transformer1.transform(new StreamSource("versioninteracciones.xml"),
                                  new StreamResult(new FileOutputStream("salida1.xml")));


          TransformerFactory t2Factory = TransformerFactory.newInstance();
            Transformer transformer2 =
                    t2Factory.newTransformer(new StreamSource("hoja2.xsl"));
            transformer2.transform(new StreamSource("salida1.xml"),
                                  new StreamResult(new
                    FileOutputStream("salida2.xml")));









          TransformerFactory t3Factory = TransformerFactory.newInstance();
            Transformer transformer3 =
                    t3Factory.newTransformer(new StreamSource("hoja3.xsl"));
            transformer3.transform(new StreamSource("salida2.xml"),
                                  new StreamResult(new
                    FileOutputStream("final.xml")));






          FileInputStream fr=new FileInputStream("final.xml");
          BufferedInputStream br=new BufferedInputStream(fr);
          int str;
          while((str=br.read())!=-1)
          {
          char c=(char)str;
          String s=""+c;
          jTextArea2.append(s);
           }

       /**
        else
        {

           jTextArea1.setText("Seleccione el fichero que desea transformar!!!");
        }*/
        }
        catch (Exception excp){
            excp.printStackTrace();
        }





    }

    public void jButton1_actionPerformed(ActionEvent e) {
        FileDialog fd=new FileDialog(new JFrame("prueba"),"Open File",FileDialog.LOAD);
        fd.show();

       if ( fd.getFile() != null )
       {

         String path=fd.getDirectory() + fd.getFile();
         try
         {
           FileInputStream fr=new FileInputStream(path);
           BufferedInputStream br=new BufferedInputStream(fr);
           int str;
           while((str=br.read())!=-1)
           {
           char c=(char)str;
           String s=""+c;
           jTextArea1.append(s);
                }


         }

         catch(Exception exc)
         {
           System.out.println(exc);
         }
            System.out.println(fd.getDirectory() + fd.getFile());


    }

}

    public void jButton5_actionPerformed(ActionEvent e) {
        jTextArea1.setText("");
        jTextArea2.setText("");

    }

    public void jButton2_actionPerformed(ActionEvent e) {
        jTextArea1.setText("");
    }


}


class HolaATodosMarco_jButton2_actionAdapter implements ActionListener {
    private HolaATodosMarco adaptee;
    HolaATodosMarco_jButton2_actionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class HolaATodosMarco_jButton5_actionAdapter implements ActionListener {
    private HolaATodosMarco adaptee;
    HolaATodosMarco_jButton5_actionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton5_actionPerformed(e);
    }
}


class HolaATodosMarco_jButton1_actionAdapter implements ActionListener {
    private HolaATodosMarco adaptee;
    HolaATodosMarco_jButton1_actionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}


class HolaATodosMarco_jButton4_actionAdapter implements ActionListener {
    private HolaATodosMarco adaptee;
    HolaATodosMarco_jButton4_actionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton4_actionPerformed(e);
    }
}
class HolaATodosMarco_jMenuFileSaveFileWS_ActionAdapter implements ActionListener{
    HolaATodosMarco adaptee;
    HolaATodosMarco_jMenuFileSaveFileWS_ActionAdapter(HolaATodosMarco adaptee){
        this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent actionEvent){
        adaptee.jMenuFileSaveWS_actionPerformed(actionEvent);
    }

}
class HolaATodosMarco_jMenuFileSave_ActionAdapter implements ActionListener{
    HolaATodosMarco adaptee;
    HolaATodosMarco_jMenuFileSave_ActionAdapter(HolaATodosMarco adaptee){
        this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent actionEvent){
        adaptee.jMenuFileSave_actionPerformed(actionEvent);
    }

}
class HolaATodosMarco_jMenuFileOpen_ActionAdapter implements ActionListener{
    HolaATodosMarco adaptee;
    HolaATodosMarco_jMenuFileOpen_ActionAdapter(HolaATodosMarco adaptee){
        this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent actionEvent){
        adaptee.jMenuFileOpen_actionPerformed(actionEvent);

    }

}
class HolaATodosMarco_jMenuFileExit_ActionAdapter implements ActionListener {
    HolaATodosMarco adaptee;

    HolaATodosMarco_jMenuFileExit_ActionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuFileExit_actionPerformed(actionEvent);

    }
}


class HolaATodosMarco_jMenuHelpAbout_ActionAdapter implements ActionListener {
    HolaATodosMarco adaptee;

    HolaATodosMarco_jMenuHelpAbout_ActionAdapter(HolaATodosMarco adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
    }
}
