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.plaf.css;
014:
015: import org.wings.*;
016: import org.wings.io.Device;
017: import org.wings.plaf.Update;
018: import org.wings.script.*;
019:
020: import java.io.IOException;
021:
022: public class TextFieldCG extends AbstractComponentCG implements
023: org.wings.plaf.TextFieldCG {
024:
025: private static final long serialVersionUID = 1L;
026:
027: int horizontalOversize = 4;
028:
029: public int getHorizontalOversize() {
030: return horizontalOversize;
031: }
032:
033: public void setHorizontalOversize(int horizontalOversize) {
034: this .horizontalOversize = horizontalOversize;
035: }
036:
037: public void installCG(SComponent comp) {
038: super .installCG(comp);
039: if (isMSIE(comp))
040: comp.putClientProperty("horizontalOversize", new Integer(
041: horizontalOversize));
042: }
043:
044: public void writeInternal(final Device device,
045: final SComponent component) throws IOException {
046: final STextField textField = (STextField) component;
047:
048: onChangeSubmitListener(textField);
049:
050: SDimension preferredSize = component.getPreferredSize();
051: boolean tableWrapping = Utils.isMSIE(component)
052: && preferredSize != null
053: && "%".equals(preferredSize.getWidthUnit());
054: String actualWidth = null;
055: if (tableWrapping) {
056: actualWidth = preferredSize.getWidth();
057: preferredSize.setWidth("100%");
058: device.print("<table style=\"table-layout: fixed; width: "
059: + actualWidth + "\"><tr>");
060: device.print("<td style=\"padding-right: "
061: + Utils
062: .calculateHorizontalOversize(textField,
063: true) + "px\">");
064: }
065:
066: device.print("<input type=\"text\"");
067: if (tableWrapping)
068: device.print(" wrapping=\"4\"");
069:
070: Utils.optAttribute(device, "tabindex", textField
071: .getFocusTraversalIndex());
072: Utils.optAttribute(device, "size", textField.getColumns());
073: Utils.optAttribute(device, "maxlength", textField
074: .getMaxColumns());
075: Utils.writeEvents(device, textField, null);
076: if (textField.isFocusOwner())
077: Utils.optAttribute(device, "foc", textField.getName());
078:
079: if (!textField.isEditable() || !textField.isEnabled()) {
080: device.print(" readonly=\"true\"");
081: }
082: if (textField.isEnabled()) {
083: device.print(" name=\"");
084: Utils.write(device, Utils.event(textField));
085: device.print("\"");
086: } else {
087: device.print(" disabled=\"true\"");
088: }
089:
090: Utils.writeAllAttributes(device, component);
091:
092: Utils.optAttribute(device, "value", textField.getText());
093: device.print("/>");
094:
095: printPostInput(device, textField);
096:
097: if (tableWrapping) {
098: preferredSize.setWidth(actualWidth);
099: device.print("</td></tr></table>");
100: }
101: }
102:
103: protected void onChangeSubmitListener(STextField textField) {
104: Object clientProperty = textField
105: .getClientProperty("onChangeSubmitListener");
106: // If the application developer attached any SDocumentListeners to this
107: // STextField, the surrounding form gets submitted as soon as the content
108: // of this STextField changed.
109: if (textField.getDocumentListeners().length > 1
110: || textField instanceof SFormattedTextField) {
111: // We need to test if there are at least 2 document
112: // listeners because each text component registers
113: // itself as a listener of its document as well.
114: if (clientProperty == null) {
115: String event = JavaScriptEvent.ON_CHANGE;
116: String code = "wingS.request.sendEvent(event, true, "
117: + !textField.isReloadForced() + ");";
118: JavaScriptListener javaScriptListener = new JavaScriptListener(
119: event, code);
120: textField.addScriptListener(javaScriptListener);
121: textField.putClientProperty("onChangeSubmitListener",
122: javaScriptListener);
123: }
124: } else if (clientProperty != null
125: && clientProperty instanceof JavaScriptListener) {
126: textField
127: .removeScriptListener((JavaScriptListener) clientProperty);
128: textField.putClientProperty("onChangeSubmitListener", null);
129: }
130: }
131:
132: protected void printPostInput(Device device, STextField textField)
133: throws IOException {
134: }
135:
136: public Update getTextUpdate(STextField textField, String text) {
137: return new TextUpdate(textField, text);
138: }
139:
140: protected static class TextUpdate extends AbstractUpdate {
141:
142: private String text;
143:
144: public TextUpdate(SComponent component, String text) {
145: super (component);
146: this .text = text;
147: }
148:
149: public Handler getHandler() {
150: UpdateHandler handler = new UpdateHandler("value");
151: handler.addParameter(component.getName());
152: handler.addParameter(text == null ? "" : text);
153: return handler;
154: }
155:
156: }
157:
158: }
|