CreatePageDivLayout.java
package org.flasby.util;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
public class CreatePageDivLayout {
static abstract class Component {
public abstract void print( PrintStream stream );
}
static class HTML {
private static final String TOP = "<!DOCTYPE html>\n<html>\n<head>\n";
private static final String BOTTOM = "</html>";
private final List<Component> components = new ArrayList<>();
@SuppressFBWarnings("VA_FORMAT_STRING_USES_NEWLINE")
public void print( PrintStream stream ) {
stream.printf(TOP);
for (Component component : components) {
component.print(stream);
}
stream.print(BOTTOM);
}
public Head add(Head head) {
components.add(head);
return head;
}
public Body add(Body body) {
components.add(body);
return body;
}
}
static class Head extends Component {
private final List<Component> components = new ArrayList<>();
@Override
public void print(PrintStream stream) {
stream.println("<head>");
for (Component component : components) {
component.print(stream);
}
stream.println("</head>");
}
public StyleBlock add(StyleBlock block) {
components.add(block);
return block;
}
}
static class Body extends Component {
private final List<String> components = new ArrayList<>();
@Override
public void print(PrintStream stream) {
stream.println("<body>");
for (String component : components) {
stream.print(component);
}
stream.println("</body>");
}
public Body add(String bodyContent) {
components.add(bodyContent);
return this;
}
}
static class StyleBlock extends Component {
private static final String OPEN = "<style type=\"text/css\">\n";
private static final String CLOSE = "</style>\n";
private final List<Style> styles = new ArrayList<>();
@SuppressFBWarnings("VA_FORMAT_STRING_USES_NEWLINE")
public void print( PrintStream stream ) {
stream.printf(OPEN);
for (Style style : styles) {
style.print(stream);
}
stream.printf(CLOSE);
}
public void add( Style style ) {
styles.add(style);
}
}
static abstract class Style {
private final String format;
private final Object[] args;
protected Style( String format, Object... args ) {
this.format = format;
this.args = args;
}
public void print( PrintStream stream ) {
stream.printf(format, args);
}
}
static class Header extends Style {
public Header(int padding) {
super( "#header {\n padding: %dpx;\n background-color: orange;\n}\n",
padding );
}
}
static class BodyStyle extends Style {
public BodyStyle(int padding, int rightColumnFullWidth, int leftColumnFullWidth) {
super( "#body {\n min-width: %d;\n}\n",
2 * (leftColumnFullWidth + padding ) + rightColumnFullWidth);
}
}
static class ContainerStyle extends Style {
public ContainerStyle(int padding, int rightColumnFullWidth, int leftColumnFullWidth) {
super( "#container {\n padding-left: %dpx;\n padding-right: %dpx;\n}\n"
+ "#container .column {\n position: relative;\n float: left;\n}\n",
leftColumnFullWidth+2*padding, rightColumnFullWidth+4*padding);
}
}
static class CentreStyle extends Style {
public CentreStyle(int padding) {
super( "#center {\n padding: %dpx;\n background-color: fuchsia;\n width: 100%%;\n}\n",
padding);
}
}
static class LeftStyle extends Style {
public LeftStyle(int padding, int lcfullwidth) {
super( "#left {\n background-color: teal;\n padding: %dpx;\n width: %dpx;\n"
+ " right: %dpx;\n margin-left: -100%%;\n}\n",
padding, lcfullwidth, lcfullwidth+4*padding);
}
}
static class RightStyle extends Style {
public RightStyle(int padding, int rcfullwidth) {
super( "#right {\n background-color: green;\n padding: %dpx;\n width: %dpx;\n margin-right: -%dpx;\n}\n",
padding, rcfullwidth, rcfullwidth+4*padding);
}
}
static class FooterStyle extends Style {
public FooterStyle( int padding ) {
super( "#footer {\n padding: %dpx;\n background-color: red;\n clear: both;\n}\n",
padding);
}
}
public static void main(String[] args) {
int padding = 5;
int LCfullwidth = 200;
int RCfullwidth = 150;
// int LCwidth = LCfullwidth - padding;
// int RCwidth = RCfullwidth - padding;
HTML html = new HTML();
Head head = html.add( new Head() );
StyleBlock styles = head.add( new StyleBlock() );
styles.add( new BodyStyle(padding, LCfullwidth, RCfullwidth) );
styles.add( new Header(padding) );
styles.add( new ContainerStyle(padding, RCfullwidth, LCfullwidth));
styles.add( new CentreStyle(padding));
styles.add( new LeftStyle(padding, LCfullwidth));
styles.add( new RightStyle(padding, RCfullwidth));
styles.add( new FooterStyle(padding) );
Body body = html.add( new Body() );
body
.add(" <div id=\"header\">Header</div>\n")
.add(" <div id=\"container\">\n")
.add(" <div id=\"center\" class=\"column\">center</div>\n")
.add(" <div id=\"left\" class=\"column\">left</div>\n")
.add(" <div id=\"right\" class=\"column\">right</div>\n")
.add(" </div>\n")
.add(" <div id=\"footer\">footer</div>\n");
html.print(System.out);
//new BodyStyle(padding, LCfullwidth, RCfullwidth).print(System.out);
}
}