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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Feb 10 18:36:50 CET 2012


Author: mojays
Date: 2012-02-10 18:36:50 +0100 (Fri, 10 Feb 2012)
New Revision: 1863

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/testing/SwingUtilTest.java
Log:
SwingUtil: create DecimalFormat with limitation generated from pattern

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-10 16:18:55 UTC (rev 1862)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-10 17:36:50 UTC (rev 1863)
@@ -54,6 +54,7 @@
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.UndeclaredThrowableException;
+import java.text.DecimalFormat;
 import java.text.Format;
 import java.text.NumberFormat;
 import java.text.ParseException;
@@ -113,6 +114,7 @@
 import javax.swing.tree.TreeNode;
 import javax.swing.tree.TreePath;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.SystemUtils;
 import org.apache.log4j.Appender;
 import org.apache.log4j.FileAppender;
@@ -1677,7 +1679,39 @@
 		}
 	}
 	
+	/**
+	 * Creates a decimal format whose input is limited to the number of
+	 * signs defined by the format.<br>
+	 * <b>Note:<b> The method simply counts the '0' and '#' before and after
+	 * the decimal sign '.' to indentify the limit. So the method only works correct
+	 * for simple {@link DecimalFormat} patterns For more complex patterns it maybe
+	 * has an unexpected result!
+	 */
+	public static DecimalFormat createLimitedDecimalFormat(String pattern) {
+      String beforeDot = pattern;
+      String afterDot  = "";
+	  int dotIdx = pattern.indexOf('.');
+	  if ( dotIdx >= 0 ) {
+  	    beforeDot = pattern.substring(0,dotIdx);
+        afterDot  = pattern.substring(pattern.indexOf('.')+1);
+	  }
+	  
+	  int maxIntDigits  = StringUtils.countMatches(beforeDot, "0")
+	                    + StringUtils.countMatches(beforeDot, "#");
+      int maxFracDigits = StringUtils.countMatches(afterDot, "0")
+                        + StringUtils.countMatches(afterDot, "#");
+	  
+	  DecimalFormat format = new DecimalFormat(pattern);
+	  format.setMaximumIntegerDigits(maxIntDigits);
+	  // Only set the fraction limit if it is really used
+	  // Otherwise a JFormattedTextField may return a Double
+	  // unexpectedly instead of a Long/Integer!)
+	  if ( maxFracDigits > 0 )
+        format.setMaximumFractionDigits(maxFracDigits);
+	  return format;
+	}
 	
+	
 	/**
 	 * Creates a textfield limited to date inputs (in format 
 	 * @param initialValue

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/testing/SwingUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/testing/SwingUtilTest.java	2012-02-10 16:18:55 UTC (rev 1862)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/testing/SwingUtilTest.java	2012-02-10 17:36:50 UTC (rev 1863)
@@ -4,6 +4,7 @@
 import static org.junit.Assert.assertNotNull;
 
 import java.awt.Color;
+import java.text.DecimalFormat;
 
 import org.junit.Ignore;
 import org.junit.Test;
@@ -77,4 +78,27 @@
       else
         System.out.println("Passwort was: "+ new String((char[])values[0]));
     }
+	
+	@Test
+	public void testLimitedDecimalFormat() {
+	  String pattern = "00000000";
+	  DecimalFormat format = SwingUtil.createLimitedDecimalFormat(pattern);
+	  assertEquals(8, format.getMaximumIntegerDigits());
+	  assertEquals(0, format.getMaximumFractionDigits());
+
+	  pattern = "#######0";
+	  format = SwingUtil.createLimitedDecimalFormat(pattern);
+	  assertEquals(8, format.getMaximumIntegerDigits());
+	  assertEquals(0, format.getMaximumFractionDigits());
+
+      pattern = "#######0.00";
+      format = SwingUtil.createLimitedDecimalFormat(pattern);
+      assertEquals(8, format.getMaximumIntegerDigits());
+      assertEquals(2, format.getMaximumFractionDigits());
+
+      pattern = "##,###,##0.00";
+      format = SwingUtil.createLimitedDecimalFormat(pattern);
+      assertEquals(8, format.getMaximumIntegerDigits());
+      assertEquals(2, format.getMaximumFractionDigits());
+}
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list