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

import java.util.*;

// Sortieren einer Liste
// eine Objekt-Familien
// Sortieren mit Compare Interface

public class Liste07 extends JFrame {

   JList liste1;
   Vector PListe;

  JRadioButton rb_name;
  JRadioButton rb_note;
  JRadioButton rb_semester;


  //Frame konstruieren
  public Liste07() {
     setSize(420, 320);
     setTitle("JList Liste07, sortieren");
     setGUI();
  }

  void setGUI() {
     JButton bnOk;  // Aktionsschalter Links
     JButton bnESC;  // Aktionsschalter Abbruch
     JScrollPane scroll;

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


    rb_name = new JRadioButton("Namen", false);
    rb_note = new JRadioButton("Note", false);
    rb_semester = new JRadioButton("Semester", false);

    ButtonGroup group1 = new ButtonGroup();
    group1.add(rb_name);
    group1.add(rb_note);
    group1.add(rb_semester);

    JPanel panel_rb = new JPanel();
    panel_rb.setLayout ( new FlowLayout() ); // 5 Zeilen , 2 Spalte
    panel_rb.add( rb_name );
    panel_rb.add( rb_note );
    panel_rb.add( rb_semester );

    rb_name.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        rb_name_click();
      }
    });


    rb_note.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        rb_note_click();
      }
    });

    rb_semester.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        rb_semester_click();
      }
    });


    this.getContentPane().add(panel_rb, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));


      // Liste
    setData();  // create + insert
    liste1 = new JList(PListe);
    liste1.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    scroll = new JScrollPane(liste1);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,20,0,20), 0, 0));
    /*
    SINGLE_SELECTION
    SINGLE_INTERVAL_SELECTION
    MULTIPLE_INTERVAL_SELECTION
    */

    bnOk = new JButton("Ok");  // Label erzeugen

    this.getContentPane().add(bnOk, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));


    bnOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bnOK_click(e);
      }
    });

    bnESC = new JButton("Abbruch");  // Label erzeugen
    this.getContentPane().add(bnESC, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));

    bnESC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bnESC_actionPerformed(e);
      }
    });

  } // setGUI

  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void  rb_name_click() {
   System.out.println("name");
   myPruefung._sorttyp = 0;
   Collections.sort( PListe );
   liste1.updateUI();
  }

  void  rb_note_click() {
   System.out.println("note");
   myPruefung._sorttyp = 1;
   Collections.sort( PListe );
   liste1.updateUI();
  }

  void  rb_semester_click() {
   System.out.println("semester");
   myPruefung._sorttyp = 2;
   Collections.sort( PListe );
   liste1.updateUI();
  }

   // Ausgabe der selektierten Elemente
  void bnOK_click(ActionEvent e) {
     myPruefung Result;
     Object[] elements = liste1.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
          // Casting
         Result = (myPruefung) elements[i];  // Typecasting
         System.out.println( Result.toString() );
       }
       System.out.println("");
       System.out.println("");
     }
  }


  void bnESC_actionPerformed(ActionEvent e) {
      System.exit(0);
  }


     // Eintragen
    private void setData() {
      myPruefung data;

      PListe = new Vector();

      data = new myPruefung( "Grundlagen der Informatik 1", 200101, 1.3);
      PListe.add(data);

      data = new myPruefung( "Grundlagen der Informatik 2", 200102, 2.3);
      PListe.add(data);

      data = new myPruefung( "Grundlagen der Informatik 3", 200201, 1.7);
      PListe.add(data);

      data = new myPruefung( "Mathematik 1", 200101, 2.3);
      PListe.add(data);

      data = new myPruefung( "Mathematik 2", 200201, 1.0);
      PListe.add(data);

      data = new myPruefung( "Mathematik 3", 200401, 2.0);
      PListe.add(data);

      data = new myPruefung( "Physik 1", 200101, 2.3);
      PListe.add(data);

      data = new myPruefung( "Physik 2", 200201, 3.3);
      PListe.add(data);


      data = new myPruefung( "Elektrotechnik 1", 200401, 3.0);
      PListe.add(data);

      data = new myPruefung( "Elektrotechnik 2", 200202, 5.0);
      PListe.add(data);

      data = new myPruefung( "PDS", 200202, 5.0);
      PListe.add(data);

      data = new myPruefung( "PDS", 200301, 2.0);
      PListe.add(data);

    }  // setData




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


}




class myPruefung  implements Comparable {
    protected String _Pruefung;
    protected double _Note;
    protected int    _Semester;

    public static int _sorttyp=0;

    public myPruefung() {
    }

    public myPruefung( String Pruefung, int Semester, double Note) {
      _Pruefung = Pruefung;
      _Semester = Semester;
      _Note = Note;
    }

   public int compareTo(Object obj) {
     myPruefung prf = (myPruefung) obj;
     switch (_sorttyp) {
      case 0: return _Pruefung.compareToIgnoreCase(prf._Pruefung);
      case 1: if (this._Note < prf._Note)
                 return -1;
               else {
                 if (this._Note > prf._Note)
                   return 1;
                 else
                   return 0;
               }
      case 2: if (this._Semester < prf._Semester)
                 return -1;
               else {
                 if (this._Semester > prf._Semester)
                   return 1;
                 else
                   return 0;
               }
      default: return 0;
     }

   }

   public String getPruefung() {
     return _Pruefung;
   }
   public void setPruefung(String Pruefung) {
     _Pruefung = Pruefung;
   }


   public Double getNote() {
     return new Double (_Note);
   }
   public void setVorname(double Note) {
     _Note = Note;
   }

   public String toString() {
     return _Pruefung+"   "+_Semester+"    "+_Note;
   }

}  // myPruefungen



