<commit_before>package nl.mpi.kinnate.ui.menu;

import java.io.File;
import java.util.HashMap;
import javax.swing.filechooser.FileFilter;
import nl.mpi.arbil.userstorage.SessionStorage;
import nl.mpi.arbil.util.BugCatcherManager;
import nl.mpi.arbil.util.MessageDialogHandler;
import nl.mpi.kinnate.KinTermSavePanel;
import nl.mpi.kinnate.SavePanel;
import nl.mpi.kinnate.export.ExportToR;
import nl.mpi.kinnate.transcoder.DiagramTranscoder;
import nl.mpi.kinnate.ui.ImportSamplesFileMenu;
import nl.mpi.kinnate.ui.KinDiagramPanel;
import nl.mpi.kinnate.ui.window.AbstractDiagramManager;

/**
 * Document : FileMenu
 * Created on : Dec 1, 2011, 4:04:06 PM
 * Author : <NAME>
 */
public class FileMenu extends javax.swing.JMenu {

    private javax.swing.JMenuItem importGedcomUrl;
    private javax.swing.JMenuItem importGedcomFile;
//    private javax.swing.JMenuItem importCsvFile;
    private javax.swing.JMenuItem closeTabMenuItem;
    private javax.swing.JMenuItem entityUploadMenuItem;
    private javax.swing.JMenuItem exitApplication;
    private javax.swing.JMenuItem exportToR;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JMenuItem newDiagramMenuItem;
    private javax.swing.JMenuItem openDiagram;
    private RecentFileMenu recentFileMenu;
    private javax.swing.JMenuItem saveAsDefaultMenuItem;
    private javax.swing.JMenuItem saveDiagram;
    private javax.swing.JMenuItem saveDiagramAs;
    private javax.swing.JMenuItem savePdfMenuItem;
    private AbstractDiagramManager diagramWindowManager;
    private SessionStorage sessionStorage;
    private MessageDialogHandler dialogHandler; //ArbilWindowManager

    public FileMenu(AbstractDiagramManager diagramWindowManager, SessionStorage sessionStorage, MessageDialogHandler dialogHandler) {
        this.diagramWindowManager = diagramWindowManager;
        this.sessionStorage = sessionStorage;
        this.dialogHandler = dialogHandler;
        this.diagramWindowManager = diagramWindowManager;
        importGedcomUrl = new javax.swing.JMenuItem();
        importGedcomFile = new javax.swing.JMenuItem();
//        importCsvFile = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        newDiagramMenuItem = new javax.swing.JMenuItem();
        jMenu3 = new DocumentNewMenu(diagramWindowManager);
        openDiagram = new javax.swing.JMenuItem();
        recentFileMenu = new RecentFileMenu(diagramWindowManager, sessionStorage);
        jMenu1 = new SamplesFileMenu(diagramWindowManager, dialogHandler);
        jMenu2 = new ImportSamplesFileMenu(diagramWindowManager);
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        entityUploadMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        saveDiagram = new javax.swing.JMenuItem();
        saveDiagramAs = new javax.swing.JMenuItem();
        savePdfMenuItem = new javax.swing.JMenuItem();
        exportToR = new javax.swing.JMenuItem();
        closeTabMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        saveAsDefaultMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        exitApplication = new javax.swing.JMenuItem();
        // todo: Ticket #1297 add an import gedcom and csv menu item


        this.setText("File");
        this.addMenuListener(new javax.swing.event.MenuListener() {

            public void menuSelected(javax.swing.event.MenuEvent evt) {
                fileMenuMenuSelected(evt);
            }

            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }

            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
        });
        this.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        newDiagramMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newDiagramMenuItem.setText("New (default diagram)");
        newDiagramMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDiagramMenuItemActionPerformed(evt);
            }
        });
        this.add(newDiagramMenuItem);

        jMenu3.setText("New Diagram of Type");
        this.add(jMenu3);

        openDiagram.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openDiagram.setText("Open Diagram");
        openDiagram.setActionCommand("open");
        openDiagram.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openDiagramActionPerformed(evt);
            }
        });
        this.add(openDiagram);


        this.add(recentFileMenu);

        jMenu1.setText("Open Sample Diagram");
        this.add(jMenu1);

        this.add(jSeparator1);

        importGedcomFile.setText("Import Gedcom / CSV File");
        importGedcomFile.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importGedcomFileActionPerformed(evt);
            }
        });
        this.add(importGedcomFile);

