View Javadoc
1   /*
2    * Tethys: GUI Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * HTML Manager.
33   */
34  public abstract class TethysUICoreHTMLManager
35          extends TethysUICoreComponent
36          implements TethysUIHTMLManager {
37      /**
38       * The logger.
39       */
40      private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(TethysUICoreHTMLManager.class);
41  
42      /**
43       * Reference Separator.
44       */
45      private static final String REF_SEPARATOR = "#";
46  
47      /**
48       * The id.
49       */
50      private final Integer theId;
51  
52      /**
53       * The Event Manager.
54       */
55      private final OceanusEventManager<TethysUIEvent> theEventManager;
56  
57      /**
58       * The HTMLToFile Manager.
59       */
60      private final TethysUICoreHTMLToFile theHTMLToFile;
61  
62      /**
63       * The ValueSet.
64       */
65      private final TethysUIValueSet theValueSet;
66  
67      /**
68       * The Current reference.
69       */
70      private String theCurrentRef;
71  
72      /**
73       * CSS Base.
74       */
75      private String theCSSBase;
76  
77      /**
78       * CSS Processed.
79       */
80      private String theCSSProcessed;
81  
82      /**
83       * HTML String.
84       */
85      private String theHTMLString;
86  
87      /**
88       * Constructor.
89       *
90       * @param pFactory the GUI Factory
91       */
92      protected TethysUICoreHTMLManager(final TethysUICoreFactory<?> pFactory) {
93          /* Build standard fields */
94          theId = pFactory.getNextId();
95          theEventManager = new OceanusEventManager<>();
96  
97          /* Create the HTMLtoFile Manager */
98          theHTMLToFile = new TethysUICoreHTMLToFile(pFactory, this);
99  
100         /* Obtain the valueSet */
101         theValueSet = pFactory.getValueSet();
102 
103         /* Listen to valueSet changes */
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      * Obtain the HTML String.
119      *
120      * @return the string
121      */
122     protected String getHTMLString() {
123         return theHTMLString;
124     }
125 
126     /**
127      * Obtain the processed CSS.
128      *
129      * @return the CSS
130      */
131     protected String getProcessedCSS() {
132         return theCSSProcessed;
133     }
134 
135     @Override
136     public void processReference(final String pReference) {
137         /* Access local copy of reference */
138         String myRef = pReference;
139         String myInternal = null;
140 
141         /* If the name has a # in it */
142         if (myRef.contains(REF_SEPARATOR)) {
143             /* Split on the # */
144             final String[] myTokens = myRef.split(REF_SEPARATOR);
145 
146             /* Allocate the values */
147             myRef = myTokens[0];
148             myInternal = myTokens[1];
149 
150             /* Handle an internal reference */
151             if (myRef.isEmpty()) {
152                 myRef = null;
153             }
154         }
155 
156         /* Check whether we need to switch pages */
157         final boolean needSwitch = (myRef != null)
158                 && !myRef.equals(theCurrentRef);
159 
160         /* If we failed to switch pages */
161         if (needSwitch
162                 && !loadNewPage(myRef)) {
163             /* Don't attempt to scroll to internal reference */
164             myInternal = null;
165             LOGGER.error("Failed to load page <%s>", myRef);
166         }
167 
168         /* If we have an internal reference */
169         if (myInternal != null) {
170             /* Scroll to the reference */
171             scrollToReference(myInternal);
172         }
173     }
174 
175     @Override
176     public void setHTMLContent(final String pHTMLString,
177                                final String pReference) {
178         /* Store the reference and string */
179         theCurrentRef = pReference;
180         theHTMLString = pHTMLString;
181 
182         /* load the content */
183         loadHTMLContent(theHTMLString);
184     }
185 
186     /**
187      * Load HTML Contents.
188      *
189      * @param pHTMLString the HTML content.
190      */
191     protected abstract void loadHTMLContent(String pHTMLString);
192 
193     /**
194      * Load CSS Contents.
195      */
196     protected abstract void loadCSSContents();
197 
198     @Override
199     public void setCSSContent(final TethysUIStyleSheetId pStyleSheet) throws OceanusException {
200         /* Store the base sheet */
201         theCSSBase = OceanusResourceLoader.loadResourceToString(pStyleSheet);
202         theCSSProcessed = null;
203 
204         /* Process the CSS */
205         processCSS();
206     }
207 
208     /**
209      * Process the CSS.
210      */
211     protected void processCSS() {
212         /* If we have a styleSheet */
213         if (theCSSBase != null) {
214             /* Process the CSS */
215             theCSSProcessed = theValueSet.resolveValues(theCSSBase);
216         }
217 
218         /* reLoad the CSS */
219         loadCSSContents();
220     }
221 
222     @Override
223     public void saveToFile() {
224         theHTMLToFile.writeToFile();
225     }
226 
227     /**
228      * Load new page.
229      *
230      * @param pPageRef the page reference
231      * @return was new page loaded? true/false
232      */
233     private boolean loadNewPage(final String pPageRef) {
234         return !theEventManager.fireEvent(TethysUIEvent.BUILDPAGE, pPageRef);
235     }
236 }