ArduinoPanelHelp.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 java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import net.sourceforge.jarduino.ArduinoException;

/**
 * Help Panel.
 */
public class ArduinoPanelHelp {
    /**
     * The Default Width.
     */
    static final int WIDTH = 600;

    /**
     * The splitPane.
     */
    private final JSplitPane thePane;

    /**
     * The help text.
     */
    private final JEditorPane theHelp;

    /**
     * Constructor.
     * @throws ArduinoException on error
     */
    ArduinoPanelHelp() throws ArduinoException {
        /* Create the TextPane */
        theHelp = new JEditorPane();
        theHelp.setEditable(false);
        theHelp.setContentType("text/html");

        /* Create the treeModel */
        final DefaultMutableTreeNode myRoot = new DefaultMutableTreeNode("Root");
        for (ArduinoHelp myId : ArduinoHelp.values()) {
            myRoot.add(new DefaultMutableTreeNode(myId));
        }
        final DefaultTreeModel myModel = new DefaultTreeModel(myRoot);

        /* Create the tree */
        final JTree myTree = new JTree(myModel);
        myTree.setEditable(false);
        myTree.setRootVisible(false);
        myTree.setExpandsSelectedPaths(true);

        /* Handle selection */
        final TreeSelectionModel mySelect = myTree.getSelectionModel();
        mySelect.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        mySelect.addTreeSelectionListener(e -> {
            final DefaultMutableTreeNode myNode = (DefaultMutableTreeNode) e.getNewLeadSelectionPath().getLastPathComponent();
            final ArduinoHelp myId = (ArduinoHelp) myNode.getUserObject();
            theHelp.setText(myId.getHTML());
            theHelp.setCaretPosition(0);
        });

        /* Set Dimensions */
        final JScrollPane myScroll = new JScrollPane(theHelp);
        myScroll.setPreferredSize(new Dimension(WIDTH, ArduinoPanelMain.HEIGHT));

        /* Create the Panel */
        final JPanel myPanel = new JPanel(new BorderLayout());
        myPanel.add(myScroll, BorderLayout.CENTER);

        /* Load the help */
        ArduinoHelp.loadAllHelp();

        /* Select the overview */
        final TreePath myPath = new TreePath(((DefaultMutableTreeNode) myRoot.getFirstChild()).getPath());
        mySelect.setSelectionPath(myPath);

        /* Create a splitPane */
        thePane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        thePane.setLeftComponent(new JScrollPane(myTree));
        thePane.setRightComponent(myPanel);
        thePane.setResizeWeight(ArduinoTableNodeConnect.THIRD_WEIGHT);
    }

    /**
     * Obtain the component.
     * @return the component
     */
    JComponent getComponent() {
        return thePane;
    }
}