ArduinoHelp.java

/*******************************************************************************
 * jArduino: Arduino C++ Code Generation From Java
 * Copyright 2020 Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.sourceforge.jarduino.gui;

import net.sourceforge.jarduino.ArduinoException;
import net.sourceforge.jarduino.message.ArduinoChar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Arduino HelpPage Ids.
 */
public enum ArduinoHelp {
    /**
     * Overview.
     */
    OVERVIEW("Overview", "Overview.html"),

    /**
     * CodeStructure.
     */
    CODESTRUCT("Code Structure", "CodeStructure.html"),

    /**
     * SourcesTab.
     */
    SRCSTAB("Source Tab", "SourcesTab.html"),

    /**
     * MessageTab.
     */
    MSGTAB("Message Tab", "MessageTab.html"),

    /**
     * MetaDataTab.
     */
    METATAB("MetaData Tab", "MetaDataTab.html");

    /**
     * The Title.
     */
    private final String theTitle;

    /**
     * The HTMLFile.
     */
    private final String theHTMLFile;

    /**
     * The HTML.
     */
    private String theHTML;

    /**
     * Constructor.
     * @param pTitle the title
     * @param pHtmlFile the filename
     */
    ArduinoHelp(final String pTitle,
                final String pHtmlFile) {
        theTitle = pTitle;
        theHTMLFile = pHtmlFile;
    }

    @Override
    public String toString() {
        return theTitle;
    }

    /**
     * Obtain the html.
     * @return the html
     */
    String getHTML() {
        return theHTML;
    }

    /**
     * LoadAllHelp.
     * @throws ArduinoException on error
     */
    static void loadAllHelp() throws ArduinoException {
        for (ArduinoHelp myId : values()) {
            myId.loadHelp();
        }
    }

    /**
     * Load Html.
     * @throws ArduinoException on error
     */
    private void loadHelp() throws ArduinoException {
        /* Protect against exceptions */
        try (InputStream myStream = ArduinoPanelHelp.class.getResourceAsStream(theHTMLFile);
             InputStreamReader myReader = new InputStreamReader(myStream);
             BufferedReader myInput = new BufferedReader(myReader)) {
            /* Reset the builder */
            final StringBuilder myBuilder = new StringBuilder();

            /* Read the header entry */
            for (;;) {
                /* Read next line */
                final String myLine = myInput.readLine();
                if (myLine == null) {
                    break;
                }

                /* Add to the string buffer */
                myBuilder.append(myLine);
                myBuilder.append(ArduinoChar.LF);
            }

            /* Build the string */
            theHTML = myBuilder.toString();

            /* Catch Exceptions */
        } catch (IOException e) {
            throw new ArduinoException("Failed to load File");
        }
    }
}