File Source: TreeScroller.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.TreeScroller__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 package com.dmdirc.addons.ui_swing.components;
23
24 import java.awt.event.MouseWheelEvent;
25 import java.awt.event.MouseWheelListener;
26
27 import javax.swing.JTree;
28 import javax.swing.tree.DefaultMutableTreeNode;
29 import javax.swing.tree.DefaultTreeModel;
30 import javax.swing.tree.TreePath;
31 import javax.swing.tree.TreeSelectionModel;
32
33 /**
34 * Utility class to provide mouse wheel scrolling to a JTree.
35 */
36 public class TreeScroller implements MouseWheelListener {
37
38 /** Tree to scroll. */
39 private final DefaultTreeModel model;
40 /** Tree to scroll. */
41 private final TreeSelectionModel selectionModel;
42 /** Root visible. */
43 private final boolean rootVisible;
44 /** Root node. */
45 private final DefaultMutableTreeNode rootNode;
46 /** Tree. */
47 protected JTree tree;
48
49 /**
50 * Creates a new instance of TreeScroller.
51 *
52 * @param tree Tree to scroll over
53 */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.TreeScroller(JTree)
*
* Preconditions:
* tree != null
*
* Presumptions:
* javax.swing.JTree:getModel(...)@60 != null
*
* Postconditions:
* init'ed(this.model)
* init'ed(this.rootNode)
* init'ed(this.rootVisible)
* init'ed(this.selectionModel)
* this.tree == tree
* this.tree != null
*/
54 public TreeScroller(final JTree tree) {
55 this.tree = tree;
56 this.model = (DefaultTreeModel) tree.getModel();
57 this.selectionModel = tree.getSelectionModel();
58
59 rootVisible = tree.isRootVisible();
60 rootNode =
61 (DefaultMutableTreeNode) tree.getModel().getRoot();
62
63 tree.addMouseWheelListener(this);
64 }
65
66 /**
67 * Creates a new instance of TreeScroller.
68 *
69 * @param model Tree model to scroll over
70 * @param selectionModel Tree selection model to scroll over
71 * @param rootVisible Is the root node visible
72 */
73 public TreeScroller(final DefaultTreeModel model,
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.TreeScroller(DefaultTreeModel, TreeSelectionModel, bool)
*
* Preconditions:
* model != null
*
* Postconditions:
* this.model == model
* this.model != null
* init'ed(this.rootNode)
* this.rootVisible == rootVisible
* init'ed(this.rootVisible)
* this.selectionModel == selectionModel
* init'ed(this.selectionModel)
*/
74 final TreeSelectionModel selectionModel, final boolean rootVisible) {
75 this.model = model;
76 this.selectionModel = selectionModel;
77
78 this.rootVisible = rootVisible;
79 rootNode = (DefaultMutableTreeNode) model.getRoot();
80 }
81
82 /**
83 * {@inheritDoc}
84 *
85 * @param e Mouse wheel event
86 */
87 @Override
88 public void mouseWheelMoved(final MouseWheelEvent e) {
/*
P/P * Method: void mouseWheelMoved(MouseWheelEvent)
*
* Preconditions:
* e != null
* (soft) this.model != null
* (soft) this.selectionModel != null
*
* Test Vectors:
* java.awt.event.MouseWheelEvent:getWheelRotation(...)@89: {0..232-1}, {-231..-1}
*/
89 if (e.getWheelRotation() < 0) {
90 changeFocus(true);
91 } else {
92 changeFocus(false);
93 }
94 }
95
96 /**
97 * Activates the node above or below the active node in the tree.
98 *
99 * @param direction true = up, false = down.
100 */
101 public void changeFocus(final boolean direction) {
102 DefaultMutableTreeNode thisNode;
103 DefaultMutableTreeNode nextNode;
104
/*
P/P * Method: void changeFocus(bool)
*
* Preconditions:
* (soft) this.model != null
* (soft) this.selectionModel != null
*
* Presumptions:
* javax.swing.tree.DefaultMutableTreeNode:getChildAt(...)@119 != null
* javax.swing.tree.TreePath:getLastPathComponent(...)@122 != null
* javax.swing.tree.TreeSelectionModel:getSelectionPath(...)@122 != null
*
* Test Vectors:
* direction: {0}, {1}
* this.rootNode: Inverse{null}, Addr_Set{null}
* this.rootVisible: {1}, {0}
* javax.swing.tree.DefaultMutableTreeNode:getChildCount(...)@110: {-231..-1, 1..232-1}, {0}
* javax.swing.tree.TreeSelectionModel:isSelectionEmpty(...)@115: {0}, {1}
*/
105 if (rootNode == null) {
106 //no root node or root node not visible
107 return;
108 }
109
110 if (!rootVisible && rootNode.getChildCount() == 0) {
111 //root node has no children
112 return;
113 }
114
115 if (selectionModel.isSelectionEmpty()) {
116 if (rootVisible) {
117 thisNode = rootNode;
118 } else {
119 thisNode = (DefaultMutableTreeNode) rootNode.getChildAt(0);
120 }
121 } else {
122 thisNode = (DefaultMutableTreeNode) selectionModel.getSelectionPath().getLastPathComponent();
123 }
124
125 //are we going up or down?
126 if (direction) {
127 //up
128 nextNode = changeFocusUp(thisNode);
129 } else {
130 //down
131 nextNode = changeFocusDown(thisNode);
132 }
133 setPath(new TreePath(model.getPathToRoot(nextNode)));
134 }
135
136 /**
137 * Sets the tree selection path.
138 *
139 * @param path Path
140 */
141 protected void setPath(final TreePath path) {
/*
P/P * Method: void setPath(TreePath)
*
* Preconditions:
* this.selectionModel != null
*/
142 selectionModel.setSelectionPath(path);
143 }
144
145 /**
146 * Changes the tree focus up.
147 *
148 * @param node Start node
149 *
150 * @return next node
151 */
152 private DefaultMutableTreeNode changeFocusUp(final DefaultMutableTreeNode node) {
153 DefaultMutableTreeNode nextNode;
154
/*
P/P * Method: DefaultMutableTreeNode changeFocusUp(DefaultMutableTreeNode)
*
* Preconditions:
* node != null
* (soft) this.model != null
* (soft) this.rootNode != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.rootVisible: {1}, {0}
* javax.swing.tree.DefaultMutableTreeNode:getPreviousNode(...)@155: Addr_Set{null}, Inverse{null}
*/
155 nextNode = node.getPreviousNode();
156
157 if (nextNode == null || (nextNode == model.getRoot() && !rootVisible)) {
158 nextNode = rootNode.getLastLeaf();
159 }
160
161 return nextNode;
162 }
163
164 /**
165 * Changes the tree focus down.
166 *
167 * @param node Start node
168 *
169 * @return next node
170 */
171 private DefaultMutableTreeNode changeFocusDown(final DefaultMutableTreeNode node) {
172 DefaultMutableTreeNode nextNode;
173
/*
P/P * Method: DefaultMutableTreeNode changeFocusDown(DefaultMutableTreeNode)
*
* Preconditions:
* node != null
* (soft) this.model != null
* (soft) this.rootNode != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.rootVisible: {1}, {0}
* javax.swing.tree.DefaultMutableTreeNode:getNextNode(...)@174: Inverse{null}, Addr_Set{null}
*/
174 nextNode = node.getNextNode();
175
176 if (nextNode == null && !rootVisible) {
177 nextNode =
178 (DefaultMutableTreeNode) rootNode.getFirstChild();
179 } else if (nextNode == null) {
180 nextNode = (DefaultMutableTreeNode) model.getRoot();
181 }
182
183 return nextNode;
184 }
185 }
SofCheck Inspector Build Version : 2.17854
| TreeScroller.java |
2009-Jun-25 01:54:24 |
| TreeScroller.class |
2009-Sep-02 17:04:14 |