/*
 * Copyright (C) 2015 SAA
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package helper;

import controlador.Diagrama;
import controlador.Editor;
import controlador.apoios.TreeItem;
import desenho.FormaElementar;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.event.ListSelectionEvent;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import principal.FramePrincipal;
import util.ItemIntStringToList;

/**
 *
 * @author SAA
 */
public class FormHelp extends javax.swing.JFrame {

    /**
     * Creates new form FormPartes
     */
    public FormHelp() {
        initComponents();
        setTitle("brModelo: " + Editor.fromConfiguracao.getValor("Controler.interface.help"));

//        addWindowListener(new WindowAdapter() {
//            @Override
//            public void windowClosed(WindowEvent e) {
//
//            }
//
//            @Override
//            public void windowClosing(WindowEvent e) {
//                btnOKActionPerformed(null);
//            }
//        });
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jToolBar3 = new javax.swing.JToolBar();
        btnEditar = new javax.swing.JToggleButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        btnUP2 = new javax.swing.JButton();
        btnDown2 = new javax.swing.JButton();
        jSeparator8 = new javax.swing.JToolBar.Separator();
        btnAdd = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();
        jSeparator9 = new javax.swing.JToolBar.Separator();
        btnSalvar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Tree = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstEncontrados = new javax.swing.JList<>();
        jPanel3 = new javax.swing.JPanel();
        txtFinder = new javax.swing.JTextField();
        btnFinder = new javax.swing.JButton();
        Tabs = new javax.swing.JTabbedPane();
        PanPrincipal = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        Pan = new javax.swing.JPanel();
        PanEditor = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnOK = new javax.swing.JButton();
        btnDefazerTudo = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel6 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        comboTopicosAlterar = new javax.swing.JComboBox<>();
        jPanel5 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        comboLinks = new javax.swing.JComboBox<>();
        jPanel11 = new javax.swing.JPanel();
        jToolBar4 = new javax.swing.JToolBar();
        jSeparator10 = new javax.swing.JToolBar.Separator();
        btnAdd1 = new javax.swing.JButton();
        btnDel1 = new javax.swing.JButton();
        jSeparator11 = new javax.swing.JToolBar.Separator();
        btnUP3 = new javax.swing.JButton();
        btnDown3 = new javax.swing.JButton();
        jSeparator12 = new javax.swing.JToolBar.Separator();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstLinks = new javax.swing.JList<>();
        jPanel12 = new javax.swing.JPanel();
        edtTitulo = new javax.swing.JTextField();
        jPanel13 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        editorHtml = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        btnToDiag = new javax.swing.JButton();
        btnFromDiag = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtEditor = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        btnFechar = new javax.swing.JButton();
        panNav = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuSalvar = new javax.swing.JMenuItem();
        menuFechar = new javax.swing.JMenuItem();

        jSplitPane2.setDividerLocation(250);
        jSplitPane2.setDividerSize(4);
        jSplitPane2.setResizeWeight(0.1);

        jToolBar3.setFloatable(false);
        jToolBar3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar3.setRollover(true);

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/edit.png"))); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("principal/Formularios_pt_BR"); // NOI18N
        btnEditar.setText(bundle.getString("FormHelp.btnEditar.text")); // NOI18N
        btnEditar.setToolTipText(bundle.getString("FormHelp.btnEditar.toolTipText")); // NOI18N
        btnEditar.setFocusable(false);
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jToolBar3.add(btnEditar);
        jToolBar3.add(jSeparator7);

        btnUP2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/up.png"))); // NOI18N
        btnUP2.setText(bundle.getString("FormHelp.btnUP2.text")); // NOI18N
        btnUP2.setToolTipText(bundle.getString("FormHelp.btnUP2.toolTipText")); // NOI18N
        btnUP2.setFocusable(false);
        btnUP2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnUP2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnUP2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUP2ActionPerformed(evt);
            }
        });
        jToolBar3.add(btnUP2);

        btnDown2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/down.png"))); // NOI18N
        btnDown2.setToolTipText(bundle.getString("FormHelp.btnDown2.toolTipText")); // NOI18N
        btnDown2.setFocusable(false);
        btnDown2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDown2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDown2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDown2ActionPerformed(evt);
            }
        });
        jToolBar3.add(btnDown2);
        btnDown2.getAccessibleContext().setAccessibleDescription(bundle.getString("FormHelp.btnDown2.AccessibleContext.accessibleDescription")); // NOI18N

        jToolBar3.add(jSeparator8);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/add_16.gif"))); // NOI18N
        btnAdd.setText(bundle.getString("FormHelp.btnAdd.text")); // NOI18N
        btnAdd.setToolTipText(bundle.getString("FormHelp.btnAdd.toolTipText")); // NOI18N
        btnAdd.setActionCommand(bundle.getString("FormHelp.btnAdd.actionCommand")); // NOI18N
        btnAdd.setFocusable(false);
        btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jToolBar3.add(btnAdd);

        btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/busy.png"))); // NOI18N
        btnDel.setText(bundle.getString("FormHelp.btnDel.text")); // NOI18N
        btnDel.setToolTipText(bundle.getString("FormHelp.btnDel.toolTipText")); // NOI18N
        btnDel.setFocusable(false);
        btnDel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });
        jToolBar3.add(btnDel);
        jToolBar3.add(jSeparator9);

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_salvar.png"))); // NOI18N
        btnSalvar.setText(bundle.getString("FormHelp.btnSalvar.text")); // NOI18N
        btnSalvar.setToolTipText(bundle.getString("FormHelp.btnSalvar.toolTipText")); // NOI18N
        btnSalvar.setFocusable(false);
        btnSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jToolBar3.add(btnSalvar);

        Tree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TreeMouseClicked(evt);
            }
        });
        Tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                TreeValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(Tree);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar3, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab(bundle.getString("ajuda.painel.tab.topicos"), jPanel4); // NOI18N

        lstEncontrados.setModel(new javax.swing.AbstractListModel<String>() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstEncontrados);

        txtFinder.setText(bundle.getString("FormHelp.txtFinder.text")); // NOI18N

        btnFinder.setForeground(new java.awt.Color(255, 255, 255));
        btnFinder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/find.png"))); // NOI18N
        btnFinder.setToolTipText(bundle.getString("ajuda.procurar.tooltip.btnprocurar")); // NOI18N
        btnFinder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(txtFinder, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFinder, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFinder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFinder))
                .addGap(1, 1, 1))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab(bundle.getString("ajuda.painel.tab.procurar"), jPanel1); // NOI18N

        jSplitPane2.setLeftComponent(jTabbedPane2);
        jTabbedPane2.getAccessibleContext().setAccessibleName(bundle.getString("ajuda.tabs.topicos")); // NOI18N

        jScrollPane3.setBackground(new java.awt.Color(255, 255, 255));

        Pan.setBackground(new java.awt.Color(255, 255, 255));
        Pan.setLayout(null);
        jScrollPane3.setViewportView(Pan);

        javax.swing.GroupLayout PanPrincipalLayout = new javax.swing.GroupLayout(PanPrincipal);
        PanPrincipal.setLayout(PanPrincipalLayout);
        PanPrincipalLayout.setHorizontalGroup(
            PanPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
        );
        PanPrincipalLayout.setVerticalGroup(
            PanPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanPrincipalLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE))
        );

        Tabs.addTab(bundle.getString("FormHelp.PanPrincipal.TabConstraints.tabTitle"), PanPrincipal); // NOI18N

        PanEditor.setBackground(new java.awt.Color(204, 204, 255));

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.add(jSeparator4);

        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/check.png"))); // NOI18N
        btnOK.setText(bundle.getString("FormHelp.btnOK.text")); // NOI18N
        btnOK.setToolTipText(bundle.getString("FormHelp.btnOK.toolTipText")); // NOI18N
        btnOK.setFocusable(false);
        btnOK.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnOK.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });
        jToolBar1.add(btnOK);

        btnDefazerTudo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/undo_16.png"))); // NOI18N
        btnDefazerTudo.setText(bundle.getString("FormHelp.btnDefazerTudo.text")); // NOI18N
        btnDefazerTudo.setToolTipText(bundle.getString("FormHelp.btnDefazerTudo.toolTipText")); // NOI18N
        btnDefazerTudo.setFocusable(false);
        btnDefazerTudo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDefazerTudo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDefazerTudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefazerTudoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDefazerTudo);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ajuda.edicao.painel.arvore"))); // NOI18N

        comboTopicosAlterar.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addComponent(comboTopicosAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 527, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 84, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addComponent(comboTopicosAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ajuda.editor.label.topico.editor"))); // NOI18N

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ajuda.editor.label.links"))); // NOI18N

        comboLinks.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addComponent(comboLinks, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 172, Short.MAX_VALUE))
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addComponent(comboLinks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1))
        );

        jToolBar4.setFloatable(false);
        jToolBar4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar4.setRollover(true);
        jToolBar4.add(jSeparator10);

        btnAdd1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/mais.png"))); // NOI18N
        btnAdd1.setText(bundle.getString("FormHelp.btnAdd1.text")); // NOI18N
        btnAdd1.setToolTipText(bundle.getString("FormHelp.btnAdd1.toolTipText")); // NOI18N
        btnAdd1.setActionCommand(bundle.getString("FormHelp.btnAdd1.actionCommand")); // NOI18N
        btnAdd1.setFocusable(false);
        btnAdd1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAdd1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAdd1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdd1ActionPerformed(evt);
            }
        });
        jToolBar4.add(btnAdd1);

        btnDel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menos.png"))); // NOI18N
        btnDel1.setText(bundle.getString("FormHelp.btnDel1.text")); // NOI18N
        btnDel1.setToolTipText(bundle.getString("FormHelp.btnDel1.toolTipText")); // NOI18N
        btnDel1.setFocusable(false);
        btnDel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDel1ActionPerformed(evt);
            }
        });
        jToolBar4.add(btnDel1);
        jToolBar4.add(jSeparator11);

        btnUP3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/up.png"))); // NOI18N
        btnUP3.setText(bundle.getString("FormHelp.btnUP3.text")); // NOI18N
        btnUP3.setToolTipText(bundle.getString("FormHelp.btnUP3.toolTipText")); // NOI18N
        btnUP3.setFocusable(false);
        btnUP3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnUP3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnUP3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUP3ActionPerformed(evt);
            }
        });
        jToolBar4.add(btnUP3);

        btnDown3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/down.png"))); // NOI18N
        btnDown3.setToolTipText(bundle.getString("FormHelp.btnDown3.toolTipText")); // NOI18N
        btnDown3.setFocusable(false);
        btnDown3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDown3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDown3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDown3ActionPerformed(evt);
            }
        });
        jToolBar4.add(btnDown3);
        jToolBar4.add(jSeparator12);

        lstLinks.setModel(new javax.swing.AbstractListModel<String>() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(lstLinks);

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane4))
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        jPanel12.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ajuda.editor.painel.titulo"))); // NOI18N

        edtTitulo.setText(bundle.getString("FormHelp.edtTitulo.text")); // NOI18N

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addComponent(edtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 525, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(edtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel13.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ajuda.editor.label.html"))); // NOI18N

        editorHtml.setColumns(20);
        editorHtml.setRows(5);
        jScrollPane6.setViewportView(editorHtml);

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6)
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        jTabbedPane1.addTab(bundle.getString("ajuda.painel.tab.titulo.principal"), jPanel6); // NOI18N

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        btnToDiag.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/down.png"))); // NOI18N
        btnToDiag.setText(bundle.getString("ajuda.editor.btnNoDiagrama")); // NOI18N
        btnToDiag.setFocusable(false);
        btnToDiag.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        btnToDiag.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToDiag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToDiagActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToDiag);

        btnFromDiag.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/up.png"))); // NOI18N
        btnFromDiag.setText(bundle.getString("ajuda.editor.btn.carregar")); // NOI18N
        btnFromDiag.setFocusable(false);
        btnFromDiag.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnFromDiag.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnFromDiag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFromDiagActionPerformed(evt);
            }
        });
        jToolBar2.add(btnFromDiag);

        txtEditor.setColumns(20);
        txtEditor.setRows(5);
        jScrollPane5.setViewportView(txtEditor);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
            .addComponent(jScrollPane5)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("ajuda.painel.tabs.titulo.XML"), jPanel7); // NOI18N

        javax.swing.GroupLayout PanEditorLayout = new javax.swing.GroupLayout(PanEditor);
        PanEditor.setLayout(PanEditorLayout);
        PanEditorLayout.setHorizontalGroup(
            PanEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanEditorLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jTabbedPane1))
        );
        PanEditorLayout.setVerticalGroup(
            PanEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane1)
        );

        Tabs.addTab(bundle.getString("FormHelp.PanPrincipal.TabConstraints.tabTitleEditor"), PanEditor); // NOI18N

        jSplitPane2.setRightComponent(Tabs);
        Tabs.getAccessibleContext().setAccessibleName(bundle.getString("FormHelp.Tabs.AccessibleContext.accessibleName")); // NOI18N

        btnFechar.setText(bundle.getString("FormHelp.btnFechar.text")); // NOI18N
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        panNav.setBackground(new java.awt.Color(204, 204, 255));
        panNav.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(panNav, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFechar))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnFechar)
                    .addComponent(panNav, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2))
        );

        jMenu1.setText(bundle.getString("FormHelp.jMenu1.text")); // NOI18N

        menuSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_salvar.png"))); // NOI18N
        menuSalvar.setText(bundle.getString("FormHelp.menuSalvar.text")); // NOI18N
        menuSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSalvarActionPerformed(evt);
            }
        });
        jMenu1.add(menuSalvar);

        menuFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/sign-out.png"))); // NOI18N
        menuFechar.setText(bundle.getString("FormHelp.menuFechar.text")); // NOI18N
        menuFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFecharActionPerformed(evt);
            }
        });
        jMenu1.add(menuFechar);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSplitPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jSplitPane2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        if (v.getID() == 0 ) {
            util.Dialogos.ShowMessageERROR(this, Editor.fromConfiguracao.getValor("Controler.MSG_ERRO_EDIT_TOPICO_HELP"));
            return;            
        }
        if (evt != null && !emEdicao(v)) {
            return;
        }

        v.setTitulo(edtTitulo.getText());
        if (!editorHtml.getText().equals(RepositorioHtml.generateHtml())) {
            v.setHtml(editorHtml.getText());
        }
        v.setXMLCopiado(txtEditor.getText());
        v.getLinks().clear();
        oslinks.forEach(k -> v.getLinks().add(k));
        int idx = topicos.get(comboTopicosAlterar.getSelectedIndex()).getID();
        ParteAjuda pai = AjudaMng.findByID(idx);
        if (v.getID() != 0 && pai != v) {
            v.SetSuperior(pai);
        }
        DoMuda(true);
        previo = null;
        PopuleTree();
        SelectNodeByID(v.getID());
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnFecharActionPerformed

    private void TreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TreeMouseClicked
        ChangeTreeItemSelection();
    }//GEN-LAST:event_TreeMouseClicked

    TreeItem previo = null;
    final ArrayList<ParteAjuda> nav = new ArrayList<>();

    private void ChangeTreeItemSelection() {
        if (disableEv) {
            return;
        }

        TreeItem tv = ((TreeItem) Tree.getLastSelectedPathComponent());
        if (tv == null) {
            return;
        }
        ParteAjuda v = AjudaMng.findByID(tv.getId());
        if (v == null) {
            return;
        }
        if (previo != null && previo != tv) {
            ParteAjuda iap = AjudaMng.findByID(previo.getId());
            if (iap != null && emEdicao(iap)) {
                if (util.Dialogos.ShowMessageConfirmYES(this, ": " + Editor.fromConfiguracao.getValor("Controler.interface.mensagem.msg201"))) {
                    disableEv = true;
                    SelectNodeByID(iap.getID());
                    disableEv = false;
                    btnOKActionPerformed(null);
                    disableEv = true;
                    SelectNodeByID(v.getID());
                    disableEv = false;
                }
            }
            int idx = nav.indexOf(iap);
            if (idx > -1) {
                nav.remove(idx);
                panNav.remove(idx);
            }
            if (nav.size() > 4) {
                nav.remove(0);
                panNav.remove(0);
            }
            nav.add(iap);
            String ps = LabelNav.textoFormatado(previo.getTitulo());
            ps = "<" + ps + (btnEditar.isSelected() ? " (ID: " + String.valueOf(previo.getId()) + ")" : "") + ">";
            LabelNav lb = new LabelNav(ps);
            lb.setId(previo.getId());
            lb.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    int i = ((LabelNav) evt.getSource()).getId();
                    SelectNodeByID(i);
                }
            });
            lb.LikeLink();
            panNav.add(lb);
            panNav.revalidate();
            panNav.repaint();
        }
        previo = tv;

        btnUP2.setEnabled(false);
        btnDown2.setEnabled(false);
        btnDel.setEnabled(false);
        if (btnEditar.isSelected()) {
            btnDel.setEnabled(v.getID() != 0);
            if (btnDel.isEnabled()) {
                int tmp = v.getSuperior().getSubs().indexOf(v);
                int tmp2 = v.getSuperior().getSubs().size();
                btnUP2.setEnabled(tmp > 0);
                btnDown2.setEnabled(tmp2 > 1 && tmp < tmp2 - 1);
            }
        }
        populeEditor(v);
    }

    private void TreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_TreeValueChanged
        ChangeTreeItemSelection();
    }//GEN-LAST:event_TreeValueChanged

    private int maxID = -1;

    private void btnUP2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUP2ActionPerformed
        performMove(-1);
    }//GEN-LAST:event_btnUP2ActionPerformed

    private void btnDown2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDown2ActionPerformed
        performMove(1);
    }//GEN-LAST:event_btnDown2ActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        if (emEdicao(v)) {
            btnOKActionPerformed(null);
        }
        maxID++;
        ParteAjuda pa = v.Add(maxID, Editor.fromConfiguracao.getValor("Controler.interface.texto.empyhtmltext"));
        //pa.setHtml(RepositorioHtml.generateHtml());
        populeEditor(pa);

        DoMuda(true);
        previo = null;
        PopuleTree();
        SelectNodeByID(maxID);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        if (v.getID() == 0) {
            util.Dialogos.ShowMessageERROR(this, Editor.fromConfiguracao.getValor("Controler.MSG_ERRO_DELETE_TOPICO_HELP"));
            return;
        }
        if (util.Dialogos.ShowMessageConfirmYES(this, Editor.fromConfiguracao.getValor("Controler.MSG_DELETE_TOPICO_HELP"))) {
            v.getSuperior().getSubs().remove(v);

            int idx = nav.indexOf(v);
            if (idx > -1) {
                nav.remove(idx);
                panNav.remove(idx);
                panNav.revalidate();
                panNav.repaint();
            }

            DoMuda(true);
            PopuleTree();
            SelectNodeByID(v.getSuperior().getID());
        }
    }//GEN-LAST:event_btnDelActionPerformed

    private void btnDefazerTudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefazerTudoActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null || !emEdicao(v)) {
            return;
        }
        ChangeTreeItemSelection();
    }//GEN-LAST:event_btnDefazerTudoActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if (!Salva()) {
            util.Dialogos.ShowMessageERROR(this, Editor.fromConfiguracao.getValor("Controler.MSG_SAVE_HELP_ERROR"));
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        btnAdd.setEnabled(btnEditar.isSelected());

        ChangeTreeItemSelection();
        Tabs.setSelectedIndex(0);
        Tabs.setEnabledAt(1, btnEditar.isSelected());
        for (int i = 0; i < panNav.getComponentCount(); i++) {
            LabelNav ln = (LabelNav) panNav.getComponent(i);
            ParteAjuda it = nav.get(i);
            String ps = LabelNav.textoFormatado(it.getTitulo());
            ps = "<" + ps + (btnEditar.isSelected() ? " (ID: " + String.valueOf(it.getID()) + ")" : "") + ">";
            ln.setText(ps);
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void menuFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFecharActionPerformed
        btnFecharActionPerformed(evt);
    }//GEN-LAST:event_menuFecharActionPerformed

    private void menuSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSalvarActionPerformed
        btnSalvarActionPerformed(evt);
    }//GEN-LAST:event_menuSalvarActionPerformed

    private void btnFinderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinderActionPerformed
        ArrayList<ParteAjuda> achados = new ArrayList<>();
        encontrados.clear();
        final DefaultListModel lst = new DefaultListModel();
        lstEncontrados.setModel(lst);
        if (txtFinder.getText().isEmpty()) {
            return;
        }
        AjudaMng.ProcurarPorTexto(txtFinder.getText(), achados);
        achados.stream().forEach(a -> {
            ItemIntStringToList toa = new ItemIntStringToList(a.getID(), a.getTitulo(), a);
            encontrados.add(toa);
            lst.addElement(a.getTitulo());
        });
    }//GEN-LAST:event_btnFinderActionPerformed

    private void btnUP3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUP3ActionPerformed
        performLinks(-1);
    }//GEN-LAST:event_btnUP3ActionPerformed

    private void btnDown3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDown3ActionPerformed
        performLinks(1);
    }//GEN-LAST:event_btnDown3ActionPerformed

    final ArrayList<Integer> oslinks = new ArrayList<>();

    private void btnAdd1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdd1ActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        int idx = comboLinks.getSelectedIndex();
        int oid = topicos.get(idx).getID();
        if (oid != v.getID() && oslinks.indexOf(oid) < 0) {
            oslinks.add(topicos.get(idx).getID());
        }
        PopuleLinks(v);
        lstLinks.setSelectedIndex(oslinks.size() - 1);
    }//GEN-LAST:event_btnAdd1ActionPerformed

    private void btnDel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDel1ActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        int idx = lstLinks.getSelectedIndex();
        if (idx < 0) {
            return;
        }
        oslinks.remove(idx);
        PopuleLinks(v);
        if (idx < oslinks.size() - 1) {
            lstLinks.setSelectedIndex(idx);
        } else if (!oslinks.isEmpty()) {
            lstLinks.setSelectedIndex(oslinks.size() - 1);
        }
    }//GEN-LAST:event_btnDel1ActionPerformed

    private void btnToDiagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToDiagActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null || v.getID() == 0) {
            return;
        }
        fmp.getEditor().AddAsAtual(v.getTipoDeDiagrama().name());
        if (!fmp.getEditor().diagramaAtual.doPaste(v.getXMLCopiado())) {
            util.Dialogos.ShowMessageERROR(this, Editor.fromConfiguracao.getValor("Controler.MSG_ERRO_PASTE"));
        }
    }//GEN-LAST:event_btnToDiagActionPerformed

    private void btnFromDiagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFromDiagActionPerformed
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        if (v.getID() == 0) {
            util.Dialogos.ShowMessageERROR(this, Editor.fromConfiguracao.getValor("Controler.MSG_ERRO_EDIT_TOPICO_HELP"));
            return;
        }
        if (emEdicao(v)) {
            btnOKActionPerformed(null);
        }
        String XMLCopiado = Diagrama.SaveToXml(fmp.getEditor().diagramaAtual, false);
        String versaoDiagrama = fmp.getEditor().diagramaAtual.getVersao();
        Diagrama.TipoDeDiagrama tipo = fmp.getEditor().diagramaAtual.getTipo();

        v.InitParteAjuda(ImagemFull(), XMLCopiado, versaoDiagrama, tipo);
        DoMuda(true);
        populeEditor(v);
    }//GEN-LAST:event_btnFromDiagActionPerformed

    public FramePrincipal fmp = null;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Pan;
    private javax.swing.JPanel PanEditor;
    private javax.swing.JPanel PanPrincipal;
    private javax.swing.JTabbedPane Tabs;
    private javax.swing.JTree Tree;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAdd1;
    private javax.swing.JButton btnDefazerTudo;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnDel1;
    private javax.swing.JButton btnDown2;
    private javax.swing.JButton btnDown3;
    private javax.swing.JToggleButton btnEditar;
    private javax.swing.JButton btnFechar;
    private javax.swing.JButton btnFinder;
    private javax.swing.JButton btnFromDiag;
    private javax.swing.JButton btnOK;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnToDiag;
    private javax.swing.JButton btnUP2;
    private javax.swing.JButton btnUP3;
    private javax.swing.JComboBox<String> comboLinks;
    private javax.swing.JComboBox<String> comboTopicosAlterar;
    private javax.swing.JTextArea editorHtml;
    private javax.swing.JTextField edtTitulo;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JToolBar.Separator jSeparator10;
    private javax.swing.JToolBar.Separator jSeparator11;
    private javax.swing.JToolBar.Separator jSeparator12;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JToolBar.Separator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JList<String> lstEncontrados;
    private javax.swing.JList<String> lstLinks;
    private javax.swing.JMenuItem menuFechar;
    private javax.swing.JMenuItem menuSalvar;
    private javax.swing.JPanel panNav;
    private javax.swing.JTextArea txtEditor;
    private javax.swing.JTextField txtFinder;
    // End of variables declaration//GEN-END:variables

    //<editor-fold defaultstate="collapsed" desc="Ultil">
    public byte[] ImagemFull() {
        Diagrama diagramaAtual = fmp.getEditor().diagramaAtual;
        final int borda = 2;
        Point p2 = diagramaAtual.getPontoExtremo();
        int minX = p2.x;
        int minY = p2.y;

        for (int i = diagramaAtual.getListaDeItens().size() - 1; i > -1; i--) {
            FormaElementar el = diagramaAtual.getListaDeItens().get(i);
            minX = Math.min(minX, el.getLeft());
            minY = Math.min(minY, el.getTop());
        }

        minX = Math.max(minX - borda, 0);
        minY = Math.max(minY - borda, 0);
//        W = p2.x - minX;
//        H = p2.y - minY;
        BufferedImage cp_img = util.ImageGenerate.geraImagemForPrn(diagramaAtual, p2.x + borda, p2.y + borda);
        return util.TratadorDeImagens.toByteArray(cp_img.getSubimage(minX, minY, p2.x - minX, p2.y - minY));
    }
    //</editor-fold>

    public ParteAjuda getSelecionado() {
        TreeItem tv = ((TreeItem) Tree.getLastSelectedPathComponent());
        if (tv == null) {
            return null;
        }
        ParteAjuda v = AjudaMng.findByID(tv.getId());
        if (v == null) {
            return null;
        }
        return v;
    }

    public boolean LoadData() {
        AjudaMng = AjudaManager.LoadDataHelp();
        boolean res = true;
        if (AjudaMng == null) {
            AjudaMng = new AjudaManager(0, Editor.fromConfiguracao.getValor("Controler.interface.help"));
            AjudaMng.setHtml(RepositorioHtml.generateSobre());
            AjudaMng.setSuperior(AjudaMng);
            res = false;
        } else {
            AjudaMng.setHtml(RepositorioHtml.generateSobre());
            AjudaMng.setTitulo(Editor.fromConfiguracao.getValor("Controler.interface.help"));
        }
        PopuleTree();
        maxID = AjudaMng.FindMaxID();
        DoMuda(false);
        btnEditarActionPerformed(null);
        Tree.setExpandsSelectedPaths(true);
        Tree.setCellRenderer(new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree,
                    Object value, boolean selected, boolean expanded,
                    boolean isLeaf, int row, boolean focused) {
                Component c = super.getTreeCellRendererComponent(tree, value,
                        selected, expanded, isLeaf, row, focused);
                if (value instanceof TreeItem) {
                    setIcon(fmp.getEditor().getControler().ImagemDeDiagrama.get("Controler.interface.ajuda.icone"));
                }
                return c;
            }
        });
        this.setIconImage(fmp.getEditor().getControler().ImagemDeDiagrama.get("Controler.interface.ajuda.icone").getImage());
        btnUP3.setEnabled(false);
        btnDown3.setEnabled(false);
        btnDel1.setEnabled(false);
        lstLinks.addListSelectionListener((ListSelectionEvent lse) -> {
            if (!lse.getValueIsAdjusting() && !disableEv) {
                btnUP3.setEnabled(lstLinks.getSelectedIndex() > 0);
                int t = lstLinks.getModel().getSize();
                btnDown3.setEnabled(t > 1 && lstLinks.getSelectedIndex() < t - 1);
                btnDel1.setEnabled(lstLinks.getSelectedIndex() > -1);
            }
        });

        lstEncontrados.addListSelectionListener((ListSelectionEvent lse) -> {
            if (!lse.getValueIsAdjusting() && !disableEv) {
                if (lstEncontrados.getSelectedIndex() > -1) {
                    int oid = encontrados.get(lstEncontrados.getSelectedIndex()).getID();
                    SelectNodeByID(oid);
                }
            }
        });
        return res;
    }

    private TreeItem TreePincipal = null;
    public AjudaManager AjudaMng = null;

    private ArrayList<util.ItemIntStringToList> topicos = new ArrayList<>();
    private ArrayList<util.ItemIntStringToList> encontrados = new ArrayList<>();

    private boolean disableEv = false;

    private TreeItem PopuleTree() {
        final TreeItem tree = new TreeItem(AjudaMng.getTitulo(), 0, "");
        disableEv = true;
        topicos.clear();
        encontrados.clear();
        lstEncontrados.setModel(new DefaultListModel());
        AjudaMng.getSubs().stream().forEach((it) -> {
            it.PopuleTree(tree);
        });
        AjudaMng.PopuleTopicos(topicos);

        final DefaultComboBoxModel lst = new DefaultComboBoxModel();
        final DefaultComboBoxModel lst2 = new DefaultComboBoxModel();
        topicos.forEach(tp -> {
            lst.addElement(tp);
            lst2.addElement(tp);
        });
        comboTopicosAlterar.setModel(lst);
        comboLinks.setModel(lst2);

        Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        Tree.setModel(new DefaultTreeModel(tree));
        Tree.setSelectionRow(0);
        TreePincipal = tree;
        nav.clear();
        panNav.removeAll();
        panNav.repaint();
        disableEv = false;
        return tree;
    }

    private void SelectNodeByID(int oid) {
        TreeItem res = findByID(oid, TreePincipal);
        if (res != null) {
            Tree.setSelectionPath(new TreePath(res.getPath()));
        }
    }

    public TreeItem findByID(int id, TreeItem atual) {
        if (id == atual.getId()) {
            return atual;
        }
        for (int i = 0; i < atual.getChildCount(); i++) {
            TreeItem ia = (TreeItem) atual.getChildAt(i);
            TreeItem res = findByID(id, ia);
            if (res != null) {
                return res;
            }
        }
        return null;
    }

    private void DoMuda(boolean b) {
        btnSalvar.setEnabled(b);
        AjudaMng.setMudou(b);
        menuSalvar.setEnabled(b);
    }

    public boolean Salva() {
        btnOKActionPerformed(new java.awt.event.ActionEvent(this, 0, ""));
        if (AjudaManager.SaveDataHelp(AjudaMng)) {
            DoMuda(false);
            return true;
        }
        return false;
    }

    private void performMove(int i) {
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        if (emEdicao(v)) {
            btnOKActionPerformed(null);
        }
        ParteAjuda pai = v.getSuperior();
        int np = pai.getSubs().indexOf(v) + i;

        pai.getSubs().remove(v);
        pai.getSubs().add(np, v);

        DoMuda(true);
        previo = null;
        PopuleTree();
        SelectNodeByID(v.getID());
    }

    public void populeEditor(ParteAjuda sel) {
        if (btnEditar.isSelected()) {
            DefaultListModel lst = new DefaultListModel();
            sel.getLinks().forEach(ls -> {
                ParteAjuda pa = AjudaMng.findByID(ls);
                lst.addElement(pa.getTitulo());
            });
            lstLinks.setModel(lst);
            txtEditor.setText(sel.getXMLCopiado());
            edtTitulo.setText(sel.getTitulo());
            comboTopicosAlterar.setSelectedItem(topicos.stream().filter(tp -> tp.getTag() == sel.getSuperior()).findAny().orElse(null));
            if (!sel.getHtml().isEmpty()) {
                editorHtml.setText(sel.getHtml());
            } else {
                editorHtml.setText(RepositorioHtml.generateHtml());
            }
            btnToDiag.setEnabled(!sel.getXMLCopiado().isEmpty());
        }
        if (sel.getByteImage() != null || sel.getHtml() != null || !sel.getLinks().isEmpty()) {//.isEmpty()) {
            Pan.removeAll();
            int H = 0;
            int W = 0;
            if (!sel.getHtml().isEmpty()) {
                JLabel htmLbl = new JLabel();
                htmLbl.setText(sel.getHtml());
                htmLbl.repaint();
                Dimension d = htmLbl.getPreferredSize();
                htmLbl.setBounds(0, 0, d.width, d.height);
                Pan.add(htmLbl);
                H = d.height + 10;
                W = d.width;
            }
            if (sel.getByteImage() != null) {
                ImageIcon img = new ImageIcon(sel.getByteImage());
                JLabel picLabel = new JLabel(img);
                W = W > img.getIconWidth() ? W : img.getIconWidth();
                picLabel.setBounds(0, H, img.getIconWidth(), img.getIconHeight());
                picLabel.setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));
                H += img.getIconHeight();
                Pan.setPreferredSize(new Dimension(W, H));
                Pan.add(picLabel);
            }
            if (!sel.getLinks().isEmpty()) {
                int tl = 1;
                for (Integer sl : sel.getLinks()) {
                    ParteAjuda lo = AjudaMng.findByID(sl);
                    String ps = String.valueOf(tl) + ". " + lo.getTitulo();
                    tl++;
                    LabelNav lb = new LabelNav(ps);
                    lb.setId(lo.getID());
                    lb.setForeground(Color.BLUE);
                    lb.LikeLink();
                    lb.addMouseListener(new java.awt.event.MouseAdapter() {
                        @Override
                        public void mouseClicked(java.awt.event.MouseEvent evt) {
                            int i = ((LabelNav) evt.getSource()).getId();
                            SelectNodeByID(i);
                        }
                    });
                    Dimension d = lb.getPreferredSize();
                    lb.setBounds(0, H, d.width, d.height);
                    H += d.height;
                    W = W > d.width ? W : d.width;
                    Pan.add(lb);
                }
                Pan.setPreferredSize(new Dimension(W, H));
            }
            Pan.revalidate();
            Pan.repaint();
        } else {
            Pan.removeAll();
            Pan.revalidate();
            Pan.repaint();
        }
        oslinks.clear();
        sel.getLinks().forEach(k -> oslinks.add(k));
        PopuleLinks(sel);
    }

    private void PopuleLinks(ParteAjuda v) {
        final DefaultListModel lst = new DefaultListModel();
        oslinks.forEach(osl -> {
            topicos.stream().filter(t -> t.getID() == osl).forEach(t -> lst.addElement(t.getValue()));
        });
        lstLinks.setModel(lst);
    }

    private boolean emEdicao(ParteAjuda iap) {
        if (!btnEditar.isSelected()) {
            return false;
        }
        boolean sn = !iap.getTitulo().equals(edtTitulo.getText())
                || !iap.getXMLCopiado().equals(txtEditor.getText());
        if (sn || oslinks.size() != iap.getLinks().size()) {
            return true;
        }
        if (iap.getHtml().isEmpty()) {
            if (!editorHtml.getText().equals(RepositorioHtml.generateHtml())) {
                return true;
            }
        } else {
            if (!iap.getHtml().equals(editorHtml.getText())) {
                return true;
            }
        }
        for (int i = 0; i < oslinks.size(); i++) {
            if (!Objects.equals(oslinks.get(i), iap.getLinks().get(i))) {
                return true;
            }
        }
        int idx = topicos.get(comboTopicosAlterar.getSelectedIndex()).getID();
        ParteAjuda pai = AjudaMng.findByID(idx);
        return iap.getSuperior() != pai;
    }

    private void performLinks(int i) {
        ParteAjuda v = getSelecionado();
        if (v == null) {
            return;
        }
        int idx = lstLinks.getSelectedIndex();
        if (idx == -1) {
            return;
        }
        int nv = idx + i;
        if ((nv < 0) || (nv >= lstLinks.getModel().getSize() - 1)) {
            return;
        }
        Integer r = oslinks.remove(idx);
        oslinks.add(nv, r);
        PopuleLinks(v);
        lstLinks.setSelectedIndex(nv);
    }
}
