package xml2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.io.*;  // Exception

// Opendialog
//import java.io.File;
//import javax.swing.JFileChooser;
//import javax.swing.filechooser.*;

// InternalFrame
//import javax.swing.event.*;

import java.io.*;
import java.util.Date;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
// import java.beans.*;


public class xml2 extends JFrame {
    // Menue Variablen
  private JMenuBar menuBar1;
  private JMenu MainFile;
  private JMenuItem MnSave;
  private JMenuItem MnOpen;
  private JMenuItem MnClose;


  private JTextArea _editor;

   // Konstruktor
  public xml2() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  public void setGUI() {
    this.getContentPane().setLayout( new BorderLayout() );
    this.setSize(new Dimension(600, 400));
    this.setVisible(true);

    _editor = new JTextArea("");
    _editor.setFont(new java.awt.Font("Courier New", 0, 24));
    this.getContentPane().add( new JScrollPane(_editor), BorderLayout.CENTER);


    setMenus();
  }  // setGUI


    // setzt die Menüs
  private void setMenus() {
    menuBar1 = new JMenuBar();

      // DATEI
    MainFile = new JMenu("Datei");
    MnSave = new JMenuItem("Save");
    MnOpen = new JMenuItem("Öffnen");
    MnClose = new JMenuItem("Schließen");

    MainFile.setFont(new java.awt.Font("Dialog", 0, 30));
    MnSave.setFont(new java.awt.Font("Dialog", 0, 30));
    MnOpen.setFont(new java.awt.Font("Dialog", 0, 30));
    MnClose.setFont(new java.awt.Font("Dialog", 0, 30));

    MnSave.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnSave_click();
      }
    });
    MnSave.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    MainFile.add(MnSave);

    MnOpen.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnOpen_click();
      }
    });
    MnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    MainFile.add(MnOpen);
    MainFile.addSeparator();

    // --------------------------
    MnClose.addActionListener(new ActionListener()  {

      public void actionPerformed(ActionEvent e) {
        MnClose_click();
      }
    });
    MainFile.add(MnClose);

    menuBar1.add(MainFile);

    this.setJMenuBar(menuBar1);
  } // setMenus






 private void add(String sValue) {    
    _editor.append(sValue+"\n");
  }


  void MnSave_click() {
      Person pers = new Person("Fred", "Weasley", "secret",1234);
      FileOutputStream fout=null;
      XMLEncoder encoder=null;
      try {
          /*pers.setFirstname("Fred");
          pers.setLastname("Weasley");
          pers.setPassword("secret");
          pers.setBetrag(1234);*/

          fout = new FileOutputStream("Person2.xml");
          encoder = new XMLEncoder(fout);
          String s = "Hallo";
          Date d = new Date();
          encoder.writeObject(s);
          encoder.writeObject( d );
          encoder.writeObject( pers );
          _editor.setText("Speichern:\n");
          add("String: "+s);
          add("Datum: "+d);
          add("Person: "+pers);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (encoder != null) {
                encoder.flush();
                encoder.close();
          }
      }
  }  // save


  void MnOpen_click() {
   JFileChooser jFileChooser1 = new JFileChooser();
   String sFileName;

   jFileChooser1.setMultiSelectionEnabled(false);   
   jFileChooser1.setCurrentDirectory(  new File(  (String) System.getProperties().get("user.dir")  )   );
   if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
      sFileName = jFileChooser1.getSelectedFile().getPath();
	  loadXML(sFileName);
    }
  }  // MnOpen


  private void 	 loadXML(String sFileName) {
      Person pers;;
      FileInputStream fin=null;
      XMLDecoder decoder=null;
      try {
          fin = new FileInputStream("Person1.xml");
          decoder = new XMLDecoder(fin);
          String s = (String) decoder.readObject();
          Date d = (Date) decoder.readObject();
          pers = (Person) decoder.readObject();
          add("\n\nLaden:");
          add("String: "+s);
          add("Datum: "+d);
          add("Person: "+pers);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (decoder != null) {
                decoder.close();
          }
      }
  }  // loadXML



  void MnClose_click() {
    System.exit(0);
  }

   // -----------------

  static public void main(String[] args) {
    xml2 frame = new xml2();
    frame.setVisible(true);
  }

}  // xml2


