View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy 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,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.gui;
18  
19  import net.sourceforge.jarduino.ArduinoException;
20  import net.sourceforge.jarduino.message.ArduinoChar;
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  
27  /**
28   * Arduino HelpPage Ids.
29   */
30  public enum ArduinoHelp {
31      /**
32       * Overview.
33       */
34      OVERVIEW("Overview", "Overview.html"),
35  
36      /**
37       * CodeStructure.
38       */
39      CODESTRUCT("Code Structure", "CodeStructure.html"),
40  
41      /**
42       * SourcesTab.
43       */
44      SRCSTAB("Source Tab", "SourcesTab.html"),
45  
46      /**
47       * MessageTab.
48       */
49      MSGTAB("Message Tab", "MessageTab.html"),
50  
51      /**
52       * MetaDataTab.
53       */
54      METATAB("MetaData Tab", "MetaDataTab.html");
55  
56      /**
57       * The Title.
58       */
59      private final String theTitle;
60  
61      /**
62       * The HTMLFile.
63       */
64      private final String theHTMLFile;
65  
66      /**
67       * The HTML.
68       */
69      private String theHTML;
70  
71      /**
72       * Constructor.
73       * @param pTitle the title
74       * @param pHtmlFile the filename
75       */
76      ArduinoHelp(final String pTitle,
77                  final String pHtmlFile) {
78          theTitle = pTitle;
79          theHTMLFile = pHtmlFile;
80      }
81  
82      @Override
83      public String toString() {
84          return theTitle;
85      }
86  
87      /**
88       * Obtain the html.
89       * @return the html
90       */
91      String getHTML() {
92          return theHTML;
93      }
94  
95      /**
96       * LoadAllHelp.
97       * @throws ArduinoException on error
98       */
99      static void loadAllHelp() throws ArduinoException {
100         for (ArduinoHelp myId : values()) {
101             myId.loadHelp();
102         }
103     }
104 
105     /**
106      * Load Html.
107      * @throws ArduinoException on error
108      */
109     private void loadHelp() throws ArduinoException {
110         /* Protect against exceptions */
111         try (InputStream myStream = ArduinoPanelHelp.class.getResourceAsStream(theHTMLFile);
112              InputStreamReader myReader = new InputStreamReader(myStream);
113              BufferedReader myInput = new BufferedReader(myReader)) {
114             /* Reset the builder */
115             final StringBuilder myBuilder = new StringBuilder();
116 
117             /* Read the header entry */
118             for (;;) {
119                 /* Read next line */
120                 final String myLine = myInput.readLine();
121                 if (myLine == null) {
122                     break;
123                 }
124 
125                 /* Add to the string buffer */
126                 myBuilder.append(myLine);
127                 myBuilder.append(ArduinoChar.LF);
128             }
129 
130             /* Build the string */
131             theHTML = myBuilder.toString();
132 
133             /* Catch Exceptions */
134         } catch (IOException e) {
135             throw new ArduinoException("Failed to load File");
136         }
137     }
138 }