KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > text > TextComponent


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.text;
31
32 import java.util.EventListener JavaDoc;
33
34 import nextapp.echo2.app.Alignment;
35 import nextapp.echo2.app.Color;
36 import nextapp.echo2.app.FillImage;
37 import nextapp.echo2.app.Border;
38 import nextapp.echo2.app.Component;
39 import nextapp.echo2.app.Extent;
40 import nextapp.echo2.app.Font;
41 import nextapp.echo2.app.Insets;
42 import nextapp.echo2.app.event.ActionEvent;
43 import nextapp.echo2.app.event.ActionListener;
44 import nextapp.echo2.app.event.DocumentEvent;
45 import nextapp.echo2.app.event.DocumentListener;
46
47 /**
48  * Abstract base class for text-entry components.
49  */

50 public abstract class TextComponent
51 extends Component {
52     
53     public static final String JavaDoc INPUT_ACTION = "action";
54
55     public static final String JavaDoc PROPERTY_ACTION_COMMAND = "actionCommand";
56     public static final String JavaDoc PROPERTY_ALIGNMENT = "alignment";
57     public static final String JavaDoc PROPERTY_BACKGROUND_IMAGE = "backgroundImage";
58     public static final String JavaDoc PROPERTY_BORDER = "border";
59     public static final String JavaDoc PROPERTY_DISABLED_BACKGROUND = "disabledBackground";
60     public static final String JavaDoc PROPERTY_DISABLED_BACKGROUND_IMAGE = "disabledBackgroundImage";
61     public static final String JavaDoc PROPERTY_DISABLED_BORDER = "disabledBorder";
62     public static final String JavaDoc PROPERTY_DISABLED_FONT = "disabledFont";
63     public static final String JavaDoc PROPERTY_DISABLED_FOREGROUND = "disabledForeground";
64     public static final String JavaDoc PROPERTY_HEIGHT = "height";
65     public static final String JavaDoc PROPERTY_HORIZONTAL_SCROLL = "horizontalScroll";
66     public static final String JavaDoc PROPERTY_INSETS = "insets";
67     public static final String JavaDoc PROPERTY_MAXIMUM_LENGTH = "maximumLength";
68     public static final String JavaDoc PROPERTY_TOOL_TIP_TEXT = "toolTipText";
69     public static final String JavaDoc PROPERTY_VERTICAL_SCROLL = "verticalScroll";
70     public static final String JavaDoc PROPERTY_WIDTH = "width";
71     
72     public static final String JavaDoc ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners";
73     public static final String JavaDoc DOCUMENT_CHANGED_PROPERTY = "document";
74     public static final String JavaDoc TEXT_CHANGED_PROPERTY = "text";
75     
76     private Document document;
77     
78     /**
79      * Local listener to monitor changes to document.
80      */

81     private DocumentListener documentListener = new DocumentListener() {
82
83         /**
84          * @see nextapp.echo2.app.event.DocumentListener#documentUpdate(nextapp.echo2.app.event.DocumentEvent)
85          */

86         public void documentUpdate(DocumentEvent e) {
87             firePropertyChange(TEXT_CHANGED_PROPERTY, null, ((Document) e.getSource()).getText());
88         }
89     };
90     
91     /**
92      * Creates a new <code>TextComponent</code> with the specified
93      * <code>Document</code> as its model.
94      *
95      * @param document the desired model
96      */

97     public TextComponent(Document document) {
98         super();
99         setDocument(document);
100     }
101     
102     /**
103      * Adds an <code>ActionListener</code> to the <code>TextField</code>.
104      * The <code>ActionListener</code> will be invoked when the user
105      * presses the ENTER key in the field.
106      *
107      * @param l the <code>ActionListener</code> to add
108      */

109     public void addActionListener(ActionListener l) {
110         getEventListenerList().addListener(ActionListener.class, l);
111         // Notification of action listener changes is provided due to
112
// existence of hasActionListeners() method.
113
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l);
114     }
115
116     /**
117      * Fires an action event to all listeners.
118      */

