Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ListScroller.java 4.9KB

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