001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.plaf.TextFieldCG;
016:
017: import javax.swing.event.EventListenerList;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020:
021: /**
022: * Single-lined input component for text input which requires a surrounding {@link SForm} element.
023: *
024: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
025: */
026: public class STextField extends STextComponent {
027:
028: /**
029: * maximum columns shown
030: */
031: protected int columns = 12;
032:
033: /**
034: * maximum columns allowed
035: */
036: protected int maxColumns = -1;
037:
038: /**
039: * default action command to fire
040: */
041: protected String actionCommand;
042:
043: // Flag to ensure that infinite loops do not occur with ActionEvents.
044: private boolean firingActionEvent = false;
045:
046: public STextField() {
047: }
048:
049: public STextField(String text) {
050: super (text);
051: }
052:
053: /**
054: * Sets the number of columns of the TextField.
055: * @param c the number of columns
056: */
057: public void setColumns(int c) {
058: int oldColumns = columns;
059: columns = c;
060: if (columns != oldColumns)
061: reload();
062: }
063:
064: /**
065: * Returns the number of columns of the TextField.
066: * @return the number of columns
067: */
068: public int getColumns() {
069: return columns;
070: }
071:
072: public void setMaxColumns(int mc) {
073: int oldMaxColumns = maxColumns;
074: maxColumns = mc;
075: if (maxColumns != oldMaxColumns)
076: reload();
077: }
078:
079: public int getMaxColumns() {
080: return maxColumns;
081: }
082:
083: public void setCG(TextFieldCG cg) {
084: super .setCG(cg);
085: }
086:
087: /**
088: * Sets the action commnand that should be included in the event
089: * sent to action listeners.
090: *
091: * @param command a string containing the "command" that is sent
092: * to action listeners. The same listener can then
093: * do different things depending on the command it
094: * receives.
095: */
096: public void setActionCommand(String command) {
097: actionCommand = command;
098: }
099:
100: /**
101: * Returns the action commnand that is included in the event sent to
102: * action listeners.
103: *
104: * @return the string containing the "command" that is sent
105: * to action listeners.
106: */
107: public String getActionCommand() {
108: if (actionCommand == null)
109: return getText();
110: return actionCommand;
111: }
112:
113: /**
114: * Adds an ActionListener to the button.
115: *
116: * @param listener the ActionListener to be added
117: */
118: public void addActionListener(ActionListener listener) {
119: addEventListener(ActionListener.class, listener);
120: }
121:
122: /**
123: * Removes the supplied Listener from teh listener list
124: */
125: public void removeActionListener(ActionListener listener) {
126: removeEventListener(ActionListener.class, listener);
127: }
128:
129: /**
130: * Returns an array of all the <code>ActionListener</code>s added
131: * to this AbstractButton with addActionListener().
132: *
133: * @return all of the <code>ActionListener</code>s added or an empty
134: * array if no listeners have been added
135: */
136: public ActionListener[] getActionListeners() {
137: return (ActionListener[]) (getListeners(ActionListener.class));
138: }
139:
140: /**
141: * Fire an ActionEvent at each registered listener.
142: *
143: * @param event the supplied ActionEvent
144: */
145: protected void fireActionPerformed(ActionEvent event) {
146: // Guaranteed to return a non-null array
147: Object[] listeners = getListenerList();
148: ActionEvent e = null;
149: // Process the listeners last to first, notifying
150: // those that are interested in this event
151: for (int i = listeners.length - 2; i >= 0; i -= 2) {
152: if (listeners[i] == ActionListener.class) {
153: if (e == null) {
154: String actionCommand = event.getActionCommand();
155: if (actionCommand == null) {
156: actionCommand = getActionCommand();
157: }
158: e = new ActionEvent(this ,
159: ActionEvent.ACTION_PERFORMED,
160: actionCommand, event.getWhen(), event
161: .getModifiers());
162: }
163: ((ActionListener) listeners[i + 1]).actionPerformed(e);
164: }
165: }
166: }
167:
168: /**
169: * Notify all listeners that have registered as ActionListeners if the
170: * selected item has changed
171: *
172: * @see EventListenerList
173: */
174: protected void fireActionEvent() {
175: if (!firingActionEvent) {
176: // Set flag to ensure that an infinite loop is not created
177: firingActionEvent = true;
178:
179: ActionEvent e = null;
180:
181: // Guaranteed to return a non-null array
182: Object[] listeners = getListenerList();
183: // Process the listeners last to first, notifying
184: // those that are interested in this event
185: for (int i = listeners.length - 2; i >= 0; i -= 2) {
186: if (listeners[i] == ActionListener.class) {
187: if (e == null)
188: e = new ActionEvent(this ,
189: ActionEvent.ACTION_PERFORMED,
190: getActionCommand());
191: ((ActionListener) listeners[i + 1])
192: .actionPerformed(e);
193: }
194: }
195: firingActionEvent = false;
196: }
197: }
198:
199: public void fireFinalEvents() {
200: super.fireFinalEvents();
201: fireActionEvent();
202: }
203: }
|