[ Back | Previous | Next ]

How to use a PlainDocument for Date entry?

Package:
javax.swing.text.*
Product:
Swing
Release:
1.0.x
Related Links:
General
JEditorPane
PlainDocument
Comment:
This is a sample filter, plaindocument for Date editing.
Especially the following methods:
remove
insert

This can also be used as an inner class.


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


package nl.rotterdam.port.swing;

import com.sun.java.swing.text.*;

// or import javax.swing.text.*; if Java 2

public class JDateFieldFilter extends PlainDocument {
		protected transient java.beans.PropertyChangeSupport propertyChange;
	private String fieldFormat = new String();
	private java.util.Date fieldDate = new java.util.Date();
	public JDateFieldFilter() {
		this("dd/yyyyy");
	}
/**
 * This method was created in VisualAge.
 * @param aFormat java.lang.String
 */
public JDateFieldFilter(String aFormat) {
	super();
	setFormat(aFormat);
}
/**
 * The addPropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
	getPropertyChange().addPropertyChangeListener(listener);
}
/**
 * The firePropertyChange method was generated to support the propertyChange field.
 */
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
	getPropertyChange().firePropertyChange(propertyName, oldValue, newValue);
}
/**
 * Gets the date property (java.util.Date) value.
 * @return The date property value.
 * @see #setDate
 */
public java.util.Date getDate() {
	return fieldDate;
}
/**
 * Gets the format property (java.lang.String) value.
 * @return The format property value.
 * @see #setFormat
 */
public String getFormat() {
	return fieldFormat;
}
/**
 * Accessor for the propertyChange field.
 */
protected java.beans.PropertyChangeSupport getPropertyChange() {
	if (propertyChange == null) {
		propertyChange = new java.beans.PropertyChangeSupport(this);
	};
	return propertyChange;
}

public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
	if (str == null)
		return;
	if (getFormat() == null || getFormat().length() == 0) {
		System.out.println("-- No format set.");
		return;
	}
	if (offset >= getFormat().length())
		return;
	char aChar = str.charAt(0);
	char fChar = getFormat().charAt(offset);
	System.out.println("-- a=" + Character.isDigit(aChar) + ", f=" + Character.isLetterOrDigit(fChar));
	if (Character.isDigit(aChar) == Character.isLetter(fChar)) {
		if (!Character.isLetterOrDigit(fChar)) {
			// if char is not a number

		} else {
			System.out.println("Parsing! " + str + "-off=" + offset + ", len=" + getText(0, offset).length() + " with format " + getFormat().substring(0, offset + 1));
			super.insertString(offset, str, attr);
			offset++;
			if (offset < getFormat().length()) {
				char bChar = getFormat().charAt(offset);
				// if the next char in the format is not a digit append the sign.
				if (!Character.isLetterOrDigit(bChar)) {
					super.insertString(offset, "" + bChar, attr);
				}
			} else {
				if (offset == getFormat().length()) {
					System.out.println("Evalueer");
					java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(getFormat());
					java.text.ParsePosition pos;
					System.out.println("Parsing! " + str + "-off=" + offset + ", len=" + getText(0, offset).length() + " with format " + getFormat());
					pos = new java.text.ParsePosition(0);
					System.out.println("-- parsing= " + getText(0, offset));
					java.util.Date visited = sdf.parse(getText(0, offset), pos);
					System.out.println("== " + visited);
					if (visited == null)
						return;
					setDate( visited );
				}
			}
		}
	}
}


/**
 * This method was created in VisualAge.
 * @param offset int
 * @param len int
 */
public void remove(int offset, int len) throws BadLocationException {
	if (getFormat() == null || getFormat().length() == 0) {
		System.out.println("-- No format set.");
		return;
	}
	//
	//
	super.remove(offset, len);
	char bChar = getFormat().charAt(offset);
	System.out.println("-- remove " + bChar );
	// if the next char in the format is not a digit append the sign.
	if (!Character.isLetterOrDigit(bChar)) {
		super.remove(--offset, 1);
	}
}
	

/**
 * The removePropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
	getPropertyChange().removePropertyChangeListener(listener);
}
/**
 * Sets the date property (java.util.Date) value.
 * @param date The new value for the property.
 * @see #getDate
 */
public void setDate(java.util.Date date) {
	java.util.Date oldValue = fieldDate;
	fieldDate = date;
	firePropertyChange("date", oldValue, date);
}
/**
 * Sets the format property (java.lang.String) value.
 * @param format The new value for the property.
 * @see #getFormat
 */
public void setFormat(String format) {
	String oldValue = fieldFormat;
	fieldFormat = format;
	firePropertyChange("format", oldValue, format);
}
}
1