<commit_before>package com.blazemeter.jmeter.debugger.gui;

import com.blazemeter.jmeter.debugger.engine.DebuggerEngine;
import com.blazemeter.jmeter.debugger.engine.StepTrigger;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.threads.JMeterThread;
import org.apache.jmeter.threads.JMeterThreadMonitor;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class DebuggerDialog extends DebuggerDialogBase implements JMeterThreadMonitor {
    private static final Logger log = LoggingManager.getLoggerForClass();
    protected final JMeterTreeListener treeListener;

    protected DebuggerEngine engine;
    private StepOver stepper = new StepOver();

    public DebuggerDialog() {
        super();
        start.addActionListener(new StartDebugging());
        stop.addActionListener(new StopDebugging());
        step.addActionListener(stepper);
        tgCombo.addItemListener(new ThreadGroupChoiceChanged());

        treeListener = new JMeterTreeListener();
        treeListener.setJTree(tree);
        //tree.addTreeSelectionListener(treeListener);
        //tree.addMouseListener(treeListener);
        //tree.addKeyListener(treeListener);
    }

    @Override
    public void componentShown(ComponentEvent e) {
        log.debug("Showing dialog");
        HashTree testTree = getTestTree();
        this.engine = new DebuggerEngine(testTree);
        tgCombo.removeAllItems();
        for (ThreadGroup group : engine.getThreadGroups()) {
            tgCombo.addItem(group);
        }
    }


    @Override
    public void componentHidden(ComponentEvent e) {
        log.debug("Closing dialog");
        engine.stopDebugging();
    }

    protected HashTree getTestTree() {
        GuiPackage gui = GuiPackage.getInstance();
        return gui.getTreeModel().getTestPlan();
    }

    private void start() {
        log.debug("Start debugging");
        loggerPanel.clear();
        tgCombo.setEnabled(false);
        start.setEnabled(false);
        stop.setEnabled(true);
        ThreadGroup tg = (ThreadGroup) tgCombo.getSelectedItem();
        engine.startDebugging(tg, engine.getFullTree(tg), stepper, this);
    }

    private void stop() {
        engine.stopDebugging();
        start.setEnabled(true);
        stop.setEnabled(false);
        tgCombo.setEnabled(true);
    }

    @Override
    public void threadFinished(JMeterThread thread) {
        stop();
    }

    private void refreshStatus() {
        // TODO: refresh vars and properties
        // TODO: show samples
    }

    private void selectTargetInTree(TestElement te) {
        tree.setSelectionPath(getTreePathFor(te));
    }

    private TreePath getTreePathFor(TestElement te) {
        List<Object> nodes = new ArrayList<>();
        JMeterTreeModel model = (JMeterTreeModel) tree.getModel();

        TreeNode treeNode = model.getNodeOf(te);
        if (treeNode != null) {
            nodes.add(treeNode);
            treeNode = treeNode.getParent();
            while (treeNode != null) {
                nodes.add(0, treeNode);
                treeNode = treeNode.getParent();
            }
        }

        return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
    }

    private class ThreadGroupChoiceChanged implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                log.debug("Item choice changed: " + event.getItem());
                if (event.getItem() instanceof ThreadGroup) {
                    HashTree val = engine.getFullTree((ThreadGroup) event.getItem());
                    tree.setModel(new DebuggerTreeModel(val));
                }
            }
        }
    }

    private class StartDebugging implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            start();
        }
    }

    private class StepOver implements ActionListener, StepTrigger {
        @Override
        public void actionPerformed(ActionEvent e) {
            synchronized (this) {
                this.notifyAll();
            }
        }

        @Override
        public void notify(Object t) {
            step.setEnabled(true);
            log.debug("Stopping before: " + t);
            if (t instanceof TestElement) {
                selectTargetInTree((TestElement) t);
            }
            refreshStatus();
            try {
                synchronized (this) {
                    this.wait();
                }
            } catch (InterruptedException e) {
                stop();
            }
            step.setEnabled(false);
        }
    }


    private class StopDebugging implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            stop();
        }
    }
}<commit_msg>First wprked step-by-step flow<commit_after>package com.blazemeter.jmeter.debugger.gui;

