1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.devacfr.maven.skins.reflow.model;
20
21 import javax.annotation.Nonnull;
22
23 import java.util.List;
24
25 import com.google.common.base.Joiner;
26 import com.google.common.base.Strings;
27 import com.google.common.collect.FluentIterable;
28 import com.google.common.collect.Lists;
29 import org.devacfr.maven.skins.reflow.HtmlTool;
30 import org.devacfr.maven.skins.reflow.ISkinConfig;
31
32 import static com.google.common.collect.FluentIterable.concat;
33 import static java.util.Objects.requireNonNull;
34
35
36
37
38
39
40
41 public abstract class Component {
42
43
44 private String cssClass;
45
46
47 private final List<String> cssOptions = Lists.newArrayList();
48
49
50 private final List<Component> children = Lists.newArrayList();
51
52
53
54
55 @Nonnull
56 public String getCssClass() {
57 if (Strings.isNullOrEmpty(cssClass)) {
58 return "";
59 }
60 return cssClass;
61 }
62
63
64
65
66
67 protected void setCssClass(final String cssClass) {
68 this.cssClass = cssClass;
69 }
70
71
72
73
74
75
76
77
78
79
80 @Nonnull
81 public final String getCssOptions() {
82 return concat(this.cssOptions,
83 concat(FluentIterable.from(children).transform(component -> component.cssOptions))).join(Joiner.on(' '));
84 }
85
86
87
88
89
90
91
92 protected void addChildren(final Component... components) {
93 this.children.addAll(Lists.newArrayList(components));
94 }
95
96
97
98
99
100
101
102 protected final void addCssOptions(@Nonnull final String... cssOptions) {
103 this.cssOptions.addAll(Lists.newArrayList(cssOptions));
104 }
105
106
107
108
109
110
111
112
113
114
115 protected String onPreRender(@Nonnull final ISkinConfig skinConfig, @Nonnull final String bodyContent) {
116 final StringBuilder str = new StringBuilder(bodyContent);
117 this.children.forEach(component -> {
118 final String content = str.toString();
119 str.setLength(0);
120 str.append(component.onPreRender(skinConfig, content));
121 });
122 return str.toString();
123 }
124
125
126
127
128
129
130
131
132
133 protected HtmlTool getHtmlTool(@Nonnull final ISkinConfig skinConfig) {
134 return requireNonNull(skinConfig).getToolbox("htmlTool", HtmlTool.class);
135 }
136
137
138
139
140
141
142
143 @Nonnull
144 protected String getBodyContent(@Nonnull final ISkinConfig skinConfig) {
145 return requireNonNull(skinConfig).getContextValue("bodyContent", String.class);
146 }
147 }