<commit_before>package me.legrange.panstamp.gui.model.tree;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import me.legrange.panstamp.Endpoint;
import me.legrange.panstamp.GatewayException;
import me.legrange.panstamp.gui.network.NetworkAddDialog;
import me.legrange.panstamp.gui.PanStampParamDialog;
import me.legrange.panstamp.gui.PanStampSettingsDialog;
import me.legrange.panstamp.gui.chart.ChartFactory;
import me.legrange.panstamp.gui.model.DataModel;
import me.legrange.panstamp.gui.model.Format;
import me.legrange.panstamp.json.JsonException;
import me.legrange.panstamp.json.JsonLoad;
import me.legrange.panstamp.json.JsonStore;
import me.legrange.swap.SWAPException;

/**
 *
 * @author gideon
 */
public class NetworkTreeNodeRenderer extends DefaultTreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof NetworkTreeNode) {
            try {
                NetworkTreeNode node = (NetworkTreeNode) value;
                Component com;
                switch (node.getType()) {
                    case ENDPOINT:
                        com = renderEndpoint((EndpointNode) node);
                        break;
                    case REGISTER:
                        com = renderRegister((RegisterNode) node);
                        break;
                    case PANSTAMP:
                        com = renderPanStamp((PanStampNode) node);
                        break;
                    case GATEWAY:
                        com = renderGateway((GatewayNode) node);
                        break;
                    case WORLD:
                        com = renderWorld((WorldNode) node);
                        break;
                    default:
                        com = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                        break;
                }
                JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
                panel.add(com);
                panel.setBackground(sel ? backgroundSelectionColor : tree.getBackground());
                com.setForeground(sel ? textSelectionColor : textNonSelectionColor);
                return panel;
            } catch (GatewayException ex) {
                Logger.getLogger(NetworkTreeNodeRenderer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    }

    public JPopupMenu getPopupMenu(TreePath path) {
        NetworkTreeNode node = (NetworkTreeNode) path.getLastPathComponent();
        switch (node.getType()) {
            case WORLD:
                return getWorldPopupMenu((WorldNode) node);
            case GATEWAY:
                return getGatewayPopupMenu((GatewayNode) node);
            case PANSTAMP:
                return getPanStampPopupMenu((PanStampNode) node);
            case ENDPOINT:
                return getEndpointPopupMenu((EndpointNode) node);
            default:
                return null;
        }

    }

    public NetworkTreeNodeRenderer(DataModel model) {
        this.model = model;
    }

    private JPopupMenu getWorldPopupMenu(final WorldNode wn) {
        JPopupMenu menu = popupMenus.get(wn);
        if (menu == null) {
            menu = new JPopupMenu(wn.toString());
            final JMenuItem addSerialItem = new JMenuItem("Add network...");
            addSerialItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    new NetworkAddDialog(null, model).setVisible(true);
                }
            });
            menu.add(addSerialItem);
        }
        return menu;
    }

    private JPopupMenu getGatewayPopupMenu(final GatewayNode gn) {
        JPopupMenu menu = popupMenus.get(gn);
        if (menu == null) {
            
            menu = new JPopupMenu(gn.toString());
            
            final JMenuItem exportItem = new JMenuItem("Export...");
            exportItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        try {
                            //System.out.println(JsonStore.networkAsJson(gn.getGateway()));
                            new JsonLoad().loadJson(JsonStore.networkAsJson(gn.getGateway()));
                            /// put code here to select file/place to export to. 
                        } catch (JsonException ex) {
                            Logger.getLogger(NetworkTreeNodeRenderer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (SWAPException ex) {
                        Logger.getLogger(NetworkTreeNodeRenderer.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (GatewayException ex) {
                        Logger.getLogger(NetworkTreeNodeRenderer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            menu.add(exportItem);
            popupMenus.put(gn, menu);
        }
        return menu;
    }

    private JPopupMenu getPanStampPopupMenu(final PanStampNode psn) {
        JPopupMenu menu = popupMenus.get(psn);
        if (menu == null) {

            menu = new JPopupMenu(psn.toString());

            final JMenuItem settingsItem = new JMenuItem("Settings...");
            settingsItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    new PanStampSettingsDialog(null, model, psn.getPanStamp()).setVisible(true);
                }
            });
            menu.add(settingsItem);
<commit_msg>Removed JSON code, working on that in branch<commit_after>package me.legrange.panstamp.gui.model.tree;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import me.legrange.panstamp.Endpoint;
import me.legrange.panstamp.GatewayException;
import me.legrange.panstamp.gui.network.NetworkAddDialog;
import me.legrange.panstamp.gui.PanStampParamDialog;
import me.legrange.panstamp.gui.PanStampSettingsDialog;
import me.legrange.panstamp.gui.chart.ChartFactory;
import me.legrange.panstamp.gui.model.DataModel;
import me.legrange.panstamp.gui.model.Format;

/**
 *
 * @author gideon
 */
public class NetworkTreeNodeRenderer extends DefaultTreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof NetworkTreeNode) {
            try {
                NetworkTreeNode node = (NetworkTreeNode) value;
                Component com;
                switch (node.getType()) {
                    case ENDPOINT:
                        com = renderEndpoint((EndpointNode) node);
                        break;
                    case REGISTER:
                        com = renderRegister((RegisterNode) node);
                        break;
                    case PANSTAMP:
                        com = renderPanStamp((PanStampNode) node);
                        break;
                    case GATEWAY:
                        com = renderGateway((GatewayNode) node);
                        break;
                    case WORLD:
                        com = renderWorld((WorldNode) node);
                        break;
                    default:
                        com = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                        break;
                }
                JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
                panel.add(com);
                panel.setBackground(sel ? backgroundSelectionColor : tree.getBackground());
                com.setForeground(sel ? textSelectionColor : textNonSelectionColor);
                return panel;
            } catch (GatewayException ex) {
                Logger.getLogger(NetworkTreeNodeRenderer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    }

    public JPopupMenu getPopupMenu(TreePath path) {
        NetworkTreeNode node = (NetworkTreeNode) path.getLastPathComponent();
        switch (node.getType()) {
            case WORLD:
                return getWorldPopupMenu((WorldNode) node);
            case GATEWAY:
                return getGatewayPopupMenu((GatewayNode) node);
            case PANSTAMP:
                return getPanStampPopupMenu((PanStampNode) node);
            case ENDPOINT:
                return getEndpointPopupMenu((EndpointNode) node);
            default:
                return null;
        }

    }

    public NetworkTreeNodeRenderer(DataModel model) {
        this.model = model;
    }

    private JPopupMenu getWorldPopupMenu(final WorldNode wn) {
        JPopupMenu menu = popupMenus.get(wn);
        if (menu == null) {
            menu = new JPopupMenu(wn.toString());
            final JMenuItem addSerialItem = new JMenuItem("Add network...");
            addSerialItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    new NetworkAddDialog(null, model).setVisible(true);
                }
            });
            menu.add(addSerialItem);
        }
        return menu;
    }

    private JPopupMenu getGatewayPopupMenu(final GatewayNode gn) {
        JPopupMenu menu = popupMenus.get(gn);
        if (menu == null) {
            
            menu = new JPopupMenu(gn.toString());
            
            final JMenuItem exportItem = new JMenuItem("Export...");
            exportItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                }
            });
            menu.add(exportItem);
            popupMenus.put(gn, menu);
        }
        return menu;
    }

    private JPopupMenu getPanStampPopupMenu(final PanStampNode psn) {
        JPopupMenu menu = popupMenus.get(psn);
        if (menu == null) {

            menu = new JPopupMenu(psn.toString());

            final JMenuItem settingsItem = new JMenuItem("Settings...");
            settingsItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    new PanStampSettingsDialog(null, model, psn.getPanStamp()).setVisible(true);
                }
            });
            menu.add(settingsItem);

{'fraction_non_alphanumeric': 0.08974464920254524, 'fraction_numerical': 0.0003305511941161887, 'mean_word_length': 1.9402332361516035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5328479', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3111, 'n_words': 651}