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.plaf.Update;
017: import org.wings.util.SStringBuilder;
018: import org.wings.io.Device;
019:
020: import java.io.IOException;
021:
022: public class ButtonCG extends AbstractLabelCG implements
023: org.wings.plaf.ButtonCG {
024:
025: /**
026: * a serializable class is supposed to have this ID.
027: */
028: private static final long serialVersionUID = -1794530181411426283L;
029:
030: public void writeInternal(final Device device,
031: final SComponent component) throws IOException {
032: final SAbstractButton button = (SAbstractButton) component;
033:
034: String text = button.getText();
035: final SIcon icon = getIcon(button);
036:
037: if (icon == null && text == null)
038: text = "";
039: final String ftext = text;
040:
041: if (icon == null) {
042: device.print("<table");
043: tableAttributes(device, button);
044: device.print("><tr><td");
045: PaddingVoodoo
046: .doSimpleTablePaddingWorkaround(device, button);
047: device.print(">");
048: writeText(device, text, button.isWordWrap());
049: device.print("</td></tr></table>");
050: } else if (text == null) {
051: device.print("<table");
052: tableAttributes(device, button);
053: device.print("><tr><td");
054: PaddingVoodoo
055: .doSimpleTablePaddingWorkaround(device, button);
056: device.print(">");
057: writeIcon(device, icon, Utils.isMSIE(component));
058: device.print("</td></tr></table>");
059: } else {
060: new IconTextCompound() {
061: protected void text(Device d) throws IOException {
062: writeText(d, ftext, button.isWordWrap());
063: }
064:
065: protected void icon(Device d) throws IOException {
066: writeIcon(d, icon, Utils.isMSIE(component));
067: }
068:
069: protected void tableAttributes(Device d)
070: throws IOException {
071: ButtonCG.this .tableAttributes(d, button);
072: }
073: }.writeCompound(device, component, button
074: .getHorizontalTextPosition(), button
075: .getVerticalTextPosition(), false);
076: }
077: }
078:
079: protected void tableAttributes(Device device, SAbstractButton button)
080: throws IOException {
081: Utils.printClickability(device, button, button
082: .getToggleSelectionParameter(), button.isEnabled(),
083: button.getShowAsFormComponent());
084:
085: String style = button.getStyle();
086: SStringBuilder className = new SStringBuilder(
087: style != null ? style : "SButton");
088: if (button.getShowAsFormComponent())
089: className.append("_form");
090: if (!button.isEnabled())
091: className.append("_disabled");
092: if (button.isSelected())
093: className.append("_selected");
094:
095: button.setStyle(className.toString());
096: Utils.writeAllAttributes(device, button);
097: button.setStyle(style);
098:
099: if (button.isFocusOwner())
100: Utils.optAttribute(device, "foc", button.getName());
101:
102: Utils.optAttribute(device, "tabindex", button
103: .getFocusTraversalIndex());
104: Utils.optAttribute(device, "accesskey", button.getMnemonic());
105: }
106:
107: /* Retrieve according icon for a button. */
108: public static SIcon getIcon(SAbstractButton abstractButton) {
109: if (abstractButton.isSelected()) {
110: return abstractButton.isEnabled() ? abstractButton
111: .getSelectedIcon() : abstractButton
112: .getDisabledSelectedIcon();
113: } else {
114: return abstractButton.isEnabled() ? abstractButton
115: .getIcon() : abstractButton.getDisabledIcon();
116: }
117: }
118:
119: public Update getTextUpdate(SButton button, String text) {
120: return text == null ? null : new TextUpdate(button, text);
121: }
122:
123: public Update getIconUpdate(SButton button, SIcon icon) {
124: return icon == null ? null : new IconUpdate(button, icon);
125: }
126:
127: }
|