1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.tethys.core.control;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
22 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogManager;
23 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogger;
24 import io.github.tonywasher.joceanus.oceanus.resource.OceanusResourceLoader;
25 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
26 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIValueSet;
27 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIHTMLManager;
28 import io.github.tonywasher.joceanus.tethys.core.base.TethysUICoreComponent;
29 import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
30
31
32
33
34 public abstract class TethysUICoreHTMLManager
35 extends TethysUICoreComponent
36 implements TethysUIHTMLManager {
37
38
39
40 private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(TethysUICoreHTMLManager.class);
41
42
43
44
45 private static final String REF_SEPARATOR = "#";
46
47
48
49
50 private final Integer theId;
51
52
53
54
55 private final OceanusEventManager<TethysUIEvent> theEventManager;
56
57
58
59
60 private final TethysUICoreHTMLToFile theHTMLToFile;
61
62
63
64
65 private final TethysUIValueSet theValueSet;
66
67
68
69
70 private String theCurrentRef;
71
72
73
74
75 private String theCSSBase;
76
77
78
79
80 private String theCSSProcessed;
81
82
83
84
85 private String theHTMLString;
86
87
88
89
90
91
92 protected TethysUICoreHTMLManager(final TethysUICoreFactory<?> pFactory) {
93
94 theId = pFactory.getNextId();
95 theEventManager = new OceanusEventManager<>();
96
97
98 theHTMLToFile = new TethysUICoreHTMLToFile(pFactory, this);
99
100
101 theValueSet = pFactory.getValueSet();
102
103
104 theValueSet.getEventRegistrar().addEventListener(e -> processCSS());
105 }
106
107 @Override
108 public Integer getId() {
109 return theId;
110 }
111
112 @Override
113 public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
114 return theEventManager.getEventRegistrar();
115 }
116
117
118
119
120
121
122 protected String getHTMLString() {
123 return theHTMLString;
124 }
125
126
127
128
129
130
131 protected String getProcessedCSS() {
132 return theCSSProcessed;
133 }
134
135 @Override
136 public void processReference(final String pReference) {
137
138 String myRef = pReference;
139 String myInternal = null;
140
141
142 if (myRef.contains(REF_SEPARATOR)) {
143
144 final String[] myTokens = myRef.split(REF_SEPARATOR);
145
146
147 myRef = myTokens[0];
148 myInternal = myTokens[1];
149
150
151 if (myRef.isEmpty()) {
152 myRef = null;
153 }
154 }
155
156
157 final boolean needSwitch = (myRef != null)
158 && !myRef.equals(theCurrentRef);
159
160
161 if (needSwitch
162 && !loadNewPage(myRef)) {
163
164 myInternal = null;
165 LOGGER.error("Failed to load page <%s>", myRef);
166 }
167
168
169 if (myInternal != null) {
170
171 scrollToReference(myInternal);
172 }
173 }
174
175 @Override
176 public void setHTMLContent(final String pHTMLString,
177 final String pReference) {
178
179 theCurrentRef = pReference;
180 theHTMLString = pHTMLString;
181
182
183 loadHTMLContent(theHTMLString);
184 }
185
186
187
188
189
190
191 protected abstract void loadHTMLContent(String pHTMLString);
192
193
194
195
196 protected abstract void loadCSSContents();
197
198 @Override
199 public void setCSSContent(final TethysUIStyleSheetId pStyleSheet) throws OceanusException {
200
201 theCSSBase = OceanusResourceLoader.loadResourceToString(pStyleSheet);
202 theCSSProcessed = null;
203
204
205 processCSS();
206 }
207
208
209
210
211 protected void processCSS() {
212
213 if (theCSSBase != null) {
214
215 theCSSProcessed = theValueSet.resolveValues(theCSSBase);
216 }
217
218
219 loadCSSContents();
220 }
221
222 @Override
223 public void saveToFile() {
224 theHTMLToFile.writeToFile();
225 }
226
227
228
229
230
231
232
233 private boolean loadNewPage(final String pPageRef) {
234 return !theEventManager.fireEvent(TethysUIEvent.BUILDPAGE, pPageRef);
235 }
236 }