<commit_before>package pipe.handlers;

import pipe.controllers.PetriNetController;
import pipe.controllers.PipeApplicationController;
import pipe.gui.*;
import pipe.historyActions.AddPetriNetObject;
import pipe.historyActions.HistoryManager;
import pipe.models.*;
import pipe.utilities.Copier;
import pipe.views.*;
import pipe.views.TokenView;
import pipe.views.MarkingView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Class used to implement methods corresponding to mouse events on places.
 *
 * @author <NAME> - changed the mousePressed method to only allow the
 *         creation of an arc by left-clicking
 * @author <NAME> - modified the handler which was causing the
 *         null pointer exceptions and incorrect petri nets xml representation.
 */
public class PlaceTransitionObjectHandler<T extends Connectable, V extends ConnectableView>
        extends PetriNetObjectHandler<T, V> {
    // STATIC ATTRIBUTES AND METHODS
    private static boolean mouseDown = false;

    public static boolean isMouseDown() {
        return mouseDown;
    }


    private ArcKeyboardEventHandler keyHandler = null;

    // constructor passing in all required objects
    PlaceTransitionObjectHandler(V view, Container contentpane,
                                 T obj, PetriNetController controller) {
        super(view, contentpane, obj, controller);
        enablePopup = true;
    }


    private void createArc(ArcView newArcView, ConnectableView source) {
        TokenView tokenView = ApplicationSettings.getApplicationView().getCurrentPetriNetView().getActiveTokenView();
        MarkingView markingView = new MarkingView(tokenView, 1 + "");
        markingView.addObserver(newArcView);  // Steve Doubleday:  ArcView tracks updates to MarkingView

        LinkedList<MarkingView> markingViews = new LinkedList<MarkingView>();
        markingViews.add(markingView);

        newArcView.setWeight(markingViews);
        newArcView.setZoom(ApplicationSettings.getApplicationView().getCurrentTab().getZoom());

        contentPane.add(newArcView);

        source.addOutbound(newArcView);

        PipeApplicationView applicationView = ApplicationSettings.getApplicationView();
        PetriNetTab tab = applicationView.getCurrentTab();

        //tab._createArcView = newArcView;

        // addPetriNetObject a handler for shift & esc actions drawing arc
        // this is removed when the arc is finished drawing:
//       keyHandler = new ArcKeyboardEventHandler(null);
        newArcView.addKeyListener(keyHandler);
        newArcView.requestFocusInWindow();
        newArcView.setSelectable(false);
    }


    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            JPopupMenu menu = getPopup(e);
            menu.show(viewComponent, 0, 0);
            return;
        }

        super.mousePressed(e);
        mouseDown = true;


        // Prevent creating arcs with a right-click or a middle-click
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }

        Connectable currentObject = component;
        switch (ApplicationSettings.getApplicationModel().getMode()) {
            case Constants.ARC:
                if (e.isControlDown()) {
                    // user is holding Ctrl key; switch to fast mode
                    if (this.component instanceof Place) {
                        ApplicationSettings.getApplicationModel().enterFastMode(Constants.FAST_TRANSITION);
                    } else if (this.component instanceof Transition) {
                        ApplicationSettings.getApplicationModel().enterFastMode(Constants.FAST_PLACE);
                    }
                }
            case Constants.INHIBARC:
            case Constants.FAST_PLACE:
            case Constants.FAST_TRANSITION:
                if (!petriNetController.isCurrentlyCreatingArc()) {
                    if (ApplicationSettings.getApplicationModel().getMode() == Constants.INHIBARC) {
                        if (currentObject instanceof Place) {
                            petriNetController.startCreatingArc(currentObject);
                            //createArc(new InhibitorArcView(currentObject), currentObject);
                        }
                    } else {
                        petriNetController.startCreatingArc(currentObject);
                        //createArc(new NormalArcView(currentObject), currentObject);
                    }
                }
                break;

            default:
                break;
        }
    }


    public void mouseReleased(MouseEvent e) {
        boolean isNewArc = true; // true if we have to add a new arc to the Petri Net
        boolean fastMode = false;

        PetriNetTab view = ApplicationSettings.getApplicationView().getCurrentTab();
        PetriNetView model = ApplicationSettings.getApplicationView().getCurrentPetriNetView();
//       HistoryManager historyManager = view.getHistoryManager();<commit_msg>wrap creating arc in mousepressed into an if<commit_after>package pipe.handlers;

import pipe.controllers.PetriNetController;
import pipe.controllers.PipeApplicationController;
import pipe.gui.*;
import pipe.historyActions.AddPetriNetObject;
import pipe.historyActions.HistoryManager;
import pipe.models.*;
import pipe.utilities.Copier;
import pipe.views.*;
import pipe.views.TokenView;
import pipe.views.MarkingView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Class used to implement methods corresponding to mouse events on places.
 *
 * @author <NAME> - changed the mousePressed method to only allow the
 *         creation of an arc by left-clicking
 * @author <NAME> - modified the handler which was causing the
 *         null pointer exceptions and incorrect petri nets xml representation.
 */
public class PlaceTransitionObjectHandler<T extends Connectable, V extends ConnectableView>
        extends PetriNetObjectHandler<T, V> {
    // STATIC ATTRIBUTES AND METHODS
    private static boolean mouseDown = false;

    public static boolean isMouseDown() {
        return mouseDown;
    }


    private ArcKeyboardEventHandler keyHandler = null;

    // constructor passing in all required objects
    PlaceTransitionObjectHandler(V view, Container contentpane,
                                 T obj, PetriNetController controller) {
        super(view, contentpane, obj, controller);
        enablePopup = true;
    }


    private void createArc(ArcView newArcView, ConnectableView source) {
        TokenView tokenView = ApplicationSettings.getApplicationView().getCurrentPetriNetView().getActiveTokenView();
        MarkingView markingView = new MarkingView(tokenView, 1 + "");
        markingView.addObserver(newArcView);  // Steve Doubleday:  ArcView tracks updates to MarkingView

        LinkedList<MarkingView> markingViews = new LinkedList<MarkingView>();
        markingViews.add(markingView);

        newArcView.setWeight(markingViews);
        newArcView.setZoom(ApplicationSettings.getApplicationView().getCurrentTab().getZoom());

        contentPane.add(newArcView);

        source.addOutbound(newArcView);

        PipeApplicationView applicationView = ApplicationSettings.getApplicationView();
        PetriNetTab tab = applicationView.getCurrentTab();

        //tab._createArcView = newArcView;

        // addPetriNetObject a handler for shift & esc actions drawing arc
        // this is removed when the arc is finished drawing:
//       keyHandler = new ArcKeyboardEventHandler(null);
        newArcView.addKeyListener(keyHandler);
        newArcView.requestFocusInWindow();
        newArcView.setSelectable(false);
    }


    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
        mouseDown = true;

        if (e.isPopupTrigger()) {
            JPopupMenu menu = getPopup(e);
            menu.show(viewComponent, 0, 0);
        } else if (e.getButton() == MouseEvent.BUTTON1) {
            Connectable currentObject = component;
            switch (ApplicationSettings.getApplicationModel().getMode()) {
                case Constants.ARC:
                    if (e.isControlDown()) {
                        // user is holding Ctrl key; switch to fast mode
                        if (this.component instanceof Place) {
                            ApplicationSettings.getApplicationModel().enterFastMode(Constants.FAST_TRANSITION);
                        } else if (this.component instanceof Transition) {
                            ApplicationSettings.getApplicationModel().enterFastMode(Constants.FAST_PLACE);
                        }
                    }
                case Constants.INHIBARC:
                case Constants.FAST_PLACE:
                case Constants.FAST_TRANSITION:
                    if (!petriNetController.isCurrentlyCreatingArc()) {
                        if (ApplicationSettings.getApplicationModel().getMode() == Constants.INHIBARC) {
                            if (currentObject instanceof Place) {
                                petriNetController.startCreatingArc(currentObject);
                                //createArc(new InhibitorArcView(currentObject), currentObject);
                            }
                        } else {
                            petriNetController.startCreatingArc(currentObject);
                            //createArc(new NormalArcView(currentObject), currentObject);
                        }
                    }
                    break;

                default:
                    break;
            }
        }


    }


    public void mouseReleased(MouseEvent e) {
        boolean isNewArc = true; // true if we have to add a new arc to the Petri Net
        boolean fastMode = false;

        PetriNetTab view = ApplicationSettings.getApplicationView().getCurrentTab();
        PetriNetView model = ApplicationSettings.getApplicationView().getCurrentPetriNetView();
//       HistoryManager historyManager = view.getHistoryManager();
{'fraction_non_alphanumeric': 0.0794974973010109, 'fraction_numerical': 0.000785160467170478, 'mean_word_length': 2.3853820598006643, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2274802', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2441, 'n_words': 614}