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.awt.*;  // Font

import java.util.Collections; // sortieren



// Tabelle mit einer Klasse

public class TabelleVorl extends JFrame {

  JTable tabelle;
  Vector liste;
  JButton BnNr;
  JButton BnName;
  JButton BnMatrnr;

  public TabelleVorl() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(100,100);
    this.setSize(400, 400);
    this.setTitle("Tabelle Vorlesung");
    liste = new Vector();
    setGUI();
    setToolBar();
  }


    private void setGUI() {

        setdata();
        tabelle = createTable();
        addMouseListenerToHeaderInTable();
        tabelle.setFont( new Font("Arial", Font.PLAIN,24));

        JTableHeader th = tabelle.getTableHeader();
        th.setFont( new Font("Arial", Font.BOLD,24));

        tabelle.setRowHeight(26);

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


       // this.pack();
    }

  private void  setToolBar() {
    JToolBar  toolBar = new JToolBar();

    BnNr = new JButton("Nr");
    BnNr.setFont( new Font("Arial", Font.BOLD,28));
    BnNr.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
         Bn_Click(e);
      }
    });
    toolBar.add(BnNr);


    BnName = new JButton("Name");
    BnName.setFont( new Font("Arial", Font.BOLD,28));
    BnName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
         Bn_Click(e);
      }
    });
    toolBar.add(BnName);


    BnMatrnr = new JButton("Matrnr");
    BnMatrnr.setFont( new Font("Arial", Font.BOLD,28));
    BnMatrnr.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
         Bn_Click(e);
      }
    });
    toolBar.add(BnMatrnr);

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

  private void Bn_Click(ActionEvent e) {
    if (e.getSource() == BnNr )
      Sort_click(0);
    if (e.getSource() == BnName )
      Sort_click(1);
    if (e.getSource() == BnMatrnr )
      Sort_click(2);
  }

  // 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 3;
            }
            public int getRowCount() {
              return liste.size();
            }

            public Object getValueAt(int row, int col) {
              CData data = (CData) liste.elementAt(row);
              switch (col) {
                case 0: return data.getNr();
                case 1: return data.getName();
                case 2: return data.getMatrNummer();
                default: return "xxxxxxx";
              }
            }

            public String getColumnName(int column) {
              switch (column) {
                case 0: return "Nr";
                case 1: return "Name";
                case 2: return "Matrnr";
                default: return "xxxxxxx";
              }
            }

            public Class getColumnClass(int c) {
              return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
              return false;  // Dialog
            }

            public void setValueAt(Object aValue, int row, int column) {
              CData data = (CData) liste.elementAt(row);
              switch( column ) {
                case 1: data.setName( (String) aValue );
                        break;
                case 2: String sNumber = (String) aValue;
                        int number = Integer.parseInt(sNumber);
                        data.setMatrNummer( number );
                        break;
              }
            }
         };

     JTable table = new JTable(dataModel);


     return table;
  } // createTable



    public void addMouseListenerToHeaderInTable() {
        tabelle.setColumnSelectionAllowed(false);

        MouseAdapter listMouseListener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                TableColumnModel columnModel = tabelle.getColumnModel();
                int viewColumn = columnModel.getColumnIndexAtX(e.getX());
                int column = tabelle.convertColumnIndexToModel(viewColumn);
                if(e.getClickCount() == 1 && column != -1) {
                    System.out.println("Sortierung der Spalte: "+column);
                    //int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK;
                    //boolean ascending = (shiftPressed == 0);
                    //sorter.sortByColumn(column, ascending);
                }
             }
         };
        JTableHeader th = tabelle.getTableHeader();
        th.addMouseListener(listMouseListener);
    }


   private void Sort_click(int nr) {
      if (nr>=0) {
        if (CData.sorttyp==nr) {
          CData.Sortfactor=(-1)*CData.Sortfactor;
        }
        else {
          CData.sorttyp =nr;
          CData.Sortfactor=+1;
        }

        // Statische Methode
        Collections.sort( liste );
        tabelle.updateUI();
        // testausgabe
        for (int i=0; i<liste.size(); i++) {
          CData d = (CData) liste.elementAt(i);
          System.out.println(d);
        }
      }
   }



      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() {
      }  // edit Tabelle
      

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

      data = new CData(1, "Mueller", 4722);
        liste.add(data);

      data = new CData(2, "Meyer", 4712);
        liste.add(data);

      data = new CData(3, "Schmidt", 4714);
        liste.add(data);

      data = new CData(4, "Brian", 6714);
        liste.add(data);

      data = new CData(5, "Hold", 6614);
        liste.add(data);

      data = new CData(6, "Boll", 6613);
        liste.add(data);

      data = new CData(7, "Seneca", 6314);
        liste.add(data);

      data = new CData(8, "Kredel", 1613);
        liste.add(data);


      data = new CData(9, "Braeunl", 1623);
        liste.add(data);
      data = new CData(10, "Doug", 2613);
        liste.add(data);
      data = new CData(11, "Rauber", 1612);
        liste.add(data);
      data = new CData(12, "Walrath", 1613);
        liste.add(data);
      data = new CData(13, "Weber", 1613);
        liste.add(data);

    } // setData


    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




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

}


//
class CData implements  Comparable {
    public static int sorttyp=0;
    public static int Sortfactor=+1;

    private int _Nr;
    private String _Name;
    private int _MatrNummer;

    public CData() {
       this(0,"leer",0);
    }


    public CData( int Nr, String Name, int MatrNummer) {
      _Nr = Nr;
      _Name = Name;
      _MatrNummer = MatrNummer;
    }


  public int compareTo(Object obj) {
     CData myD;
     myD = (CData) obj;
     //System.out.println("   "+toString()+"  "+myD);
     int result=0;
     // INteger.compareTo
     switch (sorttyp) {
      case 0: if (_Nr < myD._Nr )
                 result=-1;
               else {
                 if (_Nr > myD._Nr )
                   result=1;
                 else
                   result=0;
               }
              break;
      case 1: result=_Name.compareToIgnoreCase(myD._Name );
              break;
      case 2: if (_MatrNummer < myD._MatrNummer )
                 result=-1;
               else {
                 if (_MatrNummer > myD._MatrNummer )
                   result=1;
                 else
                   result=0;
               }
              break;
      default: result=0;
     }
     return result*Sortfactor;

   }

   public String toString() {
     return "  Nr: "+_Nr+"    Name: "+_Name+"     Matrnr: "+_MatrNummer;
   }

   public String getNr() {
     return Integer.toString(_Nr);
   }

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


   public String getMatrNummer() {
     return Integer.toString(_MatrNummer);
   }
   public void setMatrNummer(int MatrNummer) {
     _MatrNummer = MatrNummer;
   }

}  // CData




