
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

import java.util.Vector;

// Schalter Event
import java.awt.event.*;

import java.util.Collection;
import java.util.Collections;




// Tabelle mit einer Klasse  und sortierung

public class Tabelle08 extends JFrame {

   JComboBox comboSort;
   JTable tabelle;
   Vector sortListe;

   Vector liste = new Vector();

   final String[]
   names = {"Vorname", "Nachname", "MatrNr", "UNumer", "Farbe"};

    public Tabelle08() {
      // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setGUI();
      setSize(600,400);
    }


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


    private void setGUI() {

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

        JToolBar toolBar = new JToolBar();
        JLabel  label1 = new JLabel("Sortierung");
        toolBar.add( label1 );
        label1.setFont( new Font("Arial", Font.BOLD,18)  );

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

        sortListe = new Vector();
        sortListe.add("keine");
        sortListe.add("Vorname");
        sortListe.add("Nachname");
        sortListe.add("MatrNr");
        sortListe.add("UNummer");
        //sortListe.add("Farbe");
        comboSort = new JComboBox(sortListe);

        comboSort.setSelectedIndex( 0 );
        toolBar.add( comboSort );
        comboSort.setFont( new Font("Arial", Font.BOLD,18)  );

        this.getContentPane().add(toolBar, BorderLayout.NORTH);

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

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


        setdata();
        tabelle = createTable();
        tabelle.setFont( new Font("Arial", Font.BOLD,18)  );


        // Finish setting up the table.
        JScrollPane scrollpane = new JScrollPane(tabelle);
      	scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));

        scrollpane.setPreferredSize(new Dimension(430, 200));

        this.getContentPane().add(scrollpane, BorderLayout.CENTER);

        tabelle.addMouseListener(new java.awt.event.MouseAdapter() {

          public void mouseClicked(MouseEvent e) {
            tabelle_mouseClicked(e);
          }
        });
    }


    public int getButton(MouseEvent e) {
        int mode = e.getModifiers();
        if ((mode & e.BUTTON2_MASK) > 0) {
            return 2;
        }
        if ((mode & e.BUTTON3_MASK) > 0) {
            return 3;
        }
        return 1;
    } // getButton

      void tabelle_mouseClicked(MouseEvent e) {
        if (  getButton(e) == 1) {
           if ( e.getClickCount() == 2) {
            System.out.println("doppel");
           }
           if ( e.getClickCount() == 1) {
            System.out.println("einfach");
           }
         }
      }



  // createTable
  public JTable createTable() {
    // Create a model of the data.
        TableModel dataModel = new AbstractTableModel() {
            // These methods always need to be implemented.
            public int getColumnCount() {
              return names.length;
            }
            public int getRowCount() {
              return liste.size();
            }

            public Object getValueAt(int row, int col) {
              myData data = (myData) liste.elementAt(row);
             // return data.returnTableData(col);

              switch (col) {
                case 0: return data.getVorname();
                case 1: return data.getName();
                case 2: return data.getMatrNummer();
                case 3: return data.getUNummer();
                case 4: return data.getFarbe();
                default: return "xxxxx"+Integer.toString(col);
              }
            }

            // The default implementations of these methods in
              // AbstractTableModel would work, but we can refine them.
            public String getColumnName(int column) {
              return names[column];
            }
            public Class getColumnClass(int c) {
              return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
              return false; //true;
            }
            public void setValueAt(Object aValue, int row, int column) {
              myData data = (myData) liste.elementAt(row);
              switch( column ) {
                case 0: data.setName( (String) aValue );
                case 1: data.setVorname( (String) aValue );
                case 2: //data.setMatrNummer( aValue );
                case 3: data.setUNummer( (String) aValue );
              }
            }
         };


     JTable table = new JTable(dataModel);

         // One renderer for each column
        TableColumn tc;
        for(int i=0; i<dataModel.getColumnCount(); i++) {
          tc = table.getColumnModel().getColumn(i);
          if(tc!=null) {
            tc.setHeaderValue( Integer.toString(i+1) );

            // tc.setHeaderRenderer(new TableHeadRenderer("/printer.gif"));
          }
        }

        return table;
  } // createTable

   void TableHeader_mouseClicked(MouseEvent e) {
   }

   private void   comboSort_click() {
       int i = comboSort.getSelectedIndex();
       if (i>0) {
        myData.sorttyp =i;
        Collections.sort( liste );
        tabelle.updateUI();

       }

   }


     // Eintragen
    private void setdata() {
      myData data;

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

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

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

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

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

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

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

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


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

    }


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

}


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


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

    public myData() {
    }

    public int getColumns() {
      return 5;
    }

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


  public int compareTo(Object obj) {
     myData myD;
     myD = (myData) obj;
     System.out.println(_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;
   }

}  // myData


