<commit_before>package org.chrisle.gitrepoviewer.components;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.util.ImageUtilities;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.chrisle.gitrepoviewer.components//RepoViewer//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "GitRepoViewerTopComponent",
        iconBase = "org/chrisle/gitrepoviewer/resources/world.png"
)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "org.chrisle.gitrepoviewer.components.GitRepoViewerTopComponent")
@ActionReferences({
    @ActionReference(path = "Menu/Window" /*, position = 333 */),
    @ActionReference(path = "Shortcuts", name = "DO-G DO-R")
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_GitRepoViewerAction",
        preferredID = "GitRepoViewerTopComponent"
)
@Messages({
    "CTL_GitRepoViewerAction=Git Repository Viewer",
    "CTL_GitRepoViewerTopComponent=Git Repository Viewer",
    "HINT_GitRepoViewerTopComponent=This is a Git Repository Viewer"
})
public final class GitRepoViewerTopComponent extends TopComponent {
    private static Icon _rootNodeIcon;
    private static DefaultMutableTreeNode _hostTreeRootNode;
    private final DefaultTreeModel _hostTreeModel;
    private TreePath _treeClickedPath;
    private static JPopupMenu _treeNodePopup;
    private static AbstractAction _popupAddHostAction;
    private static AbstractAction _popupRemoveHostAction;
    
    private static boolean isRootNode(DefaultMutableTreeNode selectedTreeNode) {
        return selectedTreeNode != null && selectedTreeNode.equals(selectedTreeNode.getRoot());
    }

    public GitRepoViewerTopComponent() {
        initComponents();

         _rootNodeIcon = ImageUtilities.image2Icon(ImageUtilities.loadImage("org/chrisle/gitrepoviewer/resources/world.png"));
        _hostTreeRootNode = new DefaultMutableTreeNode(new IconData(_rootNodeIcon, "Repository Hosts - (No hosts added)"));
        _hostTreeModel = new DefaultTreeModel(_hostTreeRootNode);
        _treeNodePopup = new JPopupMenu();

        setName(Bundle.CTL_GitRepoViewerTopComponent());
        setToolTipText(Bundle.HINT_GitRepoViewerTopComponent());

        _popupAddHostAction = new AbstractAction("Add host") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (_treeClickedPath == null) {
                    return;
                }

                AddMethod();
            }
        };
        
        _popupRemoveHostAction = new AbstractAction("Remove host") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (_treeClickedPath == null) {
                    return;
                }

                AddMethod();
            }
        };
    }

    private void AddMethod() {
        AddHostDialog hostsDialog = new AddHostDialog(null, true);
        hostsDialog.setVisible(true);
    }

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _hostScrollPanel = new javax.swing.JScrollPane();
        _hostTree = new javax.swing.JTree();
        _addHost = new javax.swing.JButton();
        _removeHost = new javax.swing.JButton();

        _hostTree.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        _hostScrollPanel.setViewportView(_hostTree);

        _addHost.setMnemonic('o');
        org.openide.awt.Mnemonics.setLocalizedText(_addHost, org.openide.util.NbBundle.getMessage(GitRepoViewerTopComponent.class, "GitRepoViewerTopComponent._addHost.text")); // NOI18N
        _addHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _addHostActionPerformed(evt);
            }
        });

        _removeHost.setMnemonic('m');
        org.openide.awt.Mnemonics.setLocalizedText(_removeHost, org.openide.util.NbBundle.getMessage(GitRepoViewerTopComponent.class, "GitRepoViewerTopComponent._removeHost.text")); // NOI18N
        _removeHost.setEnabled(false);
        _removeHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _removeHostActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(_hostScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(_addHost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(_removeHost, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(_addHost)
                                .addComponent(_removeHost))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_hostScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                        .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void _addHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__addHostActionPerformed
        AddMethod();
    }//GEN-LAST:event__addHostActionPerformed

    private void _removeHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__removeHostActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event__removeHostActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _addHost;
    private javax.swing.JScrollPane _hostScrollPanel;
    private static javax.swing.JTree _hostTree;
    private static javax.swing.JButton _removeHost;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        _hostTree.setModel(_hostTreeModel);
        _hostTree.setRowHeight(20);
        _hostTree.setShowsRootHandles(true);
        _hostTree.add(_treeNodePopup);
        _hostTree.addMouseListener(new PopupTrigger());
        
        IconCellRenderer renderer = new IconCellRenderer();
        _hostTree.setCellRenderer(renderer);

        _popupRemoveHostAction.setEnabled(false);
        
        _treeNodePopup.setInvoker(_hostTree);
        _treeNodePopup.add(_popupAddHostAction);
        _treeNodePopup.add(_popupRemoveHostAction);
    }

    class PopupTrigger extends MouseAdapter {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                int x = e.getX();
                int y = e.getY();
                TreePath path = _hostTree.getPathForLocation(x, y);
                if (path != null) {
                    _hostTree.setSelectionPath(path);
                    _treeNodePopup.show(_hostTree, x, y);
                    _treeClickedPath = path;
                }
            }
        }
    }

    public static void addTreeNode(MutableTreeNode host) {
        final boolean isRootNode = isRootNode((DefaultMutableTreeNode)_hostTree.getLastSelectedPathComponent());
        
        _popupRemoveHostAction.setEnabled(!isRootNode);
        
        _hostTree.addTreeSelectionListener((TreeSelectionEvent e) -> {
            boolean isRootNode1 = isRootNode((DefaultMutableTreeNode)_hostTree.getLastSelectedPathComponent());
            _popupRemoveHostAction.setEnabled(!isRootNode1);
            _removeHost.setEnabled(!isRootNode1);
        });
        
        _hostTreeRootNode.add(host);
        _hostTreeRootNode.setUserObject(new IconData(_rootNodeIcon, "Repository Hosts (" + _hostTreeRootNode.getChildCount() + ")"));

        _hostTree.expandRow(0);
        _hostTree.updateUI();
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
<commit_msg>Added dummy dialog to test the remove host button.<commit_after>package org.chrisle.gitrepoviewer.components;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPopupMenu;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.util.ImageUtilities;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.chrisle.gitrepoviewer.components//RepoViewer//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "GitRepoViewerTopComponent",
        iconBase = "org/chrisle/gitrepoviewer/resources/world.png"
)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "org.chrisle.gitrepoviewer.components.GitRepoViewerTopComponent")
@ActionReferences({
    @ActionReference(path = "Menu/Window" /*, position = 333 */),
    @ActionReference(path = "Shortcuts", name = "DO-G DO-R")
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_GitRepoViewerAction",
        preferredID = "GitRepoViewerTopComponent"
)
@Messages({
    "CTL_GitRepoViewerAction=Git Repository Viewer",
    "CTL_GitRepoViewerTopComponent=Git Repository Viewer",
    "HINT_GitRepoViewerTopComponent=This is a Git Repository Viewer"
})
public final class GitRepoViewerTopComponent extends TopComponent {
    private static Icon _rootNodeIcon;
    private static DefaultMutableTreeNode _hostTreeRootNode;
    private final DefaultTreeModel _hostTreeModel;
    private TreePath _treeClickedPath;
    private static JPopupMenu _treeNodePopup;
    private static AbstractAction _popupAddHostAction;
    private static AbstractAction _popupRemoveHostAction;
    
    private static boolean isRootNode(DefaultMutableTreeNode selectedTreeNode) {
        return selectedTreeNode != null && selectedTreeNode.equals(selectedTreeNode.getRoot());
    }

    private void AddHost() {
        AddHostDialog hostsDialog = new AddHostDialog(null, true);
        hostsDialog.setVisible(true);
    }

    private void RemoveHost() {
        JDialog test = new JDialog();
        test.setVisible(true);
    }

    public GitRepoViewerTopComponent() {
        initComponents();

         _rootNodeIcon = ImageUtilities.image2Icon(ImageUtilities.loadImage("org/chrisle/gitrepoviewer/resources/world.png"));
        _hostTreeRootNode = new DefaultMutableTreeNode(new IconData(_rootNodeIcon, "Repository Hosts - (No hosts added)"));
        _hostTreeModel = new DefaultTreeModel(_hostTreeRootNode);
        _treeNodePopup = new JPopupMenu();

        setName(Bundle.CTL_GitRepoViewerTopComponent());
        setToolTipText(Bundle.HINT_GitRepoViewerTopComponent());

        _popupAddHostAction = new AbstractAction("Add host") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (_treeClickedPath == null) {
                    return;
                }

                AddHost();
            }
        };
        
        _popupRemoveHostAction = new AbstractAction("Remove host") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (_treeClickedPath == null) {
                    return;
                }

                RemoveHost();
            }
        };
    }

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _hostScrollPanel = new javax.swing.JScrollPane();
        _hostTree = new javax.swing.JTree();
        _addHost = new javax.swing.JButton();
        _removeHost = new javax.swing.JButton();

        _hostTree.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        _hostScrollPanel.setViewportView(_hostTree);

        _addHost.setMnemonic('o');
        org.openide.awt.Mnemonics.setLocalizedText(_addHost, org.openide.util.NbBundle.getMessage(GitRepoViewerTopComponent.class, "GitRepoViewerTopComponent._addHost.text")); // NOI18N
        _addHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _addHostActionPerformed(evt);
            }
        });

        _removeHost.setMnemonic('m');
        org.openide.awt.Mnemonics.setLocalizedText(_removeHost, org.openide.util.NbBundle.getMessage(GitRepoViewerTopComponent.class, "GitRepoViewerTopComponent._removeHost.text")); // NOI18N
        _removeHost.setEnabled(false);
        _removeHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _removeHostActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(_hostScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(_addHost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(_removeHost, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(_addHost)
                                .addComponent(_removeHost))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_hostScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                        .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void _addHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__addHostActionPerformed
        AddHost();
    }//GEN-LAST:event__addHostActionPerformed

    private void _removeHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__removeHostActionPerformed
        RemoveHost();
    }//GEN-LAST:event__removeHostActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _addHost;
    private javax.swing.JScrollPane _hostScrollPanel;
    private static javax.swing.JTree _hostTree;
    private static javax.swing.JButton _removeHost;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        _hostTree.setModel(_hostTreeModel);
        _hostTree.setRowHeight(20);
        _hostTree.setShowsRootHandles(true);
        _hostTree.add(_treeNodePopup);
        _hostTree.addMouseListener(new PopupTrigger());
        
        IconCellRenderer renderer = new IconCellRenderer();
        _hostTree.setCellRenderer(renderer);

        _popupRemoveHostAction.setEnabled(false);
        
        _treeNodePopup.setInvoker(_hostTree);
        _treeNodePopup.add(_popupAddHostAction);
        _treeNodePopup.add(_popupRemoveHostAction);
    }

    class PopupTrigger extends MouseAdapter {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                int x = e.getX();
                int y = e.getY();
                TreePath path = _hostTree.getPathForLocation(x, y);
                if (path != null) {
                    _hostTree.setSelectionPath(path);
                    _treeNodePopup.show(_hostTree, x, y);
                    _treeClickedPath = path;
                }
            }
        }
    }

    public static void addTreeNode(MutableTreeNode host) {
        final boolean isRootNode = isRootNode((DefaultMutableTreeNode)_hostTree.getLastSelectedPathComponent());
        
        _popupRemoveHostAction.setEnabled(!isRootNode);
        
        _hostTree.addTreeSelectionListener((TreeSelectionEvent e) -> {
            boolean isRootNode1 = isRootNode((DefaultMutableTreeNode)_hostTree.getLastSelectedPathComponent());
            _popupRemoveHostAction.setEnabled(!isRootNode1);
            _removeHost.setEnabled(!isRootNode1);
        });
        
        _hostTreeRootNode.add(host);
        _hostTreeRootNode.setUserObject(new IconData(_rootNodeIcon, "Repository Hosts (" + _hostTreeRootNode.getChildCount() + ")"));

        _hostTree.expandRow(0);
        _hostTree.updateUI();
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}

{'fraction_non_alphanumeric': 0.09291000238720458, 'fraction_numerical': 0.0023872045834328003, 'mean_word_length': 2.7105403011514615, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7591477', 'n_tokens_mistral': 5736, 'n_tokens_neox': 5664, 'n_words': 953}