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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2006-2013 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
  46. * to add a listener to trigger the scrolling behaviour either by adding a
  47. * mouse wheel listener or by triggering {@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
  59. * list.
  60. *
  61. * @param list List to scroll over
  62. *
  63. * @since 0.6.6
  64. */
  65. public static void register(final JList list) {
  66. final ListScroller scroller = new ListScroller(list);
  67. list.addMouseWheelListener(scroller);
  68. }
  69. /**
  70. * Creates a new instance of ListScroller and attaches it to the specified
  71. * model and selection model. You will need to manually need to add a
  72. * listener to trigger the scrolling behaviour either by adding a mouse
  73. * wheel listener or by triggering {@link changeFocus(boolean)}.
  74. *
  75. * @param model List model to scroll over
  76. * @param selectionModel List selection model to scroll over
  77. *
  78. * @since 0.6.6
  79. */
  80. public static void register(final ListModel model,
  81. final ListSelectionModel selectionModel) {
  82. new ListScroller(model, selectionModel); //NOPMD
  83. }
  84. /**
  85. * {@inheritDoc}
  86. *
  87. * @param e Mouse wheel event
  88. */
  89. @Override
  90. public void mouseWheelMoved(final MouseWheelEvent e) {
  91. if (e.getWheelRotation() < 0) {
  92. changeFocus(true);
  93. } else {
  94. changeFocus(false);
  95. }
  96. }
  97. /**
  98. * Activates the node above or below the active node in the list.
  99. *
  100. * @param direction true = up, false = down.
  101. */
  102. public void changeFocus(final boolean direction) {
  103. int index;
  104. //are we going up or down?
  105. if (direction) {
  106. //up
  107. index = changeFocusUp(selectionModel.getMinSelectionIndex());
  108. } else {
  109. //down
  110. index = changeFocusDown(selectionModel.getMinSelectionIndex());
  111. }
  112. selectionModel.setSelectionInterval(index, index);
  113. }
  114. /**
  115. * Changes the list focus up.
  116. *
  117. * @param index Start index
  118. *
  119. * @return next index
  120. */
  121. private int changeFocusUp(final int index) {
  122. int nextIndex;
  123. if (index == 0 || index == -1) {
  124. nextIndex = model.getSize() - 1;
  125. } else {
  126. nextIndex = index - 1;
  127. }
  128. return nextIndex;
  129. }
  130. /**
  131. * Changes the list focus down.
  132. *
  133. * @param index Start index
  134. *
  135. * @return next index
  136. */
  137. private int changeFocusDown(final int index) {
  138. int nextIndex;
  139. if (index == model.getSize() - 1) {
  140. nextIndex = 0;
  141. } else {
  142. nextIndex = index + 1;
  143. }
  144. return nextIndex;
  145. }
  146. }