/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author smuRf
 */
public class TreeSelector {
    
    private final static String[] PREFIXES = new String[]{"part", "phrase", "note", "motif", "match"};
    
    public static int[][] getSelectedId(TreePath[] paths)
    {
        if(paths != null)
        {
            int[][] itemIds = new int[paths.length][];
            for(int i = 0; i < paths.length; i++)
            {
                TreePath path = paths[i];
                int depth = path.getPathCount();
                
                itemIds[i] = new int[depth-1];


                DefaultMutableTreeNode node = (DefaultMutableTreeNode)(path.getLastPathComponent());

                int tDepth = depth;
                while(tDepth > 1)
                {
                    String nodeName = (String)node.getUserObject();

                    for(String prefix:PREFIXES)
                    {
                        if(nodeName.contains(prefix))
                        {
                            itemIds[i][depth-tDepth--] = Integer.parseInt(nodeName.replace(prefix, ""));
                            break;
                        }
                    }

                    node = (DefaultMutableTreeNode)node.getParent();
                }
            }
            return itemIds;
        }
        return null;
    }
    
    public static int[] getSelectedId(TreePath path)
    {
        if(path != null)
        {
            int depth = path.getPathCount();
            int[] itemIds = new int[depth-1];
            
            
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)(path.getLastPathComponent());
            
            int tDepth = depth;
            while(tDepth > 1)
            {
                String nodeName = (String)node.getUserObject();

                for(String prefix:PREFIXES)
                {
                    if(nodeName.contains(prefix))
                    {
                        itemIds[depth-tDepth--] = Integer.parseInt(nodeName.replace(prefix, ""));
                        break;
                    }
                }
                
                node = (DefaultMutableTreeNode)node.getParent();
            }
            return itemIds;
        }
        return null;
    }
    
    public static String getSelectedName(TreePath path)
    {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)(path.getLastPathComponent());
        return (String)node.getUserObject();
    }
}
