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

import java.util.*;

// Sortieren einer Liste
// zwei Objekt-Familien
// Sortieren mit Compare Interface
// Zwei Klassen, mit einer Superklasse Fahrzeug
// Sortierung nach Preis, Name, Firma

public class Liste03 extends JFrame {

   JList liste1;
   Vector PListe;

  JRadioButton rb_name;
  JRadioButton rb_firma;
  JRadioButton rb_preis;


  //Frame konstruieren
  public Liste03() {
     setSize(420, 320);
     setTitle("JList Liste03, 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_firma = new JRadioButton("Firma", false);
    rb_preis = new JRadioButton("Preis", false);

    ButtonGroup group1 = new ButtonGroup();
    group1.add(rb_name);
    group1.add(rb_firma);
    group1.add(rb_preis);

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

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

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


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

    rb_preis.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        rb_preis_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));


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

    JPanel panelBn = new JPanel();
    panelBn.setLayout( new FlowLayout() );

    panelBn.add(bnOk);
    panelBn.add(bnESC);


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


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

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

  } // setGUI


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

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

  void  rb_preis_click() {
   System.out.println("semester");
   Fahrzeug._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
//    public myKFZ(String Name, String Firma, double Preis, int PS) {
//    public myLKW(String Name, String Firma, double Preis, int Ladevolumen) {
//    public myFahrrad(String Name, String Firma, double Preis, String Material) {
    private void setData() {
      PListe = new Vector();
      myKFZ kfz;
      myLKW lkw;

      kfz = new myKFZ( "Golf IV", "VW",12034.54, 88);
      PListe.add(kfz);

      kfz = new myKFZ( "A140", "Mercedes",14000.00, 120);
      PListe.add(kfz);

      kfz = new myKFZ( "Vetra", "Opel",19000.00, 165);
      PListe.add(kfz);

      kfz = new myKFZ( "Panda", "Fiat",8500.00, 160);
      PListe.add(kfz);

      lkw = new myLKW( "M100", "MAN",35001.00, 2);
      PListe.add(lkw);

      lkw = new myLKW( "A 123", "Renault",25000.00, 6);
      PListe.add(lkw);

      lkw = new myLKW( "Z 333", "Fiat",35000.00, 12);
      PListe.add(lkw);

    }  // 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) {
    Liste03 frame = new Liste03();
    frame.setVisible(true);
  }


}



class Fahrzeug  {
    protected String  _Name;
    protected double  _Preis;
    protected String  _Firma;
    protected String  _KFZ;

    public static int _sorttyp=0;

    public Fahrzeug(String Name, String Firma, double Preis) {
       _Name = Name;
       _Firma = Firma;
       _Preis = Preis;
    }


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


    public String getKFZ() {
      return _KFZ;
    }
    public void setKFZ(String KFZ) {
      _KFZ = KFZ;
    }

    public String getFirma() {
      return _Firma;
    }
    public void setFirma(String Firma) {
      _Firma = Firma;
    }

    public double getPreis() {
      return _Preis;
    }
    public void setPreis(double Preis) {
      _Preis = Preis;
    }



} // Fahrzeug

class myKFZ extends Fahrzeug implements  Comparable {
    protected int  _PS;

    public myKFZ(String Name, String Firma, double Preis, int PS) {
       super(Name, Firma, Preis);
       _PS = PS;
    }

   public int compareTo(Object obj) {
     Fahrzeug myF;
     myF =(Fahrzeug) obj;
     System.out.println(_Name+"    "+_Firma+"    "+_sorttyp );
     switch (_sorttyp) {
      case 0: return _Name.compareToIgnoreCase(myF._Name );
      case 1: return _Firma.compareToIgnoreCase(myF._Firma );
      case 2: if (this._Preis < myF._Preis )
                 return -1;
               else {
                 if (this._Preis > myF._Preis )
                   return 1;
                 else
                   return 0;
               }
      default: return 0;
     }

   } // compare



    public int getPS() {
      return _PS;
    }
    public void setPS(int PS) {
      _PS = PS;
    }

   public String toString() {
     return _Name+"   "+_Firma+"    "+_Preis+" EUR       "+_PS+" PS";
   }

}

class myLKW extends Fahrzeug implements  Comparable{
    protected int  _Ladevolumen;

    public myLKW(String Name, String Firma, double Preis, int Ladevolumen) {
       super(Name, Firma, Preis);
       _Ladevolumen = Ladevolumen;
    }

   public int compareTo(Object obj) {
     Fahrzeug myF;
     myF =(Fahrzeug) obj;
     System.out.println(_Name+"    "+_Firma+"    "+_sorttyp );
     switch (_sorttyp) {
      case 0: return _Name.compareToIgnoreCase(myF._Name );
      case 1: return _Firma.compareToIgnoreCase(myF._Firma );
      case 2: if (this._Preis < myF._Preis )
                 return -1;
               else {
                 if (this._Preis > myF._Preis )
                   return 1;
                 else
                   return 0;
               }
      default: return 0;
     }

   } // compare


    public int getLadevolumen() {
      return _Ladevolumen;
    }
    public void setLadevolumen(int Ladevolumen) {
      _Ladevolumen = Ladevolumen;
    }

   public String toString() {
     return _Name+"   "+_Firma+"    "+_Preis+" EUR      "+_Ladevolumen+ " t";
   }

}