//        importCsvFile.setText("Import CSV File");
//        importCsvFile.addActionListener(new java.awt.event.ActionListener() {
//
//            public void actionPerformed(java.awt.event.ActionEvent evt) {
//                importCsvFileActionPerformed(evt);
//            }
//        });
//        this.add(importCsvFile);

        jMenu2.setText("Import Sample Data");
        this.add(jMenu2);

        importGedcomUrl.setText("Import Gedcom Samples (from internet)");
        importGedcomUrl.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importGedcomUrlActionPerformed(evt);
            }
        });
        importGedcomUrl.setEnabled(false);
        this.add(importGedcomUrl);

        this.add(jSeparator2);

        entityUploadMenuItem.setText("Entity Upload");
        entityUploadMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entityUploadMenuItemActionPerformed(evt);
            }
        });
        this.add(entityUploadMenuItem);
        this.add(jSeparator4);

        saveDiagram.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveDiagram.setText("Save");
        saveDiagram.setActionCommand("save");
        saveDiagram.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDiagramActionPerformed(evt);
            }
        });
        this.add(saveDiagram);

        saveDiagramAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        saveDiagramAs.setText("Save As");
        saveDiagramAs.setActionCommand("saveas");
        saveDiagramAs.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDiagramAsActionPerformed(evt);
            }
        });
        this.add(saveDiagramAs);

        savePdfMenuItem.setText("Export as PDF");
        savePdfMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePdfMenuItemActionPerformed(evt);
            }
        });
        this.add(savePdfMenuItem);

        exportToR.setText("Export to R / SPSS");
        exportToR.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportToRActionPerformed(evt);
            }
        });
        this.add(exportToR);

        closeTabMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        closeTabMenuItem.setText("Close");
        closeTabMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeTabMenuItemActionPerformed(evt);
            }
        });
        this.add(closeTabMenuItem);
        this.add(jSeparator3);

        saveAsDefaultMenuItem.setText("Save as Default Diagram");
        saveAsDefaultMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsDefaultMenuItemActionPerformed(evt);
            }
        });
        this.add(saveAsDefaultMenuItem);
        this.add(jSeparator5);

        exitApplication.setText("Exit");
        exitApplication.setActionCommand("exit");
        exitApplication.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitApplicationActionPerformed(evt);
            }
        });
        this.add(exitApplication);
    }

    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private HashMap<String, FileFilter> getSvgFileFilter() {
        HashMap<String, FileFilter> fileFilterMap = new HashMap<String, FileFilter>(2);
        for (final String[] currentType : new String[][]{{"Kinship Diagram (SVG format)", ".svg"}}) { // "Scalable Vector Graphics (SVG)";
            fileFilterMap.put(currentType[0], new FileFilter() {

                @Override
                public boolean accept(File selectedFile) {
                    final String extensionLowerCase = currentType[1].toLowerCase();
                    return (selectedFile.exists() && (selectedFile.isDirectory() || selectedFile.getName().toLowerCase().endsWith(extensionLowerCase)));
                }

                @Override
                public String getDescription() {
                    return currentType[0];
                }
            });
        }
        return fileFilterMap;
    }

    private void openDiagramActionPerformed(java.awt.event.ActionEvent evt) {
        final File[] selectedFilesArray = dialogHandler.showFileSelectBox("Open Diagram", false, true, getSvgFileFilter(), MessageDialogHandler.DialogueType.open);
        if (selectedFilesArray != null) {
            for (File selectedFile : selectedFilesArray) {
                diagramWindowManager.openDiagram(selectedFile.getName(), selectedFile.toURI(), true);
            }
        }
    }

    private void saveDiagramActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        savePanel.saveToFile();
    }

    private void saveDiagramAsActionPerformed(java.awt.event.ActionEvent evt) {
        final File[] selectedFilesArray = dialogHandler.showFileSelectBox("Open Diagram", false, false, getSvgFileFilter(), MessageDialogHandler.DialogueType.save);
        if (selectedFilesArray != null) {
            for (File selectedFile : selectedFilesArray) {
                if (!selectedFile.getName().toLowerCase().endsWith(".svg")) {
                    selectedFile = new File(selectedFile.getParentFile(), selectedFile.getName() + ".svg");
                }
                int tabIndex = Integer.valueOf(evt.getActionCommand());
                SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
                savePanel.saveToFile(selectedFile);
                RecentFileMenu.addRecentFile(sessionStorage, selectedFile);
                diagramWindowManager.setDiagramTitle(tabIndex, selectedFile.getName());
            }
        }
    }

    private void exitApplicationActionPerformed(java.awt.event.ActionEvent evt) {
        // check that things are saved and ask user if not
        if (diagramWindowManager.offerUserToSaveAll()) {
            System.exit(0);
        }
    }

    private void fileMenuMenuSelected(javax.swing.event.MenuEvent evt) {
        // set the save, save as and close text to include the tab to which the action will occur
        int selectedIndex = diagramWindowManager.getSavePanelIndex();
        SavePanel savePanel = null;
        if (selectedIndex > -1) {
            String currentTabText = diagramWindowManager.getSavePanelTitle(selectedIndex);
            savePanel = diagramWindowManager.getSavePanel(selectedIndex);
            saveDiagramAs.setText("Save As (" + currentTabText + ")");
            saveDiagramAs.setActionCommand(Integer.toString(selectedIndex));
            saveDiagram.setText("Save (" + currentTabText + ")");
            saveDiagram.setActionCommand(Integer.toString(selectedIndex));
            closeTabMenuItem.setText("Close (" + currentTabText + ")");
            closeTabMenuItem.setActionCommand(Integer.toString(selectedIndex));
            saveAsDefaultMenuItem.setText("Set Default Diagram as (" + currentTabText + ")");
            saveAsDefaultMenuItem.setActionCommand(Integer.toString(selectedIndex));
        }
        if (savePanel != null) {
            saveDiagram.setEnabled(savePanel.hasSaveFileName() && savePanel.requiresSave());
            saveDiagramAs.setEnabled(true);
            exportToR.setEnabled(true);
            closeTabMenuItem.setEnabled(true);
            saveAsDefaultMenuItem.setEnabled(true);
            savePdfMenuItem.setEnabled(true);
        } else {
            saveDiagramAs.setEnabled(false);
            saveDiagram.setEnabled(false);
            exportToR.setEnabled(false);
            closeTabMenuItem.setEnabled(false);
            saveAsDefaultMenuItem.setEnabled(false);
            savePdfMenuItem.setEnabled(false);
        }
    }

    private void closeTabMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        String diagramTitle = diagramWindowManager.getSavePanelTitle(tabIndex);
        boolean userCanceled = diagramWindowManager.offerUserToSave(savePanel, diagramTitle);
        if (!userCanceled) {
            diagramWindowManager.closeSavePanel(tabIndex);
        }
    }

    private void newDiagramMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        diagramWindowManager.newDiagram();
    }

    private void importGedcomFileActionPerformed(java.awt.event.ActionEvent evt) {
        HashMap<String, FileFilter> fileFilterMap = new HashMap<String, FileFilter>(2);
        fileFilterMap.put("importfiles", new FileFilter() {

            @Override
            public boolean accept(File selectedFile) {
                if (selectedFile.isDirectory()) {
                    return true;
                }
                final String currentFileName = selectedFile.getName().toLowerCase();
                if (currentFileName.endsWith(".gedcom")) {
                    return true;
                }
                if (currentFileName.endsWith(".txt")) {
                    return true;
                }
                if (currentFileName.endsWith(".csv")) {
                    return true;
                }
                return false;
            }

            @Override
            public String getDescription() {
                return "GEDCOM or CSV Kinship Data";
            }
        });
        File[] importFiles = dialogHandler.showFileSelectBox("Import Kinship Data", false, true, fileFilterMap, MessageDialogHandler.DialogueType.open);
        if (importFiles != null) {
            if (importFiles.length == 0) {
                dialogHandler.addMessageDialogToQueue("No files selected for import", "Import Kinship Data");
            } else {
                for (File importFile : importFiles) {
                    diagramWindowManager.openImportPanel(importFile);
                }
            }
        }
    }

    private void importCsvFileActionPerformed(java.awt.event.ActionEvent evt) {
        importGedcomFileActionPerformed(evt);
    }

    private void importGedcomUrlActionPerformed(java.awt.event.ActionEvent evt) {
        // todo: Ticket #1297 either remove this or change it so it does not open so many tabs / windows
        String[] importList = new String[]{"http://gedcomlibrary.com/gedcoms.html",
            "http://GedcomLibrary.com/gedcoms/gl120365.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120366.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120367.ged", //	<NAME>
            // New England Genealogical Detective
            "http://GedcomLibrary.com/gedcoms/gl120368.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120369.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120370.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120371.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120372.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/liverpool.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/misc2a.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/myline.ged", //	<NAME>te

            // also look into http://gedcomlibrary.com/list.html for sample files
            "http://gedcomlibrary.com/gedcoms/gl120368.ged", //
            "http://GedcomLibrary.com/gedcoms/gl120367.ged", //
            "http://GedcomLibrary.com/gedcoms/liverpool.ged", //
            "http://GedcomLibrary.com/gedcoms/misc2a.ged", //
            "http://GedcomLibrary.com/gedcoms/gl120372.ged"};
        for (String importUrlString : importList) {
            diagramWindowManager.openImportPanel(importUrlString);
        }
    }

    private void savePdfMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        // todo: implement pdf export
        // todo: add a file select here...
        new DiagramTranscoder().saveAsPdf(diagramWindowManager.getCurrentSavePanel());
        new DiagramTranscoder().saveAsJpg(diagramWindowManager.getCurrentSavePanel());
    }

    private void exportToRActionPerformed(java.awt.event.ActionEvent evt) {

//    public KinTermSavePanel getKinTermPanel() {
//        Object selectedComponent = jTabbedPane1.getComponentAt(jTabbedPane1.getSelectedIndex());
//        KinTermSavePanel kinTermSavePanel = null;
        SavePanel currentSavePanel = diagramWindowManager.getCurrentSavePanel();
        if (currentSavePanel instanceof KinTermSavePanel) {
            new ExportToR(sessionStorage, dialogHandler, BugCatcherManager.getBugCatcher()).doExport(this, (KinTermSavePanel) currentSavePanel);
        }
    }

    private void entityUploadMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        diagramWindowManager.openEntityUploadPanel();
    }

    private void saveAsDefaultMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        savePanel.saveToFile(KinDiagramPanel.getDefaultDiagramFile(sessionStorage));
    }
}<commit_msg>Corrected issues with the R / SPSS export. Updated the export file select to filter by tab format. If the selected file does not have the tab suffix it is appended to the file name. refs #1721<commit_after>package nl.mpi.kinnate.ui.menu;

