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.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.SComponent;
018: import org.wings.SIcon;
019: import org.wings.SLabel;
020: import org.wings.SResourceIcon;
021: import org.wings.io.Device;
022: import org.wings.io.StringBuilderDevice;
023:
024: import java.io.IOException;
025:
026: public abstract class AbstractLabelCG extends AbstractComponentCG {
027:
028: private static final Log log = LogFactory
029: .getLog(AbstractLabelCG.class);
030:
031: public static SIcon TRANSPARENT_ICON = new SResourceIcon(
032: "org/wings/icons/transdot.gif");
033:
034: public void writeText(Device device, String text, boolean wordWrap)
035: throws IOException {
036: // white-space:nowrap seems to work in all major browser.
037: // Except leading and trailing spaces!
038: device.print("<span").print(
039: wordWrap ? ">" : " style=\"white-space:nowrap\">");
040: if ((text.length() > 5)
041: && (text.substring(0, 6).equalsIgnoreCase("<html>"))) {
042: Utils.writeRaw(device, text.substring(6));
043: } else {
044: // Only quote leading/trailing whitespace
045: int len = text.length();
046: int off = 0;
047: while (off < len && text.charAt(off) == ' ') {
048: off++;
049: device.print(" ");
050: }
051: while (off < len && text.charAt(len - 1) == ' ') {
052: len--;
053: }
054:
055: Utils.quote(device, text.substring(off, len), true, false,
056: false);
057:
058: for (int x = len; x < text.length(); x++) {
059: device.print(" ");
060: }
061:
062: }
063:
064: device.print("</span>");
065: }
066:
067: public void writeIcon(Device device, SIcon icon, boolean isMSIE)
068: throws IOException {
069: device.print("<img class=\"nopad\"");
070: if (isMSIE && icon.getURL().toString().endsWith(".png")
071: && icon.getIconWidth() > 0 && icon.getIconHeight() > 0) {
072: Utils.optAttribute(device, "style",
073: "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
074: + icon.getURL()
075: + "', sizingMethod='scale')");
076: Utils
077: .optAttribute(device, "src", TRANSPARENT_ICON
078: .getURL());
079: } else
080: Utils.optAttribute(device, "src", icon.getURL());
081:
082: Utils.optAttribute(device, "width", icon.getIconWidth());
083: Utils.optAttribute(device, "height", icon.getIconHeight());
084: Utils.attribute(device, "alt", icon.getIconTitle());
085: device.print("/>");
086: }
087:
088: protected class TextUpdate extends AbstractUpdate {
089:
090: private String text;
091:
092: public TextUpdate(SComponent component, String text) {
093: super (component);
094: this .text = text;
095: }
096:
097: public Handler getHandler() {
098: String textCode = "";
099: String exception = null;
100:
101: try {
102: StringBuilderDevice textDevice = new StringBuilderDevice(
103: 512);
104: boolean wordWrap = false;
105: if (component instanceof SLabel)
106: wordWrap = ((SLabel) component).isWordWrap();
107: writeText(textDevice, text, wordWrap);
108: textCode = textDevice.toString();
109: } catch (Throwable t) {
110: log.fatal("An error occured during rendering", t);
111: exception = t.getClass().getName();
112: }
113:
114: UpdateHandler handler = new UpdateHandler("text");
115: handler.addParameter(component.getName());
116: handler.addParameter(textCode);
117: if (exception != null) {
118: handler.addParameter(exception);
119: }
120: return handler;
121: }
122:
123: }
124:
125: protected class IconUpdate extends AbstractUpdate {
126:
127: private SIcon icon;
128:
129: public IconUpdate(SComponent component, SIcon icon) {
130: super (component);
131: this .icon = icon;
132: }
133:
134: public Handler getHandler() {
135: String iconCode = "";
136: String exception = null;
137:
138: try {
139: StringBuilderDevice iconDevice = new StringBuilderDevice(
140: 128);
141: writeIcon(iconDevice, icon, Utils.isMSIE(component));
142: iconCode = iconDevice.toString();
143: } catch (Throwable t) {
144: log.fatal("An error occured during rendering", t);
145: exception = t.getClass().getName();
146: }
147:
148: UpdateHandler handler = new UpdateHandler("icon");
149: handler.addParameter(component.getName());
150: handler.addParameter(iconCode);
151: if (exception != null) {
152: handler.addParameter(exception);
153: }
154: return handler;
155: }
156:
157: }
158:
159: }
|