001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.awt.Component;
021: import java.lang.reflect.Method;
022:
023: import javax.swing.ComboBoxEditor;
024: import javax.swing.JTextField;
025:
026: /**
027: * {@link JTextField} that only allows a defined set of characters as input. It
028: * used a {@ RestrictedDocument} for that purpose. Every check is done there.
029: *
030: * @see RestrictedDocument
031: */
032: public class RestrictedTextField extends JTextField implements
033: ComboBoxEditor {
034:
035: protected RestrictedDocument rDoc;
036:
037: protected Object item = null;
038:
039: /**
040: * Creates a new empty RestrictedTextField.
041: */
042: public RestrictedTextField() {
043: super ();
044: init();
045: }
046:
047: /**
048: * Creates a new RestrictedTextField initally containting <code>text</code>
049: *
050: * @param text
051: * initial text
052: */
053: public RestrictedTextField(String text) {
054: super (text);
055: init();
056: setText(text);
057: }
058:
059: /**
060: * Sets the set of explicitely allowed characters in text field.
061: */
062: public void setAllowString(String allowString) {
063: rDoc.setAllowString(allowString);
064: }
065:
066: /**
067: * Gets the set of explicitely allowed characters in text field.
068: */
069: public String getAllowString() {
070: String allow = rDoc.getAllowString();
071: return (allow == null) ? "" : allow;
072: }
073:
074: /**
075: * Sets the set of characters explicitely not allowed in text field.
076: */
077: public void setDenyString(String denyString) {
078: rDoc.setDenyString(denyString);
079: }
080:
081: /**
082: * Gets the set of characters explicitely not allowed in text field.
083: */
084: public String getDenyString() {
085: String denyString = rDoc.getDenyString();
086: return (denyString == null) ? "" : denyString;
087: }
088:
089: /**
090: * Sets the the maximum length of characters allowed in text field.
091: *
092: * @param maxLength
093: * the maximum length or <code>-1</code> if unlimited
094: */
095: public void setMaxLength(int maxLength) {
096: rDoc.setMaxLength(maxLength);
097: }
098:
099: /**
100: * Gets the the maximum length of characters allowed in text field.
101: *
102: * @return the maximum length or <code>-1</code> if unlimited
103: */
104: public int getMaxLength() {
105: return rDoc.getMaxLength();
106: }
107:
108: /**
109: * Checks if the text of this text field is valid.
110: */
111: public boolean isStringValid() {
112: return isStringValid(getText());
113: }
114:
115: /**
116: * Checks if the given text is valid.
117: */
118: public boolean isStringValid(String text) {
119: if (text == null) {
120: return true;
121: }
122: return rDoc.inputOK(text);
123: }
124:
125: protected void init() {
126: rDoc = new RestrictedDocument();
127: setDocument(rDoc);
128: }
129:
130: public Component getEditorComponent() {
131: return this ;
132: }
133:
134: public void setItem(Object o) {
135: if (o != null) {
136: setText(o.toString());
137: item = o;
138: } else {
139: setText("");
140: }
141: }
142:
143: // partly copied from BasicComboBoxEditor
144: public Object getItem() {
145: Object newValue = getText();
146:
147: if (item != null && !(item instanceof String)) {
148: // The original value is not a string. Should return the value in
149: // it's
150: // original type.
151: if (newValue.equals(item.toString())) {
152: return item;
153: } else {
154: // Must take the value from the editor and get the value and
155: // cast it to the new type.
156: Class clazz = item.getClass();
157: try {
158: Method method = clazz.getMethod("valueOf",
159: new Class[] { String.class });
160: newValue = method.invoke(item,
161: new Object[] { getText() });
162: } catch (Exception ex) {
163: // Fail silently and return the newValue (a String object)
164: }
165: }
166: }
167: return newValue;
168: }
169: }
|