KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > AbstractLayoutCG


1 /*
2  * $Id: AbstractLayoutCG.java,v 1.15 2005/06/03 13:39:26 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16 import org.wings.SComponent;
17 import org.wings.SLayoutManager;
18 import org.wings.io.Device;
19 import org.wings.plaf.LayoutCG;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Abstract super class for layout CGs using invisible tables to arrange their contained components.
27  *
28  * @author bschmid
29  */

30 public abstract class AbstractLayoutCG implements LayoutCG {
31
32     /**
33      * Print HTML table element declaration of a typical invisible layouter table.
34      */

35     protected void printLayouterTableHeader(Device d, String JavaDoc styleClass, int hgap, int vgap,
36                                             int border, SLayoutManager layout)
37             throws IOException JavaDoc {
38         Utils.printDebugNewline(d, layout.getContainer());
39         Utils.printDebug(d, "<!-- START LAYOUT: ").print(styleClass).print(" -->");
40
41         // Generate CSS Inline Style
42
// we don't need to do that here once we have set all layoutmanagers
43
// to 100% width/height
44
StringBuffer JavaDoc styleString = Utils.generateCSSInlinePreferredSize(layout.getContainer().getPreferredSize());
45         styleString.append(Utils.generateCSSInlineBorder(border));
46         styleString.append(createInlineStylesForGaps(hgap, vgap));
47
48         Utils.printNewline(d, layout.getContainer());
49         d.print("<table ");
50         /* This won't work any longer as we override padding/spacing with default SLayout styles class
51         d.print(" cellspacing=\"").print(cellSpacing < 0 ? 0 : cellSpacing).print("\"");
52         d.print(" cellpadding=\"").print(cellPadding < 0 ? 0 : cellPadding).print("\""); */

53         Utils.optAttribute(d, "class", styleClass != null ? styleClass + " SLayout" : "SLayout");
54         Utils.optAttribute(d, "style", styleString.toString());
55         d.print("><tbody>");
56         Utils.printNewline(d, layout.getContainer());
57     }
58
59     /**
60      * Counterpart to {@link #printLayouterTableHeader}
61      */

62     protected void printLayouterTableFooter(Device d, String JavaDoc styleClass, SLayoutManager layout) throws IOException JavaDoc {
63         Utils.printNewline(d, layout.getContainer());
64         d.print("</tbody></table>");
65
66         Utils.printDebugNewline(d, layout.getContainer());
67         Utils.printDebug(d, "<!-- END LAYOUT: ").print(styleClass).print(" -->");
68     }
69
70     /**
71      * Render passed list of components to a table body.
72      * Use {@link #printLayouterTableHeader(org.wings.io.Device, String, int, int, int, org.wings.SLayoutManager)} in front
73      * and {@link #printLayouterTableFooter(org.wings.io.Device, String, org.wings.SLayoutManager)} afterwards!
74      *
75      * @param d The device to write to
76      * @param cols Wrap after this amount of columns
77      * @param renderFirstLineAsHeader Render cells in first line as TH-Element or regular TD.
78      * @param components The components to layout
79      * @param hgap Horizontal gap between components in px
80      * @param vgap Vertical gap between components in px
81      * @param border Border width to draw.
82      */

83     protected void printLayouterTableBody(Device d, int cols, final boolean renderFirstLineAsHeader,
84                                           int hgap, int vgap, int border, final List JavaDoc components)
85             throws IOException JavaDoc {
86         boolean firstRow = true;
87         int col = 0;
88         for (Iterator JavaDoc iter = components.iterator(); iter.hasNext();) {
89             final SComponent c = (SComponent) iter.next();
90
91             if (col == 0) {
92                 d.print("<tr>");
93             } else if (col % cols == 0) {
94                 d.print("</tr>");
95                 Utils.printNewline(d, c.getParent());
96                 d.print("<tr>");
97                 firstRow = false;
98             }
99
100             openLayouterCell(d, firstRow && renderFirstLineAsHeader, hgap, vgap, border, c);
101             d.print(">");
102
103             Utils.printNewline(d, c);
104             c.write(d); // Render component
105

106             closeLayouterCell(d, firstRow && renderFirstLineAsHeader);
107
108             col++;
109
110             if (!iter.hasNext()) {
111                 d.print("</tr>");
112                 Utils.printNewline(d, c.getParent());
113             }
114         }
115     }
116
117     /**
118      * Converts a hgap/vgap in according inline css padding style.
119      *
120      * @param hgap Horizontal gap between components in px
121      * @param vgap Vertical gap between components in px
122      */

123     protected static StringBuffer JavaDoc createInlineStylesForGaps(int hgap, int vgap) {
124         StringBuffer JavaDoc inlineStyle = new StringBuffer JavaDoc();
125         if (hgap > 0 || vgap > 0) {
126             int hPaddingTop = (int) Math.round((vgap < 0 ? 0 : vgap) / 2.0);
127             int hPaddingBottom = (int) Math.round((vgap < 0 ? 0 : vgap) / 2.0 + 0.1); // round up
128
int vPaddingLeft = (int) Math.round((hgap < 0 ? 0 : hgap) / 2.0);
129             int vPaddingRight = (int) Math.round((hgap < 0 ? 0 : hgap) / 2.0 + 0.1); // round up
130
if (hPaddingBottom == hPaddingTop && hPaddingTop == vPaddingRight && vPaddingRight == vPaddingLeft) {
131                 inlineStyle.append("padding:").append(hPaddingTop).append("px;");
132             } else {
133                 inlineStyle.append("padding:").append(hPaddingTop).append("px ").append(vPaddingRight).append("px ")
134                         .append(hPaddingBottom).append("px ").append(vPaddingLeft).append("px;");
135             }
136         }
137         return inlineStyle;
138     }
139
140     /**
141      * Opens a TD or TH cell of an invisible layouter table. This method also does component alignment.
142      * <b>Attention:</b> As you want to attach more attributes you need to close the tag with &gt; on your own!
143      *
144      * @param renderAsHeader Print TH instead of TD
145      */

146     public static void openLayouterCell(Device d, boolean renderAsHeader, int hgap, int vgap, int border,
147                                         SComponent containedComponent) throws IOException JavaDoc {
148         if (renderAsHeader) {
149             d.print("<th");
150         } else {
151             d.print("<td");
152         }
153
154         d.print(" class=\"SLayout\"");
155         Utils.printTableCellAlignment(d, containedComponent);
156
157         // CSS inline attributes
158
StringBuffer JavaDoc inlineAttributes = Utils.generateCSSInlineBorder(border);
159         inlineAttributes.append(createInlineStylesForGaps(hgap, vgap));
160         Utils.optAttribute(d, "style", inlineAttributes.toString());
161     }
162
163     /**
164      * Closes a TD or TH cell of an invisible layouter table.
165      *
166      * @param renderAsHeader Print TH instead of TD
167      */

168     public static void closeLayouterCell(Device d, boolean renderAsHeader) throws IOException JavaDoc {
169         d.print(renderAsHeader ? "</th>" : "</td>");
170     }
171 }
172
Popular Tags