選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TreeScroller.java 6.4KB

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