001: /*
002: * $Id: STextComponent.java 3423 2007-07-05 09:47:56Z hengels $
003: * Copyright 2000,2005 wingS development team.
004: *
005: * This file is part of wingS (http://wingsframework.org).
006: *
007: * wingS is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.wings;
015:
016: import org.wings.event.SDocumentEvent;
017: import org.wings.event.SDocumentListener;
018: import org.wings.plaf.TextAreaCG;
019: import org.wings.plaf.TextFieldCG;
020: import org.wings.text.DefaultDocument;
021: import org.wings.text.SDocument;
022:
023: import javax.swing.text.BadLocationException;
024:
025: /**
026: * Abstract base class of input text components like {@link STextArea} and {@link STextField}.
027: * Requires a surrounding {@link SForm} element!
028: *
029: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
030: * @version $Revision: 3423 $
031: */
032: public abstract class STextComponent extends SComponent implements
033: LowLevelEventListener, SDocumentListener {
034:
035: private boolean editable = true;
036:
037: private SDocument document;
038:
039: /**
040: * @see LowLevelEventListener#isEpochCheckEnabled()
041: */
042: private boolean epochCheckEnabled = true;
043:
044: public STextComponent() {
045: this (new DefaultDocument(), true);
046: }
047:
048: public STextComponent(String text) {
049: this (new DefaultDocument(text), true);
050: }
051:
052: public STextComponent(SDocument document) {
053: this (document, true);
054: }
055:
056: public STextComponent(SDocument document, boolean editable) {
057: setDocument(document);
058: setEditable(editable);
059: }
060:
061: public SDocument getDocument() {
062: return document;
063: }
064:
065: public void setDocument(SDocument document) {
066: if (document == null)
067: throw new IllegalArgumentException("null");
068:
069: SDocument oldDocument = this .document;
070: this .document = document;
071: if (oldDocument != null)
072: oldDocument.removeDocumentListener(this );
073: document.addDocumentListener(this );
074: reloadIfChange(oldDocument, document);
075: }
076:
077: /**
078: * Defines if the textcomponent is editable or not.
079: * @see #isEditable()
080: * @param ed true if the text component is to be editable false if not.
081: */
082: public void setEditable(boolean ed) {
083: boolean oldEditable = editable;
084: editable = ed;
085: if (editable != oldEditable)
086: reload();
087: }
088:
089: public boolean isEditable() {
090: return editable;
091: }
092:
093: /**
094: * Sets the text of the component to the specified text.
095: * @see #getText()
096: * @param text the new text for the component.
097: */
098: public void setText(String text) {
099: document.setText(text);
100: }
101:
102: public String getText() {
103: return document.getText();
104: }
105:
106: /**
107: * Appends the given text to the end of the document. Does nothing
108: * if the string is null or empty.
109: *
110: * @param text the text to append.
111: */
112: public void append(String text) {
113: try {
114: document.insert(document.getLength(), text);
115: } catch (BadLocationException e) {
116: }
117: }
118:
119: public void processLowLevelEvent(String action, String[] values) {
120: processKeyEvents(values);
121: if (action.endsWith("_keystroke"))
122: return;
123:
124: if (isEditable() && isEnabled()) {
125: String newValue = values[0];
126: if (newValue != null && newValue.length() == 0) {
127: newValue = null;
128: }
129: String text = getText();
130: if (text != null && text.length() == 0) {
131: text = null;
132: }
133:
134: if (isDifferent(newValue, text)) {
135: getDocument().setDelayEvents(true);
136: setText(newValue);
137: getDocument().setDelayEvents(false);
138: SForm.addArmedComponent(this );
139: }
140: }
141: }
142:
143: public SDocumentListener[] getDocumentListeners() {
144: return getDocument().getDocumentListeners();
145: }
146:
147: public void addDocumentListener(SDocumentListener listener) {
148: getDocument().addDocumentListener(listener);
149: }
150:
151: public void removeDocumentListener(SDocumentListener listener) {
152: getDocument().removeDocumentListener(listener);
153: }
154:
155: public void fireIntermediateEvents() {
156: getDocument().fireDelayedIntermediateEvents();
157: }
158:
159: public void fireFinalEvents() {
160: super .fireFinalEvents();
161: getDocument().fireDelayedFinalEvents();
162: }
163:
164: /**
165: * @see LowLevelEventListener#isEpochCheckEnabled()
166: */
167: public boolean isEpochCheckEnabled() {
168: return epochCheckEnabled;
169: }
170:
171: /**
172: * @see LowLevelEventListener#isEpochCheckEnabled()
173: */
174: public void setEpochCheckEnabled(boolean epochCheckEnabled) {
175: this .epochCheckEnabled = epochCheckEnabled;
176: }
177:
178: public void insertUpdate(SDocumentEvent e) {
179: //reload();
180: }
181:
182: public void removeUpdate(SDocumentEvent e) {
183: //reload();
184: }
185:
186: public void changedUpdate(SDocumentEvent e) {
187: if (isUpdatePossible()) {
188: if (STextField.class.isAssignableFrom(getClass())
189: && !SPasswordField.class
190: .isAssignableFrom(getClass()))
191: update(((TextFieldCG) getCG()).getTextUpdate(
192: (STextField) this , getText()));
193: else if (STextArea.class.isAssignableFrom(getClass()))
194: update(((TextAreaCG) getCG()).getTextUpdate(
195: (STextArea) this, getText()));
196: else
197: reload();
198: } else {
199: reload();
200: }
201: }
202: }
|