119     private void fireActionEvent() {
120         if (!hasEventListenerList()) {
121             return;
122         }
123         EventListener JavaDoc[] listeners = getEventListenerList().getListeners(ActionListener.class);
124         ActionEvent e = null;
125         for (int i = 0; i < listeners.length; ++i) {
126             if (e == null) {
127                 e = new ActionEvent(this, (String JavaDoc) getRenderProperty(PROPERTY_ACTION_COMMAND));
128             }
129             ((ActionListener) listeners[i]).actionPerformed(e);
130         }
131     }
132     
133     /**
134      * Returns the action command which will be provided in
135      * <code>ActionEvent</code>s fired by this <code>TextField</code>.
136      *
137      * @return the action command
138      */

139     public String JavaDoc getActionCommand() {
140         return (String JavaDoc) getProperty(PROPERTY_ACTION_COMMAND);
141     }
142     
143     /**
144      * Returns the alignment of the text component.
145      *
146      * @return the alignment
147      */

148     public Alignment getAlignment() {
149         return (Alignment) getProperty(PROPERTY_ALIGNMENT);
150     }
151     
152     /**
153      * Returns the default background image of the text component.
154      *
155      * @return the background image
156      */

157     public FillImage getBackgroundImage() {
158         return (FillImage) getProperty(PROPERTY_BACKGROUND_IMAGE);
159     }
160     
161     /**
162      * Returns the border of the text component.
163      *
164      * @return the border
165      */

166     public Border getBorder() {
167         return (Border) getProperty(PROPERTY_BORDER);
168     }
169     
170     /**
171      * Returns the background color displayed when the text component is
172      * disabled.
173      *
174      * @return the color
175      */

176     public Color getDisabledBackground() {
177         return (Color) getProperty(PROPERTY_DISABLED_BACKGROUND);
178     }
179
180     /**
181      * Returns the background image displayed when the text component is
182      * disabled.
183      *
184      * @return the background image
185      */

186     public FillImage getDisabledBackgroundImage() {
187         return (FillImage) getProperty(PROPERTY_DISABLED_BACKGROUND_IMAGE);
188     }
189
190     /**
191      * Returns the border displayed when the text component is
192      * disabled.
193      *
194      * @return the border
195      */

196     public Border getDisabledBorder() {
197         return (Border) getProperty(PROPERTY_DISABLED_BORDER);
198     }
199
200     /**
201      * Returns the font displayed when the text component is
202      * disabled.
203      *
204      * @return the font
205      */

206     public Font getDisabledFont() {
207         return (Font) getProperty(PROPERTY_DISABLED_FONT);
208     }
209
210     /**
211      * Returns the foreground color displayed when the text component is
212      * disabled.
213      *
214      * @return the color
215      */

216     public Color getDisabledForeground() {
217         return (Color) getProperty(PROPERTY_DISABLED_FOREGROUND);
218     }
219
220     /**
221      * Returns the model associated with this <code>TextComponent</code>.
222      *
223      * @return the model
224      */

225     public Document getDocument() {
226         return document;
227     }
228
229     /**
230      * Returns the height of the text component.
231      * This property only supports <code>Extent</code>s with
232      * fixed (i.e., not percent) units.
233      *
234      * @return the height
235      */

236     public Extent getHeight() {
237         return (Extent) getProperty(PROPERTY_HEIGHT);
238     }
239     
240     /**
241      * Returns the horizontal scroll bar position.
242      *
243      * @return the scroll bar position
244      */

245     public Extent getHorizontalScroll() {
246         return (Extent) getProperty(PROPERTY_HORIZONTAL_SCROLL);
247     }
248     
249     /**
250      * Returns the insets of the text component.
251      *
252      * @return the insets
253      */

254     public Insets getInsets() {
255         return (Insets) getProperty(PROPERTY_INSETS);
256     }
257     
258     /**
259      * Returns the maximum length (in characters) of the text which may be
260      * entered into the component.
261      *
262      * @return the maximum length, or -1 if no value is specified
263      */

264     public int getMaximumLength() {
265         Integer JavaDoc value = (Integer JavaDoc) getProperty(PROPERTY_MAXIMUM_LENGTH);
266         return value == null ? -1 : value.intValue();
267     }
268     
269     /**
270      * Returns the text contained in the <code>Document</code> model of
271      * this text component.
272      *
273      * @return the text contained in the document
274      */

275     public String JavaDoc getText() {
276         return document.getText();
277     }
278     
279     /**
280      * Returns the tool tip text (displayed when the mouse cursor is hovered
281      * over the component).
282      *
283      * @return the tool tip text
284      */

