[Schmitzm-commits] r1663 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . table

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Aug 1 16:30:42 CEST 2011


Author: mojays
Date: 2011-08-01 16:30:42 +0200 (Mon, 01 Aug 2011)
New Revision: 1663

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/NumberFormatCellEditor.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
Log:
new NumberFormatCellEditor
LimitedDocument extended with new style STYLE_DECIMAL_COMMA

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java	2011-07-29 16:49:13 UTC (rev 1662)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java	2011-08-01 14:30:42 UTC (rev 1663)
@@ -25,6 +25,8 @@
   public static final int STYLE_INTEGER = 3;
   /** Document style to only accept number signs and one dot. */
   public static final int STYLE_DECIMAL = 4;
+  /** Document style to only accept number signs and one comma. */
+  public static final int STYLE_DECIMAL_COMMA = 7;
   
   /** Document style to only accept number signs and up to two dots. */
   public static final int STYLE_DOTTED_DATE = 5;
@@ -115,6 +117,9 @@
     // Check limit
     if ( getLimit() >= 0 && (getLength() + str.length()) > getLimit() )
       return;
+
+    String completeStr = getText(0, getLength()) + str;
+
     // Check/Perform style
     switch ( getStyle() ) {
       case STYLE_LOWERCASE:
@@ -128,23 +133,27 @@
           return;
         break;
       case STYLE_DECIMAL:
-        if ( !str.matches("[\\d\\.]*") )
+//        if ( !str.matches("[\\d\\.]*") )
+//          return;
+//        // check for maximum 1 dot
+//        if ( getText(0, getLength()).contains(".") &&
+//             str.contains(".") )
+//          return;
+//        if ( str.indexOf('.') != str.lastIndexOf('.') )
+//          return;
+        if ( !completeStr.matches("\\d+[\\.]?\\d*") )
           return;
-        // check for maximum 1 dot
-        if ( getText(0, getLength()).contains(".") &&
-             str.contains(".") )
+        break;
+      case STYLE_DECIMAL_COMMA:
+        if ( !completeStr.matches("\\d+[\\,]?\\d*") )
           return;
-        if ( str.indexOf('.') != str.lastIndexOf('.') )
-          return;
-        break;
+      break;
       case STYLE_BASIC_STRING:
-        String complStr = getText(0, getLength()) + str;
 //        if ( !complStr.matches("[[\\w-]&&\\D]+[\\w-]*") )
-        if ( !complStr.matches("[\\w-&&\\D]+[\\w-]*") )
+        if ( !completeStr.matches("[\\w-&&\\D]+[\\w-]*") )
           return;
         break;
       case STYLE_DOTTED_DATE:
-        String completeStr = getText(0, getLength()) + str;
         if ( !completeStr.matches("(\\d{1,4}|$)(\\.|$)(\\d{1,2}|$)(\\.|$)(\\d{1,4}|$)") )
           return;
         break;

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/NumberFormatCellEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/NumberFormatCellEditor.java	2011-07-29 16:49:13 UTC (rev 1662)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/NumberFormatCellEditor.java	2011-08-01 14:30:42 UTC (rev 1663)
@@ -0,0 +1,97 @@
+package de.schmitzm.swing.table;
+
+import java.awt.Component;
+import java.text.NumberFormat;
+
+import javax.swing.DefaultCellEditor;
+import javax.swing.JTable;
+import javax.swing.JTextField;
+import javax.swing.table.TableCellEditor;
+
+/**
+ * {@link TableCellEditor} (using {@link JTextField}) which converts
+ * the number value according to the given {@link NumberFormat} before
+ * setting the value in the {@link JTextField}.
+ * The normal {@link DefaultCellEditor} uses the {@code toString()} method,
+ * which leads to problems ins case of decimal values (comma converted to
+ * dot). 
+ * 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class NumberFormatCellEditor  extends DefaultCellEditor {
+  /** Holds the {@link NumberFormat} used to convert the value
+   *  before editing. */
+  protected NumberFormat formatter = null;
+  
+  /**
+   * Creates a new editor using the given {@link JTextField} and
+   * {@link NumberFormat}.
+   * @param textField text field to use as editor component
+   * @param formatter number format
+   */
+  public NumberFormatCellEditor(JTextField textField, NumberFormat formatter) {
+    super(textField);
+    this.formatter = formatter;
+    
+  }
+
+  /**
+   * Creates a new editor using the given {@link JTextField}.
+   * No {@link NumberFormat} is set yet.
+   * @param textField text field to use as editor component
+   * @see #setFormat(NumberFormat)
+   */
+  public NumberFormatCellEditor(JTextField textField) {
+    this(textField,null);
+  }
+  
+  /**
+   * Creates a new editor using a normal {@link JTextField} as
+   * editor component.
+   * @param formatter number format
+   */
+  public NumberFormatCellEditor(NumberFormat formatter) {
+    this(new JTextField(),formatter);
+    
+  }
+
+  /**
+   * Creates a new editor using a normal {@link JTextField} as
+   * editor component.
+   * No {@link NumberFormat} is set yet.
+   * @see #setFormat(NumberFormat)
+   */
+  public NumberFormatCellEditor() {
+    this(new JTextField(),null);
+  }
+
+  /**
+   * Converts the given value according to the {@link NumberFormat} before
+   * calling the super-method.
+   */
+  @Override
+  public Component getTableCellEditorComponent(JTable table, Object value,
+      boolean isSelected,
+      int row, int column) {
+    if ( formatter != null && value instanceof Number )
+      value = formatter.format(value);
+    return super.getTableCellEditorComponent(table, value, isSelected, row, column);
+  };
+  
+  /**
+   * Returns the {@link NumberFormat}.
+   */
+  public NumberFormat getFormat() {
+    return formatter;
+  }
+
+  /**
+   * Sets the {@link NumberFormat}.
+   */
+  public void setFormat(NumberFormat formatter) {
+    this.formatter = formatter;
+  }
+
+}
+



More information about the Schmitzm-commits mailing list