/* 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
*/

//  verwendet ein PlainDocument mit IVal zum Testen

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

import java.awt.event.*;


// 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 Plausi3 extends JFrame  {


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

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

  gIntTextDocument IntTextDocument;

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

  private void setGUI() {
    String s;
    this.getContentPane().setLayout( new GridBagLayout() );

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


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

    this.getContentPane().add(TMatrikelNr,   new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,20,0,20), 00, 0));

    JPanel panel1 = new JPanel();
    panel1.setLayout( new FlowLayout() );
    panel1.add(BnOk);
    panel1.add(BnESC);

    this.getContentPane().add(panel1,
                new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30,00,0,0), 0, 0));




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


      // NEU in diesem Beispiel
    IntTextDocument = new gIntTextDocument();
 		TMatrikelNr.setDocument( IntTextDocument );

    // TMatrikelNr.setDocument(new PlainDocument());

    //IntTextDocument.
    s = IntTextDocument.lineLimitAttribute;
      System.out.println("           gIntTextDocument");

  }



  void BnOk_actionPerformed(ActionEvent e) {
   String s;
   s = TMatrikelNr.getText();
   if ( basis.IVal(s) ) {
     basis.Message("Eingabeprüfung","Alles Okay");
   }
   else {
     basis.Message("Fehler","Fehlerhafte Matrikelnummer");
     TMatrikelNr.requestFocus();
   }

  }  // BnOk

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

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


}




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
            System.out.println("         Eingabe: "+str);
        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 ); 
            super.insertString(offs, str, a);
        }
        catch (NumberFormatException e) {
             System.out.println("           exception");
        }
    }

  public void remove(int offs, int len)  throws BadLocationException {
      System.out.println("           gIntTextDocument: remove: "+getText(0, getLength()) );
      String oldString = getText(0, getLength() );

      if (len == 0) return;
      int n = getLength();
      // Parameters:
      // beginIndex - the beginning index, inclusive.
      // endIndex - the ending index, exclusive.
      String newString = oldString.substring(0,offs) + oldString.substring(offs+len);

      //                           0 bis offs-1
      try {
        Double.parseDouble(newString + "0");  //
        super.remove(offs, len);
      }
      catch(NumberFormatException e) {
        // super.remove(offs, len); ??????
      }
  }
}