285     public String JavaDoc getToolTipText() {
286         return (String JavaDoc) getProperty(PROPERTY_TOOL_TIP_TEXT);
287     }
288     
289     /**
290      * Returns the vertical scroll bar position.
291      *
292      * @return the scroll bar position
293      */

294     public Extent getVerticalScroll() {
295         return (Extent) getProperty(PROPERTY_VERTICAL_SCROLL);
296     }
297     
298     /**
299      * Returns the width of the text component.
300      * This property supports <code>Extent</code>s with
301      * either fixed or percentage-based units.
302      *
303      * @return the width
304      */

305     public Extent getWidth() {
306         return (Extent) getProperty(PROPERTY_WIDTH);
307     }
308     
309     /**
310      * Determines the any <code>ActionListener</code>s are registered.
311      *
312      * @return true if any action listeners are registered
313      */

314     public boolean hasActionListeners() {
315         return hasEventListenerList() && getEventListenerList().getListenerCount(ActionListener.class) != 0;
316     }
317
318     /**
319      * This component does not support children.
320      *
321      * @see nextapp.echo2.app.Component#isValidChild(nextapp.echo2.app.Component)
322      */

323     public boolean isValidChild(Component component) {
324         return false;
325     }
326     
327     /**
328      * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
329      */

330     public void processInput(String JavaDoc inputName, Object JavaDoc inputValue) {
331         super.processInput(inputName, inputValue);
332         
333         if (TEXT_CHANGED_PROPERTY.equals(inputName)) {
334             setText((String JavaDoc) inputValue);
335         } else if (PROPERTY_HORIZONTAL_SCROLL.equals(inputName)) {
336             setHorizontalScroll((Extent) inputValue);
337         } else if (PROPERTY_VERTICAL_SCROLL.equals(inputName)) {
338             setVerticalScroll((Extent) inputValue);
339         } else if (INPUT_ACTION.equals(inputName)) {
340             fireActionEvent();
341         }
342     }
343     
344     /**
345      * Removes an <code>ActionListener</code> from the <code>TextField</code>.
346      *
347      * @param l the <code>ActionListener</code> to remove
348      */

349     public void removeActionListener(ActionListener l) {
350         if (!hasEventListenerList()) {
351             return;
352         }
353         getEventListenerList().removeListener(ActionListener.class, l);
354         // Notification of action listener changes is provided due to
355
// existence of hasActionListeners() method.
356
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null);
357     }
358     
359     /**
360      * Sets the action command which will be provided in
361      * <code>ActionEvent</code>s fired by this <code>TextField</code>.
362      *
363      * @param newValue the new action command
364      */

365     public void setActionCommand(String JavaDoc newValue) {
366         setProperty(PROPERTY_ACTION_COMMAND, newValue);
367     }
368
369     /**
370      * Sets the alignment of the text component.
371      *
372      * @param newValue the new alignment
373      */

374     public void setAlignment(Alignment newValue) {
375         setProperty(PROPERTY_ALIGNMENT, newValue);
376     }
377     
378     /**
379      * Sets the default background image of the text component.
380      *
381      * @param newValue the new background image
382      */

383     public void setBackgroundImage(FillImage newValue) {
384         setProperty(PROPERTY_BACKGROUND_IMAGE, newValue);
385     }
386     
387     /**
388      * Sets the border of the text component.
389      *
390      * @param newValue the new border
391      */

392     public void setBorder(Border newValue) {
393         setProperty(PROPERTY_BORDER, newValue);
394     }
395
396     /**
397      * Sets the background color displayed when the component is disabled.
398      *
399      * @param newValue the new <code>Color</code>
400      */

401     public void setDisabledBackground(Color newValue) {
402         setProperty(PROPERTY_DISABLED_BACKGROUND, newValue);
403     }
404
405     /**
406      * Sets the background image displayed when the component is disabled.
407      *
408      * @param newValue the new background image
409      */

410     public void setDisabledBackgroundImage(FillImage newValue) {
411         setProperty(PROPERTY_DISABLED_BACKGROUND_IMAGE, newValue);
412     }
413
414     /**
415      * Sets the border displayed when the component is disabled.
416      *
417      * @param newValue the new border
418      */

