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 java.awt.BorderLayout;
20  import java.awt.Dimension;
21  import javax.swing.JComponent;
22  import javax.swing.JEditorPane;
23  import javax.swing.JPanel;
24  import javax.swing.JScrollPane;
25  import javax.swing.JSplitPane;
26  import javax.swing.JTree;
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import javax.swing.tree.DefaultTreeModel;
29  import javax.swing.tree.TreePath;
30  import javax.swing.tree.TreeSelectionModel;
31  
32  import net.sourceforge.jarduino.ArduinoException;
33  
34  /**
35   * Help Panel.
36   */
37  public class ArduinoPanelHelp {
38      /**
39       * The Default Width.
40       */
41      static final int WIDTH = 600;
42  
43      /**
44       * The splitPane.
45       */
46      private final JSplitPane thePane;
47  
48      /**
49       * The help text.
50       */
51      private final JEditorPane theHelp;
52  
53      /**
54       * Constructor.
55       * @throws ArduinoException on error
56       */
57      ArduinoPanelHelp() throws ArduinoException {
58          /* Create the TextPane */
59          theHelp = new JEditorPane();
60          theHelp.setEditable(false);
61          theHelp.setContentType("text/html");
62  
63          /* Create the treeModel */
64          final DefaultMutableTreeNode myRoot = new DefaultMutableTreeNode("Root");
65          for (ArduinoHelp myId : ArduinoHelp.values()) {
66              myRoot.add(new DefaultMutableTreeNode(myId));
67          }
68          final DefaultTreeModel myModel = new DefaultTreeModel(myRoot);
69  
70          /* Create the tree */
71          final JTree myTree = new JTree(myModel);
72          myTree.setEditable(false);
73          myTree.setRootVisible(false);
74          myTree.setExpandsSelectedPaths(true);
75  
76          /* Handle selection */
77          final TreeSelectionModel mySelect = myTree.getSelectionModel();
78          mySelect.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
79          mySelect.addTreeSelectionListener(e -> {
80              final DefaultMutableTreeNode myNode = (DefaultMutableTreeNode) e.getNewLeadSelectionPath().getLastPathComponent();
81              final ArduinoHelp../../../net/sourceforge/jarduino/gui/ArduinoHelp.html#ArduinoHelp">ArduinoHelp myId = (ArduinoHelp) myNode.getUserObject();
82              theHelp.setText(myId.getHTML());
83              theHelp.setCaretPosition(0);
84          });
85  
86          /* Set Dimensions */
87          final JScrollPane myScroll = new JScrollPane(theHelp);
88          myScroll.setPreferredSize(new Dimension(WIDTH, ArduinoPanelMain.HEIGHT));
89  
90          /* Create the Panel */
91          final JPanel myPanel = new JPanel(new BorderLayout());
92          myPanel.add(myScroll, BorderLayout.CENTER);
93  
94          /* Load the help */
95          ArduinoHelp.loadAllHelp();
96  
97          /* Select the overview */
98          final TreePath myPath = new TreePath(((DefaultMutableTreeNode) myRoot.getFirstChild()).getPath());
99          mySelect.setSelectionPath(myPath);
100 
101         /* Create a splitPane */
102         thePane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
103         thePane.setLeftComponent(new JScrollPane(myTree));
104         thePane.setRightComponent(myPanel);
105         thePane.setResizeWeight(ArduinoTableNodeConnect.THIRD_WEIGHT);
106     }
107 
108     /**
109      * Obtain the component.
110      * @return the component
111      */
112     JComponent getComponent() {
113         return thePane;
114     }
115 }