File Source: ListScroller.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.ListScroller__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
23 package com.dmdirc.addons.ui_swing.components;
24
25 import java.awt.event.MouseWheelEvent;
26 import java.awt.event.MouseWheelListener;
27
28 import javax.swing.JList;
29 import javax.swing.ListModel;
30 import javax.swing.ListSelectionModel;
31
32 /**
33 * Utility class to provide mouse wheel scrolling to a JList.
34 */
35 public class ListScroller implements MouseWheelListener {
36
37 /** List to scroll. */
38 private final ListModel model;
39 /** List to scroll. */
40 private final ListSelectionModel selectionModel;
41
42 /**
43 * Creates a new instance of ListScroller.
44 *
45 * @param list List to scroll over
46 */
47 public ListScroller(final JList list) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ListScroller(JList)
*
* Preconditions:
* list != null
*
* Postconditions:
* init'ed(this.model)
* init'ed(this.selectionModel)
*/
48 this(list.getModel(), list.getSelectionModel());
49
50 list.addMouseWheelListener(this);
51 }
52
53 /**
54 * Creates a new instance of ListScroller.
55 *
56 * @param model List model to scroll over
57 * @param selectionModel List selection model to scroll over
58 */
59 public ListScroller(final ListModel model,
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ListScroller(ListModel, ListSelectionModel)
*
* Postconditions:
* this.model == model
* init'ed(this.model)
* this.selectionModel == selectionModel
* init'ed(this.selectionModel)
*/
60 final ListSelectionModel selectionModel) {
61 this.model = model;
62 this.selectionModel = selectionModel;
63 }
64
65 /**
66 * {@inheritDoc}
67 *
68 * @param e Moust wheel event
69 */
70 @Override
71 public void mouseWheelMoved(final MouseWheelEvent e) {
/*
P/P * Method: void mouseWheelMoved(MouseWheelEvent)
*
* Preconditions:
* e != null
* this.selectionModel != null
* (soft) this.model != null
*
* Test Vectors:
* java.awt.event.MouseWheelEvent:getWheelRotation(...)@72: {0..232-1}, {-231..-1}
*/
72 if (e.getWheelRotation() < 0) {
73 changeFocus(true);
74 } else {
75 changeFocus(false);
76 }
77 }
78
79 /**
80 * Activates the node above or below the active node in the list.
81 *
82 * @param direction true = up, false = down.
83 */
84 public void changeFocus(final boolean direction) {
85 int index;
86
87 //are we going up or down?
/*
P/P * Method: void changeFocus(bool)
*
* Preconditions:
* this.selectionModel != null
* (soft) this.model != null
*
* Presumptions:
* javax.swing.ListSelectionModel:getMinSelectionIndex(...)@90 >= -231+1
* javax.swing.ListSelectionModel:getMinSelectionIndex(...)@93 <= 232-2
*
* Test Vectors:
* direction: {0}, {1}
*/
88 if (direction) {
89 //up
90 index = changeFocusUp(selectionModel.getMinSelectionIndex());
91 } else {
92 //down
93 index = changeFocusDown(selectionModel.getMinSelectionIndex());
94 }
95 selectionModel.setSelectionInterval(index, index);
96 }
97
98 /**
99 * Changes the list focus up.
100 *
101 * @param index Start index
102 *
103 * @return next index
104 */
105 private int changeFocusUp(final int index) {
106 int nextIndex;
107
/*
P/P * Method: int changeFocusUp(int)
*
* Preconditions:
* index >= -231+1
* (soft) this.model != null
*
* Presumptions:
* javax.swing.ListModel:getSize(...)@109 >= -231+1
*
* Postconditions:
* return_value <= 232-2
*
* Test Vectors:
* index: {0}, {-231+1..-2, 1..232-1}, {-1}
*/
108 if (index == 0 || index == -1) {
109 nextIndex = model.getSize() - 1;
110 } else {
111 nextIndex = index - 1;
112 }
113
114 return nextIndex;
115 }
116
117 /**
118 * Changes the list focus down.
119 *
120 * @param index Start index
121 *
122 * @return next index
123 */
124 private int changeFocusDown(final int index) {
125 int nextIndex;
126
/*
P/P * Method: int changeFocusDown(int)
*
* Preconditions:
* index <= 232-2
* this.model != null
*
* Postconditions:
* return_value == One-of{0, index + 1}
* return_value >= -231+1
*/
127 if (index == model.getSize() - 1) {
128 nextIndex = 0;
129 } else {
130 nextIndex = index + 1;
131 }
132
133 return nextIndex;
134 }
135 }
SofCheck Inspector Build Version : 2.17854
| ListScroller.java |
2009-Jun-25 01:54:24 |
| ListScroller.class |
2009-Sep-02 17:04:15 |