
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.*;


// setAutoCreateRowSorter(true)

// Tabelle mit einer Klasse  und Sortierung

public class Tabelle11 extends JFrame implements ActionListener {

    private final JButton bnFirst = new JButton("Erster", new ImageIcon("first.png"));
    private final JButton bnPrev = new JButton("Vorheriger", new ImageIcon("prev.png"));
    private final JButton bnNext = new JButton("Nächster", new ImageIcon("next.png"));
    private final JButton bnLast = new JButton("Letzter", new ImageIcon("last.png"));
    private final JButton bnDelete = new JButton("Löschen", new ImageIcon("delete.png"));
    private final JButton bnInsert = new JButton("Einfügen", new ImageIcon("plus.png"));


   JTable table;
   Vector liste = new Vector();


    public Tabelle11() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setGUI();
      setToolBars();
      setSize(600,200);
      setTitle("Tabelle11.java");
    }



    private void setGUI() {
        this.getContentPane().setLayout( new BorderLayout() );
        setdata();
        table= createTable();
        table.setFont( new Font("Arial", Font.BOLD,18)  );


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

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

        this.getContentPane().add(scrollpane, BorderLayout.CENTER);
        // this.pack();
       
        table.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            tabelle_mouseClicked(e);
          }
        });

    }

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

    toolBar.add(bnFirst);
    toolBar.add(bnPrev);
    toolBar.add(bnNext);
    toolBar.add(bnLast);
    toolBar.addSeparator( new Dimension(20,10) );
    toolBar.add(bnDelete);
    toolBar.add(bnInsert);
    bnFirst.addActionListener(this);
    bnPrev.addActionListener(this);
    bnNext.addActionListener(this);
    bnLast.addActionListener(this);
    bnInsert.addActionListener(this);
    bnDelete.addActionListener(this);    
    
    this.add(toolBar, BorderLayout.NORTH);
  } // setToolBars



    @Override
    public void actionPerformed(ActionEvent e) {        
        if (e.getSource()==bnFirst) {
            bnFirst_click();            
        }
        if (e.getSource()==bnPrev) {
            bnPrev_click();
        }
        if (e.getSource()==bnNext) {
            bnNext_click();            
        }
        if (e.getSource()==bnLast) {
            bnLast_click();
        }
        if (e.getSource()==bnInsert) {
            //bnInsert_click();            
        }
        if (e.getSource()==bnDelete) {
            // bnDelete_click();
        }
    }
    
    private void selectAndScroll(int row) {
        Rectangle cellRect = table.getCellRect(row, 0, true);
        table.scrollRectToVisible(cellRect);

        table.clearSelection();
        table.setRowSelectionInterval(row, row);        
    }  
  

    private void bnFirst_click() {
        selectAndScroll(0);
    }
    
    private void  bnPrev_click() {
        int row = table.getSelectedRow();
        if (row>0) {
            selectAndScroll(row-1);
        }
    }
    
    private void bnNext_click() {
        int row = table.getSelectedRow();
        if (row<(liste.size()-1)) {
            selectAndScroll(row+1);
        }
    }
    private void bnLast_click() {
        int row = table.getSelectedRow();
        if (row<(liste.size())) {
            selectAndScroll(liste.size()-1);
        }
    }


 

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

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

              switch (col) {
                case 0: return std.getVorname();
                case 1: return std.getName();
                case 2: return std.getMatrNummer();
                case 3: return std.getUNummer();
                case 4: return std.getStrasse();
                case 5: return std.getOrt();
                case 6: return std.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 Student.getColumnCaption(column);
            }
            public Class getColumnClass(int column) {
              return getValueAt(0, column).getClass();
            }
            public boolean isCellEditable(int row, int column) {
              return false; // true;
            }
            public void setValueAt(Object aValue, int row, int column) {
              Student std = (Student) liste.elementAt(row);
              switch( column ) {
                case 0: std.setName( (String) aValue );
                case 1: std.setVorname( (String) aValue );
                case 2: std.setMatrNummer( (Integer) aValue );
                case 3: std.setUNummer( (String) aValue );
                case 4: std.setStrasse( (String) aValue );
                case 5: std.setOrt( (String) aValue );
              }
            }
         };

        JTable table = new JTable(dataModel);

        table.setAutoCreateRowSorter(true);

         // setzen individueller Spaltebreite
        table.setAutoCreateRowSorter(true);
     
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     table.getColumnModel().getColumn(0).setPreferredWidth(145);
     table.getColumnModel().getColumn(1).setPreferredWidth(145);
     table.getColumnModel().getColumn(2).setPreferredWidth(120);
     int n=Student.getColumns();
     for (int col=3; col<n; col++) {
        table.getColumnModel().getColumn(col).setPreferredWidth(100);
     }
     table.getColumnModel().getColumn(4).setPreferredWidth(180);
     table.getColumnModel().getColumn(5).setPreferredWidth(160);

        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= table.getSelectedRow();
        Student std = (Student) liste.get(k);
        // Aufruf JDialog
        CDialog dlg = new CDialog(this, std);
        if ( dlg.result ) {
          // Ok wurde gedrückt, daten übernehmen
          System.out.println("Ok wurde gedrückt, Daten übernehmen");
          std.setMatrNummer( dlg.getMatrnr() );
          table.updateUI();
        }
      }

     // Eintragen
    private void setdata() {
      Student std;

      std = new Student( "Müller","Kai", 4722, "U12345","LangeStrasse 5","Magdeburg","Yellow");
        liste.add(std);

      std = new Student( "Meyer","Uwe", 4712, "U12345","Kastanienalle 123","Hamburg", "Red");
        liste.add(std);

      std = new Student( "Schmidt","Petra", 4714, "U12325","Roter Platz","Moskau","Blue");
        liste.add(std);

      std = new Student( "Brian","Adams", 6714, "U13325","Yellow Road 42a","London","Pink");
        liste.add(std);

      std = new Student( "Hold","Toni", 6614, "U13345","Hopfenbreite 12c","Wellen","White");
        liste.add(std);

      std = new Student( "Boll","Toni", 6613, "U13345",",Friedrichstraße 55","Wernigerode","Maroon");
        liste.add(std);

      std = new Student( "Seneca","Toni", 6314, "U13341","Rhodensienweg 8","Halberstadt","Gray");
        liste.add(std);

      std = new Student( "Kredel","Helmut", 8613, "U23345","Blumenstrasse 12","Haldensleben","Phthalo Blue");
        liste.add(std);


      std = new Student( "Bräunl","Thomas", 8700, "U23315","Dorfgasse 1","Berlin","Maroon");
        liste.add(std);
      std = new Student( "Doug","Lea", 3613, "U11345","Langestraße 12","Kirchberg","Blue");
        liste.add(std);
      std = new Student( "Rauber","Rünger", 5612, "U53344","Extrakaeseweg 1234","Benzigerode","Red");
        liste.add(std);
      std = new Student( "Walrath","Kathy", 4613, "U33345","Hallenserweg 4","Leipzig","Green");
        liste.add(std);
      std = new Student( "Weber","Harald", 9613, "U23345","Werner von Siemens Strasse 3","Muenchen","Black");
        liste.add(std);
    }



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

}



