//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


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

import java.util.Vector;

// sortieren
import java.util.Collection;
import java.util.Collections;



public class sort1 extends JFrame {

  JButton BnSortVornamen = new JButton("Vornamen");
  JButton BnSortNamen = new JButton("Namen");
  JButton BnSortMatrnr = new JButton("Matrnr");
  JButton BnSortUnr = new JButton("Unr");
  JTextArea editor = new JTextArea();

  Vector<CData> liste = new Vector<CData>();

  public sort1() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setdata();
    setGUI();
  }

  private void setGUI() {
    setSize(600, 530);
    setTitle("JFrame als Dialog");

    BnSortVornamen.setFont( new Font("Arial", Font.BOLD,18)  );
    BnSortNamen.setFont( new Font("Arial", Font.BOLD,18)  );
    BnSortMatrnr.setFont( new Font("Arial", Font.BOLD,18)  );
    BnSortUnr.setFont( new Font("Arial", Font.BOLD,18)  );
    editor.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().setLayout( new BorderLayout() );

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnSortVornamen);
	pnButton.add(BnSortNamen);
	pnButton.add(BnSortMatrnr);
	pnButton.add(BnSortUnr);

    this.getContentPane().add( new JScrollPane(editor), BorderLayout.CENTER );
    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

          BnSortVornamen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnSort_Click(e);
            }
          });
          BnSortNamen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnSort_Click(e);
            }
          });
          BnSortMatrnr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnSort_Click(e);
            }
          });
          BnSortUnr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnSort_Click(e);
            }
          });


  } // setGUI

    private void BnSort_Click(ActionEvent e) {
       if (e.getSource() == BnSortVornamen) {
        CData.sorttyp=1;
       } 
       if (e.getSource() == BnSortNamen) {
        CData.sorttyp=2;
       } 
       if (e.getSource() == BnSortMatrnr) {
        CData.sorttyp=3;
       } 
       if (e.getSource() == BnSortUnr) {
        CData.sorttyp=4;
       } 
       Collections.sort( liste );
       editor.setText("");
       for (CData data:liste) {
         editor.append( data+"\n" );
       }
 
    }

  // Eintragen der Daten
    private void setdata() {
      CData data;

      data = new CData( "Müller","Kai", 4722, "U12345","Yellow");
        liste.add(data);

      data = new CData( "Meyer","Uwe", 4712, "U12345","Red");
        liste.add(data);

      data = new CData( "Schmidt","Petra", 4714, "U12325","Blue");
        liste.add(data);

      data = new CData( "Brian","Adams", 6714, "U13325","Pink");
        liste.add(data);

      data = new CData( "Hold","Toni", 6614, "U13345","White");
        liste.add(data);

      data = new CData( "Boll","Toni", 6613, "U13345","Maroon");
        liste.add(data);

      data = new CData( "Seneca","Toni", 6314, "U13341","Gray");
        liste.add(data);

      data = new CData( "Kredel","Helmut", 8613, "U23345","Phthalo Blue");
        liste.add(data);


      data = new CData( "Bräunl","Thomas", 8700, "U23315","Maroon");
        liste.add(data);
      data = new CData( "Doug","Lea", 3613, "U11345","Blue");
        liste.add(data);
      data = new CData( "Rauber","Rünger", 5612, "U53344","Red");
        liste.add(data);
      data = new CData( "Walrath","Kathy", 4613, "U33345","Green");
        liste.add(data);
      data = new CData( "Weber","Harald", 9613, "U23345","Black");
        liste.add(data);

    }  // setData



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

}  // sort1


class CData implements  Comparable{
    public static int sorttyp=0;


    private String _Name;
    private String _Vorname;
    private int _MatrNummer;
    private String _UNummer;
    private String _Farbe;

    public CData() {
    }


    public CData( String Name, String Vorname, int MatrNummer, String UNummer, String Farbe) {
      _Name = Name;
      _Vorname = Vorname;
      _MatrNummer = MatrNummer;
      _UNummer = UNummer;
      _Farbe = Farbe;
    }


  public int compareTo2(Object obj) {
     CData myD;
     myD = (CData) obj;
     System.out.println("in compare: "+_Vorname+"   "+_Name+"    "+_MatrNummer+"    "+sorttyp );
     switch (sorttyp) {
      case 1: return _Vorname.compareToIgnoreCase(myD._Vorname );
      case 2: return _Name.compareToIgnoreCase(myD._Name );
      case 3: if (_MatrNummer < myD._MatrNummer )
                 return -1;
               else {
                 if (_MatrNummer > myD._MatrNummer )
                   return 1;
                 else
                   return 0;
               }
      case 4: return _UNummer.compareToIgnoreCase(myD._UNummer );
      default: return 0;
     }

   }

   public String toString() {
     return _Vorname+"  "+_Name+"  "+_MatrNummer+"  "+_UNummer;


   }

   public String getName() {
     return _Name;
   }
   public void setName(String Name) {
     _Name = Name;
   }


   public String getVorname() {
     return _Vorname;
   }
   public void setVorname(String Vorname) {
     _Vorname = Vorname;
   }

   public String getMatrNummer() {
     return Integer.toString(_MatrNummer);
   }
   public void setMatrNummer(int MatrNummer) {
     _MatrNummer = MatrNummer;
   }



   public String getUNummer() {
     return _UNummer;
   }
   public void setUNummer(String UNummer) {
     _UNummer = UNummer;
   }


   public String getFarbe() {
     return _Farbe;
   }
   public void setFarbe(String Farbe) {
     _Farbe = Farbe;
   }

}  // CData
