File: TreeUtils.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 2,003
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (168 lines) | stat: -rw-r--r-- 6,164 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.explodingpixels.widgets;

import java.awt.Rectangle;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.TreePath;

public class TreeUtils {

    // TODO add Javadoc.

    private TreeUtils() {
        // no constructor - utility class.
    }

    /**
     * Set's the collapsed icon to use for the given {@link JTree} if that
     * tree's UI delegate exetends from {@link BasicTreeUI}. If the given tree's
     * UI delegate does not extend from {@code BasicTreeUI} then the given
     * tree will not be changed.
     *
     * @param tree the tree to set the collapsed icon for.
     * @param icon the new collapsed icon to use.
     * @see BasicTreeUI#setCollapsedIcon(javax.swing.Icon)
     */
    public static void setCollapsedIcon(JTree tree, Icon icon) {
        if (tree.getUI() instanceof BasicTreeUI) {
            ((BasicTreeUI) tree.getUI()).setCollapsedIcon(icon);
        }
    }

    /**
     * Set's the expanded icon to use for the given {@link JTree} if that
     * tree's UI delegate extends from {@link BasicTreeUI}. If the given tree's
     * UI delegate does not extend from {@code BasicTreeUI} then the given
     * tree will not be changed.
     *
     * @param tree the tree to set the expanded icon for.
     * @param icon the new collapsed icon to use.
     * @see BasicTreeUI#setExpandedIcon(javax.swing.Icon)
     */
    public static void setExpandedIcon(JTree tree, Icon icon) {
        ((BasicTreeUI) tree.getUI()).setExpandedIcon(icon);
    }

    /**
     * Set's the left indent in pixels to use for the given {@link JTree}'s
     * collapsed and expanded icon. This value in conjuction with the right
     * indent comprises the total amount of space that the collapsed and
     * expanded icon draw into. If the given tree's UI delegate does not extend
     * from {@code BasicTreeUI} then the given tree will not be changed.
     *
     * @param tree   the tree to set the left indent for.
     * @param indent the new left indent in pixels.
     * @see BasicTreeUI#setLeftChildIndent(int)
     */
    public static void setLeftChildIndent(JTree tree, int indent) {
        ((BasicTreeUI) tree.getUI()).setLeftChildIndent(indent);
    }

    /**
     * Set's the right indent in pixels to use for the given {@link JTree}'s
     * collapsed and expanded icon. This value in conjuction with the left
     * indent comprises the total amount of space that the collapsed and
     * expanded icon draw into. If the given tree's UI delegate does not extend
     * from {@code BasicTreeUI} then the given tree will not be changed.
     *
     * @param tree   the tree to set the right indent for.
     * @param indent the new left indent in pixels.
     * @see BasicTreeUI#setRightChildIndent(int)
     */
    public static void setRightChildIndent(JTree tree, int indent) {
        ((BasicTreeUI) tree.getUI()).setRightChildIndent(indent);
    }

    /**
     * Repaints the selection. This allows the row selection to have a
     * background color that changes based on the focus state of the
     * component.
     *
     * @param tree the {@code JTree} to repaint the selection of.
     */
    public static void repaintSelection(JTree tree) {
        int[] selectedRows = tree.getSelectionRows();

        // if there is a selected row, then repaint it.
        if (selectedRows != null && selectedRows.length > 0) {
            // grab the bounds of the first and last selected cells in column
            // one (index zero).
            Rectangle firstSelectedCell = tree.getRowBounds(selectedRows[0]);
            Rectangle lastSelectedCell =
                    tree.getRowBounds(selectedRows[selectedRows.length - 1]);

            // create the rectangle to repaint by unioning the first and last
            // selected cells in column one and then extending that rectangle
            // to extend from the left edge of the table all the way to the
            // right edge.
            Rectangle repaintRectangle = firstSelectedCell.union(lastSelectedCell);
            repaintRectangle.x = 0;
            repaintRectangle.width = tree.getWidth();

            // repaint the selection area.
            tree.repaint(repaintRectangle);
        }

    }

    public static void setExpandedOnEdt(JTree tree, TreePath path, boolean expanded) {    	
        if (expanded) {
            expandPathOnEdt(tree, path);
        } else {
            collapsePathOnEdt(tree, path);
        }
    }

    public static void expandPathOnEdt(final JTree tree, final TreePath path) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	if ((tree == null) || (path == null))
            		return;
            	tree.expandPath(path);
            }
        });
    }

    public static void collapsePathOnEdt(final JTree tree, final TreePath path) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	if ((tree == null) || (path == null))
                	return;

                tree.collapsePath(path);
            }
        });
    }

    public static void installRootExpandingTreeModelListener(final JTree tree) {

        TreeModelListener listener = new TreeModelListener() {

            public void treeNodesChanged(TreeModelEvent e) {
            }

            public void treeNodesInserted(TreeModelEvent e) {
                // if this is the root node, and it hasn't yet been expanded
                // (because no children have been added, then expand the root now).
                if (e.getTreePath().getParentPath() == null
                        && tree.isCollapsed(e.getTreePath())) {
                    TreeUtils.expandPathOnEdt(tree, e.getTreePath());
                }
            }

            public void treeNodesRemoved(TreeModelEvent e) {
            }

            public void treeStructureChanged(TreeModelEvent e) {
            }
        };

        tree.getModel().addTreeModelListener(listener);
    }

}