import java.io.File;
import java.util.HashMap;
import javax.swing.filechooser.FileFilter;
import nl.mpi.arbil.userstorage.SessionStorage;
import nl.mpi.arbil.util.BugCatcherManager;
import nl.mpi.arbil.util.MessageDialogHandler;
import nl.mpi.kinnate.KinTermSavePanel;
import nl.mpi.kinnate.SavePanel;
import nl.mpi.kinnate.export.ExportToR;
import nl.mpi.kinnate.transcoder.DiagramTranscoder;
import nl.mpi.kinnate.ui.ImportSamplesFileMenu;
import nl.mpi.kinnate.ui.KinDiagramPanel;
import nl.mpi.kinnate.ui.window.AbstractDiagramManager;

/**
 * Document : FileMenu
 * Created on : Dec 1, 2011, 4:04:06 PM
 * Author : <NAME>
 */
public class FileMenu extends javax.swing.JMenu {

    private javax.swing.JMenuItem importGedcomUrl;
    private javax.swing.JMenuItem importGedcomFile;
//    private javax.swing.JMenuItem importCsvFile;
    private javax.swing.JMenuItem closeTabMenuItem;
    private javax.swing.JMenuItem entityUploadMenuItem;
    private javax.swing.JMenuItem exitApplication;
    private javax.swing.JMenuItem exportToR;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JMenuItem newDiagramMenuItem;
    private javax.swing.JMenuItem openDiagram;
    private RecentFileMenu recentFileMenu;
    private javax.swing.JMenuItem saveAsDefaultMenuItem;
    private javax.swing.JMenuItem saveDiagram;
    private javax.swing.JMenuItem saveDiagramAs;
    private javax.swing.JMenuItem savePdfMenuItem;
    private AbstractDiagramManager diagramWindowManager;
    private SessionStorage sessionStorage;
    private MessageDialogHandler dialogHandler; //ArbilWindowManager

