//Titel:            SDI-Fenster
//Version:          1.0
//Copyright:
//Autor:
//Firma:
//Beschreibung:


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 javax.xml.stream.XMLInputFactory;
//import javax.xml.stream.*;
import java.io.*;
import java.util.Date;

import java.beans.XMLEncoder;
import java.beans.*;

//import org.w3c.dom.Document;
//import org.xml.sax.SAXException;

//dient der formatierten Ausgabe von XML-Strings
//import com.sun.org.apache.xml.internal.serialize.OutputFormat;
//serialize xml
//import com.sun.org.apache.xml.internal.serialize.XMLSerializer;


public class xml2 extends JFrame {
    // Menü 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








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

          fout = new FileOutputStream("Person1.xml");
          encoder = new XMLEncoder(fout);
          //encoder.writeObject("hallo");
          // encoder.writeObject( new Date() );
          encoder.writeObject( pers );
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (encoder != null) {
                encoder.flush();
                encoder.close();
          }
      }   
  }


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

   jFileChooser1.setMultiSelectionEnabled(false);
   jFileChooser1.setCurrentDirectory( new File("C:\\DATEN") );
   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 add(int tab, String sValue) {
    String sTab="";
    int i;
    for (i=0; i<tab; i++) sTab+="  ";
    _editor.append(sTab+sValue+"\n");
  }

  private void  loadXML(String sFileName) {
    int i;
    int tab;
  }



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

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

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

}  // xml2



class Person implements Serializable {

   // private static final long serialVersionUID = 20080330L;

    private  String firstname;
    private String lastname;
    private String password;
    private double betrag=2;

    public Person() {
        this.firstname = "";
        this.lastname = "";
        this.password = "";
        betrag=0;
    }

    public Person(String firstname, String lastname, String password) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        betrag=123;
    }

    public String toString() {
        return new String(" "+lastname + ", " + firstname+ "\n Passwort: " + password+" betrag:" +betrag);
    }

    public void setFirstname(String firstName) {
      this.firstname = firstname;
    }
    public void setLastname(String lastName) {
      this.lastname = lastname;
    }
    public void setPassword(String password) {
      this.password = password;
    }
    public void  setBetrag(double betrag) {
      this.betrag = betrag;
    }

    public String getFirstname() {
      return  firstname;
    }
    public String getLastname() {
      return  lastname;
    }
    public String getPassword() {
      return  password;
    }

    public double getBetrag() {
      return betrag;
    }

}  // Person

