[Schmitzm-commits] r1565 - in trunk/schmitzm-core/src: main/java/de/schmitzm/lang main/java/de/schmitzm/swing test/java/de/schmitzm/swing
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Apr 25 17:45:02 CEST 2011
Author: mojays
Date: 2011-04-25 17:45:01 +0200 (Mon, 25 Apr 2011)
New Revision: 1565
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DateTextField.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/JPanel.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
Log:
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -187,6 +187,15 @@
// nur Jahr ergaenzen
dateStr += "." + CURR_YEAR;
}
+ } else {
+ // 2 Punkte vorhanden
+ // --> wenn Jahr < 100 eingegeben wird, Jahr um
+ // "19" erweitern
+ String yearStr = dateStr.substring(dateStr.lastIndexOf(".")+1);
+ if ( Integer.parseInt(yearStr) < 100 )
+ dateStr = dateStr.substring(0,dateStr.lastIndexOf("."))
+ + ".19" + yearStr;
+
}
return dateStr;
}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DateTextField.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DateTextField.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DateTextField.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -0,0 +1,190 @@
+package de.schmitzm.swing;
+
+import java.awt.Font;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.text.DateFormatter;
+import javax.swing.text.DefaultFormatterFactory;
+
+import de.schmitzm.lang.LangUtil;
+
+/**
+ * {@link JFormattedTextField} which is limited to
+ * date inputs in a special date format.
+ * Incomplete inputs are automatically completed
+ * with the current month and/or year.
+ *
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public abstract class DateTextField extends JFormattedTextField {
+ /**
+ * Creates a new input field.
+ * @param format date format
+ * @param maxInputLength maximum number of accepted digits (-1 = unlimited)
+ * @param value initial value
+ */
+ public DateTextField(DateFormat format, int maxInputLength, String value) {
+ super(format);
+ // To allow NULL-Entries, we create a new DateFormatter
+ AbstractFormatter formatter = new DateFormatter(format) {
+ // allow NULL inputs
+ public Object stringToValue(String text) throws ParseException {
+ if ( text == null || "".equals(text.trim()))
+ return null;
+ return super.stringToValue(text);
+ }
+ };
+ setFormatterFactory( new DefaultFormatterFactory(formatter) );
+
+// setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
+ if ( maxInputLength > 0 )
+ setColumns(maxInputLength);
+ //MS: Klappt in Verbindung mit dem JFormattedTextField leider
+ // nicht richtig.
+ //setDocument(new LimitedDocument(maxInputLength, LimitedDocument.STYLE_DOTTED_DATE));
+ if ( value != null ) {
+ setText(value);
+ select(0, getText().length());
+ }
+ }
+
+ /**
+ * Creates a new input field.
+ * @param format date format
+ * @param maxInputLength maximum number of accepted digits (-1 = unlimited)
+ */
+ public DateTextField(DateFormat format, int maxInputLength) {
+ this(format, maxInputLength, null);
+ }
+
+ /**
+ * Creates a new input field.
+ * @param formatPattern pattern a {@link SimpleDateFormat} is created from
+ * @param maxInputLength maximum number of accepted digits (-1 = unlimited)
+ * @param value initial value
+ */
+ public DateTextField(String formatPattern, int maxInputLength, String value) {
+ this(new SimpleDateFormat(formatPattern), maxInputLength, value);
+ }
+
+ /**
+ * Creates a new input field. Pattern length is taken as
+ * maximum input length!
+ * @param formatPattern pattern a {@link SimpleDateFormat} is created from
+ * @param value initial value
+ */
+ public DateTextField(String formatPattern, String value) {
+ this(formatPattern, formatPattern.length(), value);
+ }
+
+ /**
+ * Creates a new input field. Pattern length is taken as
+ * maximum input length!
+ * @param formatPattern pattern a {@link SimpleDateFormat} is created from
+ */
+ public DateTextField(String formatPattern) {
+ this(formatPattern, null);
+ }
+
+ /**
+ * In case of incomplete inputs this method calls
+ * {@link #completeInput(String)} to create a valid
+ * input automatically.
+ */
+ @Override
+ public void commitEdit() throws ParseException {
+ String completedInput = completeInput(getText());
+ setText(completedInput);
+ super.commitEdit();
+// try {
+// String completedInput = completeInput(getText());
+// setText(completedInput);
+// super.commitEdit();
+// } catch (Exception err) {
+// // if the given string can not be interpreted
+// // try to complete the input
+// String completedInput = completeInput(getText());
+// setText(completedInput);
+// super.commitEdit();
+// }
+ }
+
+ /**
+ * Returns the current input as date.
+ * @return {@code null} if current input can not be
+ * parsed
+ */
+ public Date getDate() {
+ try {
+ if ( getText() == null || "".equals(getText().trim()))
+ return null;
+ return (Date)getFormatter().stringToValue(getText());
+ } catch (ParseException err) {
+ return null;
+ }
+ }
+
+ /**
+ * Sets the current text field value by date.
+ */
+ public void setText(Date date) {
+ setDate(date);
+ }
+
+ /**
+ * Sets the current text field value by date.
+ */
+ public void setDate(Date date) {
+ try {
+ setText( getFormatter().valueToString(date) );
+ } catch (ParseException err) {
+ }
+ }
+
+ /**
+ * Called when the input does not satisfies the
+ * format. This method should complete the input to
+ * be valid.
+ * @param input current input
+ * @return {@code null} if the input can not be completed
+ */
+ protected abstract String completeInput(String input);
+
+ /**
+ * Input field to accept the german date format "dd.MM.YYYY".
+ * It will have a maximum input length of 10 signs.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+ public static class German extends DateTextField {
+ public static final String FORMAT_PATTERN = "dd.MM.yyyy";
+ /**
+ * Creates a new input field.
+ * @param value initial value
+ */
+ public German(String value) {
+ super(FORMAT_PATTERN,FORMAT_PATTERN.length(),value);
+ }
+
+ /**
+ * Creates a new input field.
+ */
+ public German() {
+ this(null);
+ }
+
+ /**
+ * Calls {@link LangUtil#completeEuropeanDateString(String)}
+ * to complete the current date input.
+ */
+ @Override
+ protected String completeInput(String input) {
+ return LangUtil.completeEuropeanDateString(input);
+ }
+
+ }
+}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/JPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/JPanel.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/JPanel.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -143,6 +143,13 @@
}
/**
+ * Enables or disables all input components of this panel recursively..
+ */
+ public void setEditable(boolean editable) {
+ SwingUtil.setEditable(this, editable);
+ }
+
+ /**
* Setzt die Hintergrund-Farbe der Komponente und optional die der
* untergeordneten Komponenten
*
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -26,6 +26,9 @@
/** 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 up to two dots. */
+ public static final int STYLE_DOTTED_DATE = 5;
+
/** Holds the conversion style for the document. */
protected int style = STYLE_CASESENSITIVE;
/** Holds the character limit for the document. */
@@ -119,6 +122,7 @@
case STYLE_INTEGER:
if ( !str.matches("\\d*") )
return;
+ break;
case STYLE_DECIMAL:
if ( !str.matches("[\\d\\.]*") )
return;
@@ -128,8 +132,13 @@
return;
if ( str.indexOf('.') != str.lastIndexOf('.') )
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;
}
-
super.insertString(offset, str, attr);
}
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -53,7 +53,10 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
+import java.text.Format;
import java.text.NumberFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@@ -71,6 +74,7 @@
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
+import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
@@ -85,6 +89,7 @@
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
+import javax.swing.text.JTextComponent;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
@@ -809,6 +814,22 @@
}
/**
+ * Enables or disables all input components ({@link JTextComponent}, {@link JComboBox},
+ * ...) of a panel recursively..
+ */
+ public static void setEditable(javax.swing.JPanel panel, boolean editable) {
+ for (Component comp : panel.getComponents()) {
+ if ( comp instanceof JTextComponent )
+ ((JTextComponent)comp).setEditable(editable);
+ if ( comp instanceof JComboBox )
+// ((JComboBox)comp).setEditable(editable);
+ ((JComboBox)comp).setEnabled(editable);
+ if ( comp instanceof de.schmitzm.swing.JPanel )
+ ((de.schmitzm.swing.JPanel)comp).setEditable(editable);
+ }
+ }
+
+ /**
* Erzeugt 5 {@link InputOption} fuer ein {@link MultipleOptionPane} zur
* Datenbank-Anmeldung.
* <ol>
@@ -1500,7 +1521,41 @@
}
}
+
/**
+ * Creates a textfield limited to date inputs (in format
+ * @param initialValue
+ * @return
+ */
+ public static JFormattedTextField createEuropeanDateTextField(String initialValue) {
+ String formatStr = "dd.MM.yyyy";
+ Format format = new SimpleDateFormat(formatStr);
+ JFormattedTextField field = new JFormattedTextField(format) {
+ @Override
+ public void commitEdit() throws ParseException {
+ try {
+ super.commitEdit();
+ } catch (Exception err) {
+ // if the given string can not be interpreted
+ // try to complete the input
+ String completedInput = LangUtil
+ .completeEuropeanDateString(getText());
+ setText(completedInput);
+ super.commitEdit();
+ }
+ }
+ };
+ field.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
+ field.setColumns(formatStr.length());
+ field.setDocument(new LimitedDocument(10, LimitedDocument.STYLE_DECIMAL));
+ if ( initialValue != null ) {
+ field.setText(initialValue);
+ field.select(0, field.getText().length());
+ }
+ return field;
+ }
+
+ /**
* Creates limited text input field.
* @param limit limits the character count
* @param style see styled in {@link LimitedDocument}
@@ -1519,7 +1574,6 @@
*/
public static TableCellEditor createLimitedCellEditor(int limit, int style) {
JTextField field = createLimitedTextField(limit, style);
- field.setBorder( BorderFactory.createEmptyBorder() );
return new DefaultCellEditor(field);
}
Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java 2011-04-24 13:23:57 UTC (rev 1564)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/swing/FormattedTextFieldTest.java 2011-04-25 15:45:01 UTC (rev 1565)
@@ -9,6 +9,7 @@
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
+import javax.swing.JTextField;
import org.junit.Test;
@@ -23,28 +24,29 @@
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.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());
+// 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();
+// }
+// }
+// };
+ JTextField field = new DateTextField.German();
+// field.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
+// field.setText("dd.MM.yyyy");
+// field.setColumns(10);
+// field.setDocument(new LimitedDocument(10, LimitedDocument.STYLE_DOTTED_DATE));
+// field.select(0, field.getText().length());
// field.setFocusLostBehavior( JFormattedTextField.)
// field.set
// field.setPreferredSize( new Dimension(200,20) );
@@ -56,6 +58,6 @@
panel.add(infoField);
panel.add(field);
panel.add(button);
- TestingUtil.testGui(panel, 0);
+ TestingUtil.testGui(panel, -1);
}
}
More information about the Schmitzm-commits
mailing list