/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  05.09.2007
   Letzte Änderung: 12.11.2010
*/

// Serialize4 mit zwei Klassen (Person Parson  und Gehalt)
// Verschlüsselung des Passwortes
// eine Person HAT eine Gehalt
// Verschluesselung des Passworts
//  BnRead_Clicked
//  BnWrite_Clicked
// Ausgabe in einem Editor


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;

import java.util.Vector;

public class Serialize4 extends JFrame {

  private final String C_FILENAME = "serialize4.ser";

    // Menü Variablen
  private JMenuBar menuBar1;
  private JMenu MainFile;

  private JMenuItem MnLoad;
  private JMenuItem MnSave;
  private JMenuItem MnClose;

    JLabel jLabel1;
    JTextField TName;

    JLabel jLabel2;
    JTextField TMatrNr;

   // Konstruktor
  public Serialize4() {
    setSize(new Dimension(550, 400));
    setLocation(200,100);
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
    setMenus();
    setToolBars();
  }

  public void setGUI() {
    this.getContentPane().setLayout( new BorderLayout() );
    jLabel1 = new JLabel("Name");
    TName = new JTextField("Test");

    jLabel2 = new JLabel("MatrNr");
    TMatrNr = new JTextField("123");

    jLabel1.setFont( new Font("Arial", Font.BOLD,28));
    jLabel2.setFont( new Font("Arial", Font.BOLD,28));
    TName.setFont( new Font("Arial", Font.BOLD,28));
    TMatrNr.setFont( new Font("Arial", Font.BOLD,28));

    JPanel pnCenter=new JPanel();
    pnCenter.setLayout( new GridBagLayout() );
    this.getContentPane().add(pnCenter, BorderLayout.CENTER);

    pnCenter.add(jLabel1,
            new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20,20,20,0), 0, 0));

    pnCenter.add(TName,
            new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));

    pnCenter.add(jLabel2,
            new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20,20,20,0), 0, 0));

    pnCenter.add(TMatrNr,
            new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));


  }  // setGUI


  private void setToolBars() {
    JToolBar toolBar = new JToolBar();

    JButton BnLoad = new JButton("Laden");
    JButton BnSave = new JButton("Speichern");
    JButton BnClear = new JButton("Löschen");

    BnLoad.setFont( new Font("Arial", Font.BOLD,28));
    BnSave.setFont( new Font("Arial", Font.BOLD,28));
    BnClear.setFont( new Font("Arial", Font.BOLD,28));

    toolBar.add(BnLoad);
    BnLoad.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        MnLoad_click();  
      }
    });


    toolBar.addSeparator( new Dimension(20,10) );

    BnSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        MnSave_click();
      }
    });
    toolBar.add(BnSave);

    toolBar.addSeparator( new Dimension(20,10) );
    BnClear.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnClear_click();
      }
    });
    toolBar.add(BnClear);

    this.getContentPane().add(toolBar, BorderLayout.NORTH);
  } // setToolBars



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

      // DATEI
    MainFile = new JMenu("Datei");
    MnLoad = new JMenuItem("Laden");
    MnSave = new JMenuItem("Sichern");
    MnClose = new JMenuItem("Schließen");

     // Hauptmenü
    MnLoad.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnLoad_click();
      }
    });
    MnLoad.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
    MainFile.add(MnLoad);
    MainFile.addSeparator();


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

    // --------------------------
    MnClose.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnClose_click();
      }
    });
    MainFile.add(MnClose);


    menuBar1.add(MainFile);


    // setzen aller Menüeinträge auf einen Font,
    // Beachten:  Separator !
    int m = menuBar1.getMenuCount();
    for (int i=0; i<m; i++) {
      JMenu main = menuBar1.getMenu(i);
      main.setFont( new Font("Arial", Font.BOLD,18)  );
      int n = main.getItemCount();
      JMenuItem mn;
      for (int j=0; j<n; j++) {
        mn = main.getItem(j);
        // Abfrage auf Separator, sonst Absturz
        if ( mn instanceof JMenuItem )
          mn.setFont( new Font("Arial", Font.BOLD,18)  );
      }
    }
    this.setJMenuBar(menuBar1);
  } // setMenus



  void MnSave_click() {
    System.out.println("Menüeintrag MnSave");
		String sPath = "./"+C_FILENAME;
     // wenn mehrere Daten gespeichert werden sollen
       // LinkedList<CStudent> list = null;  
       //Vector<CStudent> list=new Vector<CStudent>();
       //list = new LinkedList<CStudent>(); 
    try{
      String sName, sMatrNr;
      sName = TName.getText();
      sMatrNr = TMatrNr.getText();
      int MatrNr = Integer.parseInt(sMatrNr);
      CStudent std = new CStudent(sName, MatrNr);
       //list.add( std );
      ObjectOutputStream objectOutput = new ObjectOutputStream( new FileOutputStream(sPath));
      objectOutput.writeObject(  std );
  	} catch (FileNotFoundException e1) {
  		//e.printStackTrace();
       System.out.println("FilenotFoundException");
    } catch (IOException e2) {
  		//e.printStackTrace();
       System.out.println("IOException");
    }

  }  // MnSave_click


    // öffnet eine Datei, und liest per Serialize
    // eventuell auch die Extension abprüfen
  void MnLoad_click() {
    System.out.println("Menüeintrag MnLoad");
    String sPath = "./"+C_FILENAME;
    try{
      CStudent std; // = new CStudent(sName, MatrNr);
      ObjectInputStream objectInput = new ObjectInputStream( 	new FileInputStream(sPath));
      std = (CStudent) objectInput.readObject();
      TName.setText( std.getName() );
      TMatrNr.setText( std.getsMatrnr() );
  	} catch (ClassNotFoundException e1) {
  		//e.printStackTrace();
  	} catch (FileNotFoundException e1) {
       System.out.println("FilenotFoundException");
  		//e.printStackTrace();
    } catch (IOException e2) {
  		//e.printStackTrace();
       System.out.println("IOException");
    }

  }  // MnLoad

  private void BnClear_click() {
    TName.setText( "-" );
    TMatrNr.setText( "-" );
  }

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


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

}


// benoetigt import java.io.Serializable;

class CStudent implements Serializable {

  private static final long serialVersionUID = 4711L;

  private String _sName;
  private int _MatrNr;

  public  CStudent(String sName, int MatrNr) {
    _sName=sName;
    _MatrNr=MatrNr;
  }

  public String getName() {
    return _sName;
  }

  public String getsMatrnr() {
    return Integer.toString(_MatrNr);
  }


}  // CStudent
