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.resource.ResourceManager;
019: import org.wings.script.JavaScriptEvent;
020: import org.wings.script.JavaScriptListener;
021: import org.wings.util.SStringBuilder;
022:
023: import java.io.IOException;
024:
025: public class CheckBoxCG extends ButtonCG implements
026: org.wings.plaf.CheckBoxCG {
027:
028: private static final long serialVersionUID = 1L;
029: protected boolean useIconsInForms = false;
030:
031: public boolean isUseIconsInForm() {
032: return useIconsInForms;
033: }
034:
035: public void setUseIconsInForm(boolean useIconsInForm) {
036: this .useIconsInForms = useIconsInForm;
037: }
038:
039: public void installCG(SComponent component) {
040: super .installCG(component);
041: final SAbstractButton button = (SAbstractButton) component;
042: installIcons(button);
043: }
044:
045: protected void installIcons(final SAbstractButton button) {
046: button.setIcon((SIcon) ResourceManager.getObject(
047: "SCheckBox.icon", SIcon.class));
048: button.setSelectedIcon((SIcon) ResourceManager.getObject(
049: "SCheckBox.selectedIcon", SIcon.class));
050: button.setRolloverIcon((SIcon) ResourceManager.getObject(
051: "SCheckBox.rolloverIcon", SIcon.class));
052: button.setRolloverSelectedIcon((SIcon) ResourceManager
053: .getObject("SCheckBox.rolloverSelectedIcon",
054: SIcon.class));
055: button.setPressedIcon((SIcon) ResourceManager.getObject(
056: "SCheckBox.pressedIcon", SIcon.class));
057: button.setDisabledIcon((SIcon) ResourceManager.getObject(
058: "SCheckBox.disabledIcon", SIcon.class));
059: button.setDisabledSelectedIcon((SIcon) ResourceManager
060: .getObject("SCheckBox.disabledSelectedIcon",
061: SIcon.class));
062: }
063:
064: public void writeInternal(final Device device,
065: final SComponent component) throws IOException {
066: final SAbstractButton button = (SAbstractButton) component;
067:
068: final boolean showAsFormComponent = button
069: .getShowAsFormComponent();
070: final String text = button.getText();
071: final SIcon icon = getIcon(button);
072:
073: /*
074: // text only
075: if (icon == null && text != null && (!showAsFormComponent || useIconsInForms && showAsFormComponent)) {
076: device.print("<table");
077: tableAttributes(device, button);
078: device.print("><tr><td>");
079: writeText(device, text, button.isWordWrap());
080: device.print("</td></tr></table>");
081: }
082: */
083: // icon or input only
084: if (text == null) {
085: device.print("<table");
086: tableAttributes(device, button);
087: device.print("><tr><td>");
088: if ((showAsFormComponent && !useIconsInForms)
089: || icon == null)
090: writeInput(device, button);
091: else
092: writeIcon(device, icon, Utils.isMSIE(component));
093:
094: device.print("</td></tr></table>");
095: } else {
096: new IconTextCompound() {
097: protected void text(Device device) throws IOException {
098: writeText(device, text, button.isWordWrap());
099: }
100:
101: protected void icon(Device device) throws IOException {
102: if ((showAsFormComponent && !useIconsInForms)
103: || icon == null)
104: writeInput(device, button);
105: else
106: writeIcon(device, icon, Utils.isMSIE(component));
107: }
108:
109: protected void tableAttributes(Device d)
110: throws IOException {
111: CheckBoxCG.this .tableAttributes(d, button);
112: }
113: }.writeCompound(device, component, button
114: .getHorizontalTextPosition(), button
115: .getVerticalTextPosition(), false);
116: }
117: }
118:
119: protected void tableAttributes(Device device, SAbstractButton button)
120: throws IOException {
121: final boolean showAsFormComponent = button
122: .getShowAsFormComponent();
123: // table is clickable
124: if (!showAsFormComponent || useIconsInForms) {
125: // Remove superfluous 'onChangeSubmitListener' (in case there is any).
126: // This is because we don't want to render 'onclick' AND 'onchange'.
127: Object clientProperty = button
128: .getClientProperty("onChangeSubmitListener");
129: if (clientProperty != null
130: && clientProperty instanceof JavaScriptListener) {
131: button
132: .removeScriptListener((JavaScriptListener) clientProperty);
133: button
134: .putClientProperty("onChangeSubmitListener",
135: null);
136: }
137:
138: tableClickability(device, button);
139:
140: if (button.isFocusOwner())
141: Utils.optAttribute(device, "foc", button.getName());
142:
143: Utils.optAttribute(device, "tabindex", button
144: .getFocusTraversalIndex());
145: Utils.optAttribute(device, "accesskey", button
146: .getMnemonic());
147: }
148:
149: if (button.isSelected())
150: device.print(" checked=\"true\"");
151:
152: String style = button.getStyle();
153: SStringBuilder className = new SStringBuilder(style);
154: if (button.getShowAsFormComponent())
155: className.append("_form");
156: if (!button.isEnabled())
157: className.append("_disabled");
158: if (button.isSelected())
159: className.append("_selected");
160:
161: button.setStyle(className.toString());
162: Utils.writeAllAttributes(device, button);
163: button.setStyle(style);
164: }
165:
166: protected void tableClickability(Device device,
167: SAbstractButton button) throws IOException {
168: Utils.printClickability(device, button, button
169: .getToggleSelectionParameter(), button.isEnabled(),
170: button.getShowAsFormComponent());
171: }
172:
173: protected void writeInput(Device device, SAbstractButton button)
174: throws IOException {
175: if (button.getShowAsFormComponent() && !useIconsInForms) {
176: Object clientProperty = button
177: .getClientProperty("onChangeSubmitListener");
178: // If the application developer attached any ActionListeners, ItemListeners or
179: // ChangeListeners to this SCheckBox or its ButtonGroup, the surrounding form
180: // gets submitted as soon as the state of this SCheckBox changed.
181: if (button.getActionListeners().length > 0
182: || button.getItemListeners().length > 0
183: || button.getChangeListeners().length > 0
184: || (button.getGroup() != null && button.getGroup()
185: .getActionListeners().length > 0)) {
186: if (clientProperty == null) {
187: String event = JavaScriptEvent.ON_CHANGE;
188: String code = "wingS.request.sendEvent(event, true, "
189: + !button.isReloadForced() + ");";
190: if (Utils.isMSIE(button)) {
191: // In IE the "onchange"-event gets fired when a control loses the
192: // input focus and its value has been modified since gaining focus.
193: // Even though this is actually the correct behavior, we want the
194: // event to get fired immediately - thats why we use IE's proprietary
195: // "onpropertychange"-event.
196: event = "onpropertychange";
197: }
198: JavaScriptListener javaScriptListener = new JavaScriptListener(
199: event, code);
200: button.addScriptListener(javaScriptListener);
201: button.putClientProperty("onChangeSubmitListener",
202: javaScriptListener);
203: }
204: } else if (clientProperty != null
205: && clientProperty instanceof JavaScriptListener) {
206: button
207: .removeScriptListener((JavaScriptListener) clientProperty);
208: button
209: .putClientProperty("onChangeSubmitListener",
210: null);
211: }
212: }
213:
214: device.print("<input type=\"hidden\" name=\"");
215: Utils.write(device, Utils.event(button));
216: device.print("\" value=\"hidden_reset\"/>");
217:
218: device.print("<input type=\"checkbox\" name=\"");
219:
220: Utils.write(device, Utils.event(button));
221: device.print("\"");
222: Utils.writeEvents(device, button, null);
223: Utils.optAttribute(device, "tabindex", button
224: .getFocusTraversalIndex());
225:
226: if (!button.isEnabled())
227: device.print(" disabled=\"true\"");
228: if (button.isSelected())
229: device.print(" checked=\"true\"");
230: if (button.isFocusOwner())
231: Utils.optAttribute(device, "foc", button.getName());
232:
233: device.print("/>");
234: }
235:
236: public Update getTextUpdate(SCheckBox checkBox, String text) {
237: return text == null ? null : new TextUpdate(checkBox, text);
238: }
239:
240: public Update getIconUpdate(SCheckBox checkBox, SIcon icon) {
241: return icon == null ? null : new IconUpdate(checkBox, icon);
242: }
243: }
|