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 wingset;
014:
015: import org.wings.*;
016: import org.wings.border.SLineBorder;
017: import org.wings.event.*;
018: import org.wings.session.SessionManager;
019: import org.wings.text.SAbstractFormatter;
020: import org.wings.text.SDateFormatter;
021:
022: import java.awt.*;
023: import java.awt.event.ActionEvent;
024: import java.text.DateFormat;
025: import java.text.NumberFormat;
026: import java.text.ParseException;
027:
028: /**
029: * @author <a href="mailto:mreinsch@to.com">Michael Reinsch</a>
030: */
031: public class TextComponentExample extends WingSetPane {
032: private final SLabel actionEvent = new SLabel(
033: "(no form or button event)");
034: private final STextArea eventLog = new STextArea(
035: "(no document events fired)");
036: private ComponentControls controls;
037: private SDateFormatter dateFormatter = new SDateFormatter(
038: DateFormat.getDateInstance(DateFormat.SHORT));
039: private STextField textField;
040: private SFormattedTextField numberTextFieldCoR;
041: private SFormattedTextField dateTextFieldCoR;
042: private SFormattedTextField numberTextFieldC;
043: private SFormattedTextField dateTextFieldC;
044: private SPasswordField passwordField;
045: private STextArea textArea;
046:
047: protected SComponent createControls() {
048: controls = new TextComponentControls();
049: return controls;
050: }
051:
052: public SComponent createExample() {
053: SGridLayout gridLayout = new SGridLayout(2);
054: gridLayout.setHgap(10);
055: gridLayout.setVgap(4);
056: SPanel panel = new SPanel(gridLayout);
057:
058: panel.add(new SLabel("STextField: "));
059: textField = new STextField();
060: textField.setName("textfield");
061: textField
062: .setToolTipText("Here you can enter any abritriary text.");
063: textField
064: .addDocumentListener(new MyDocumentListener(textField));
065: textField.setHorizontalAlignment(SConstants.LEFT_ALIGN);
066: panel.add(textField);
067:
068: panel
069: .add(new SLabel(
070: "SFormattedTextField (NumberFormat) :\n[COMMIT_OR_REVERT]"));
071: SFormattedTextField numberTextFieldCoR = new SFormattedTextField(
072: new NumberFormatter());
073: this .numberTextFieldCoR = numberTextFieldCoR;
074: this .numberTextFieldCoR.setName("numberfieldCoR");
075: numberTextFieldCoR
076: .setToolTipText("Text entered here will be formatted as number when you leave focus.<br>"
077: + "If you entered an invalid number the text should become red.<br>"
078: + "This uses code executed on server side in Java!");
079: numberTextFieldCoR.addDocumentListener(new MyDocumentListener(
080: numberTextFieldCoR));
081: numberTextFieldCoR
082: .setHorizontalAlignment(SConstants.LEFT_ALIGN);
083: panel.add(numberTextFieldCoR);
084:
085: panel
086: .add(new SLabel(
087: "SFormattedTextField (DateFormat) :\n[COMMIT_OR_REVERT]"));
088: SFormattedTextField dateTextFieldCoR = new SFormattedTextField(
089: dateFormatter);
090: this .dateTextFieldCoR = dateTextFieldCoR;
091: this .dateTextFieldCoR.setName("datefieldCoR");
092: dateTextFieldCoR
093: .setToolTipText("Enter a valid/invalid date here.<br>"
094: + "Dates will be parsed on server side and reformatted accordingly.");
095: dateTextFieldCoR.addDocumentListener(new MyDocumentListener(
096: dateTextFieldCoR));
097: dateTextFieldCoR.setHorizontalAlignment(SConstants.LEFT_ALIGN);
098: panel.add(dateTextFieldCoR);
099:
100: panel.add(new SLabel(
101: "SFormattedTextField (NumberFormat) :\n[COMMIT]"));
102: SFormattedTextField numberTextFieldC = new SFormattedTextField(
103: new NumberFormatter());
104: this .numberTextFieldC = numberTextFieldC;
105: this .numberTextFieldC.setName("numberfieldC");
106: this .numberTextFieldC
107: .setFocusLostBehavior(SFormattedTextField.COMMIT);
108: numberTextFieldC
109: .setToolTipText("Text entered here will be formatted as number when you leave focus.<br>"
110: + "If you entered an invalid number the text should become red.<br>"
111: + "This uses code executed on server side in Java!");
112: numberTextFieldC.addDocumentListener(new MyDocumentListener(
113: numberTextFieldC));
114: numberTextFieldC.setHorizontalAlignment(SConstants.LEFT_ALIGN);
115: panel.add(numberTextFieldC);
116:
117: panel.add(new SLabel(
118: "SFormattedTextField (DateFormat) :\n[COMMIT]: "));
119: SFormattedTextField dateTextFieldC = new SFormattedTextField(
120: dateFormatter);
121: this .dateTextFieldC = dateTextFieldC;
122: this .dateTextFieldC.setName("datefieldC");
123: this .dateTextFieldC
124: .setFocusLostBehavior(SFormattedTextField.COMMIT);
125: dateTextFieldC
126: .setToolTipText("Enter a valid/invalid date here.<br>"
127: + "Dates will be parsed on server side and reformatted accordingly.");
128: dateTextFieldC.addDocumentListener(new MyDocumentListener(
129: dateTextFieldC));
130: dateTextFieldC.setHorizontalAlignment(SConstants.LEFT_ALIGN);
131: panel.add(dateTextFieldC);
132:
133: panel.add(new SLabel("SPasswordField: "));
134: SPasswordField passwordField = new SPasswordField();
135: this .passwordField = passwordField;
136: this .passwordField.setName("passwordfield");
137: passwordField.setToolTipText("Just a regular passsword input.");
138: passwordField.addDocumentListener(new MyDocumentListener(
139: passwordField));
140: passwordField.setHorizontalAlignment(SConstants.LEFT_ALIGN);
141: panel.add(passwordField);
142:
143: panel.add(new SLabel("STextArea: "));
144: STextArea textArea = new STextArea("");
145: this .textArea = textArea;
146: this .textArea.setName("textarea");
147: textArea.setPreferredSize(new SDimension(250, 50));
148: textArea
149: .setToolTipText("Okay - but don't start writing books now ;-)");
150: textArea.addDocumentListener(new MyDocumentListener(textArea));
151: textArea.setHorizontalAlignment(SConstants.LEFT_ALIGN);
152: panel.add(textArea);
153:
154: panel.add(new SLabel("SDocumentEvents: "));
155: panel.add(eventLog);
156:
157: SLabel actionEventsLabel = new SLabel(
158: "ActionEvents (try Submit vs. Enter):");
159: panel.add(actionEventsLabel);
160: panel.add(actionEvent);
161:
162: panel.add(new SLabel());
163: SButton button = new SButton("Submit");
164: button.addActionListener(new java.awt.event.ActionListener() {
165: public void actionPerformed(ActionEvent e) {
166: actionEvent.setText("Button clicked + ");
167: }
168: });
169: button.setHorizontalAlignment(SConstants.LEFT_ALIGN);
170: panel.add(button);
171:
172: // Styling of all components.
173: for (int i = 0; i < panel.getComponents().length; i++) {
174: SComponent component = panel.getComponents()[i];
175: component.setVerticalAlignment(SConstants.TOP);
176: if ((component instanceof STextComponent) /*&& (component != disabledTextArea)*/
177: && (component != textArea))
178: component
179: .setPreferredSize(new SDimension("250px", null));
180: }
181:
182: eventLog.setEditable(false); // for multiline label
183: eventLog.setBorder(new SLineBorder(1));
184: eventLog.setBackground(new Color(240, 240, 240));
185: eventLog.setHorizontalAlignment(SConstants.LEFT_ALIGN);
186:
187: actionEvent.setBorder(new SLineBorder(1));
188: actionEvent.setBackground(new Color(240, 240, 240));
189: actionEvent.setHorizontalAlignment(SConstants.LEFT_ALIGN);
190:
191: controls.addControllable(textField);
192: controls.addControllable(textArea);
193: controls.addControllable(passwordField);
194: controls.addControllable(numberTextFieldCoR);
195: controls.addControllable(dateTextFieldCoR);
196: controls.addControllable(numberTextFieldC);
197: controls.addControllable(dateTextFieldC);
198:
199: // Clear event log for every request
200: getSession().addRequestListener(new SRequestListener() {
201: public void processRequest(SRequestEvent e) {
202: if (e.getType() == SRequestEvent.DISPATCH_START) {
203: if (eventLog.getText() == null
204: || eventLog.getText().length() == 0)
205: eventLog.setText("(no document events fired)");
206: else
207: eventLog.setText(null);
208: if (actionEvent.getText() == null
209: || actionEvent.getText().length() == 0)
210: actionEvent
211: .setText("(no form or button event)");
212: else
213: actionEvent.setText(null);
214: }
215: }
216: });
217:
218: panel.addParentFrameListener(new SParentFrameListener() {
219: public void parentFrameAdded(SParentFrameEvent e) {
220: ((SForm) e.getParentFrame().getContentPane())
221: .addActionListener(new java.awt.event.ActionListener() {
222: public void actionPerformed(ActionEvent e) {
223: actionEvent.setText(actionEvent
224: .getText()
225: + " Form event");
226: }
227: });
228: }
229:
230: public void parentFrameRemoved(SParentFrameEvent e) {
231: }
232: });
233: return panel;
234: }
235:
236: /**
237: * Listener updating the document event log label.
238: */
239: private final class MyDocumentListener implements SDocumentListener {
240: private STextComponent eventSource;
241:
242: public MyDocumentListener(STextComponent eventSource) {
243: this .eventSource = eventSource;
244: }
245:
246: public void insertUpdate(SDocumentEvent e) {
247: String eventString = "insertUpdate() for "
248: + eventSource.getName() + ": length: "
249: + e.getLength() + " offset: " + e.getOffset()
250: + "\n";
251: eventLog.setText(eventLog.getText() + eventString);
252: }
253:
254: public void removeUpdate(SDocumentEvent e) {
255: String eventString = "removeUpdate() for "
256: + eventSource.getName() + ": length: "
257: + e.getLength() + " offset: " + e.getOffset()
258: + "\n";
259: eventLog.setText(eventLog.getText() + eventString);
260: }
261:
262: public void changedUpdate(SDocumentEvent e) {
263: } // Never called for unstyled documents
264: }
265:
266: private static class NumberFormatter extends SAbstractFormatter {
267: NumberFormat format = NumberFormat
268: .getNumberInstance(SessionManager.getSession()
269: .getLocale());
270:
271: public Object stringToValue(String text) throws ParseException {
272: if (text == null || text.trim().length() == 0)
273: return null;
274: else
275: return format.parse(text.trim());
276: }
277:
278: public String valueToString(Object value) throws ParseException {
279: if (value == null)
280: return "";
281: else
282: return format.format(value);
283: }
284: }
285:
286: class TextComponentControls extends ComponentControls {
287: public TextComponentControls() {
288: widthTextField.setText("100%");
289: formComponentCheckBox.setVisible(false);
290:
291: final SCheckBox editable = new SCheckBox("editable");
292: editable.setSelected(true);
293: editable
294: .addActionListener(new java.awt.event.ActionListener() {
295: public void actionPerformed(ActionEvent e) {
296: textField
297: .setEditable(editable.isSelected());
298: numberTextFieldCoR.setEditable(editable
299: .isSelected());
300: dateTextFieldCoR.setEditable(editable
301: .isSelected());
302: passwordField.setEditable(editable
303: .isSelected());
304: textArea.setEditable(editable.isSelected());
305: }
306: });
307: addControl(editable);
308: }
309: }
310: }
|