419     public void setDisabledBorder(Border newValue) {
420         setProperty(PROPERTY_DISABLED_BORDER, newValue);
421     }
422
423     /**
424      * Sets the font displayed when the component is disabled.
425      *
426      * @param newValue the new <code>Font</code>
427      */

428     public void setDisabledFont(Font newValue) {
429         setProperty(PROPERTY_DISABLED_FONT, newValue);
430     }
431
432     /**
433      * Sets the foreground color displayed when the component is disabled.
434      *
435      * @param newValue the new <code>Color</code>
436      */

437     public void setDisabledForeground(Color newValue) {
438         setProperty(PROPERTY_DISABLED_FOREGROUND, newValue);
439     }
440
441     /**
442      * Sets the model associated with this <code>TextComponent</code>.
443      *
444      * @param newValue the new model (may not be null)
445      */

446     public void setDocument(Document newValue) {
447         if (newValue == null) {
448             throw new IllegalArgumentException JavaDoc("Document may not be null.");
449         }
450         Document oldValue = getDocument();
451         if (oldValue != null) {
452             oldValue.removeDocumentListener(documentListener);
453         }
454         newValue.addDocumentListener(documentListener);
455         document = newValue;
456     }
457     
458     /**
459      * Sets the height of the text component.
460      * This property only supports <code>Extent</code>s with
461      * fixed (i.e., not percent) units.
462      *
463      * @param newValue the new height
464      */

465     public void setHeight(Extent newValue) {
466         setProperty(PROPERTY_HEIGHT, newValue);
467     }
468     
469     /**
470      * Sets the horizontal scroll bar position.
471      * The provided <code>Extent</code> value must be in pixel units.
472      *
473      * @param newValue the new scroll bar position
474      */

475     public void setHorizontalScroll(Extent newValue) {
476         setProperty(PROPERTY_HORIZONTAL_SCROLL, newValue);
477     }
478     
479     /**
480      * Sets the insets of the text component.
481      *
482      * @param newValue the new insets
483      */

484     public void setInsets(Insets newValue) {
485         setProperty(PROPERTY_INSETS, newValue);
486     }
487     
488     /**
489      * Sets the maximum length (in characters) of the text which may be
490      * entered into the component.
491      *
492      * @param newValue the new maximum length, or -1 if to specify an
493      * unlimited length
494      */

495     public void setMaximumLength(int newValue) {
496         if (newValue < 0) {
497             setProperty(PROPERTY_MAXIMUM_LENGTH, null);
498         } else {
499             setProperty(PROPERTY_MAXIMUM_LENGTH, new Integer JavaDoc(newValue));
500         }
501     }
502     
503     /**
504      * Sets the text of document model of this text component.
505      *
506      * @param newValue the new text
507      */

508     public void setText(String JavaDoc newValue) {
509         Integer JavaDoc maxLength = (Integer JavaDoc) getProperty(PROPERTY_MAXIMUM_LENGTH);
510         if (newValue != null && maxLength != null && maxLength.intValue() > 0
511                 && newValue.length() > maxLength.intValue()) {
512             getDocument().setText(newValue.substring(0, maxLength.intValue()));
513         } else {
514             getDocument().setText(newValue);
515         }
516     }
517     
518     /**
519      * Sets the tool tip text (displayed when the mouse cursor is hovered
520      * over the component).
521      *
522      * @param newValue the new tool tip text
523      */

524     public void setToolTipText(String JavaDoc newValue) {
525         setProperty(PROPERTY_TOOL_TIP_TEXT, newValue);
526     }
527
528     /**
529      * Sets the vertical scroll bar position.
530      * The provided <code>Extent</code> value must be in pixel units.
531      *
532      * @param newValue the new scroll bar position
533      */

534     public void setVerticalScroll(Extent newValue) {
535         setProperty(PROPERTY_VERTICAL_SCROLL, newValue);
536     }
537     
538     /**
539      * Sets the width of the text component.
540      * This property supports <code>Extent</code>s with
541      * either fixed or percentage-based units.
542      *
543      * @param newValue the new width
544      */

545     public void setWidth(Extent newValue) {
546         setProperty(PROPERTY_WIDTH, newValue);
547     }
548 }
549
Popular Tags