001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui;
029:
030: /**
031: * @author Joshua J. Gertzen
032: */
033: abstract class AbstractMaskEditorComponent extends
034: AbstractEditorComponent implements MaskEditorComponent {
035: private static final String MASK_CHARS = "9#MdyAaXxhmp";
036:
037: private String editMask = "";
038: String formattedText = "";
039: private boolean formatText = true;
040:
041: public String getEditMask() {
042: return editMask;
043: }
044:
045: public void setEditMask(String editMask) {
046: String oldEditMask = this .editMask;
047: editMask = editMask == null ? "" : editMask;
048: this .editMask = editMask;
049: firePropertyChange(this , PROPERTY_EDIT_MASK, oldEditMask,
050: editMask);
051: }
052:
053: public boolean isFormatText() {
054: return formatText;
055: }
056:
057: public void setFormatText(boolean formatText) {
058: boolean oldFormatText = this .formatText;
059: this .formatText = formatText;
060:
061: //This modifies the text without firing a propertyChange which is what
062: //you want in this case, because changing whether a value is formatted is not
063: //technically chaning the value itself.
064: if (formatText) {
065: setTextDirect(formattedText);
066: } else {
067: setTextDirect(getUnformattedText(getText()));
068: }
069:
070: firePropertyChange(this , PROPERTY_FORMAT_TEXT, oldFormatText,
071: formatText);
072: }
073:
074: String getUnformattedText(String text) {
075: if (!formatText && text.length() > 0) {
076: StringBuilder sb = new StringBuilder();
077:
078: if (editMask.indexOf('#') >= 0) {
079: boolean hasDot = false;
080: if (text.charAt(0) == '-')
081: sb.append('-');
082:
083: for (int i = 0, len = text.length(); i < len; i++) {
084: char c = text.charAt(i);
085:
086: if ("0123456789".indexOf(c) >= 0) {
087: sb.append(c);
088: } else if (c == '.' && !hasDot) {
089: sb.append(c);
090: hasDot = true;
091: }
092: }
093: } else {
094: for (int i = editMask.length(); --i >= 0;) {
095: char c = editMask.charAt(i);
096: if (MASK_CHARS.indexOf(c) == -1)
097: sb.append(c);
098: }
099:
100: String formatChars = sb.toString();
101: sb.setLength(0);
102:
103: for (int i = 0, len = text.length(); i < len; i++) {
104: char c = text.charAt(i);
105: if (formatChars.indexOf(c) == -1)
106: sb.append(c);
107: }
108: }
109:
110: text = sb.toString();
111: }
112:
113: return text;
114: }
115: }
|