import com.blazemeter.jmeter.debugger.elements.AbstractDebugElement;
import com.blazemeter.jmeter.debugger.engine.DebuggerEngine;
import com.blazemeter.jmeter.debugger.engine.StepTrigger;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.threads.JMeterThread;
import org.apache.jmeter.threads.JMeterThreadMonitor;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class DebuggerDialog extends DebuggerDialogBase implements JMeterThreadMonitor {
    private static final Logger log = LoggingManager.getLoggerForClass();
    protected final JMeterTreeListener treeListener;

    protected DebuggerEngine engine;
    private StepOver stepper = new StepOver();

    public DebuggerDialog() {
        super();
        start.addActionListener(new StartDebugging());
        stop.addActionListener(new StopDebugging());
        step.addActionListener(stepper);
        tgCombo.addItemListener(new ThreadGroupChoiceChanged());

        treeListener = new JMeterTreeListener();
        treeListener.setJTree(tree);
        //tree.addTreeSelectionListener(treeListener);
        //tree.addMouseListener(treeListener);
        //tree.addKeyListener(treeListener);
    }

    @Override
    public void componentShown(ComponentEvent e) {
        log.debug("Showing dialog");
        HashTree testTree = getTestTree();
        this.engine = new DebuggerEngine(testTree);
        tgCombo.removeAllItems();
        for (ThreadGroup group : engine.getThreadGroups()) {
            tgCombo.addItem(group);
        }
    }


    @Override
    public void componentHidden(ComponentEvent e) {
        log.debug("Closing dialog");
        engine.stopDebugging();
    }

    protected HashTree getTestTree() {
        GuiPackage gui = GuiPackage.getInstance();
        return gui.getTreeModel().getTestPlan();
    }

    private void start() {
        log.debug("Start debugging");
        loggerPanel.clear();
        tgCombo.setEnabled(false);
        start.setEnabled(false);
        stop.setEnabled(true);
        ThreadGroup tg = (ThreadGroup) tgCombo.getSelectedItem();
        engine.startDebugging(tg, engine.getFullTree(tg), stepper, this);
    }

    private void stop() {
        engine.stopDebugging();
        start.setEnabled(true);
        stop.setEnabled(false);
        tgCombo.setEnabled(true);
    }

    @Override
    public void threadFinished(JMeterThread thread) {
        stop();
    }

    private void refreshStatus() {
        // TODO: refresh vars and properties
        // TODO: show samples
    }

    private void selectTargetInTree(TestElement te) {
        tree.setSelectionPath(getTreePathFor(te));
    }

    private TreePath getTreePathFor(TestElement te) {
        List<Object> nodes = new ArrayList<>();
        JMeterTreeModel model = (JMeterTreeModel) tree.getModel();

        TreeNode treeNode = model.getNodeOf(te);
        if (treeNode != null) {
            nodes.add(treeNode);
            treeNode = treeNode.getParent();
            while (treeNode != null) {
                nodes.add(0, treeNode);
                treeNode = treeNode.getParent();
            }
        }

        return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
    }

    private class ThreadGroupChoiceChanged implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                log.debug("Item choice changed: " + event.getItem());
                if (event.getItem() instanceof ThreadGroup) {
                    HashTree val = engine.getFullTree((ThreadGroup) event.getItem());
                    tree.setModel(new DebuggerTreeModel(val));
                }
            }
        }
    }

    private class StartDebugging implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            start();
        }
    }

    private class StepOver implements ActionListener, StepTrigger {
        @Override
        public void actionPerformed(ActionEvent e) {
            synchronized (this) {
                this.notifyAll();
            }
        }

        @Override
        public void notify(Object t) {
            step.setEnabled(true);
            if (t instanceof AbstractDebugElement) {
                Object wrappedElement = ((AbstractDebugElement) t).getWrappedElement();
                log.debug("Stopping before: " + wrappedElement);
                if (wrappedElement instanceof TestElement) {
                    selectTargetInTree((TestElement) wrappedElement);
                }
            }
            refreshStatus();
            try {
                synchronized (this) {
                    this.wait();
                }
            } catch (InterruptedException e) {
                stop();
            }
            step.setEnabled(false);
        }
    }


    private class StopDebugging implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            stop();
        }
    }
}
{'fraction_non_alphanumeric': 0.09789240155296727, 'fraction_numerical': 0.00018487705675725643, 'mean_word_length': 2.3651632970451013, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6721253', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2836, 'n_words': 573}