KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > STextComponent


1 /*
2  * $Id: STextComponent.java,v 1.9 2005/04/08 15:05:19 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.event.SDocumentEvent;
17 import org.wings.event.SDocumentListener;
18 import org.wings.text.DefaultDocument;
19 import org.wings.text.SDocument;
20
21 import javax.swing.text.BadLocationException JavaDoc;
22 import java.awt.event.TextEvent JavaDoc;
23 import java.awt.event.TextListener JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
27  * @version $Revision: 1.9 $
28  */

29 public abstract class STextComponent extends SComponent implements LowLevelEventListener, SDocumentListener {
30
31     private boolean editable = true;
32
33     private SDocument document;
34
35     /**
36      * @see LowLevelEventListener#isEpochCheckEnabled()
37      */

38     private boolean epochCheckEnabled = true;
39
40     public STextComponent() {
41         this(new DefaultDocument(), true);
42     }
43
44     public STextComponent(String JavaDoc text) {
45         this(new DefaultDocument(text), true);
46     }
47
48     public STextComponent(SDocument document) {
49         this(document, true);
50     }
51
52     public STextComponent(SDocument document, boolean editable) {
53         setDocument(document);
54         setEditable(editable);
55     }
56
57     public SDocument getDocument() {
58         return document;
59     }
60
61     public void setDocument(SDocument document) {
62         if (document == null)
63             throw new IllegalArgumentException JavaDoc("null");
64
65         SDocument oldDocument = this.document;
66         this.document = document;
67         if (oldDocument != null)
68             oldDocument.removeDocumentListener(this);
69         document.addDocumentListener(this);
70         reloadIfChange(oldDocument, document);
71     }
72
73
74     public void setEditable(boolean ed) {
75         boolean oldEditable = editable;
76         editable = ed;
77         if (editable != oldEditable)
78             reload();
79     }
80
81
82     public boolean isEditable() {
83         return editable;
84     }
85
86
87     public void setText(String JavaDoc text) {
88         document.setText(text);
89     }
90
91
92     public String JavaDoc getText() {
93         return document.getText();
94     }
95
96     /**
97      * Appends the given text to the end of the document. Does nothing
98      * if the string is null or empty.
99      *
100      * @param text the text to append.
101      */

102     public void append(String JavaDoc text) {
103         try {
104             document.insert(document.getLength(), text);
105         } catch (BadLocationException JavaDoc e) {
106         }
107     }
108
109     public void processLowLevelEvent(String JavaDoc action, String JavaDoc[] values) {
110         processKeyEvents(values);
111
112         if (isEditable() && isEnabled()) {
113             if (values[0] != null)
114                 values[0] = values[0].trim();
115             if (getText() == null || !getText().equals(values[0])) {
116                 setText(values[0]);
117                 SForm.addArmedComponent(this);
118             }
119         }
120     }
121
122     public void addDocumentListener(SDocumentListener listener) {
123         getDocument().addDocumentListener(listener);
124     }
125
126     public void removeDocumentListener(SDocumentListener listener) {
127         getDocument().removeDocumentListener(listener);
128     }
129
130     /**
131      * Fire a TextEvent at each registered listener.
132      */

133     protected void fireTextValueChanged() {
134         TextEvent JavaDoc event = null;
135         // Guaranteed to return a non-null array
136
Object JavaDoc[] listeners = getListenerList();
137         // Process the listeners last to first, notifying
138
// those that are interested in this event
139
for (int i = listeners.length - 2; i >= 0; i -= 2) {
140             if (listeners[i] == TextListener JavaDoc.class) {
141                 if (event == null) {
142                     event = new TextEvent JavaDoc(this, TextEvent.TEXT_VALUE_CHANGED);
143                 } // end of if ()
144
((TextListener JavaDoc) listeners[i + 1]).textValueChanged(event);
145             }
146         }
147     }
148
149     public void fireIntermediateEvents() {
150         fireTextValueChanged();
151     }
152
153     /**
154      * @see LowLevelEventListener#isEpochCheckEnabled()
155      */

156     public boolean isEpochCheckEnabled() {
157         return epochCheckEnabled;
158     }
159
160     /**
161      * @see LowLevelEventListener#isEpochCheckEnabled()
162      */

163     public void setEpochCheckEnabled(boolean epochCheckEnabled) {
164         this.epochCheckEnabled = epochCheckEnabled;
165     }
166
167     //-- implement SDocumentListener to notify TextListeners
168
public void insertUpdate(SDocumentEvent e) {
169         fireTextValueChanged();
170         reload();
171     }
172
173     public void removeUpdate(SDocumentEvent e) {
174         fireTextValueChanged();
175         reload();
176     }
177
178     public void changedUpdate(SDocumentEvent e) {
179         fireTextValueChanged();
180         reload();
181     }
182 }
183
184
185
Popular Tags