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.io.StringBuilderDevice;
018: import org.wings.plaf.CGManager;
019: import org.wings.plaf.Update;
020: import org.wings.plaf.css.script.OnPageRenderedScript;
021: import org.wings.script.*;
022: import org.wings.util.SStringBuilder;
023:
024: import java.io.IOException;
025:
026: public final class ComboBoxCG extends AbstractComponentCG implements
027: org.wings.plaf.ComboBoxCG {
028:
029: private static final long serialVersionUID = 1L;
030:
031: public void installCG(final SComponent comp) {
032: super .installCG(comp);
033: final SComboBox component = (SComboBox) comp;
034: final CGManager manager = component.getSession().getCGManager();
035: Object value;
036: value = manager.getObject("SComboBox.renderer",
037: SDefaultListCellRenderer.class);
038: if (value != null) {
039: component.setRenderer((SDefaultListCellRenderer) value);
040: }
041: }
042:
043: protected void writeFormComboBox(Device device, SComboBox component)
044: throws IOException {
045: Object clientProperty = component
046: .getClientProperty("onChangeSubmitListener");
047: // If the application developer attached any ActionListeners or ItemListeners to
048: // this SComboBox, the surrounding form gets submitted as soon as the state / the
049: // selection of this SComboBox changed.
050: if (component.getActionListeners().length > 0
051: || component.getItemListeners().length > 0) {
052: if (clientProperty == null) {
053: String event = JavaScriptEvent.ON_CHANGE;
054: String code = "wingS.request.sendEvent(event, true, "
055: + !component.isReloadForced() + ");";
056: JavaScriptListener javaScriptListener = new JavaScriptListener(
057: event, code);
058: component.addScriptListener(javaScriptListener);
059: component.putClientProperty("onChangeSubmitListener",
060: javaScriptListener);
061: }
062: } else if (clientProperty != null
063: && clientProperty instanceof JavaScriptListener) {
064: component
065: .removeScriptListener((JavaScriptListener) clientProperty);
066: component.putClientProperty("onChangeSubmitListener", null);
067: }
068:
069: device.print("<span><select size=\"1\" wrapping=\"1\"");
070:
071: Utils.writeAllAttributes(device, component);
072:
073: Utils.optAttribute(device, "name", Utils.event(component));
074: Utils.optAttribute(device, "tabindex", component
075: .getFocusTraversalIndex());
076: Utils.writeEvents(device, component, null);
077: if (!component.isEnabled())
078: device.print(" disabled=\"true\"");
079: if (component.isFocusOwner())
080: Utils.optAttribute(device, "foc", component.getName());
081:
082: device.print(">");
083:
084: final javax.swing.ComboBoxModel model = component.getModel();
085: final int size = model.getSize();
086: final int selected = component.getSelectedIndex();
087:
088: final SListCellRenderer renderer = component.getRenderer();
089: final org.wings.io.StringBuilderDevice stringBuilderDevice = new StringBuilderDevice(
090: 512);
091: for (int i = 0; i < size; i++) {
092: SComponent cellRenderer = null;
093: if (renderer != null) {
094: cellRenderer = renderer.getListCellRendererComponent(
095: component, model.getElementAt(i), false, i);
096: } else {
097: device.print("<!--renderer==null-->");
098: }
099:
100: Utils.printNewline(device, component);
101: device.print("<option");
102: Utils.optAttribute(device, "value", component
103: .getSelectionParameter(i));
104: if (selected == i) {
105: device.print(" selected=\"selected\"");
106: }
107:
108: if (cellRenderer != null) {
109: Utils.optAttribute(device, "title", cellRenderer
110: .getToolTipText());
111: SStringBuilder buffer = Utils
112: .generateCSSComponentInlineStyle(cellRenderer);
113: Utils.optAttribute(device, "style", buffer.toString());
114: }
115:
116: device.print(">"); //option
117:
118: if (cellRenderer != null) {
119: // Hack: remove all tags, because in form selections, looks ugly.
120: stringBuilderDevice.reset();
121: cellRenderer.write(stringBuilderDevice);
122: final int printedChars = Utils.writeWithoutHTML(device,
123: stringBuilderDevice.toString());
124:
125: if (printedChars == 0) {
126: // If the option is empty ("") Firefox
127: // renders somehow smaller comboboxes!
128: device.print(" ");
129: }
130: } else {
131: device.print("<!--cellrenderer==null, use toString-->");
132: device.print(model.getElementAt(i).toString());
133: }
134:
135: device.print("</option>");
136: }
137:
138: Utils.printNewline(device, component);
139: device.print("</select>");
140: // util method
141:
142: device.print("<input type=\"hidden\"");
143: Utils.optAttribute(device, "name", Utils.event(component));
144: Utils.optAttribute(device, "value", -1);
145: device.print("/></span>");
146: if (selected == -1) {
147: component
148: .getSession()
149: .getScriptManager()
150: .addScriptListener(
151: new OnPageRenderedScript(
152: "document.getElementById(\""
153: + component.getName()
154: + "\").selectedIndex = -1;"));
155: }
156: }
157:
158: public void writeInternal(final Device device, final SComponent _c)
159: throws IOException {
160: final SComboBox comboBox = (SComboBox) _c;
161: // TODO: implement anchor combobox
162: //if (comboBox.getShowAsFormComponent()) {
163: writeFormComboBox(device, comboBox);
164: //} else {
165: // writeAnchorComboBox(device, comboBox);
166: // }
167: }
168:
169: public Update getSelectionUpdate(SComboBox comboBox,
170: int selectedIndex) {
171: return new SelectionUpdate(comboBox, selectedIndex);
172: }
173:
174: protected class SelectionUpdate extends AbstractUpdate {
175:
176: private Integer selectedIndex;
177:
178: public SelectionUpdate(SComponent component, int selectedIndex) {
179: super (component);
180: this .selectedIndex = new Integer(selectedIndex);
181: }
182:
183: public Handler getHandler() {
184: UpdateHandler handler = new UpdateHandler(
185: "selectionComboBox");
186: handler.addParameter(component.getName());
187: handler.addParameter(selectedIndex);
188: return handler;
189: }
190:
191: }
192: }
|