<commit_before>package nl.fontys.sofa.limo.view.topcomponent;

import java.awt.BorderLayout;
import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import nl.fontys.sofa.limo.api.exception.ServiceNotFoundException;
import nl.fontys.sofa.limo.domain.component.SupplyChain;
import nl.fontys.sofa.limo.view.chain.ChainGraphScene;
import nl.fontys.sofa.limo.view.chain.ChainGraphSceneImpl;
import nl.fontys.sofa.limo.view.chain.ChainPaletteFactory;
import nl.fontys.sofa.limo.view.chain.ChainToolbar;
import nl.fontys.sofa.limo.view.node.bean.AbstractBeanNode;
import nl.fontys.sofa.limo.view.util.LIMOResourceBundle;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.TopComponent;

/**
 * Top component which loads an existing
 * {@link nl.fontys.sofa.limo.domain.component.SupplyChain} and displays a
 * GraphScene and Palette to build a chain with.
 */
public final class ChainLoaderTopComponent extends TopComponent implements
        DynamicExplorerManagerProvider {

    private ExplorerManager em = new ExplorerManager();
    private ChainGraphScene graphScene;

    /**
     * Constructor creates a new ChainLoaderTopcomponent.
     * @param chainFile the file where the supplychain is located.
     */
    public ChainLoaderTopComponent(File chainFile) {
        initComponents();

        SupplyChain supplyChain = SupplyChain.createFromFile(chainFile);
        supplyChain.setName(chainFile.getName());
        supplyChain.setFilepath(chainFile.getParent());
        
        setName(supplyChain.getName());
        initCustomComponents(supplyChain);

        try {
            SavableComponent savable = new SavableComponent(graphScene.getChainBuilder());

            Lookup paletteLookup = Lookups.singleton(ChainPaletteFactory.createPalette());
            Lookup nodeLookup = ExplorerUtils.createLookup(em, getActionMap());
            Lookup graphLookup = Lookups.singleton(graphScene);
            Lookup savableLookup = Lookups.singleton(savable);
            ProxyLookup pl = new ProxyLookup(paletteLookup, nodeLookup, graphLookup, savableLookup);
            associateLookup(pl);
        } catch (ServiceNotFoundException ex) {
            Exceptions.printStackTrace(ex);
            NotifyDescriptor d = new NotifyDescriptor.Message(LIMOResourceBundle.getString("LIMO_ERROR"),
                    NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(d);
        }
    }

    /**
     * Initialize the custom components of this TopComponent.
     *
     * @param supplyChain the supplychain which is loaded.
     */
    void initCustomComponents(SupplyChain supplyChain) {
        setLayout(new BorderLayout());
        try {
            ChainToolbar toolbar = new ChainToolbar();
            add(toolbar, BorderLayout.NORTH);

            graphScene = new ChainGraphSceneImpl(this, supplyChain);
            JScrollPane shapePane = new JScrollPane();
            JComponent createView = graphScene.createView();
            createView.putClientProperty("print.printable", Boolean.TRUE);
            createView.putClientProperty("print.name", LIMOResourceBundle.getString("SUPPLY_CHAIN") + ": " + supplyChain.getName());
            shapePane.setViewportView(createView);

            add(shapePane, BorderLayout.CENTER);
            add(graphScene.createSatelliteView(), BorderLayout.SOUTH);
        } catch (IOException | IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }

    @Override
    public void setRootContext(AbstractBeanNode node) {
        em.setRootContext(node);
        try {
            em.setSelectedNodes(new Node[]{node});
        } catch (PropertyVetoException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    /**<commit_msg>A dialog is now displayed when trying to close a supplychain tab (this worked already for creating new supply chains, not for loading existing ones)<commit_after>package nl.fontys.sofa.limo.view.topcomponent;

import java.awt.BorderLayout;
import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import nl.fontys.sofa.limo.api.exception.ServiceNotFoundException;
import nl.fontys.sofa.limo.domain.component.SupplyChain;
import nl.fontys.sofa.limo.view.chain.ChainGraphScene;
import nl.fontys.sofa.limo.view.chain.ChainGraphSceneImpl;
import nl.fontys.sofa.limo.view.chain.ChainPaletteFactory;
import nl.fontys.sofa.limo.view.chain.ChainToolbar;
import nl.fontys.sofa.limo.view.node.bean.AbstractBeanNode;
import nl.fontys.sofa.limo.view.util.LIMOResourceBundle;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.TopComponent;

/**
 * Top component which loads an existing
 * {@link nl.fontys.sofa.limo.domain.component.SupplyChain} and displays a
 * GraphScene and Palette to build a chain with.
 */
public final class ChainLoaderTopComponent extends TopComponent implements
        DynamicExplorerManagerProvider {

    private ExplorerManager em = new ExplorerManager();
    private ChainGraphScene graphScene;
    private SavableComponent savable;

    /**
     * Constructor creates a new ChainLoaderTopcomponent.
     * @param chainFile the file where the supplychain is located.
     */
    public ChainLoaderTopComponent(File chainFile) {
        initComponents();

        SupplyChain supplyChain = SupplyChain.createFromFile(chainFile);
        supplyChain.setName(chainFile.getName());
        supplyChain.setFilepath(chainFile.getParent());
        
        setName(supplyChain.getName());
        initCustomComponents(supplyChain);

        try {
            savable = new SavableComponent(graphScene.getChainBuilder());

            Lookup paletteLookup = Lookups.singleton(ChainPaletteFactory.createPalette());
            Lookup nodeLookup = ExplorerUtils.createLookup(em, getActionMap());
            Lookup graphLookup = Lookups.singleton(graphScene);
            Lookup savableLookup = Lookups.singleton(savable);
            ProxyLookup pl = new ProxyLookup(paletteLookup, nodeLookup, graphLookup, savableLookup);
            associateLookup(pl);
        } catch (ServiceNotFoundException ex) {
            Exceptions.printStackTrace(ex);
            NotifyDescriptor d = new NotifyDescriptor.Message(LIMOResourceBundle.getString("LIMO_ERROR"),
                    NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(d);
        }
    }

    /**
     * Initialize the custom components of this TopComponent.
     *
     * @param supplyChain the supplychain which is loaded.
     */
    void initCustomComponents(SupplyChain supplyChain) {
        setLayout(new BorderLayout());
        try {
            ChainToolbar toolbar = new ChainToolbar();
            add(toolbar, BorderLayout.NORTH);

            graphScene = new ChainGraphSceneImpl(this, supplyChain);
            JScrollPane shapePane = new JScrollPane();
            JComponent createView = graphScene.createView();
            createView.putClientProperty("print.printable", Boolean.TRUE);
            createView.putClientProperty("print.name", LIMOResourceBundle.getString("SUPPLY_CHAIN") + ": " + supplyChain.getName());
            shapePane.setViewportView(createView);

            add(shapePane, BorderLayout.CENTER);
            add(graphScene.createSatelliteView(), BorderLayout.SOUTH);
        } catch (IOException | IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    
    /**
     * Check if the TopComponent is ready to close. In this case, the user is
     * prompted with a question to save the supply chain or discard it.
     *
     * @return true if the TopComponent should close.
     */
    @Override
    public boolean canClose() {
        DialogDescriptor dialogDescriptor = new DialogDescriptor("Do you want to save the " + graphScene.getSupplyChain().getName()
                + " supply chain?", "Save the supply chain");

        dialogDescriptor.setMessageType(DialogDescriptor.QUESTION_MESSAGE);
        dialogDescriptor.setOptions(new Object[]{"Save", "Discard"});
        Object retval = DialogDisplayer.getDefault().notify(dialogDescriptor);
        if (retval.equals("Save")) {
            try {
                savable.handleSave(); //Try to save the supply chain
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
                return false; //The supply chain window cannot be closed because an exception is trown while saving
            }
        } else if (retval.equals("Discard")) {
            savable.unregisterChainBuilder(); //Unregister supply chain from registry so it is not shown in the 'save dialog'
        }
        return true; //The supply chain window can now be closed
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }

    @Override
    public void setRootContext(AbstractBeanNode node) {
        em.setRootContext(node);
        try {
            em.setSelectedNodes(new Node[]{node});
        } catch (PropertyVetoException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.09215219976218787, 'fraction_numerical': 0.0, 'mean_word_length': 3.1931865392604903, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6024308', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2708, 'n_words': 585}