/[classpath]/classpath/javax/swing/plaf/basic/BasicTreeUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicTreeUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.54 by langel, Mon Aug 8 14:05:35 2005 UTC revision 1.55 by langel, Tue Aug 9 13:27:17 2005 UTC
# Line 585  public class BasicTreeUI extends TreeUI Line 585  public class BasicTreeUI extends TreeUI
585          TreeModel mod = tree.getModel();          TreeModel mod = tree.getModel();
586          if (mod != null)          if (mod != null)
587          {          {
588            TreeNode root = (TreeNode) mod.getRoot();            Object root = mod.getRoot();
589            if (!tree.isRootVisible()            if (!tree.isRootVisible()
590                && tree.isExpanded(new TreePath(root)))                && tree.isExpanded(new TreePath(root)))
591              root = getNextNode(root);              root = getNextNode(root);
# Line 611  public class BasicTreeUI extends TreeUI Line 611  public class BasicTreeUI extends TreeUI
611      TreeModel mod = tree.getModel();      TreeModel mod = tree.getModel();
612      if (mod != null)      if (mod != null)
613      {      {
614        TreeNode node = ((TreeNode) mod.getRoot());        Object node = mod.getRoot();
615        if (!tree.isRootVisible()        if (!tree.isRootVisible()
616            && tree.isExpanded(new TreePath(getPathToRoot(node, 0))))            && tree.isExpanded(new TreePath(getPathToRoot(node, 0))))
617          node = getNextNode(node);          node = getNextNode(node);
# Line 657  public class BasicTreeUI extends TreeUI Line 657  public class BasicTreeUI extends TreeUI
657      int count = 0;      int count = 0;
658      if (mod != null)      if (mod != null)
659      {      {
660        TreeNode node = ((TreeNode) mod.getRoot());        Object node = mod.getRoot();
661        if (!tree.isRootVisible()        if (!tree.isRootVisible()
662            && tree.isExpanded(new TreePath((getPathToRoot(node, 0)))))            && tree.isExpanded(new TreePath((getPathToRoot(node, 0)))))
663          node = getNextNode(node);          node = getNextNode(node);
# Line 1313  public class BasicTreeUI extends TreeUI Line 1313  public class BasicTreeUI extends TreeUI
1313      int count = 0;      int count = 0;
1314      if (model != null)      if (model != null)
1315      {      {
1316        TreeNode node = (TreeNode) model.getRoot();        Object node = model.getRoot();
1317        if (node != null)        if (node != null)
1318        {        {
1319          maxWidth = (int) (getCellBounds(0, 0, node).getWidth());          maxWidth = (int) (getCellBounds(0, 0, node).getWidth());
1320          while (node != null)          while (node != null)
1321          {          {
1322            count++;            count++;
1323            TreeNode nextNode = getNextVisibleNode(node);            Object nextNode = getNextVisibleNode(node);
1324            if (nextNode != null)            if (nextNode != null)
1325              maxWidth = Math.max(maxWidth, (int) (getCellBounds(0, 0, nextNode)              maxWidth = Math.max(maxWidth, (int) (getCellBounds(0, 0, nextNode)
1326                  .getWidth()));                  .getWidth()));
# Line 1530  public class BasicTreeUI extends TreeUI Line 1530  public class BasicTreeUI extends TreeUI
1530        return true;        return true;
1531            
1532      Object node = pathForRow.getLastPathComponent();      Object node = pathForRow.getLastPathComponent();
1533            return tree.getModel().isLeaf(node);
     if (node instanceof TreeNode)  
       return ((TreeNode) node).isLeaf();  
     else  
       return true;  
1534    }    }
1535        
1536    /**    /**
# Line 1705  public class BasicTreeUI extends TreeUI Line 1701  public class BasicTreeUI extends TreeUI
1701       */       */
1702      public void keyPressed(KeyEvent e)      public void keyPressed(KeyEvent e)
1703      {      {
1704          TreeModel mod = BasicTreeUI.this.tree.getModel();
1705        TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath();        TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath();
1706        TreeNode last = null;        Object last = null;
1707        if (start != null)        if (start != null)
1708          last = (TreeNode) start.getLastPathComponent();          last = start.getLastPathComponent();
1709                
1710        if (last != null)        if (last != null)
1711          {          {
# Line 1716  public class BasicTreeUI extends TreeUI Line 1713  public class BasicTreeUI extends TreeUI
1713            if (e.getKeyCode() == KeyEvent.VK_DOWN            if (e.getKeyCode() == KeyEvent.VK_DOWN
1714                || e.getKeyCode() == KeyEvent.VK_KP_DOWN)                || e.getKeyCode() == KeyEvent.VK_KP_DOWN)
1715              {              {
1716                TreeNode next = (TreeNode) BasicTreeUI.this.                Object next = BasicTreeUI.this.
1717                getNextVisibleNode(last);                  getNextVisibleNode(last);
1718                if (next != null)                if (next != null)
1719                  {                  {
1720                    TreePath newPath = new TreePath(getPathToRoot(next, 0));                    TreePath newPath = new TreePath(getPathToRoot(next, 0));
1721                    BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath);                    BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath);
1722                    if (e.isControlDown())                    if (e.isControlDown())
1723                      tree.setLeadSelectionPath(newPath);                      tree.setLeadSelectionPath(newPath);
1724                    else if (!next.isLeaf() && e.isShiftDown())                    else if (!mod.isLeaf(next) && e.isShiftDown())
1725                      {                      {
1726                        BasicTreeUI.this.tree.expandPath(newPath);                        BasicTreeUI.this.tree.expandPath(newPath);
1727                        BasicTreeUI.this.tree.fireTreeExpanded(newPath);                        BasicTreeUI.this.tree.fireTreeExpanded(newPath);
# Line 1735  public class BasicTreeUI extends TreeUI Line 1732  public class BasicTreeUI extends TreeUI
1732            else if (e.getKeyCode() == KeyEvent.VK_UP            else if (e.getKeyCode() == KeyEvent.VK_UP
1733                || e.getKeyCode() == KeyEvent.VK_KP_UP)                || e.getKeyCode() == KeyEvent.VK_KP_UP)
1734              {              {
1735                TreeNode prev = (TreeNode) BasicTreeUI.this.                Object prev = BasicTreeUI.this.
1736                getPreviousVisibleNode(last);                getPreviousVisibleNode(last);
1737                                
1738                if (prev != null)                if (prev != null)
# Line 1745  public class BasicTreeUI extends TreeUI Line 1742  public class BasicTreeUI extends TreeUI
1742                                                new TreePath(getPathToRoot(prev, 0)));                                                new TreePath(getPathToRoot(prev, 0)));
1743                    if (e.isControlDown())                    if (e.isControlDown())
1744                      tree.setLeadSelectionPath(newPath);                      tree.setLeadSelectionPath(newPath);
1745                    else if (!prev.isLeaf() && e.isShiftDown())                    else if (!mod.isLeaf(prev) && e.isShiftDown())
1746                      {                      {
1747                        BasicTreeUI.this.tree.expandPath(newPath);                        BasicTreeUI.this.tree.expandPath(newPath);
1748                        BasicTreeUI.this.tree.fireTreeExpanded(newPath);                        BasicTreeUI.this.tree.fireTreeExpanded(newPath);
# Line 1757  public class BasicTreeUI extends TreeUI Line 1754  public class BasicTreeUI extends TreeUI
1754                || e.getKeyCode() == KeyEvent.VK_KP_LEFT)                || e.getKeyCode() == KeyEvent.VK_KP_LEFT)
1755              {              {
1756                TreePath path = new TreePath(getPathToRoot(last, 0));                TreePath path = new TreePath(getPathToRoot(last, 0));
1757                TreeNode p = last.getParent();                Object p = getParent(mod.getRoot(), last);
1758                                
1759                if (!last.isLeaf() && BasicTreeUI.this.tree.isExpanded(path))                if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isExpanded(path))
1760                  {                  {
1761                    BasicTreeUI.this.tree.collapsePath(path);                    BasicTreeUI.this.tree.collapsePath(path);
1762                    BasicTreeUI.this.tree.fireTreeCollapsed(path);                    BasicTreeUI.this.tree.fireTreeCollapsed(path);
# Line 1774  public class BasicTreeUI extends TreeUI Line 1771  public class BasicTreeUI extends TreeUI
1771              {              {
1772                TreePath path = new TreePath(getPathToRoot(last, 0));                TreePath path = new TreePath(getPathToRoot(last, 0));
1773                                
1774                if (!last.isLeaf() && BasicTreeUI.this.tree.isCollapsed(path))                if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isCollapsed(path))
1775                  {                  {
1776                    BasicTreeUI.this.tree.expandPath(path);                    BasicTreeUI.this.tree.expandPath(path);
1777                    BasicTreeUI.this.tree.fireTreeExpanded(path);                    BasicTreeUI.this.tree.fireTreeExpanded(path);
1778                  }                  }
1779                else                else
1780                  {                  {
1781                    TreeNode next = (TreeNode) BasicTreeUI.this.                    Object next = BasicTreeUI.this.
1782                    getNextVisibleNode(last);                    getNextVisibleNode(last);
1783                                        
1784                    if (next != null)                    if (next != null)
# Line 1793  public class BasicTreeUI extends TreeUI Line 1790  public class BasicTreeUI extends TreeUI
1790            else if (e.getKeyCode() == KeyEvent.VK_ENTER)            else if (e.getKeyCode() == KeyEvent.VK_ENTER)
1791              {              {
1792                TreePath path = new TreePath(getPathToRoot(last, 0));                TreePath path = new TreePath(getPathToRoot(last, 0));
1793                if (!last.isLeaf())                if (!mod.isLeaf(last))
1794                  {                  {
1795                    if (BasicTreeUI.this.tree.isExpanded(path))                    if (BasicTreeUI.this.tree.isExpanded(path))
1796                      {                      {
# Line 2545  public class BasicTreeUI extends TreeUI Line 2542  public class BasicTreeUI extends TreeUI
2542      if (startNode == null || startNode.equals(node))      if (startNode == null || startNode.equals(node))
2543        {        {
2544          if (!tree.isRootVisible() && tree.isExpanded(new TreePath(mod.getRoot())))          if (!tree.isRootVisible() && tree.isExpanded(new TreePath(mod.getRoot())))
2545            return new Point(x + ((getLevel((TreeNode) node)) *            return new Point(x + ((getLevel(node)) * rightChildIndent), y);
               rightChildIndent), y);  
2546                    
2547          return new Point(x + ((getLevel((TreeNode) node) + 1)          return new Point(x + ((getLevel(node) + 1)
2548              * rightChildIndent), y);              * rightChildIndent), y);
2549        }        }
2550            
2551      if (!mod.isLeaf(startNode)      if (!mod.isLeaf(startNode)
2552          && tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) startNode),          && tree.isExpanded(new TreePath(getPathToRoot(startNode, 0))))
             0))))  
2553        {        {
2554          Object child = mod.getChild(startNode, 0);          Object child = mod.getChild(startNode, 0);
2555          if (child != null)          if (child != null)
# Line 2563  public class BasicTreeUI extends TreeUI Line 2558  public class BasicTreeUI extends TreeUI
2558        }        }
2559            
2560      return getCellLocation(x, y + rowHeight, tree, mod, node,      return getCellLocation(x, y + rowHeight, tree, mod, node,
2561                             getNextVisibleNode((TreeNode) startNode));                             getNextVisibleNode(startNode));
2562    }    }
2563        
2564    /**    /**
# Line 2578  public class BasicTreeUI extends TreeUI Line 2573  public class BasicTreeUI extends TreeUI
2573    void paintNode(Graphics g, int x, int y, JTree tree, Object node,    void paintNode(Graphics g, int x, int y, JTree tree, Object node,
2574                   boolean isLeaf)                   boolean isLeaf)
2575    {    {
2576      TreePath curr = new TreePath(getPathToRoot(((TreeNode) node), 0));      TreePath curr = new TreePath(getPathToRoot(node, 0));
2577      boolean selected = tree.isPathSelected(curr);      boolean selected = tree.isPathSelected(curr);
2578      boolean expanded = false;      boolean expanded = false;
2579      boolean hasIcons = false;      boolean hasIcons = false;
# Line 2677  public class BasicTreeUI extends TreeUI Line 2672  public class BasicTreeUI extends TreeUI
2672            }            }
2673                    
2674          int max = mod.getChildCount(curr);          int max = mod.getChildCount(curr);
2675          if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) curr), 0))))          if (tree.isExpanded(new TreePath(getPathToRoot(curr, 0))))
2676            {            {
2677              for (int i = 0; i < max; ++i)              for (int i = 0; i < max; ++i)
2678                {                {
# Line 2700  public class BasicTreeUI extends TreeUI Line 2695  public class BasicTreeUI extends TreeUI
2695            }            }
2696        }        }
2697            
2698      if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) curr), 0))))      if (tree.isExpanded(new TreePath(getPathToRoot(curr, 0))))
2699        if (y0 != heightOfLine)        if (y0 != heightOfLine)
2700          {          {
2701            g.setColor(getHashColor());            g.setColor(getHashColor());
# Line 2748  public class BasicTreeUI extends TreeUI Line 2743  public class BasicTreeUI extends TreeUI
2743            descent += rowHeight;            descent += rowHeight;
2744                    
2745          int max = mod.getChildCount(node);          int max = mod.getChildCount(node);
2746          if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) node), 0))))          if (tree.isExpanded(new TreePath(getPathToRoot(node, 0))))
2747            {            {
2748              if (!node.equals(mod.getRoot()))              if (!node.equals(mod.getRoot()))
2749                drawCentered(tree, g, ei, indentation - rightChildIndent - 3, h);                drawCentered(tree, g, ei, indentation - rightChildIndent - 3, h);
# Line 2789  public class BasicTreeUI extends TreeUI Line 2784  public class BasicTreeUI extends TreeUI
2784    }    }
2785        
2786    /**    /**
2787     * Get next visible node in the tree.     * Returns the parent of the current node
2788     * Package private for use in inner classes.     *
2789       * @param root is the root of the tree
2790       * @param node is the current node
2791       * @return is the parent of the current node
2792       */
2793      Object getParent(Object root, Object node)
2794      {
2795        if (root == null || node == null)
2796          return null;
2797        if (node instanceof TreeNode)
2798          return ((TreeNode) node).getParent();
2799        return findNode(root, node);
2800      }
2801      
2802      /**
2803       * Recursively checks the tree for the specified node, starting
2804       * at the root.
2805       *
2806       * @param root is starting node to start searching at.
2807       * @param node is the node to search for
2808       * @return the parent node of node
2809       */
2810      private Object findNode(Object root, Object node)
2811      {
2812        TreeModel mod = tree.getModel();
2813        for (int i = 0; i < mod.getChildCount(root); i++)
2814        {
2815          if (mod.getIndexOfChild(root, node) != -1)
2816            return root;
2817          
2818          Object n = findNode(mod.getChild(root, i), node);
2819          if (n != null)
2820            return  n;
2821        }
2822        return null;
2823      }
2824      
2825      /**
2826       * Get next visible node in the tree. Package private for use in inner
2827       * classes.
2828     *     *
2829     * @param the current node     * @param the current node
2830     * @return the next visible node in the JTree. Return null if there are no     * @return the next visible node in the JTree. Return null if there are no
2831     *         more.     *         more.
2832     */     */
2833    TreeNode getNextVisibleNode(TreeNode node)    Object getNextVisibleNode(Object node)
2834    {    {
2835      TreeNode next = null;      Object next = null;
2836      TreePath current = null;      TreePath current = null;
2837            
2838      if (node != null)      if (node != null)
# Line 2829  public class BasicTreeUI extends TreeUI Line 2863  public class BasicTreeUI extends TreeUI
2863     * @return the next visible node in the JTree. Return null if there are no     * @return the next visible node in the JTree. Return null if there are no
2864     *         more.     *         more.
2865     */     */
2866    TreeNode getPreviousVisibleNode(TreeNode node)    Object getPreviousVisibleNode(Object node)
2867    {    {
2868      TreeNode prev = null;      Object prev = null;
2869      TreePath current = null;      TreePath current = null;
2870            
2871      if (node != null)      if (node != null)
# Line 2858  public class BasicTreeUI extends TreeUI Line 2892  public class BasicTreeUI extends TreeUI
2892     * Returns the next node in the tree     * Returns the next node in the tree
2893     * Package private for use in inner classes.     * Package private for use in inner classes.
2894     *     *
2895     * @return TreeNode, the next node in the tree     * @param the current node
2896       * @return the next node in the tree
2897     */     */
2898    TreeNode getNextNode(TreeNode curr)    Object getNextNode(Object curr)
2899    {    {
2900      if (curr.getChildCount() != 0)      TreeModel mod = tree.getModel();
2901        return curr.getChildAt(0);      if (mod.getChildCount(curr) != 0)
2902          return mod.getChild(curr, 0);
2903    
2904      TreeNode node = curr;      Object node = curr;
2905      TreeNode sibling;      Object sibling = null;
2906            
2907      do      do
2908        {        {
2909          sibling = getNextSibling(node);          sibling = getNextSibling(node);
2910          node = node.getParent();          node = getParent(mod.getRoot(), node);
2911        }        }
2912      while (sibling == null &&      while (sibling == null &&
2913             node != null);             node != null);
# Line 2883  public class BasicTreeUI extends TreeUI Line 2919  public class BasicTreeUI extends TreeUI
2919     * Returns the previous node in the tree     * Returns the previous node in the tree
2920     * Package private for use in inner classes.     * Package private for use in inner classes.
2921     *     *
2922     * @return TreeNode, the previous node in the tree     * @param the current node
2923       * @return the previous node in the tree
2924     */     */
2925    TreeNode getPreviousNode(TreeNode node)    Object getPreviousNode(Object node)
2926    {    {
2927      TreeNode parent = node.getParent();      TreeModel mod = tree.getModel();
2928        Object parent = getParent(mod.getRoot(), node);
2929      if (parent == null)      if (parent == null)
2930        return null;        return null;
2931            
2932      TreeNode sibling = getPreviousSibling(node);      Object sibling = getPreviousSibling(node);
2933    
2934      if (sibling == null)      if (sibling == null)
2935        return parent;        return parent;
2936    
2937      int size = sibling.getChildCount();      int size = mod.getChildCount(sibling);
2938      while (size > 0)      while (size > 0)
2939      {      {
2940        sibling = sibling.getChildAt(size - 1);        sibling = mod.getChild(sibling, size - 1);
2941        size = sibling.getChildCount();        size = mod.getChildCount(sibling);
2942      }      }
2943                
2944      return sibling;      return sibling;
# Line 2910  public class BasicTreeUI extends TreeUI Line 2948  public class BasicTreeUI extends TreeUI
2948     * Returns the next sibling in the tree     * Returns the next sibling in the tree
2949     * Package private for use in inner classes.     * Package private for use in inner classes.
2950     *     *
2951     * @return TreeNode, the next sibling in the tree     * @param the current node
2952       * @return the next sibling in the tree
2953     */     */
2954    TreeNode getNextSibling(TreeNode node)    Object getNextSibling(Object node)
2955    {    {
2956      TreeNode parent = node.getParent();      TreeModel mod = tree.getModel();
2957        Object parent = getParent(mod.getRoot(), node);
2958      if (parent == null)      if (parent == null)
2959        return null;        return null;
2960    
2961      int index = parent.getIndex(node) + 1;      int index = mod.getIndexOfChild(parent, node) + 1;
2962            
2963      if (index == parent.getChildCount())      if (index == 0 || index >= mod.getChildCount(parent))
2964        return null;        return null;
2965    
2966      return parent.getChildAt(index);      return mod.getChild(parent, index);
2967    }    }
2968    
2969    /**    /**
2970     * Returns the previous sibling in the tree     * Returns the previous sibling in the tree
2971     * Package private for use in inner classes.     * Package private for use in inner classes.
2972     *     *
2973     * @return TreeNode, the previous sibling in the tree     * @param the current node
2974       * @return the previous sibling in the tree
2975     */     */
2976    TreeNode getPreviousSibling(TreeNode node)    Object getPreviousSibling(Object node)
2977    {    {
2978      TreeNode parent = node.getParent();      TreeModel mod = tree.getModel();
2979        Object parent = getParent(mod.getRoot(), node);
2980      if (parent == null)      if (parent == null)
2981        return null;        return null;
2982    
2983      int index = parent.getIndex(node) - 1;      int index = mod.getIndexOfChild(parent, node) - 1;
2984    
2985      if (index < 0)      if (index < 0)
2986        return null;        return null;
2987    
2988      return parent.getChildAt(index);      return mod.getChild(parent, index);
2989    }    }
2990        
2991    /**    /**
# Line 2990  public class BasicTreeUI extends TreeUI Line 3032  public class BasicTreeUI extends TreeUI
3032     *     *
3033     * @return an array of tree nodes that represent the path to node.     * @return an array of tree nodes that represent the path to node.
3034     */     */
3035    TreeNode[] getPathToRoot(TreeNode node, int depth)    Object[] getPathToRoot(Object node, int depth)
3036    {    {
3037        TreeModel mod = tree.getModel();
3038      if (node == null)      if (node == null)
3039        {        {
3040          if (depth == 0)          if (depth == 0)
3041            return null;            return null;
3042                    
3043          return new TreeNode[depth];          return new Object[depth];
3044        }        }
3045    
3046      TreeNode[] path = getPathToRoot(node.getParent(), depth + 1);      Object[] path = getPathToRoot(getParent(mod.getRoot(),
3047            node), depth + 1);
3048      path[path.length - depth - 1] = node;      path[path.length - depth - 1] = node;
3049      return path;      return path;
3050    }    }
# Line 3008  public class BasicTreeUI extends TreeUI Line 3052  public class BasicTreeUI extends TreeUI
3052    /**    /**
3053     * Returns the level of the node in the tree.     * Returns the level of the node in the tree.
3054     *     *
3055       * @param the current node
3056     * @return the number of the level     * @return the number of the level
3057     */     */
3058    public int getLevel(TreeNode node)    int getLevel(Object node)
3059    {    {
3060      int count = -1;      int count = -1;
3061      TreeNode current = node;      Object current = node;
3062    
3063      do      do
3064        {        {
3065          current = current.getParent();          current = getParent(tree.getModel().getRoot(), current);
3066          count++;          count++;
3067        }        }
3068      while (current != null);      while (current != null);

Legend:
Removed from v.1.54  
changed lines
  Added in v.1.55

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26