class Student  {


    private String _name;
    private String _vorname;
    private int _matrnummer;
    private String _unummer;
    private String _strasse;
    private String _ort;
    private String _farbe;

    public Student() {
    }

    public static int getColumns() {
      return 7;
    }

    public static String getColumnCaption(int i) {
      switch (i) {
       case 0: return "Vorname";
       case 1: return "Nachname";
       case 2: return  "MatrNr";
       case 3: return  "UNummer";
       case 4: return  "Strasse";
       case 5: return "Ort";
       case 6: return "Farbe";
       default:  return "yyyyyyyyyyy";
     }
    }


    public Student( String name, String vorname, int matrnummer, String unummer, 
                                         String strasse, String ort, String farbe) {
      _name = name;
      _vorname = vorname;
      _matrnummer = matrnummer;
      _unummer = unummer;
      _strasse = strasse;
      _ort = ort;
      _farbe = farbe;
    }



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

   public String getStrasse() {
     return _strasse;
   }
   public void setStrasse(String strasse) {
     _strasse = strasse;
   }

   public String getOrt() {
     return _ort;
   }
   public void setOrt(String ort) {
     _ort = ort;
   }



}  // Student








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, Student std) {
   super(parent);
   setModal(true);
   setTitle("Bearbeiten eines Datensatzes");
   setSize(400,350);
   result=false;
   setGUI(std);

   setVisible(true);

  }

  private void setGUI(Student std) {
        this.getContentPane().setLayout( new GridBagLayout() );

        vorname.setText( std.getVorname() );
        name.setText( std.getName() );
        matrnr.setText( std.getMatrNummer() );
        unummer.setText( std.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 Variablen
    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();
   }


}
