// setAutoCreateRowSorter(true)
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;

// setAutoCreateRowSorter(true)

// Tabelle mit einer Klasse  und Sortierung

public class Tabelle07 extends JFrame {

   JComboBox _comboSort;
   JTable _tabelle;
   Vector _sortListe;


   Vector _liste = new Vector();

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

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



    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);
        // this.pack();
        scrollpane.addMouseListener(new java.awt.event.MouseAdapter() {

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

        _tabelle.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            tabelle_mouseClicked(e);
          }
        });

    }


  // 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) {
              CData data = (CData) _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) {
              CData data = (CData) _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);


        return table;
  } // createTable

     // welche Maustaste wurde gedrückt
    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");
            editTabellenEintrag();
           }
           if ( e.getClickCount() == 1) {
            System.out.println("einfach");
           }
         }
        if (  getButton(e) == 3) {
            System.out.println("rechte Maustaste");
         }

      }

      void editTabellenEintrag() {
        int k= _tabelle.getSelectedRow();
        CData data = (CData) _liste.elementAt(k);
        // Aufruf JDialog
        CDialog dlg = new CDialog(this, data);
        if ( dlg.result ) {
          // Ok wurde gedrückt, daten übernehmen
          System.out.println("Ok wurde gedrückt, daten übernehmen");
          data.setMatrNummer( dlg.getMatrnr() );
          _tabelle.updateUI();
        }
      }

   private void   comboSort_click() {
       int i = _comboSort.getSelectedIndex();
       if (i>0) {
        CData.sorttyp =i;
        Collections.sort( _liste );
        _tabelle.updateUI();
          System.out.println("" );
          System.out.println("" );
          System.out.println("" );
        for (i=0; i<_liste.size(); i++) {
          CData d = (CData) _liste.elementAt(i);
          System.out.println(d.toString() );
        }

       }

   }


     // Eintragen
    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);

    }



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

}



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 int getColumns() {
      return 5;
    }

    public CData( 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) {
     CData myD;
     myD = (CData) 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 int ccompareTo(Object obj) {
     CData myD;
     myD = (CData) obj;
     System.out.println(_Vorname+"   "+_Name+"    "+_MatrNummer+"    "+sorttyp );
 
      
      if (_MatrNummer < myD._MatrNummer )
         erg= -1;
      else {
         if (_MatrNummer > myD._MatrNummer )
           erg= 1;
         else
           erg= 0;
      }
     return erg;
    

   }


   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



class CDialog extends JDialog {
    private JLabel label1= new JLabel("Vorname");
    private JLabel label2= new JLabel("Nachname");

    private JLabel label3= new JLabel("MatrNr");
    private JLabel label4= new JLabel("UNummer");

    private JTextField vorname= new JTextField();
    private JTextField name= new JTextField();
    private JTextField matrnr= new JTextField();
    private JTextField unummer= new JTextField();

    public boolean result;  // gibt an, ob Ok oder Esc gedrückt wurde

  public CDialog( Frame parent, CData data) {
   super(parent);
   setModal(true);
   setTitle("Bearbeiten eines Datensatzes");
   setSize(400,350);
   result=false;
   setGUI(data);

   setVisible(true);

  }

  private void setGUI(CData data) {
        this.getContentPane().setLayout( new GridBagLayout() );

        vorname.setText( data.getVorname() );
        name.setText( data.getName() );
        matrnr.setText( data.getMatrNummer() );
        unummer.setText( data.getUNummer() );

       label1.setFont( new Font("Arial", Font.BOLD,18)  );
       label2.setFont( new Font("Arial", Font.BOLD,18)  );
       label3.setFont( new Font("Arial", Font.BOLD,18)  );
       label4.setFont( new Font("Arial", Font.BOLD,18)  );

       vorname.setFont( new Font("Arial", Font.BOLD,18)  );
       name.setFont( new Font("Arial", Font.BOLD,18)  );
       matrnr.setFont( new Font("Arial", Font.BOLD,18)  );
       unummer.setFont( new Font("Arial", Font.BOLD,18)  );

      this.getContentPane().add(label1,
          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
          GridBagConstraints.WEST, GridBagConstraints.NONE,
          new Insets(30,20,0,0), 0, 0));

      this.getContentPane().add(vorname,
           new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,40,0,20), 00, 0));
      // -------------------------------

      this.getContentPane().add(label2,
          new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(30,20,0,0), 0, 0));

      this.getContentPane().add(name,
           new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,40,0,20), 00, 0));
      // -------------------------------

      this.getContentPane().add(label3,
          new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(30,20,0,0), 0, 0));

      this.getContentPane().add(matrnr,
           new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,40,0,20), 00, 0));
      // -------------------------------

      this.getContentPane().add(label4,
          new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(30,20,0,0), 0, 0));

      this.getContentPane().add(unummer,
           new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,40,0,20), 00, 0));
      // -------------------------------

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


        JButton BnOk = new JButton("Ok");
        BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
        pn.add( BnOk);

        JButton BnESC = new JButton("Abbruch");
        BnESC.setFont( new Font("Arial", Font.BOLD,18)  );
        pn.add( BnESC);

        this.getContentPane().add(pn,   new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(25,0,0,0), 0, 0));


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

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

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

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

  }

  void BnESC_click() {
    result=false;
    setVisible(false);
    dispose();
  }

  void BnOk_click() {
    result=true;
    // Speichern in lokale variable
    setVisible(false);
    dispose();
  }

   public String getVorname() {
     return vorname.getText();
   }

   public String getName() {
     return name.getText();
   }

   public int getMatrnr() {
     return  Integer.parseInt( matrnr.getText() );
   }

   public String getUNummer() {
     return unummer.getText();
   }


}
