88 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
89 |
import javax.swing.plaf.TreeUI; |
import javax.swing.plaf.TreeUI; |
90 |
import javax.swing.tree.AbstractLayoutCache; |
import javax.swing.tree.AbstractLayoutCache; |
|
import javax.swing.tree.DefaultMutableTreeNode; |
|
91 |
import javax.swing.tree.DefaultTreeCellEditor; |
import javax.swing.tree.DefaultTreeCellEditor; |
92 |
import javax.swing.tree.DefaultTreeCellRenderer; |
import javax.swing.tree.DefaultTreeCellRenderer; |
93 |
import javax.swing.tree.FixedHeightLayoutCache; |
import javax.swing.tree.FixedHeightLayoutCache; |
325 |
* |
* |
326 |
* @return the indent value for the right child. |
* @return the indent value for the right child. |
327 |
*/ |
*/ |
328 |
public int getRightChildIndent(int newAmount) |
public int getRightChildIndent() |
329 |
{ |
{ |
330 |
return rightChildIndent; |
return rightChildIndent; |
331 |
} |
} |
583 |
{ |
{ |
584 |
Object cell = path.getLastPathComponent(); |
Object cell = path.getLastPathComponent(); |
585 |
TreeModel mod = tree.getModel(); |
TreeModel mod = tree.getModel(); |
586 |
DefaultMutableTreeNode root = (DefaultMutableTreeNode) mod.getRoot(); |
TreeNode root = (TreeNode) mod.getRoot(); |
587 |
if (!tree.isRootVisible() |
if (!tree.isRootVisible() |
588 |
&& tree.isExpanded(new TreePath(root))) |
&& tree.isExpanded(new TreePath(root))) |
589 |
root = root.getNextNode(); |
root = getNextNode(root); |
590 |
|
|
591 |
Point loc = getCellLocation(0, 0, tree, mod, cell, root); |
Point loc = getCellLocation(0, 0, tree, mod, cell, root); |
592 |
return getCellBounds(loc.x, loc.y, cell); |
return getCellBounds(loc.x, loc.y, cell); |
605 |
*/ |
*/ |
606 |
public TreePath getPathForRow(JTree tree, int row) |
public TreePath getPathForRow(JTree tree, int row) |
607 |
{ |
{ |
608 |
DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (tree.getModel()) |
TreeNode node = ((TreeNode) (tree.getModel()).getRoot()); |
|
.getRoot()); |
|
609 |
if (!tree.isRootVisible() |
if (!tree.isRootVisible() |
610 |
&& tree.isExpanded(new TreePath(((DefaultMutableTreeNode) node) |
&& tree.isExpanded(new TreePath(getPathToRoot(node, 0)))) |
611 |
.getPath()))) |
node = getNextNode(node); |
|
node = node.getNextNode(); |
|
612 |
|
|
613 |
for (int i = 0; i < row; i++) |
for (int i = 0; i < row; i++) |
614 |
node = getNextVisibleNode(node); |
node = getNextVisibleNode(node); |
618 |
return null; |
return null; |
619 |
|
|
620 |
// something was found |
// something was found |
621 |
return new TreePath(node.getPath()); |
return new TreePath(getPathToRoot(node, 0)); |
622 |
} |
} |
623 |
|
|
624 |
/** |
/** |
647 |
*/ |
*/ |
648 |
public int getRowCount(JTree tree) |
public int getRowCount(JTree tree) |
649 |
{ |
{ |
650 |
DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (tree.getModel()) |
TreeNode node = ((TreeNode) (tree.getModel()) |
651 |
.getRoot()); |
.getRoot()); |
652 |
if (!tree.isRootVisible() |
if (!tree.isRootVisible() |
653 |
&& tree.isExpanded(new TreePath(((DefaultMutableTreeNode) node) |
&& tree.isExpanded(new TreePath((getPathToRoot(node, 0))))) |
654 |
.getPath()))) |
node = getNextNode(node); |
|
node = node.getNextNode(); |
|
655 |
|
|
656 |
int count = 0; |
int count = 0; |
657 |
|
|
1305 |
while (node != null) |
while (node != null) |
1306 |
{ |
{ |
1307 |
count++; |
count++; |
1308 |
DefaultMutableTreeNode nextNode = getNextVisibleNode( |
TreeNode nextNode = getNextVisibleNode(node); |
|
new DefaultMutableTreeNode(node)); |
|
1309 |
if (nextNode != null) |
if (nextNode != null) |
1310 |
maxWidth = Math.max(maxWidth, (int) (getCellBounds(0, 0, nextNode) |
maxWidth = Math.max(maxWidth, (int) (getCellBounds(0, 0, nextNode) |
1311 |
.getWidth())); |
.getWidth())); |
1690 |
public void keyPressed(KeyEvent e) |
public void keyPressed(KeyEvent e) |
1691 |
{ |
{ |
1692 |
TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath(); |
TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath(); |
1693 |
DefaultMutableTreeNode last = null; |
TreeNode last = null; |
1694 |
if (start != null) |
if (start != null) |
1695 |
last = (DefaultMutableTreeNode) start.getLastPathComponent(); |
last = (TreeNode) start.getLastPathComponent(); |
1696 |
|
|
1697 |
if (last != null) |
if (last != null) |
1698 |
{ |
{ |
1700 |
if (e.getKeyCode() == KeyEvent.VK_DOWN |
if (e.getKeyCode() == KeyEvent.VK_DOWN |
1701 |
|| e.getKeyCode() == KeyEvent.VK_KP_DOWN) |
|| e.getKeyCode() == KeyEvent.VK_KP_DOWN) |
1702 |
{ |
{ |
1703 |
DefaultMutableTreeNode next = (DefaultMutableTreeNode) |
TreeNode next = (TreeNode) BasicTreeUI.this. |
1704 |
BasicTreeUI.this.getNextVisibleNode(last); |
getNextVisibleNode(last); |
1705 |
if (next != null) |
if (next != null) |
1706 |
{ |
{ |
1707 |
TreePath newPath = new TreePath(next.getPath()); |
TreePath newPath = new TreePath(getPathToRoot(next, 0)); |
1708 |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath); |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath); |
1709 |
if (e.isControlDown()) |
if (e.isControlDown()) |
1710 |
tree.setLeadSelectionPath(newPath); |
tree.setLeadSelectionPath(newPath); |
1719 |
else if (e.getKeyCode() == KeyEvent.VK_UP |
else if (e.getKeyCode() == KeyEvent.VK_UP |
1720 |
|| e.getKeyCode() == KeyEvent.VK_KP_UP) |
|| e.getKeyCode() == KeyEvent.VK_KP_UP) |
1721 |
{ |
{ |
1722 |
DefaultMutableTreeNode prev = (DefaultMutableTreeNode) |
TreeNode prev = (TreeNode) BasicTreeUI.this. |
1723 |
BasicTreeUI.this.getPreviousVisibleNode(last); |
getPreviousVisibleNode(last); |
1724 |
|
|
1725 |
if (prev != null) |
if (prev != null) |
1726 |
{ |
{ |
1727 |
TreePath newPath = new TreePath(prev.getPath()); |
TreePath newPath = new TreePath(getPathToRoot(prev, 0)); |
1728 |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
1729 |
new TreePath(prev.getPath())); |
new TreePath(getPathToRoot(prev, 0))); |
1730 |
if (e.isControlDown()) |
if (e.isControlDown()) |
1731 |
tree.setLeadSelectionPath(newPath); |
tree.setLeadSelectionPath(newPath); |
1732 |
else if (!prev.isLeaf() && e.isShiftDown()) |
else if (!prev.isLeaf() && e.isShiftDown()) |
1740 |
else if (e.getKeyCode() == KeyEvent.VK_LEFT |
else if (e.getKeyCode() == KeyEvent.VK_LEFT |
1741 |
|| e.getKeyCode() == KeyEvent.VK_KP_LEFT) |
|| e.getKeyCode() == KeyEvent.VK_KP_LEFT) |
1742 |
{ |
{ |
1743 |
TreePath path = new TreePath(last.getPath()); |
TreePath path = new TreePath(getPathToRoot(last, 0)); |
1744 |
DefaultMutableTreeNode p = (DefaultMutableTreeNode) last |
TreeNode p = last.getParent(); |
|
.getParent(); |
|
1745 |
|
|
1746 |
if (!last.isLeaf() && BasicTreeUI.this.tree.isExpanded(path)) |
if (!last.isLeaf() && BasicTreeUI.this.tree.isExpanded(path)) |
1747 |
{ |
{ |
1750 |
} |
} |
1751 |
else if (p != null) |
else if (p != null) |
1752 |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
1753 |
new TreePath(p.getPath())); |
new TreePath(getPathToRoot(p, 0))); |
1754 |
} |
} |
1755 |
// RIGHT, KP_RIGHT |
// RIGHT, KP_RIGHT |
1756 |
else if (e.getKeyCode() == KeyEvent.VK_RIGHT |
else if (e.getKeyCode() == KeyEvent.VK_RIGHT |
1757 |
|| e.getKeyCode() == KeyEvent.VK_KP_RIGHT) |
|| e.getKeyCode() == KeyEvent.VK_KP_RIGHT) |
1758 |
{ |
{ |
1759 |
TreePath path = new TreePath(last.getPath()); |
TreePath path = new TreePath(getPathToRoot(last, 0)); |
1760 |
|
|
1761 |
if (!last.isLeaf() && BasicTreeUI.this.tree.isCollapsed(path)) |
if (!last.isLeaf() && BasicTreeUI.this.tree.isCollapsed(path)) |
1762 |
{ |
{ |
1765 |
} |
} |
1766 |
else |
else |
1767 |
{ |
{ |
1768 |
DefaultMutableTreeNode next = (DefaultMutableTreeNode) |
TreeNode next = (TreeNode) BasicTreeUI.this. |
1769 |
BasicTreeUI.this.getNextVisibleNode(last); |
getNextVisibleNode(last); |
1770 |
|
|
1771 |
if (next != null) |
if (next != null) |
1772 |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, |
1773 |
new TreePath(next.getPath())); |
new TreePath(getPathToRoot(next, 0))); |
1774 |
} |
} |
1775 |
} |
} |
1776 |
// Enter |
// Enter |
1777 |
else if (e.getKeyCode() == KeyEvent.VK_ENTER) |
else if (e.getKeyCode() == KeyEvent.VK_ENTER) |
1778 |
{ |
{ |
1779 |
TreePath path = new TreePath(last.getPath()); |
TreePath path = new TreePath(getPathToRoot(last, 0)); |
1780 |
if (!last.isLeaf()) |
if (!last.isLeaf()) |
1781 |
{ |
{ |
1782 |
if (BasicTreeUI.this.tree.isExpanded(path)) |
if (BasicTreeUI.this.tree.isExpanded(path)) |
2529 |
if (startNode == null || startNode.equals(node)) |
if (startNode == null || startNode.equals(node)) |
2530 |
{ |
{ |
2531 |
if (!tree.isRootVisible() && tree.isExpanded(new TreePath(mod.getRoot()))) |
if (!tree.isRootVisible() && tree.isExpanded(new TreePath(mod.getRoot()))) |
2532 |
return new Point(x + ((((DefaultMutableTreeNode) node).getLevel()) |
return new Point(x + ((getLevel((TreeNode) node)) * |
2533 |
* rightChildIndent), y); |
rightChildIndent), y); |
2534 |
|
|
2535 |
return new Point(x + ((((DefaultMutableTreeNode) node).getLevel() + 1) |
return new Point(x + ((getLevel((TreeNode) node) + 1) |
2536 |
* rightChildIndent), y); |
* rightChildIndent), y); |
2537 |
} |
} |
2538 |
|
|
2539 |
if (!mod.isLeaf(startNode) |
if (!mod.isLeaf(startNode) |
2540 |
&& tree.isExpanded(new TreePath( |
&& tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) startNode), |
2541 |
((DefaultMutableTreeNode) startNode).getPath()))) |
0)))) |
2542 |
{ |
{ |
2543 |
Object child = mod.getChild(startNode, 0); |
Object child = mod.getChild(startNode, 0); |
2544 |
if (child != null) |
if (child != null) |
2547 |
} |
} |
2548 |
|
|
2549 |
return getCellLocation(x, y + rowHeight, tree, mod, node, |
return getCellLocation(x, y + rowHeight, tree, mod, node, |
2550 |
getNextVisibleNode((DefaultMutableTreeNode) startNode)); |
getNextVisibleNode((TreeNode) startNode)); |
2551 |
} |
} |
2552 |
|
|
2553 |
/** |
/** |
2562 |
void paintNode(Graphics g, int x, int y, JTree tree, Object node, |
void paintNode(Graphics g, int x, int y, JTree tree, Object node, |
2563 |
boolean isLeaf) |
boolean isLeaf) |
2564 |
{ |
{ |
2565 |
TreePath curr = new TreePath(((DefaultMutableTreeNode) node).getPath()); |
TreePath curr = new TreePath(getPathToRoot(((TreeNode) node), 0)); |
2566 |
boolean selected = tree.isPathSelected(curr); |
boolean selected = tree.isPathSelected(curr); |
2567 |
boolean expanded = false; |
boolean expanded = false; |
2568 |
boolean hasIcons = false; |
boolean hasIcons = false; |
2569 |
|
|
2570 |
if (tree.isVisible(curr)) |
if (tree.isVisible(curr)) |
2571 |
{ |
{ |
2572 |
DefaultTreeCellRenderer dtcr = (DefaultTreeCellRenderer) tree |
DefaultTreeCellRenderer dtcr = (DefaultTreeCellRenderer) |
2573 |
.getCellRenderer(); |
createDefaultCellRenderer(); |
2574 |
|
|
2575 |
if (!isLeaf) |
if (!isLeaf) |
2576 |
expanded = tree.isExpanded(curr); |
expanded = tree.isExpanded(curr); |
2661 |
} |
} |
2662 |
|
|
2663 |
int max = mod.getChildCount(curr); |
int max = mod.getChildCount(curr); |
2664 |
if (tree.isExpanded(new TreePath(((DefaultMutableTreeNode) curr) |
if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) curr), 0)))) |
|
.getPath()))) |
|
2665 |
{ |
{ |
2666 |
for (int i = 0; i < max; ++i) |
for (int i = 0; i < max; ++i) |
2667 |
{ |
{ |
2678 |
} |
} |
2679 |
|
|
2680 |
descent = paintRecursive(g, indent, |
descent = paintRecursive(g, indent, |
2681 |
descent, i, depth + 1, tree, mod, mod.getChild(curr, i)); |
descent, i, depth + 1, tree, mod, |
2682 |
|
mod.getChild(curr, i)); |
2683 |
} |
} |
2684 |
} |
} |
2685 |
} |
} |
2686 |
|
|
2687 |
if (tree.isExpanded(new TreePath(((DefaultMutableTreeNode) curr) |
if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) curr), 0)))) |
|
.getPath()))) |
|
2688 |
if (y0 != heightOfLine) |
if (y0 != heightOfLine) |
2689 |
{ |
{ |
2690 |
g.setColor(getHashColor()); |
g.setColor(getHashColor()); |
2732 |
descent += rowHeight; |
descent += rowHeight; |
2733 |
|
|
2734 |
int max = mod.getChildCount(node); |
int max = mod.getChildCount(node); |
2735 |
if (tree.isExpanded(new TreePath(((DefaultMutableTreeNode) node) |
if (tree.isExpanded(new TreePath(getPathToRoot(((TreeNode) node), 0)))) |
|
.getPath()))) |
|
2736 |
{ |
{ |
2737 |
if (!node.equals(mod.getRoot())) |
if (!node.equals(mod.getRoot())) |
2738 |
ei.paintIcon(tree, g, indentation - rightChildIndent - 3, h); |
drawCentered(tree, g, ei, indentation - rightChildIndent - 3, h); |
2739 |
|
|
2740 |
for (int i = 0; i < max; ++i) |
for (int i = 0; i < max; ++i) |
2741 |
{ |
{ |
2744 |
indent = -1; |
indent = -1; |
2745 |
|
|
2746 |
descent = paintControlIcons(g, indent, |
descent = paintControlIcons(g, indent, |
2747 |
descent, i, depth + 1, tree, mod, mod.getChild(node, i)); |
descent, i, depth + 1, tree, mod, |
2748 |
|
mod.getChild(node, i)); |
2749 |
} |
} |
2750 |
} |
} |
2751 |
else if (!node.equals(mod.getRoot())) |
else if (!node.equals(mod.getRoot())) |
2752 |
ci.paintIcon(tree, g, indentation - rightChildIndent - 3, |
drawCentered(tree, g, ei, indentation - rightChildIndent - 3, |
2753 |
descent - getRowHeight()); |
descent - getRowHeight()); |
2754 |
} |
} |
2755 |
|
|
2780 |
* @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 |
2781 |
* more. |
* more. |
2782 |
*/ |
*/ |
2783 |
DefaultMutableTreeNode getNextVisibleNode(DefaultMutableTreeNode node) |
TreeNode getNextVisibleNode(TreeNode node) |
2784 |
{ |
{ |
2785 |
DefaultMutableTreeNode next = null; |
TreeNode next = null; |
2786 |
TreePath current = null; |
TreePath current = null; |
2787 |
|
|
2788 |
if (node != null) |
if (node != null) |
2789 |
next = node.getNextNode(); |
next = getNextNode(node); |
2790 |
|
|
2791 |
if (next != null) |
if (next != null) |
2792 |
{ |
{ |
2793 |
current = new TreePath(next.getPath()); |
current = new TreePath(getPathToRoot(next, 0)); |
2794 |
if (tree.isVisible(current)) |
if (tree.isVisible(current)) |
2795 |
return next; |
return next; |
2796 |
|
|
2797 |
while (next != null && !tree.isVisible(current)) |
while (next != null && !tree.isVisible(current)) |
2798 |
{ |
{ |
2799 |
next = next.getNextNode(); |
next = getNextNode(next); |
2800 |
|
|
2801 |
if (next != null) |
if (next != null) |
2802 |
current = new TreePath(next.getPath()); |
current = new TreePath(getPathToRoot(next, 0)); |
2803 |
} |
} |
2804 |
} |
} |
2805 |
return next; |
return next; |
2813 |
* @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 |
2814 |
* more. |
* more. |
2815 |
*/ |
*/ |
2816 |
DefaultMutableTreeNode getPreviousVisibleNode |
TreeNode getPreviousVisibleNode(TreeNode node) |
|
(DefaultMutableTreeNode node) |
|
2817 |
{ |
{ |
2818 |
DefaultMutableTreeNode prev = null; |
TreeNode prev = null; |
2819 |
TreePath current = null; |
TreePath current = null; |
2820 |
|
|
2821 |
if (node != null) |
if (node != null) |
2822 |
prev = node.getPreviousNode(); |
prev = getPreviousNode(node); |
2823 |
|
|
2824 |
if (prev != null) |
if (prev != null) |
2825 |
{ |
{ |
2826 |
current = new TreePath(prev.getPath()); |
current = new TreePath(getPathToRoot(prev, 0)); |
2827 |
if (tree.isVisible(current)) |
if (tree.isVisible(current)) |
2828 |
return prev; |
return prev; |
2829 |
|
|
2830 |
while (prev != null && !tree.isVisible(current)) |
while (prev != null && !tree.isVisible(current)) |
2831 |
{ |
{ |
2832 |
prev = prev.getPreviousNode(); |
prev = getPreviousNode(prev); |
2833 |
|
|
2834 |
if (prev != null) |
if (prev != null) |
2835 |
current = new TreePath(prev.getPath()); |
current = new TreePath(getPathToRoot(prev, 0)); |
2836 |
} |
} |
2837 |
} |
} |
2838 |
return prev; |
return prev; |
2839 |
} |
} |
2840 |
|
|
2841 |
/** |
/** |
2842 |
|
* Returns the next node in the tree |
2843 |
|
* Package private for use in inner classes. |
2844 |
|
* |
2845 |
|
* @return TreeNode, the next node in the tree |
2846 |
|
*/ |
2847 |
|
TreeNode getNextNode(TreeNode curr) |
2848 |
|
{ |
2849 |
|
if (curr.getChildCount() != 0) |
2850 |
|
return curr.getChildAt(0); |
2851 |
|
|
2852 |
|
TreeNode node = curr; |
2853 |
|
TreeNode sibling; |
2854 |
|
|
2855 |
|
do |
2856 |
|
{ |
2857 |
|
sibling = getNextSibling(node); |
2858 |
|
node = node.getParent(); |
2859 |
|
} |
2860 |
|
while (sibling == null && |
2861 |
|
node != null); |
2862 |
|
|
2863 |
|
return sibling; |
2864 |
|
} |
2865 |
|
|
2866 |
|
/** |
2867 |
|
* Returns the previous node in the tree |
2868 |
|
* Package private for use in inner classes. |
2869 |
|
* |
2870 |
|
* @return TreeNode, the previous node in the tree |
2871 |
|
*/ |
2872 |
|
TreeNode getPreviousNode(TreeNode node) |
2873 |
|
{ |
2874 |
|
TreeNode parent = node.getParent(); |
2875 |
|
if (parent == null) |
2876 |
|
return null; |
2877 |
|
|
2878 |
|
TreeNode sibling = getPreviousSibling(node); |
2879 |
|
|
2880 |
|
if (sibling == null) |
2881 |
|
return parent; |
2882 |
|
|
2883 |
|
int size = sibling.getChildCount(); |
2884 |
|
while (size > 0) |
2885 |
|
{ |
2886 |
|
sibling = sibling.getChildAt(size - 1); |
2887 |
|
size = sibling.getChildCount(); |
2888 |
|
} |
2889 |
|
|
2890 |
|
return sibling; |
2891 |
|
} |
2892 |
|
|
2893 |
|
/** |
2894 |
|
* Returns the next sibling in the tree |
2895 |
|
* Package private for use in inner classes. |
2896 |
|
* |
2897 |
|
* @return TreeNode, the next sibling in the tree |
2898 |
|
*/ |
2899 |
|
TreeNode getNextSibling(TreeNode node) |
2900 |
|
{ |
2901 |
|
TreeNode parent = node.getParent(); |
2902 |
|
if (parent == null) |
2903 |
|
return null; |
2904 |
|
|
2905 |
|
int index = parent.getIndex(node) + 1; |
2906 |
|
|
2907 |
|
if (index == parent.getChildCount()) |
2908 |
|
return null; |
2909 |
|
|
2910 |
|
return parent.getChildAt(index); |
2911 |
|
} |
2912 |
|
|
2913 |
|
/** |
2914 |
|
* Returns the previous sibling in the tree |
2915 |
|
* Package private for use in inner classes. |
2916 |
|
* |
2917 |
|
* @return TreeNode, the previous sibling in the tree |
2918 |
|
*/ |
2919 |
|
TreeNode getPreviousSibling(TreeNode node) |
2920 |
|
{ |
2921 |
|
TreeNode parent = node.getParent(); |
2922 |
|
if (parent == null) |
2923 |
|
return null; |
2924 |
|
|
2925 |
|
int index = parent.getIndex(node) - 1; |
2926 |
|
|
2927 |
|
if (index < 0) |
2928 |
|
return null; |
2929 |
|
|
2930 |
|
return parent.getChildAt(index); |
2931 |
|
} |
2932 |
|
|
2933 |
|
/** |
2934 |
* Selects the specified path in the tree depending on modes. |
* Selects the specified path in the tree depending on modes. |
2935 |
* Package private for use in inner classes. |
* Package private for use in inner classes. |
2936 |
* |
* |
2964 |
} |
} |
2965 |
} |
} |
2966 |
} |
} |
2967 |
|
|
2968 |
|
/** |
2969 |
|
* Returns the path from node to the root. |
2970 |
|
* Package private for use in inner classes. |
2971 |
|
* |
2972 |
|
* @param node the node to get the path to |
2973 |
|
* @param depth the depth of the tree to return a path for |
2974 |
|
* |
2975 |
|
* @return an array of tree nodes that represent the path to node. |
2976 |
|
*/ |
2977 |
|
TreeNode[] getPathToRoot(TreeNode node, int depth) |
2978 |
|
{ |
2979 |
|
if (node == null) |
2980 |
|
{ |
2981 |
|
if (depth == 0) |
2982 |
|
return null; |
2983 |
|
|
2984 |
|
return new TreeNode[depth]; |
2985 |
|
} |
2986 |
|
|
2987 |
|
TreeNode[] path = getPathToRoot(node.getParent(), depth + 1); |
2988 |
|
path[path.length - depth - 1] = node; |
2989 |
|
return path; |
2990 |
|
} |
2991 |
|
|
2992 |
|
/** |
2993 |
|
* Returns the level of the node in the tree. |
2994 |
|
* |
2995 |
|
* @return the number of the level |
2996 |
|
*/ |
2997 |
|
public int getLevel(TreeNode node) |
2998 |
|
{ |
2999 |
|
int count = -1; |
3000 |
|
TreeNode current = node; |
3001 |
|
|
3002 |
|
do |
3003 |
|
{ |
3004 |
|
current = current.getParent(); |
3005 |
|
count++; |
3006 |
|
} |
3007 |
|
while (current != null); |
3008 |
|
|
3009 |
|
return count; |
3010 |
|
} |
3011 |
|
|
3012 |
|
/** |
3013 |
|
* Draws a vertical line using the given graphic context |
3014 |
|
* |
3015 |
|
* @param g is the graphic context |
3016 |
|
* @param c is the component the new line will belong to |
3017 |
|
* @param x is the horizonal position |
3018 |
|
* @param top specifies the top of the line |
3019 |
|
* @param bottom specifies the bottom of the line |
3020 |
|
*/ |
3021 |
|
protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) { |
3022 |
|
g.drawLine(x, top, x, bottom); |
3023 |
|
} |
3024 |
|
|
3025 |
|
/** |
3026 |
|
* Draws a horizontal line using the given graphic context |
3027 |
|
* |
3028 |
|
* @param g is the graphic context |
3029 |
|
* @param c is the component the new line will belong to |
3030 |
|
* @param y is the vertical position |
3031 |
|
* @param left specifies the left point of the line |
3032 |
|
* @param right specifies the right point of the line |
3033 |
|
*/ |
3034 |
|
protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) { |
3035 |
|
g.drawLine(left, y, right, y); |
3036 |
|
} |
3037 |
|
|
3038 |
|
/** |
3039 |
|
* Draws an icon at around a specific position |
3040 |
|
* |
3041 |
|
* @param c is the component the new line will belong to |
3042 |
|
* @param g is the graphic context |
3043 |
|
* @param icon is the icon which will be drawn |
3044 |
|
* @param x is the center position in x-direction |
3045 |
|
* @param y is the center position in y-direction |
3046 |
|
* |
3047 |
|
* FIXME what to do if x < (icon.width / 2). Same with y |
3048 |
|
*/ |
3049 |
|
protected void drawCentered(JComponent c, Graphics g, Icon icon, int x, int y) { |
3050 |
|
int beginPositionX = x - icon.getIconWidth() / 2; |
3051 |
|
int beginPositionY = y - icon.getIconHeight() / 2; |
3052 |
|
icon.paintIcon(c, g, beginPositionX, beginPositionY); |
3053 |
|
} |
3054 |
} // BasicTreeUI |
} // BasicTreeUI |