You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListScroller.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 java.awt.event.MouseWheelEvent;
  19. import java.awt.event.MouseWheelListener;
  20. import javax.swing.JList;
  21. import javax.swing.ListModel;
  22. import javax.swing.ListSelectionModel;
  23. /**
  24. * Utility class to provide mouse wheel scrolling to a JList.
  25. */
  26. public class ListScroller implements MouseWheelListener {
  27. /** List to scroll. */
  28. private final ListModel<?> model;
  29. /** List to scroll. */
  30. private final ListSelectionModel selectionModel;
  31. /**
  32. * Creates a new instance of ListScroller.
  33. *
  34. * @param list List to scroll over
  35. */
  36. public ListScroller(final JList<?> list) {
  37. this(list.getModel(), list.getSelectionModel());
  38. }
  39. /**
  40. * Creates a new instance of ListScroller. You will need to manually need to add a listener to
  41. * trigger the scrolling behaviour either by adding a mouse wheel listener or by triggering
  42. * {@link #changeFocus(boolean)}.
  43. *
  44. * @param model List model to scroll over
  45. * @param selectionModel List selection model to scroll over
  46. */
  47. public ListScroller(final ListModel<?> model,
  48. final ListSelectionModel selectionModel) {
  49. this.model = model;
  50. this.selectionModel = selectionModel;
  51. }
  52. /**
  53. * Creates a new instance of ListScroller and attaches it to the specified list.
  54. *
  55. * @param list List to scroll over
  56. *
  57. * @since 0.6.6
  58. */
  59. public static void register(final JList<?> list) {
  60. final ListScroller scroller = new ListScroller(list);
  61. list.addMouseWheelListener(scroller);
  62. }
  63. /**
  64. * Creates a new instance of ListScroller and attaches it to the specified model and selection
  65. * model. You will need to manually need to add a listener to trigger the scrolling behaviour
  66. * either by adding a mouse wheel listener or by triggering {@link #changeFocus(boolean)}.
  67. *
  68. * @param model List model to scroll over
  69. * @param selectionModel List selection model to scroll over
  70. *
  71. * @since 0.6.6
  72. */
  73. public static void register(final ListModel<?> model,
  74. final ListSelectionModel selectionModel) {
  75. new ListScroller(model, selectionModel); //NOPMD
  76. }
  77. @Override
  78. public void mouseWheelMoved(final MouseWheelEvent e) {
  79. if (e.getWheelRotation() < 0) {
  80. changeFocus(true);
  81. } else {
  82. changeFocus(false);
  83. }
  84. }
  85. /**
  86. * Activates the node above or below the active node in the list.
  87. *
  88. * @param direction true = up, false = down.
  89. */
  90. public void changeFocus(final boolean direction) {
  91. final int index;
  92. //are we going up or down?
  93. if (direction) {
  94. //up
  95. index = changeFocusUp(selectionModel.getMinSelectionIndex());
  96. } else {
  97. //down
  98. index = changeFocusDown(selectionModel.getMinSelectionIndex());
  99. }
  100. selectionModel.setSelectionInterval(index, index);
  101. }
  102. /**
  103. * Changes the list focus up.
  104. *
  105. * @param index Start index
  106. *
  107. * @return next index
  108. */
  109. private int changeFocusUp(final int index) {
  110. final int nextIndex;
  111. if (index == 0 || index == -1) {
  112. nextIndex = model.getSize() - 1;
  113. } else {
  114. nextIndex = index - 1;
  115. }
  116. return nextIndex;
  117. }
  118. /**
  119. * Changes the list focus down.
  120. *
  121. * @param index Start index
  122. *
  123. * @return next index
  124. */
  125. private int changeFocusDown(final int index) {
  126. final int nextIndex;
  127. if (index == model.getSize() - 1) {
  128. nextIndex = 0;
  129. } else {
  130. nextIndex = index + 1;
  131. }
  132. return nextIndex;
  133. }
  134. }