1 /*
2 * Metis: Java Data Framework
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.metis.help;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.resource.OceanusResourceId;
21 import io.github.tonywasher.joceanus.oceanus.resource.OceanusResourceLoader;
22 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIHTMLManager.TethysUIStyleSheetId;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28 * The help module that is implemented by each Help System.
29 */
30 public abstract class MetisHelpModule {
31 /**
32 * HelpId.
33 */
34 public interface MetisHelpId extends OceanusResourceId {
35 }
36
37 /**
38 * The Hash prime.
39 */
40 protected static final int HASH_PRIME = 19;
41
42 /**
43 * Document name for Help Contents.
44 */
45 protected static final String DOC_NAME = "HelpContents";
46
47 /**
48 * Attribute name for Initial page.
49 */
50 protected static final String ATTR_INITIAL = "initial";
51
52 /**
53 * The Help Entries.
54 */
55 private final List<MetisHelpEntry> theEntries;
56
57 /**
58 * The title of the Help System.
59 */
60 private final String theTitle;
61
62 /**
63 * The CSS of the help system.
64 */
65 private TethysUIStyleSheetId theCSS;
66
67 /**
68 * The initial entry of the help system.
69 */
70 private String theInitial;
71
72 /**
73 * Constructor.
74 *
75 * @param pTitle the title
76 */
77 protected MetisHelpModule(final String pTitle) {
78 /* Store parameters */
79 theTitle = pTitle;
80
81 /* Create entry list */
82 theEntries = new ArrayList<>();
83 }
84
85 /**
86 * Set the initial name.
87 *
88 * @param pInitial the initial name
89 */
90 public void setInitialName(final String pInitial) {
91 theInitial = pInitial;
92 }
93
94 /**
95 * Obtain the initial name.
96 *
97 * @return the initial name
98 */
99 protected String getInitialName() {
100 return theInitial;
101 }
102
103 /**
104 * Obtain the title.
105 *
106 * @return the title
107 */
108 protected String getTitle() {
109 return theTitle;
110 }
111
112 /**
113 * Obtain the CSS.
114 *
115 * @return the CSS
116 */
117 protected TethysUIStyleSheetId getCSS() {
118 return theCSS;
119 }
120
121 /**
122 * Obtain the help entries.
123 *
124 * @return the help entries
125 */
126 public List<MetisHelpEntry> getHelpEntries() {
127 return theEntries;
128 }
129
130 /**
131 * Add root entry.
132 *
133 * @param pEntry the entry
134 * @return the HelpEntry
135 */
136 public MetisHelpEntry addRootEntry(final MetisHelpEntry pEntry) {
137 theEntries.add(pEntry);
138 return pEntry;
139 }
140
141 /**
142 * Define Standard Help entry.
143 *
144 * @param <K> the type of the key
145 * @param pName the name
146 * @param pHelpId the helpId
147 * @return the HelpEntry
148 */
149 public static <K extends Enum<K> & MetisHelpId> MetisHelpEntry defineHelpEntry(final String pName,
150 final K pHelpId) {
151 return defineTitledHelpEntry(pName, pName, pHelpId);
152 }
153
154 /**
155 * Define Titled Help entry.
156 *
157 * @param <K> the type of the key
158 * @param pName the name
159 * @param pTitle the title
160 * @param pHelpId the helpId
161 * @return the HelpEntry
162 */
163 public static <K extends Enum<K> & MetisHelpId> MetisHelpEntry defineTitledHelpEntry(final String pName,
164 final String pTitle,
165 final K pHelpId) {
166 return new MetisHelpEntry(pName, pTitle, pHelpId);
167 }
168
169 /**
170 * Define Contents Help entry.
171 *
172 * @param pName the name
173 * @return the HelpEntry
174 */
175 public static MetisHelpEntry defineContentsEntry(final String pName) {
176 return defineTitledContentsEntry(pName, pName);
177 }
178
179 /**
180 * Define Contents Help entry.
181 *
182 * @param pName the name
183 * @param pTitle the title
184 * @return the HelpEntry
185 */
186 public static MetisHelpEntry defineTitledContentsEntry(final String pName,
187 final String pTitle) {
188 return new MetisHelpEntry(pName, pTitle);
189 }
190
191 /**
192 * Load Help entries from the file system.
193 *
194 * @throws OceanusException on error
195 */
196 protected void loadHelpPages() throws OceanusException {
197 loadHelpPages(theEntries);
198 }
199
200 /**
201 * Load CSS.
202 *
203 * @param <K> the keyType
204 * @param pKey the styleSheetKey
205 * @throws OceanusException on error
206 */
207 protected <K extends Enum<K> & TethysUIStyleSheetId> void loadCSS(final K pKey) throws OceanusException {
208 theCSS = pKey;
209 }
210
211 /**
212 * Load Help entries from the file system.
213 *
214 * @param pEntries the Help Entries
215 * @throws OceanusException on error
216 */
217 private static void loadHelpPages(final List<MetisHelpEntry> pEntries) throws OceanusException {
218 /* Loop through the entities */
219 for (MetisHelpEntry myEntry : pEntries) {
220 /* If we have a helpId */
221 if (myEntry.getHelpId() != null) {
222 /* Reset the builder */
223 final String myPage = OceanusResourceLoader.loadResourceToString(myEntry.getHelpId());
224
225 /* Set the HTML for the entry */
226 myEntry.setHtml(myPage);
227 }
228
229 /* If we have children */
230 if (myEntry.getChildren() != null) {
231 /* Load the entries */
232 loadHelpPages(myEntry.getChildren());
233 }
234 }
235 }
236 }