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.css.PaddingVoodoo;
017: import org.wings.io.Device;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: import java.io.IOException;
022: import java.awt.*;
023:
024: /**
025: * @author hengels
026: */
027: public abstract class IconTextCompound {
028: protected static final Log log = LogFactory
029: .getLog(IconTextCompound.class);
030:
031: public void writeCompound(Device device, SComponent component,
032: int horizontalTextPosition, int verticalTextPosition,
033: boolean writeAllAttributes) throws IOException {
034: if (horizontalTextPosition == SConstants.NO_ALIGN)
035: horizontalTextPosition = SConstants.RIGHT;
036: if (verticalTextPosition == SConstants.NO_ALIGN)
037: verticalTextPosition = SConstants.CENTER;
038: if (verticalTextPosition == SConstants.CENTER
039: && horizontalTextPosition == SConstants.CENTER)
040: horizontalTextPosition = SConstants.RIGHT;
041: int iconTextGap = getIconTextGap(component);
042:
043: boolean renderTextFirst = verticalTextPosition == SConstants.TOP
044: || (verticalTextPosition == SConstants.CENTER && horizontalTextPosition == SConstants.LEFT);
045:
046: device.print("<table");
047: tableAttributes(device);
048: device.print(">");
049:
050: // []
051: // []
052: if (verticalTextPosition == SConstants.TOP
053: && horizontalTextPosition == SConstants.LEFT
054: || verticalTextPosition == SConstants.BOTTOM
055: && horizontalTextPosition == SConstants.RIGHT) {
056: final Insets insets = new Insets(0, 0, iconTextGap,
057: iconTextGap);
058: device.print("<tr><td align=\"left\" valign=\"top\"");
059: doBorderPaddingsWorkaround(component, insets, true, true,
060: false, false);
061: Utils.optAttribute(device, "style", Utils
062: .createInlineStylesForInsets(insets));
063: device.print(">");
064: first(device, renderTextFirst);
065: device.print("</td><td></td></tr>");
066: device
067: .print("<tr><td></td><td align=\"right\" valign=\"bottom\"");
068: insets.left = 0;
069: insets.top = 0;
070: insets.right = 0;
071: insets.bottom = 0;
072: doBorderPaddingsWorkaround(component, insets, false, false,
073: true, true);
074: Utils.optAttribute(device, "style", Utils
075: .createInlineStylesForInsets(insets));
076: device.print(">");
077: last(device, renderTextFirst);
078: device.print("</td></tr>");
079: }
080: // []
081: // []
082: else if (verticalTextPosition == SConstants.TOP
083: && horizontalTextPosition == SConstants.RIGHT
084: || verticalTextPosition == SConstants.BOTTOM
085: && horizontalTextPosition == SConstants.LEFT) {
086: final Insets insets = new Insets(0, iconTextGap,
087: iconTextGap, 0);
088: device
089: .print("<tr><td></td><td align=\"right\" valign=\"top\"");
090: doBorderPaddingsWorkaround(component, insets, true, false,
091: true, false);
092: Utils.optAttribute(device, "style", Utils
093: .createInlineStylesForInsets(insets));
094: device.print(">");
095: first(device, renderTextFirst);
096: device
097: .print("</td></tr><tr><td align=\"left\" valign=\"bottom\"");
098: insets.left = 0;
099: insets.top = 0;
100: insets.right = 0;
101: insets.bottom = 0;
102: doBorderPaddingsWorkaround(component, insets, false, true,
103: false, true);
104: Utils.optAttribute(device, "style", Utils
105: .createInlineStylesForInsets(insets));
106: device.print(">");
107: last(device, renderTextFirst);
108: device.print("</td><td></td></tr>");
109: }
110: // []
111: // []
112: else if (verticalTextPosition == SConstants.TOP
113: && horizontalTextPosition == SConstants.CENTER
114: || verticalTextPosition == SConstants.BOTTOM
115: && horizontalTextPosition == SConstants.CENTER) {
116: final Insets insets = new Insets(0, 0, iconTextGap, 0);
117: device.print("<tr><td align=\"center\" valign=\"top\"");
118: doBorderPaddingsWorkaround(component, insets, true, true,
119: true, false);
120: Utils.optAttribute(device, "style", Utils
121: .createInlineStylesForInsets(insets));
122: device.print(">");
123: first(device, renderTextFirst);
124: device
125: .print("</td></tr><tr><td align=\"center\" valign=\"bottom\"");
126: insets.left = 0;
127: insets.top = 0;
128: insets.right = 0;
129: insets.bottom = 0;
130: doBorderPaddingsWorkaround(component, insets, false, true,
131: true, true);
132: Utils.optAttribute(device, "style", Utils
133: .createInlineStylesForInsets(insets));
134: device.print(">");
135: last(device, renderTextFirst);
136: device.print("</td></tr>");
137: }
138: // [][]
139: else if (verticalTextPosition == SConstants.CENTER
140: && horizontalTextPosition == SConstants.LEFT
141: || verticalTextPosition == SConstants.CENTER
142: && horizontalTextPosition == SConstants.RIGHT) {
143: final Insets insets = new Insets(0, 0, 0, iconTextGap);
144: device.print("<tr><td align=\"left\"");
145: doBorderPaddingsWorkaround(component, insets, true, true,
146: false, true);
147: Utils.optAttribute(device, "style", Utils
148: .createInlineStylesForInsets(insets));
149: device.print(">");
150: first(device, renderTextFirst);
151: device.print("</td><td align=\"right\"");
152: insets.left = 0;
153: insets.top = 0;
154: insets.right = 0;
155: insets.bottom = 0;
156: doBorderPaddingsWorkaround(component, insets, true, false,
157: true, true);
158: Utils.optAttribute(device, "style", Utils
159: .createInlineStylesForInsets(insets));
160: device.print(">");
161: last(device, renderTextFirst);
162: device.print("</td></tr>");
163: } else {
164: log.warn("horizontal = " + horizontalTextPosition);
165: log.warn("vertical = " + verticalTextPosition);
166: }
167: device.print("</table>");
168: }
169:
170: private void doBorderPaddingsWorkaround(SComponent component,
171: final Insets targetInsets, boolean firstRow,
172: boolean firstCol, boolean lastCol, boolean lastRow) {
173: if (component.getBorder() != null)
174: PaddingVoodoo.doBorderPaddingsWorkaround(component
175: .getBorder(), targetInsets, firstRow, firstCol,
176: lastCol, lastRow);
177: }
178:
179: protected int getIconTextGap(SComponent component) {
180: if (component instanceof SLabel)
181: return ((SLabel) component).getIconTextGap();
182: if (component instanceof SAbstractButton)
183: return ((SAbstractButton) component).getIconTextGap();
184: return 4;
185: }
186:
187: protected void first(Device device, boolean textFirst)
188: throws IOException {
189: if (textFirst) {
190: // avoid that in case that horizontalAlignment of text is right, that the text itself
191: // becomes right-aligned
192: device.print("<div style=\"text-align:left\">");
193: text(device);
194: device.print("</div>");
195: } else
196: icon(device);
197: }
198:
199: protected void last(Device device, boolean textFirst)
200: throws IOException {
201: first(device, !textFirst);
202: }
203:
204: protected abstract void text(Device d) throws IOException;
205:
206: protected abstract void icon(Device d) throws IOException;
207:
208: protected abstract void tableAttributes(Device d)
209: throws IOException;
210: }
|