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

import java.util.Vector;

// Beispiel mit zwei Listen
// Listen durch ein Array

public class Liste09c extends JFrame {

   JList jlist;
   Vector liste;

  //Frame konstruieren
  public Liste09c() {
     JButton bnOk;  // Aktionsschalter Links
     JButton bnESC;  // Aktionsschalter Abbruch
     JScrollPane scroll;

     setSize(420, 320);
     setTitle("JList Liste09b");

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

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

    scroll = new JScrollPane(jlist);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 0, 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, 1, 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, 1, 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);
      }
    });

    jlist.setFont( new Font("Arial", Font.BOLD,18)  );
    bnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    bnESC.setFont( new Font("Arial", Font.BOLD,18)  );
  } // create


   // Ausgabe der selektierten Elemente
  void bnOK_click(ActionEvent e) {
     CPruefung prf;
     CCircle kreis;
     Object[] elements = jlist.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
          // Casting, geht hier schief, da zwei Klassen
         ITyp iObj;
         iObj = (ITyp) elements[i];

         switch (iObj.getTyp() ) {
           case 1: System.out.println( "Prüfung" );
                   break;
           case 2: System.out.println( "Kreis" );
                   break;
           default: System.out.println( "error" );
                   break;
         }


       }
       System.out.println("");
       System.out.println("");
     }
  }


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


     // Eintragen
    private void setData() {
      CCircle c;
      CPruefung prf;

      liste = new Vector();

      prf = new CPruefung("GDI1",1.3);
      liste.add(prf);

      c = new CCircle(0,0,1);
      liste.add(c);
      c = new CCircle(1,0,1);
      liste.add(c);

      prf = new CPruefung("PDS2",1.7);
      liste.add(prf);

      c = new CCircle(3,2,1);
      liste.add(c);

      prf = new CPruefung("GDI2",2.0);
      liste.add(prf);
    }  // setData


  //Ü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);
    }
  }


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


}



interface ITyp {
  public int getTyp();
}

class CPruefung    implements   ITyp {
    private String _Pruefung;
    private double _Note;

    public int getTyp() {
      return 1;
    }
    public CPruefung() {
    }

    public CPruefung( String Pruefung, double Note) {
      _Pruefung = Pruefung;
      _Note = Note;
    }


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


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

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


}  // CPruefungen


class CCircle implements ITyp {
  int r, x,y;

    public int getTyp() {
      return 2;
    }

  public CCircle(int x, int y, int r) {
    this.x=x;
    this.y=y;
    this.r=r;
  }

  public String toString() {
    return "X: "+Integer.toString(x)+
      " Y: "+Integer.toString(y)+
      "     R: "+Integer.toString(r);
  }

}