    public FileMenu(AbstractDiagramManager diagramWindowManager, SessionStorage sessionStorage, MessageDialogHandler dialogHandler) {
        this.diagramWindowManager = diagramWindowManager;
        this.sessionStorage = sessionStorage;
        this.dialogHandler = dialogHandler;
        this.diagramWindowManager = diagramWindowManager;
        importGedcomUrl = new javax.swing.JMenuItem();
        importGedcomFile = new javax.swing.JMenuItem();
//        importCsvFile = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        newDiagramMenuItem = new javax.swing.JMenuItem();
        jMenu3 = new DocumentNewMenu(diagramWindowManager);
        openDiagram = new javax.swing.JMenuItem();
        recentFileMenu = new RecentFileMenu(diagramWindowManager, sessionStorage);
        jMenu1 = new SamplesFileMenu(diagramWindowManager, dialogHandler);
        jMenu2 = new ImportSamplesFileMenu(diagramWindowManager);
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        entityUploadMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        saveDiagram = new javax.swing.JMenuItem();
        saveDiagramAs = new javax.swing.JMenuItem();
        savePdfMenuItem = new javax.swing.JMenuItem();
        exportToR = new javax.swing.JMenuItem();
        closeTabMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        saveAsDefaultMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        exitApplication = new javax.swing.JMenuItem();
        // todo: Ticket #1297 add an import gedcom and csv menu item


        this.setText("File");
        this.addMenuListener(new javax.swing.event.MenuListener() {

            public void menuSelected(javax.swing.event.MenuEvent evt) {
                fileMenuMenuSelected(evt);
            }

            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }

            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
        });
        this.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        newDiagramMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newDiagramMenuItem.setText("New (default diagram)");
        newDiagramMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDiagramMenuItemActionPerformed(evt);
            }
        });
        this.add(newDiagramMenuItem);

        jMenu3.setText("New Diagram of Type");
        this.add(jMenu3);

        openDiagram.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openDiagram.setText("Open Diagram");
        openDiagram.setActionCommand("open");
        openDiagram.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openDiagramActionPerformed(evt);
            }
        });
        this.add(openDiagram);


        this.add(recentFileMenu);

        jMenu1.setText("Open Sample Diagram");
        this.add(jMenu1);

        this.add(jSeparator1);

        importGedcomFile.setText("Import Gedcom / CSV File");
        importGedcomFile.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importGedcomFileActionPerformed(evt);
            }
        });
        this.add(importGedcomFile);

