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 AbstractEditorComponent extends AbstractTextComponent
034: implements EditorComponent {
035: private int maxLength = 0;
036: private int selectionBeginIndex;
037: private int selectionEndIndex;
038: private int cursorIndex;
039: private AlignX alignX = AlignX.LEFT;
040:
041: public void setSelectionRange(int selectionBeginIndex,
042: int selectionEndIndex) {
043: validateSelectionIndex(selectionBeginIndex);
044: validateSelectionIndex(selectionEndIndex);
045: if (selectionBeginIndex > selectionEndIndex)
046: throw new IllegalArgumentException(
047: "selectionBeginIndex > selectionEndIndex");
048: applySelectionRange(selectionBeginIndex, selectionEndIndex);
049: }
050:
051: public int getSelectionBeginIndex() {
052: return selectionBeginIndex;
053: }
054:
055: public void setSelectionBeginIndex(int selectionBeginIndex) {
056: validateSelectionIndex(selectionBeginIndex);
057:
058: if (selectionEndIndex < 0
059: || selectionEndIndex > getText().length()
060: || selectionEndIndex <= selectionBeginIndex) {
061: applySelectionRange(selectionBeginIndex,
062: selectionBeginIndex);
063: } else {
064: applySelectionRange(selectionBeginIndex, selectionEndIndex);
065: }
066: }
067:
068: public int getSelectionEndIndex() {
069: return selectionEndIndex;
070: }
071:
072: public void setSelectionEndIndex(int selectionEndIndex) {
073: validateSelectionIndex(selectionEndIndex);
074:
075: if (selectionBeginIndex < 0
076: || selectionBeginIndex > getText().length()
077: || selectionBeginIndex >= selectionEndIndex) {
078: applySelectionRange(selectionEndIndex, selectionEndIndex);
079: } else {
080: applySelectionRange(selectionBeginIndex, selectionEndIndex);
081: }
082: }
083:
084: public int getCursorIndex() {
085: return cursorIndex;
086: }
087:
088: public void setCursorIndex(int index) {
089: setSelectionRange(index, index);
090: }
091:
092: private void validateSelectionIndex(int index) {
093: if (index < 0 || index > getText().length())
094: throw new IllegalArgumentException("[index=" + index
095: + ",length=" + getText().length() + "]: "
096: + "index < 0 || index > text.length()");
097: }
098:
099: private void applySelectionRange(int selectionBeginIndex,
100: int selectionEndIndex) {
101: int oldSelectionBeginIndex = this .selectionBeginIndex;
102: int oldSelectionEndIndex = this .selectionEndIndex;
103: int oldCursorIndex = this .cursorIndex;
104: this .selectionBeginIndex = selectionBeginIndex;
105: this .selectionEndIndex = selectionEndIndex;
106: this .cursorIndex = selectionBeginIndex == selectionEndIndex ? selectionEndIndex
107: : -1;
108: firePropertyChange(this , PROPERTY_SELECTION_BEGIN_INDEX,
109: oldSelectionBeginIndex, selectionBeginIndex);
110: firePropertyChange(this , PROPERTY_SELECTION_END_INDEX,
111: oldSelectionEndIndex, selectionEndIndex);
112: firePropertyChange(this , PROPERTY_CURSOR_INDEX, oldCursorIndex,
113: cursorIndex);
114: }
115:
116: public void setMaxLength(int maxLength) {
117: int oldMaxLength = this .maxLength;
118: maxLength = ((maxLength < 0) ? 0 : maxLength);
119: this .maxLength = maxLength;
120: firePropertyChange(this , PROPERTY_MAX_LENGTH, oldMaxLength,
121: maxLength);
122: }
123:
124: public int getMaxLength() {
125: return maxLength;
126: }
127:
128: public AlignX getAlignX() {
129: return alignX;
130: }
131:
132: public void setAlignX(AlignX alignX) {
133: if (alignX == null)
134: throw new IllegalArgumentException(PROPERTY_ALIGN_X
135: + " == null");
136: AlignX oldAlignX = this.alignX;
137: this.alignX = alignX;
138: firePropertyChange(this, PROPERTY_ALIGN_X, oldAlignX, alignX);
139: }
140: }
|