<commit_before>package galvin.swing.tree;

import galvin.swing.ApplicationWindow;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class DragAndDropTree
    extends JTree
    implements DragSourceListener,
               DropTargetListener,
               DragGestureListener
{

    public static final int PIXELS_ABOVE_AND_BELOW = 5;
    private static DataFlavor[] supportedFlavors;
    private DragSource dragSource;
    private DropTarget dropTarget;
    private DraggableTreeNode dropTargetNode;
    private TreePath dropTargetPath;
    private boolean nodeWillBeInsertedAbove;
    private boolean nodeWillBeInsertedBelow;
    private List draggedObjects = new ArrayList();
    private DraggableTreeNode rootNode;

    public DragAndDropTree()
    {
        this( new DraggableTreeNode( "Root" ) );
    }

    public DragAndDropTree( DraggableTreeNode rootNode )
    {
        this.rootNode = rootNode;
        setCellRenderer( new DraggableTreeCellRenderer() );
        setModel( new DefaultTreeModel( rootNode ) );

        dragSource = new DragSource();
        dragSource.createDefaultDragGestureRecognizer( this,
                                                       DnDConstants.ACTION_COPY_OR_MOVE,
                                                       this );

        dropTarget = new DropTarget( this, this );
    }

    public List<DefaultMutableTreeNode> getSelectedNodes()
    {
        List<DefaultMutableTreeNode> result = new ArrayList();

        TreePath[] selectedPaths = getSelectionPaths();
        if( selectedPaths != null )
        {
            for(TreePath treePath : selectedPaths)
            {
                Object lastComponent = treePath.getLastPathComponent();
                if( lastComponent instanceof DefaultMutableTreeNode )
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastComponent;
                    result.add( node );
                }
            }
        }

        return result;
    }

    public void select( DefaultMutableTreeNode node )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();

            TreeNode parent = node.getParent();
            if( parent != null )
            {
                TreeNode[] parentNodes = model.getPathToRoot( parent );
                TreePath parentPath = new TreePath( parentNodes );
                expandPath( parentPath );
            }

            TreeNode[] treeNodes = model.getPathToRoot( node );
            TreePath treePath = new TreePath( treeNodes );
            setSelectionPath( treePath );
        }
    }

    public boolean isExpanded( DefaultMutableTreeNode node )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();
            TreeNode[] nodes = model.getPathToRoot( node );
            if( nodes != null )
            {
                TreePath path = new TreePath( nodes );
                return isExpanded( path );
            }
        }

        return false;
    }

    public void setExpanded( DefaultMutableTreeNode node, boolean expanded )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();
            TreeNode[] nodes = model.getPathToRoot( node );
            if( nodes != null )
            {
                TreePath path = new TreePath( nodes );

                if( expanded )
                {<commit_msg>Added utility to get the path to a JTree node<commit_after>package galvin.swing.tree;

import galvin.swing.ApplicationWindow;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class DragAndDropTree
    extends JTree
    implements DragSourceListener,
               DropTargetListener,
               DragGestureListener
{

    public static final int PIXELS_ABOVE_AND_BELOW = 5;
    private static DataFlavor[] supportedFlavors;
    private DragSource dragSource;
    private DropTarget dropTarget;
    private DraggableTreeNode dropTargetNode;
    private TreePath dropTargetPath;
    private boolean nodeWillBeInsertedAbove;
    private boolean nodeWillBeInsertedBelow;
    private List draggedObjects = new ArrayList();
    private DraggableTreeNode rootNode;

    public DragAndDropTree()
    {
        this( new DraggableTreeNode( "Root" ) );
    }

    public DragAndDropTree( DraggableTreeNode rootNode )
    {
        this.rootNode = rootNode;
        setCellRenderer( new DraggableTreeCellRenderer() );
        setModel( new DefaultTreeModel( rootNode ) );

        dragSource = new DragSource();
        dragSource.createDefaultDragGestureRecognizer( this,
                                                       DnDConstants.ACTION_COPY_OR_MOVE,
                                                       this );

        dropTarget = new DropTarget( this, this );
    }

    public List<DefaultMutableTreeNode> getSelectedNodes()
    {
        List<DefaultMutableTreeNode> result = new ArrayList();

        TreePath[] selectedPaths = getSelectionPaths();
        if( selectedPaths != null )
        {
            for(TreePath treePath : selectedPaths)
            {
                Object lastComponent = treePath.getLastPathComponent();
                if( lastComponent instanceof DefaultMutableTreeNode )
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastComponent;
                    result.add( node );
                }
            }
        }

        return result;
    }

    public void select( DefaultMutableTreeNode node )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();

            TreeNode parent = node.getParent();
            if( parent != null )
            {
                TreeNode[] parentNodes = model.getPathToRoot( parent );
                TreePath parentPath = new TreePath( parentNodes );
                expandPath( parentPath );
            }

            TreeNode[] treeNodes = model.getPathToRoot( node );
            TreePath treePath = new TreePath( treeNodes );
            setSelectionPath( treePath );
        }
    }
    
    public TreeNode[] getPath( DefaultMutableTreeNode node ){
        if( node != null ) {
            DefaultTreeModel model = (DefaultTreeModel) getModel();
            TreeNode[] treeNodes = model.getPathToRoot( node );
            return treeNodes;
        }
        
        return new TreeNode[0];
    }
    
    public boolean isExpanded( DefaultMutableTreeNode node )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();
            TreeNode[] nodes = model.getPathToRoot( node );
            if( nodes != null )
            {
                TreePath path = new TreePath( nodes );
                return isExpanded( path );
            }
        }

        return false;
    }

    public void setExpanded( DefaultMutableTreeNode node, boolean expanded )
    {
        if( node != null )
        {
            DefaultTreeModel model = (DefaultTreeModel) getModel();
            TreeNode[] nodes = model.getPathToRoot( node );
            if( nodes != null )
            {
                TreePath path = new TreePath( nodes );

                if( expanded )
                {
{'fraction_non_alphanumeric': 0.07879178759539933, 'fraction_numerical': 0.0003224766204450177, 'mean_word_length': 2.2497380370241005, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6642136', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2415, 'n_words': 562}