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.SComponent;
016: import org.wings.SConstants;
017: import org.wings.SFlowLayout;
018: import org.wings.SLayoutManager;
019: import org.wings.io.Device;
020: import org.wings.util.SStringBuilder;
021:
022: import java.awt.*;
023: import java.io.IOException;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: public class FlowLayoutCG extends AbstractLayoutCG {
028: private static final long serialVersionUID = 1L;
029:
030: /**
031: * @param d the device to write the code to
032: * @param l the layout manager
033: * @throws IOException
034: */
035: public void write(Device d, SLayoutManager l) throws IOException {
036: final SFlowLayout layout = (SFlowLayout) l;
037: final Insets insets = convertGapsToInset(layout.getHgap(),
038: layout.getVgap());
039: final List components = layout.getComponents();
040: final int alignment = layout.getAlignment();
041:
042: openLayouterBody(d, layout);
043: d.print("<tr><td");
044: Utils.printDivHorizontalAlignment(d,
045: alignment != SConstants.NO_ALIGN ? alignment
046: : SConstants.LEFT_ALIGN);
047: Utils.printDivVerticalAlignment(d, SConstants.TOP_ALIGN);
048: if (alignment == SConstants.CENTER) {
049: // Cheat -- margin left/right to simulate center float. Will not wrap
050: d.print(" style=\"margin-left:auto; margin-right:auto\"");
051: } else {
052: d.print(" style=\"width:100%\""); // gecko bug workaround: inherit surrounding panel bg color.
053: }
054: d.print(" class=\"SFlowLayout\">");
055:
056: final String alignmentStyle;
057: if (alignment == SConstants.LEFT) {
058: alignmentStyle = "float:left;";
059: } else if (alignment == SConstants.RIGHT) {
060: alignmentStyle = "float:right;";
061: } else if (alignment == SConstants.CENTER) {
062: alignmentStyle = "float:left; "; // Floating does not work with center :-(
063: } else {
064: alignmentStyle = "";
065: }
066:
067: if (alignment == SConstants.CENTER) {
068: d.print("<table><tr><td>");
069: }
070:
071: if (components.size() > 1) {
072: /* We need two spacer divs (end/beginning) so that the sourrounding flow layout takes up
073: the whole space instead of 0px heigth. See http://www.alistapart.com/articles/practicalcss/.
074: The nbsp's are only needed for firefox... */
075: d.print("<div class=\"spacer\"></div>");
076:
077: for (int x = 0, y = components.size(); x < y; x++) {
078: printComponent(d, (SComponent) components.get(x),
079: alignmentStyle, insets);
080: }
081:
082: /* Second spacer. See upper. */
083: d.print("<div class=\"spacer\"></div>");
084: } else if (components.size() == 1) {
085: // Special handling of trivial case
086: //
087: // Why: FlowLayout may be default for many containers.
088: // BUT: The effect is that in MSIE oversized, floating componentns DO NOT lead to a scrollbar
089: //
090: // Try this:
091: // <TABLE><TR style="background-color:yellow; border:5px solid red" ><TD>
092: // <DIV style="WIDTH: 100%" align=left >
093: // <DIV class=spacer></DIV>
094: // <DIV style="FLOAT: left; border:2px solid blue;">
095: // <table width="1000" heigth=10 bgcolor="#ff000"><tr>
096: // <td align=center>1000px widht</td></tr></table>
097: // </div>
098: // <DIV class=spacer></DIV>
099: // </div>
100: // </td></tr></table>
101: printComponent(d, (SComponent) components.get(0), "",
102: insets);
103: }
104:
105: if (alignment == SConstants.CENTER) {
106: d.print("</td></tr></table>");
107: }
108:
109: d.print("</td></tr>");
110: closeLayouterBody(d, layout);
111: }
112:
113: private void printComponent(Device d, SComponent component,
114: String alignmentStlye, Insets insets) throws IOException {
115: if (component.isVisible()) {
116: Utils.printNewline(d, component);
117: d.print("<div");
118: Utils.optAttribute(d, "style", Utils
119: .createInlineStylesForInsets(new SStringBuilder(
120: alignmentStlye), insets));
121: d.print(">");
122: component.write(d); // Render contained component
123: Utils.printNewline(d, component);
124: d.print("</div>");
125: }
126: }
127:
128: protected int getLayoutHGap(SLayoutManager layout) {
129: SFlowLayout flowLayout = (SFlowLayout) layout;
130: return flowLayout.getHgap();
131: }
132:
133: protected int getLayoutVGap(SLayoutManager layout) {
134: SFlowLayout flowLayout = (SFlowLayout) layout;
135: return flowLayout.getVgap();
136: }
137:
138: protected int getLayoutBorder(SLayoutManager layout) {
139: SFlowLayout flowLayout = (SFlowLayout) layout;
140: return flowLayout.getBorder();
141: }
142:
143: protected int layoutOversize(SLayoutManager layout) {
144: return 0;
145: }
146:
147: public int getDefaultLayoutCellHAlignment() {
148: return SConstants.NO_ALIGN; // Don't knoff.
149: }
150:
151: public int getDefaultLayoutCellVAlignment() {
152: return SConstants.NO_ALIGN; // Don't knoff.
153: }
154:
155: }
|