KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > STextField


1 /*
2  * $Id: STextField.java,v 1.8 2004/12/21 09:14:24 hengels 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.plaf.TextFieldCG;
17
18 import javax.swing.event.EventListenerList JavaDoc;
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
24  * @version $Revision: 1.8 $
25  */

26 public class STextField
27         extends STextComponent {
28
29     /**
30      * maximum columns shown
31      */

32     protected int columns = 12;
33
34     /**
35      * maximum columns allowed
36      */

37     protected int maxColumns = -1;
38
39     /**
40      * default action command to fire
41      */

42     protected String JavaDoc actionCommand;
43
44     // Flag to ensure that infinite loops do not occur with ActionEvents.
45
private boolean firingActionEvent = false;
46
47     public STextField() {
48     }
49
50     public STextField(String JavaDoc text) {
51         super(text);
52     }
53
54     public void setColumns(int c) {
55         int oldColumns = columns;
56         columns = c;
57         if (columns != oldColumns)
58             reload();
59     }
60
61     public int getColumns() {
62         return columns;
63     }
64
65     public void setMaxColumns(int mc) {
66         int oldMaxColumns = maxColumns;
67         maxColumns = mc;
68         if (maxColumns != oldMaxColumns)
69             reload();
70     }
71
72     public int getMaxColumns() {
73         return maxColumns;
74     }
75
76     public void setCG(TextFieldCG cg) {
77         super.setCG(cg);
78     }
79
80     /**
81      * Sets the action commnand that should be included in the event
82      * sent to action listeners.
83      *
84      * @param command a string containing the "command" that is sent
85      * to action listeners. The same listener can then
86      * do different things depending on the command it
87      * receives.
88      */

89     public void setActionCommand(String JavaDoc command) {
90         actionCommand = command;
91     }
92
93     /**
94      * Returns the action commnand that is included in the event sent to
95      * action listeners.
96      *
97      * @return the string containing the "command" that is sent
98      * to action listeners.
99      */

100     public String JavaDoc getActionCommand() {
101         if (actionCommand == null)
102             return getText();
103         return actionCommand;
104     }
105
106     /**
107      * Adds an ActionListener to the button.
108      *
109      * @param listener the ActionListener to be added
110      */

111     public void addActionListener(ActionListener JavaDoc listener) {
112         addEventListener(ActionListener JavaDoc.class, listener);
113     }
114
115     /**
116      * Removes the supplied Listener from teh listener list
117      */

118     public void removeActionListener(ActionListener JavaDoc listener) {
119         removeEventListener(ActionListener JavaDoc.class, listener);
120     }
121
122     /**
123      * Returns an array of all the <code>ActionListener</code>s added
124      * to this AbstractButton with addActionListener().
125      *
126      * @return all of the <code>ActionListener</code>s added or an empty
127      * array if no listeners have been added
128      */

129     public ActionListener JavaDoc[] getActionListeners() {
130         return (ActionListener JavaDoc[]) (getListeners(ActionListener JavaDoc.class));
131     }
132
133     /**
134      * Fire an ActionEvent at each registered listener.
135      *
136      * @param event the supplied ActionEvent
137      */

138     protected void fireActionPerformed(ActionEvent JavaDoc event) {
139         // Guaranteed to return a non-null array
140
Object JavaDoc[] listeners = getListenerList();
141         ActionEvent JavaDoc e = null;
142         // Process the listeners last to first, notifying
143
// those that are interested in this event
144
for (int i = listeners.length - 2; i >= 0; i -= 2) {
145             if (listeners[i] == ActionListener JavaDoc.class) {
146                 if (e == null) {
147                     String JavaDoc actionCommand = event.getActionCommand();
148                     if (actionCommand == null) {
149                         actionCommand = getActionCommand();
150                     }
151                     e = new ActionEvent JavaDoc(this,
152                             ActionEvent.ACTION_PERFORMED,
153                             actionCommand,
154                             event.getWhen(),
155                             event.getModifiers());
156                 }
157                 ((ActionListener JavaDoc) listeners[i + 1]).actionPerformed(e);
158             }
159         }
160     }
161
162     /**
163      * Notify all listeners that have registered as ActionListeners if the
164      * selected item has changed
165      *
166      * @see EventListenerList
167      */

168     protected void fireActionEvent() {
169         if (!firingActionEvent) {
170             // Set flag to ensure that an infinite loop is not created
171
firingActionEvent = true;
172
173             ActionEvent JavaDoc e = null;
174
175             // Guaranteed to return a non-null array
176
Object JavaDoc[] listeners = getListenerList();
177             // Process the listeners last to first, notifying
178
// those that are interested in this event
179
for (int i = listeners.length - 2; i >= 0; i -= 2) {
180                 if (listeners[i] == ActionListener JavaDoc.class) {
181                     if (e == null)
182                         e = new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED,
183                                 getActionCommand());
184                     ((ActionListener JavaDoc) listeners[i + 1]).actionPerformed(e);
185                 }
186             }
187             firingActionEvent = false;
188         }
189     }
190
191     public void fireFinalEvents() {
192         super.fireFinalEvents();
193         fireActionEvent();
194     }
195 }
196
197
198
Popular Tags