/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  07.06.2006
   Letzte Änderung: 12.11.2010
*/

//  PlainDocument

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

import java.awt.event.*;
import com.borland.jbcl.layout.*;

// Plain Dokument
import javax.swing.text.PlainDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;



import javax.swing.text.Document;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;


public class Plausi10 extends JFrame  {
  XYLayout xYLayout1 = new XYLayout();

  JLabel jLabel1 = new JLabel();
  JTextField TMatrikelNr = new JTextField();

  JButton BnOk = new JButton();
  JButton BnESC = new JButton();

  public Plausi10() {
  public Plausi1() {
    setSize(400, 200);
    setTitle("Plausibilität 10. Beispiel");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    this.getContentPane().setLayout(xYLayout1);

    jLabel1.setText("MatrikelNr");
    TMatrikelNr.setText("-");


    BnOk.setText("Ok");
    BnOk.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnOk_actionPerformed(e);
      }
    });
    BnESC.setText("Abbruch");
    BnESC.addActionListener(new java.awt.event.ActionListener() {

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

    xYLayout1.setHeight(200);
    xYLayout1.setWidth(400);
    this.getContentPane().add(jLabel1, new XYConstraints(21, 18, -1, -1));
    this.getContentPane().add(TMatrikelNr, new XYConstraints(115, 17, 257, -1));
    this.getContentPane().add(BnESC, new XYConstraints(206, 94, 85, -1));
    this.getContentPane().add(BnOk, new XYConstraints(81, 94, 95, -1));


 		TMatrikelNr.setDocument(new gIntTextDocument() );

  }


  //Ü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) {
    Plausi10 frame = new Plausi10();
    frame.show();
 }

  void BnOk_actionPerformed(ActionEvent e) {

  }

  void BnESC_actionPerformed(ActionEvent e) {
    System.exit(0);
  }


}




class gIntTextDocument extends PlainDocument {

    public gIntTextDocument() {
      System.out.println("           gIntTextDocument");
    }

    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
      System.out.println("           gIntTextDocument: insertString");
        if (str == null) {
            return;
        }
        int n = getLength(); // alter Wert
        String oldString = getText(0, getLength());
        // Parameters:
        // beginIndex - the beginning index, inclusive.
        // endIndex - the ending index, exclusive.
        String newString = oldString.substring(0, offs) + str + oldString.substring(offs);
        try {
            Integer.parseInt(newString + "0"); //
            super.insertString(offs, str, a);
        }
        catch (NumberFormatException e) {
        }
    }
}



/*


  JTextField searchFor = new JTextField(20);



    searchFor.getDocument().addDocumentListener( new SearchL());





  class SearchL implements DocumentListener {
    public void changedUpdate(DocumentEvent e){
    }

    public void insertUpdate(DocumentEvent e){
      textValueChanged();
    }
    public void removeUpdate(DocumentEvent e){
      textValueChanged();
    }
  }




  public void textValueChanged() {
    ResultSet r;
    if(searchFor.getText().length() == 0) {
      completion.setText("");
      results.setText("");
      return;
    }
    try {
      // Name completion:
      r = s.executeQuery(
        "SELECT LAST FROM people.csv people " +
        "WHERE (LAST Like '" +
        searchFor.getText()  +
        "%') ORDER BY LAST");
      if(r.next())
        completion.setText(
          r.getString("last"));
      r = s.executeQuery(
        "SELECT FIRST, LAST, EMAIL " +
        "FROM people.csv people " +
        "WHERE (LAST='" +
        completion.getText() +
        "') AND (EMAIL Is Not Null) " +
        "ORDER BY FIRST");
    } catch(Exception e) {
      results.setText(
        searchFor.getText() + "\n");
      results.append(e.toString());
      return;
    }
    results.setText("");
    try {
      while(r.next()) {
        results.append(
          r.getString("Last") + ", "
          + r.getString("fIRST") +
          ": " + r.getString("EMAIL") + "\n");
      }
    } catch(Exception e) {
      results.setText(e.toString());
    }
  }




    DocumentListener myListener = ??;
    JTextField myArea = ??;
    myArea.getDocument().addDocumentListener(myListener);
 




*/