001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029:
030: import javax.swing.JPasswordField;
031: import javax.swing.JTextField;
032: import javax.swing.event.DocumentEvent;
033: import javax.swing.event.DocumentListener;
034:
035: import org.beryl.gui.GUIEvent;
036: import org.beryl.gui.GUIEventListener;
037: import org.beryl.gui.GUIException;
038: import org.beryl.gui.Widget;
039: import org.beryl.gui.WidgetInfo;
040: import org.beryl.gui.model.MapChangeEvent;
041: import org.beryl.gui.model.MapDataModel;
042: import org.beryl.gui.model.ModelChangeEvent;
043: import org.beryl.gui.validators.ValidationException;
044:
045: public class TextField extends Widget {
046: protected static WidgetInfo textFieldInfo = null;
047: public static final Color DEFAULT_COLOR = new Color(255, 255, 255);
048: public static final Color ERROR_COLOR = new Color(255, 200, 200);
049:
050: protected JTextField textField = null;
051: private String key = null;
052: private boolean sendEvents = true;
053: private boolean processEvents = true;
054:
055: static {
056: textFieldInfo = new WidgetInfo(TextField.class, widgetInfo);
057: textFieldInfo.addProperty("key", "string", "");
058: textFieldInfo.addEvent("activated");
059: };
060:
061: public TextField(Widget parent, String name) throws GUIException {
062: super (parent, name);
063: textField = new JTextField();
064: }
065:
066: public void setProperty(String name, Object value)
067: throws GUIException {
068: if ("key".equals(name))
069: key = (String) value;
070: else
071: super .setProperty(name, value);
072: }
073:
074: private void reload() throws GUIException {
075: MapDataModel model = getDataModel();
076: if (model != null && key != null) {
077: try {
078: processEvents = false;
079: String value = (String) model.getValue(key);
080: if (value != null) {
081: textField.setText(value);
082: } else {
083: String text = null;
084: if (textField instanceof JPasswordField)
085: text = new String(((JPasswordField) textField)
086: .getPassword());
087: else
088: text = textField.getText();
089: model.setValue(TextField.this , key, text);
090: }
091: } finally {
092: processEvents = true;
093: }
094: }
095: }
096:
097: public String getText() {
098: return textField.getText();
099: }
100:
101: public void validate() throws ValidationException, GUIException {
102: if (hasValidators()) {
103: try {
104: super .validate();
105: if (textField.getBackground() != DEFAULT_COLOR) {
106: textField.setBackground(DEFAULT_COLOR);
107: }
108: if (getParentWidget() instanceof LabeledWidget) {
109: ((LabeledWidget) getParentWidget()).clearError();
110: }
111: } catch (ValidationException e) {
112: textField.setBackground(ERROR_COLOR);
113: if (getParentWidget() instanceof LabeledWidget) {
114: ((LabeledWidget) getParentWidget()).setError(e);
115: }
116: throw e;
117: }
118: }
119: }
120:
121: public void modelChanged(ModelChangeEvent e) throws GUIException {
122: if (processEvents) {
123: sendEvents = false;
124: try {
125: if (e.getSource() == this ) {
126: /* New data model */
127: reload();
128: try {
129: validate();
130: } catch (ValidationException ex) {
131: /* Ignore, error status is displayed already */
132: }
133: } else if (e instanceof MapChangeEvent) {
134: MapChangeEvent event = (MapChangeEvent) e;
135: if (event.getKey() == null) {
136: reload();
137: } else if (event.getKey().equals(key)) {
138: textField.setText((String) event.getNewValue());
139: }
140: try {
141: validate();
142: } catch (ValidationException ex) {
143: /* Ignore, error status is displayed already */
144: }
145: }
146: } finally {
147: sendEvents = true;
148: }
149: }
150: }
151:
152: public void finalizeConstruction() {
153: textField.setPreferredSize(new Dimension(MAX_WIDTH,
154: DEFAULT_HEIGHT));
155: textField.getDocument().addDocumentListener(
156: new DocumentListener() {
157: public void insertUpdate(DocumentEvent e) {
158: changedUpdate(e);
159: }
160:
161: public void removeUpdate(DocumentEvent e) {
162: changedUpdate(e);
163: }
164:
165: public void changedUpdate(DocumentEvent e) {
166: if (sendEvents) {
167: try {
168: MapDataModel model = getDataModel();
169: if (model != null && key != null) {
170: String text = null;
171: if (textField instanceof JPasswordField)
172: text = new String(
173: ((JPasswordField) textField)
174: .getPassword());
175: else
176: text = textField.getText();
177: try {
178: processEvents = false;
179: model.setValue(TextField.this ,
180: key, text);
181: } finally {
182: processEvents = true;
183: }
184: try {
185: validate();
186: } catch (ValidationException ex) {
187: /* Ignore, error status is displayed already */
188: }
189: }
190: } catch (GUIException ex) {
191: throw new RuntimeException(ex);
192: }
193: }
194: }
195: });
196: }
197:
198: public void addListener(String event, final String name,
199: final GUIEventListener listener) throws GUIException {
200: if ("activated".equals(event)) {
201: textField.addActionListener(new ActionListener() {
202: public void actionPerformed(ActionEvent e) {
203: listener.eventOccured(new GUIEvent(TextField.this ,
204: name, e));
205: }
206: });
207: } else {
208: super .addListener(event, name, listener);
209: }
210: }
211:
212: public Component getWidget() {
213: return textField;
214: }
215:
216: public WidgetInfo getWidgetInfo() {
217: return textFieldInfo;
218: }
219: }
|