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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Mar 29 14:24:48 CEST 2011


Author: mojays
Date: 2011-03-29 14:24:47 +0200 (Tue, 29 Mar 2011)
New Revision: 1540

Added:
   trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
new method to complete a date string

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-03-25 11:51:54 UTC (rev 1539)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-03-29 12:24:47 UTC (rev 1540)
@@ -39,10 +39,12 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.Locale;
 import java.util.TreeSet;
 import java.util.Vector;
@@ -130,6 +132,65 @@
 		return string.trim();
 	}
 
+    /**
+     * Completes an incomplete date definition (in european
+     * dotted-format {@code dd.MM.yyyy}). Missing dots are also added.
+     * In this case the method expects that day is given
+     * with 2 digits. If month and/or year is not given, these information
+     * is completed with the current month/year.<br>
+     * Examples: Expect that current date is 29.03.2011, then
+     * <ul>
+     *   <li>"12102009" is completed to "12.10.2009"</li>
+     *   <li>"111" is completed to "11.1.2011"</li
+     *   <li>"11" is completed to "11.3.2011"</li
+     *   <li>"12.10" is completed to "12.10.2011"</li
+     *   <li>"12.10." is completed to "12.10.2011"</li
+     *   <li>"1204" is completed to "12.04.2011"</li>
+     * </ul>
+     * @param dateStr a european date definition as string
+     * @return the source string if the given string contains other
+     *         signs than digits and dots
+     */
+    public static String completeEuropeanDateString(String dateStr) {
+      if ( dateStr == null || !dateStr.matches("[\\d\\.]+") )
+        return dateStr;
+      
+      final Calendar CURR_DATE = new GregorianCalendar();
+      final int CURR_MONTH = CURR_DATE.get(Calendar.MONTH)+1;
+      final int CURR_DAY = CURR_DATE.get(Calendar.DAY_OF_MONTH);
+      final int CURR_YEAR = CURR_DATE.get(Calendar.YEAR);
+
+      if ( dateStr.endsWith(".") )
+        dateStr = dateStr.substring(0,dateStr.length()-1);
+      
+      // Punkte automatisch ergaenzen (Ziffern muessen
+      // 2-stellig eingegeben werden
+      if ( dateStr.indexOf(".") < 0 ) {
+        if ( dateStr.length() > 2 )
+          dateStr = dateStr.substring(0,2) +
+                    "." +
+                    dateStr.substring(2);
+        if ( dateStr.length() > 5 )
+          dateStr = dateStr.substring(0,5) +
+                    "." +
+                    dateStr.substring(5);
+      }
+      // keine 2 Punkte vorhanden
+      // -> aktuelles Jahr/Monat ergaenzen
+      if ( dateStr.indexOf(".") == dateStr.lastIndexOf(".") ) {
+        // kein Punkt vorhanden
+        // Monat und Jahr ergaenzen
+        if ( dateStr.indexOf(".") < 0 ) {
+          int month = CURR_DATE.get(Calendar.MONTH)+1;
+          dateStr += "." + CURR_MONTH + "." + CURR_YEAR;
+        } else {
+          // nur Jahr ergaenzen
+          dateStr += "." + CURR_YEAR;
+        }
+      }
+      return dateStr;
+    }
+
 	/**
 	 * Returns the current time as formatted string.
 	 * 

Added: trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java	2011-03-25 11:51:54 UTC (rev 1539)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java	2011-03-29 12:24:47 UTC (rev 1540)
@@ -0,0 +1,61 @@
+package de.schmitzm.swing;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.text.Format;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JFormattedTextField;
+import javax.swing.JFormattedTextField.AbstractFormatter;
+import javax.swing.JLabel;
+
+import org.junit.Test;
+
+import de.schmitzm.lang.LangUtil;
+import de.schmitzm.testing.TestingClass;
+import de.schmitzm.testing.TestingUtil;
+
+public class FormattedTextFieldTest extends TestingClass {
+    @Test
+    public void testFormattedTextField() throws Throwable {
+      final JLabel infoField = new JLabel(" ");
+      
+      Format format = new SimpleDateFormat("dd.MM.yyyy");
+      JFormattedTextField field = new JFormattedTextField(format) {
+        @Override
+        public void commitEdit() throws ParseException {
+          infoField.setText(" ");
+          try {
+            super.commitEdit();
+          } catch (Exception err) {
+            // if the given string can not be interpreted
+            // try to complete the input
+            String completedInput = LangUtil.completeEuropeanDateString( getText() );
+            infoField.setText(completedInput);
+            setText(completedInput);
+            super.commitEdit();
+          }
+        }
+      };
+      field.setText("dd.MM.yyyy");
+      field.select(0, field.getText().length());
+//      field.setFocusLostBehavior( JFormattedTextField.)
+//      field.set
+      field.setPreferredSize( new Dimension(200,20) );
+      
+      JButton button = new JButton("Test");
+      
+      JPanel panel = new JPanel();
+      panel.setLayout( new BoxLayout(panel,BoxLayout.Y_AXIS) );
+      panel.add(infoField);
+      panel.add(field);
+      panel.add(button);
+      TestingUtil.testGui(panel, -1);
+    }
+}



More information about the Schmitzm-commits mailing list