[Schmitzm-commits] r1541 - in trunk/schmitzm-core/src: main/java/de/schmitzm/swing test/java/de/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Mar 29 15:33:00 CEST 2011


Author: mojays
Date: 2011-03-29 15:32:59 +0200 (Tue, 29 Mar 2011)
New Revision: 1541

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
Modified:
   trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
Log:
new LimitedDocument

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java	2011-03-29 12:24:47 UTC (rev 1540)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java	2011-03-29 13:32:59 UTC (rev 1541)
@@ -0,0 +1,132 @@
+package de.schmitzm.swing;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.PlainDocument;
+
+/**
+ * A document which size can be limited and reduced to
+ * upper oder lowercase contents.
+ */
+public class LimitedDocument extends PlainDocument {
+  /** Document style to do no conversion to
+   *  upper or lower case. */
+  public static final int STYLE_CASESENSITIVE = 0;
+  /** Document style to automatically convert to
+   *  upper case. */
+  public static final int STYLE_UPPERCASE = 1;
+  /** Document style to automatically convert to
+   *  lower case. */
+  public static final int STYLE_LOWERCASE = 2;
+  /** Document style to only accept number signs (no dots). */
+  public static final int STYLE_INTEGER = 3;
+  /** Document style to only accept number signs and one dot. */
+  public static final int STYLE_DECIMAL = 4;
+  
+  /** Holds the conversion style for the document. */
+  protected int style = STYLE_CASESENSITIVE;
+  /** Holds the character limit for the document. */
+  protected int limit = -1;
+  
+  /**
+   * Creates a new document with no limit and no
+   * conversion.
+   */
+  public LimitedDocument() {
+    this(-1);
+  }
+
+  /**
+   * Creates limited document with no conversion.
+   * @param limit limits the character count (-1 = no limit)
+   */
+  public LimitedDocument(int limit) {
+    this(limit,STYLE_CASESENSITIVE);
+  }
+
+  /**
+   * Creates limited document.
+   * @param limit limits the character count
+   * @param style {@link #STYLE_CASESENSITIVE}, {@link #STYLE_UPPERCASE}
+   *              or {@link #STYLE_LOWERCASE}
+   */
+  public LimitedDocument(int limit, int style) {
+    super();
+    this.style = style;
+    this.limit = limit;
+  }
+  
+  /**
+   * Returns the conversion style.
+   * @see #STYLE_CASESENSITIVE
+   * @see #STYLE_UPPERCASE
+   * @see #STYLE_LOWERCASE
+   */
+  public int getStyle() {
+    return style;
+  }
+
+  /**
+   * Sets the conversion style.
+   * @param style the style to set
+   * @see #STYLE_CASESENSITIVE
+   * @see #STYLE_UPPERCASE
+   * @see #STYLE_LOWERCASE
+   */
+  public void setStyle(int style) {
+    this.style = style;
+  }
+
+  /**
+   * Returns the number of characters the document is limited
+   * to.
+   * @return -1 if there is no limit
+   */
+  public int getLimit() {
+    return limit;
+  }
+
+  /**
+   * Sets the number of characters the document is limited
+   * to.
+   * @param limit the limit to set (-1 for no limit)
+   */
+  public void setLimit(int limit) {
+    this.limit = limit;
+  }
+  
+  /**
+   * Checks for limit and automatic text conversion.
+   */
+  @Override
+  public void insertString(int offset, String  str, AttributeSet attr) throws BadLocationException {
+    if (str == null) return;
+
+    // Check limit
+    if ( (getLength() + str.length()) > getLimit() )
+      return;
+    // Check/Perform style
+    switch ( getStyle() ) {
+      case STYLE_LOWERCASE:
+        str = str.toLowerCase();
+        break;
+      case STYLE_UPPERCASE:
+        str = str.toUpperCase();
+        break;
+      case STYLE_INTEGER:
+        if ( !str.matches("\\d*") )
+          return;
+      case STYLE_DECIMAL:
+        if ( !str.matches("[\\d\\.]*") )
+          return;
+        // check for maximum 1 dot
+        if ( getText(0, getLength()).contains(".") &&
+             str.contains(".") )
+          return;
+        if ( str.indexOf('.') != str.lastIndexOf('.') )
+          return;
+    }
+
+    super.insertString(offset, str, attr);
+   }
+  }

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java	2011-03-29 12:24:47 UTC (rev 1540)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java	2011-03-29 13:32:59 UTC (rev 1541)
@@ -2,6 +2,7 @@
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
+import java.awt.Font;
 import java.text.Format;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -14,6 +15,9 @@
 import javax.swing.JFormattedTextField;
 import javax.swing.JFormattedTextField.AbstractFormatter;
 import javax.swing.JLabel;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.PlainDocument;
 
 import org.junit.Test;
 
@@ -27,6 +31,7 @@
       final JLabel infoField = new JLabel(" ");
       
       Format format = new SimpleDateFormat("dd.MM.yyyy");
+
       JFormattedTextField field = new JFormattedTextField(format) {
         @Override
         public void commitEdit() throws ParseException {
@@ -43,11 +48,14 @@
           }
         }
       };
+      field.setFont( new Font(Font.MONOSPACED,Font.PLAIN,12) );
       field.setText("dd.MM.yyyy");
+      field.setColumns(10);
+      field.setDocument( new LimitedDocument(10,LimitedDocument.STYLE_DECIMAL) );
       field.select(0, field.getText().length());
 //      field.setFocusLostBehavior( JFormattedTextField.)
 //      field.set
-      field.setPreferredSize( new Dimension(200,20) );
+//      field.setPreferredSize( new Dimension(200,20) );
       
       JButton button = new JButton("Test");
       



More information about the Schmitzm-commits mailing list