//        importCsvFile.setText("Import CSV File");
//        importCsvFile.addActionListener(new java.awt.event.ActionListener() {
//
//            public void actionPerformed(java.awt.event.ActionEvent evt) {
//                importCsvFileActionPerformed(evt);
//            }
//        });
//        this.add(importCsvFile);

        jMenu2.setText("Import Sample Data");
        this.add(jMenu2);

        importGedcomUrl.setText("Import Gedcom Samples (from internet)");
        importGedcomUrl.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importGedcomUrlActionPerformed(evt);
            }
        });
        importGedcomUrl.setEnabled(false);
        this.add(importGedcomUrl);

        this.add(jSeparator2);

        entityUploadMenuItem.setText("Entity Upload");
        entityUploadMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entityUploadMenuItemActionPerformed(evt);
            }
        });
        this.add(entityUploadMenuItem);
        this.add(jSeparator4);

        saveDiagram.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveDiagram.setText("Save");
        saveDiagram.setActionCommand("save");
        saveDiagram.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDiagramActionPerformed(evt);
            }
        });
        this.add(saveDiagram);

        saveDiagramAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        saveDiagramAs.setText("Save As");
        saveDiagramAs.setActionCommand("saveas");
        saveDiagramAs.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDiagramAsActionPerformed(evt);
            }
        });
        this.add(saveDiagramAs);

        savePdfMenuItem.setText("Export as PDF");
        savePdfMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePdfMenuItemActionPerformed(evt);
            }
        });
        this.add(savePdfMenuItem);

        exportToR.setText("Export for R / SPSS");
        exportToR.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportToRActionPerformed(evt);
            }
        });
        this.add(exportToR);

        closeTabMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        closeTabMenuItem.setText("Close");
        closeTabMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeTabMenuItemActionPerformed(evt);
            }
        });
        this.add(closeTabMenuItem);
        this.add(jSeparator3);

        saveAsDefaultMenuItem.setText("Save as Default Diagram");
        saveAsDefaultMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsDefaultMenuItemActionPerformed(evt);
            }
        });
        this.add(saveAsDefaultMenuItem);
        this.add(jSeparator5);

        exitApplication.setText("Exit");
        exitApplication.setActionCommand("exit");
        exitApplication.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitApplicationActionPerformed(evt);
            }
        });
        this.add(exitApplication);
    }

    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private HashMap<String, FileFilter> getSvgFileFilter() {
        HashMap<String, FileFilter> fileFilterMap = new HashMap<String, FileFilter>(2);
        for (final String[] currentType : new String[][]{{"Kinship Diagram (SVG format)", ".svg"}}) { // "Scalable Vector Graphics (SVG)";
            fileFilterMap.put(currentType[0], new FileFilter() {

                @Override
                public boolean accept(File selectedFile) {
                    final String extensionLowerCase = currentType[1].toLowerCase();
                    return (selectedFile.exists() && (selectedFile.isDirectory() || selectedFile.getName().toLowerCase().endsWith(extensionLowerCase)));
                }

                @Override
                public String getDescription() {
                    return currentType[0];
                }
            });
        }
        return fileFilterMap;
    }

    private void openDiagramActionPerformed(java.awt.event.ActionEvent evt) {
        final File[] selectedFilesArray = dialogHandler.showFileSelectBox("Open Diagram", false, true, getSvgFileFilter(), MessageDialogHandler.DialogueType.open);
        if (selectedFilesArray != null) {
            for (File selectedFile : selectedFilesArray) {
                diagramWindowManager.openDiagram(selectedFile.getName(), selectedFile.toURI(), true);
            }
        }
    }

    private void saveDiagramActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        savePanel.saveToFile();
    }

    private void saveDiagramAsActionPerformed(java.awt.event.ActionEvent evt) {
        final File[] selectedFilesArray = dialogHandler.showFileSelectBox("Save Diagram As", false, false, getSvgFileFilter(), MessageDialogHandler.DialogueType.save);
        if (selectedFilesArray != null) {
            for (File selectedFile : selectedFilesArray) {
                if (!selectedFile.getName().toLowerCase().endsWith(".svg")) {
                    selectedFile = new File(selectedFile.getParentFile(), selectedFile.getName() + ".svg");
                }
                int tabIndex = Integer.valueOf(evt.getActionCommand());
                SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
                savePanel.saveToFile(selectedFile);
                RecentFileMenu.addRecentFile(sessionStorage, selectedFile);
                diagramWindowManager.setDiagramTitle(tabIndex, selectedFile.getName());
            }
        }
    }

    private void exitApplicationActionPerformed(java.awt.event.ActionEvent evt) {
        // check that things are saved and ask user if not
        if (diagramWindowManager.offerUserToSaveAll()) {
            System.exit(0);
        }
    }

    private void fileMenuMenuSelected(javax.swing.event.MenuEvent evt) {
        // set the save, save as and close text to include the tab to which the action will occur
        int selectedIndex = diagramWindowManager.getSavePanelIndex();
        SavePanel savePanel = null;
        if (selectedIndex > -1) {
            String currentTabText = diagramWindowManager.getSavePanelTitle(selectedIndex);
            savePanel = diagramWindowManager.getSavePanel(selectedIndex);
            saveDiagramAs.setText("Save As (" + currentTabText + ")");
            saveDiagramAs.setActionCommand(Integer.toString(selectedIndex));
            saveDiagram.setText("Save (" + currentTabText + ")");
            saveDiagram.setActionCommand(Integer.toString(selectedIndex));
            closeTabMenuItem.setText("Close (" + currentTabText + ")");
            closeTabMenuItem.setActionCommand(Integer.toString(selectedIndex));
            saveAsDefaultMenuItem.setText("Set Default Diagram as (" + currentTabText + ")");
            saveAsDefaultMenuItem.setActionCommand(Integer.toString(selectedIndex));
        }
        if (savePanel != null) {
            saveDiagram.setEnabled(savePanel.hasSaveFileName() && savePanel.requiresSave());
            saveDiagramAs.setEnabled(true);
            exportToR.setEnabled(true);
            closeTabMenuItem.setEnabled(true);
            saveAsDefaultMenuItem.setEnabled(true);
            savePdfMenuItem.setEnabled(true);
        } else {
            saveDiagramAs.setEnabled(false);
            saveDiagram.setEnabled(false);
            exportToR.setEnabled(false);
            closeTabMenuItem.setEnabled(false);
            saveAsDefaultMenuItem.setEnabled(false);
            savePdfMenuItem.setEnabled(false);
        }
    }

    private void closeTabMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        String diagramTitle = diagramWindowManager.getSavePanelTitle(tabIndex);
        boolean userCanceled = diagramWindowManager.offerUserToSave(savePanel, diagramTitle);
        if (!userCanceled) {
            diagramWindowManager.closeSavePanel(tabIndex);
        }
    }

    private void newDiagramMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        diagramWindowManager.newDiagram();
    }

    private void importGedcomFileActionPerformed(java.awt.event.ActionEvent evt) {
        HashMap<String, FileFilter> fileFilterMap = new HashMap<String, FileFilter>(2);
        fileFilterMap.put("importfiles", new FileFilter() {

            @Override
            public boolean accept(File selectedFile) {
                if (selectedFile.isDirectory()) {
                    return true;
                }
                final String currentFileName = selectedFile.getName().toLowerCase();
                if (currentFileName.endsWith(".gedcom")) {
                    return true;
                }
                if (currentFileName.endsWith(".txt")) {
                    return true;
                }
                if (currentFileName.endsWith(".csv")) {
                    return true;
                }
                return false;
            }

            @Override
            public String getDescription() {
                return "GEDCOM or CSV Kinship Data";
            }
        });
        File[] importFiles = dialogHandler.showFileSelectBox("Import Kinship Data", false, true, fileFilterMap, MessageDialogHandler.DialogueType.open);
        if (importFiles != null) {
            if (importFiles.length == 0) {
                dialogHandler.addMessageDialogToQueue("No files selected for import", "Import Kinship Data");
            } else {
                for (File importFile : importFiles) {
                    diagramWindowManager.openImportPanel(importFile);
                }
            }
        }
    }

    private void importCsvFileActionPerformed(java.awt.event.ActionEvent evt) {
        importGedcomFileActionPerformed(evt);
    }

    private void importGedcomUrlActionPerformed(java.awt.event.ActionEvent evt) {
        // todo: Ticket #1297 either remove this or change it so it does not open so many tabs / windows
        String[] importList = new String[]{"http://gedcomlibrary.com/gedcoms.html",
            "http://GedcomLibrary.com/gedcoms/gl120365.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120366.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120367.ged", //	<NAME>
            // New England Genealogical Detective
            "http://GedcomLibrary.com/gedcoms/gl120368.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120369.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120370.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120371.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/gl120372.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/liverpool.ged", //	<NAME>
            "http://GedcomLibrary.com/gedcoms/misc2a.ged", //	<NAME>te
            "http://GedcomLibrary.com/gedcoms/myline.ged", //	<NAME>

            // also look into http://gedcomlibrary.com/list.html for sample files
            "http://gedcomlibrary.com/gedcoms/gl120368.ged", //
            "http://GedcomLibrary.com/gedcoms/gl120367.ged", //
            "http://GedcomLibrary.com/gedcoms/liverpool.ged", //
            "http://GedcomLibrary.com/gedcoms/misc2a.ged", //
            "http://GedcomLibrary.com/gedcoms/gl120372.ged"};
        for (String importUrlString : importList) {
            diagramWindowManager.openImportPanel(importUrlString);
        }
    }

    private void savePdfMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        // todo: implement pdf export
        // todo: add a file select here...
        new DiagramTranscoder().saveAsPdf(diagramWindowManager.getCurrentSavePanel());
        new DiagramTranscoder().saveAsJpg(diagramWindowManager.getCurrentSavePanel());
    }

    private void exportToRActionPerformed(java.awt.event.ActionEvent evt) {
        SavePanel currentSavePanel = diagramWindowManager.getCurrentSavePanel();

        HashMap<String, FileFilter> fileFilterMap = new HashMap<String, FileFilter>(2);
        for (final String[] currentType : new String[][]{{"Data Frame Tab-separated Values", ".tab"}}) { // "Data Frame (CSV)"
            fileFilterMap.put(currentType[0], new FileFilter() {

                @Override
                public boolean accept(File selectedFile) {
                    final String extensionLowerCase = currentType[1].toLowerCase();
                    return (selectedFile.exists() && (selectedFile.isDirectory() || selectedFile.getName().toLowerCase().endsWith(extensionLowerCase)));
                }

                @Override
                public String getDescription() {
                    return currentType[0];
                }
            });
        }
        final File[] selectedFilesArray = dialogHandler.showFileSelectBox("Export Tab-separated Values", false, false, fileFilterMap, MessageDialogHandler.DialogueType.save);
        if (selectedFilesArray != null) {
            for (File selectedFile : selectedFilesArray) {
                if (!selectedFile.getName().toLowerCase().endsWith(".tab")) {
                    selectedFile = new File(selectedFile.getParentFile(), selectedFile.getName() + ".tab");
                }
                new ExportToR(sessionStorage, dialogHandler).doExport(this, currentSavePanel, selectedFile);
            }
        }
    }

    private void entityUploadMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        diagramWindowManager.openEntityUploadPanel();
    }

    private void saveAsDefaultMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        int tabIndex = Integer.valueOf(evt.getActionCommand());
        SavePanel savePanel = diagramWindowManager.getSavePanel(tabIndex);
        savePanel.saveToFile(KinDiagramPanel.getDefaultDiagramFile(sessionStorage));
    }
}
{'fraction_non_alphanumeric': 0.10192550198855369, 'fraction_numerical': 0.006111165001455039, 'mean_word_length': 2.6596556620518284, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401153', 'n_tokens_mistral': 11325, 'n_tokens_neox': 11175, 'n_words': 1817}