Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
26   113   13   2.6
6   74   0.5   10
10     1.3  
1    
2.3% of code in this file is excluded from these metrics.
 
  JListScrollToItemTask       Line # 43 26 2.3% 13 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Nov 4, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.driver;
17   
18    import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
19    import static org.fest.swing.driver.JListCellBoundsQuery.cellBounds;
20    import static org.fest.swing.driver.JListCellCenterQuery.cellCenter;
21    import static org.fest.swing.driver.JListItemIndexValidator.validateIndex;
22    import static org.fest.swing.driver.JListMatchingItemQuery.matchingItemIndex;
23    import static org.fest.swing.edt.GuiActionRunner.execute;
24   
25    import java.awt.Point;
26    import java.awt.Rectangle;
27   
28    import javax.swing.JList;
29   
30    import org.fest.swing.annotation.RunsInCurrentThread;
31    import org.fest.swing.annotation.RunsInEDT;
32    import org.fest.swing.cell.JListCellReader;
33    import org.fest.swing.edt.GuiQuery;
34    import org.fest.swing.util.Pair;
35    import org.fest.swing.util.TextMatcher;
36   
37    /**
38    * Understands actions, executed in the event dispatch thread, that perform scrolling to an element in a
39    * <code>{@link JList}</code>.
40    *
41    * @author Alex Ruiz
42    */
 
43    final class JListScrollToItemTask {
44   
45    static final Pair<Integer, Point> ITEM_NOT_FOUND = new Pair<Integer, Point>(-1, null);
46   
 
47  12 toggle @RunsInEDT
48    // returns the point that the JList was scrolled to.
49    static Point scrollToItem(final JList list, final int index) {
50  12 return execute(new GuiQuery<Point>() {
 
51  12 toggle protected Point executeInEDT() {
52  12 validateIsEnabledAndShowing(list);
53  7 validateIndex(list, index);
54  7 return scrollToItemWithIndex(list, index);
55    }
56    });
57    }
58   
 
59  32 toggle @RunsInEDT
60    // returns the index of first matching element and the point that the JList was scrolled to.
61    static Pair<Integer, Point> scrollToItem(final JList list, final TextMatcher matcher, final JListCellReader cellReader) {
62  32 return execute(new GuiQuery<Pair<Integer, Point>>() {
 
63  32 toggle protected Pair<Integer, Point> executeInEDT() {
64  32 validateIsEnabledAndShowing(list);
65  15 int index = matchingItemIndex(list, matcher, cellReader);
66  2 if (index < 0) return ITEM_NOT_FOUND;
67  13 return new Pair<Integer, Point>(index, scrollToItemWithIndex(list, index));
68    }
69    });
70    }
71   
 
72  14 toggle @RunsInEDT
73    // returns the index of first matching element and the point that the JList was scrolled to.
74    static Pair<Integer, Point> scrollToItemIfNotSelectedYet(final JList list, final TextMatcher matcher,
75    final JListCellReader cellReader) {
76  14 return execute(new GuiQuery<Pair<Integer, Point>>() {
 
77  14 toggle protected Pair<Integer, Point> executeInEDT() {
78  14 validateIsEnabledAndShowing(list);
79  10 int index = matchingItemIndex(list, matcher, cellReader);
80  2 if (index < 0) return ITEM_NOT_FOUND;
81  8 return new Pair<Integer, Point>(index, scrollToItemWithIndexIfNotSelectedYet(list, index));
82    }
83    });
84    }
85   
 
86  52 toggle @RunsInEDT
87    // returns the point that the JList was scrolled to.
88    static Point scrollToItemIfNotSelectedYet(final JList list, final int index) {
89  52 return execute(new GuiQuery<Point>() {
 
90  52 toggle protected Point executeInEDT() {
91  52 validateIsEnabledAndShowing(list);
92  40 validateIndex(list, index);
93  36 return scrollToItemWithIndexIfNotSelectedYet(list, index);
94    }
95    });
96    }
97   
 
98  44 toggle @RunsInCurrentThread
99    // returns the point that the JList was scrolled to.
100    private static Point scrollToItemWithIndexIfNotSelectedYet(final JList list, final int index) {
101  7 if (list.getSelectedIndex() == index) return null;
102  37 return scrollToItemWithIndex(list, index);
103    }
104   
 
105  57 toggle @RunsInCurrentThread
106    private static Point scrollToItemWithIndex(JList list, int index) {
107  57 Rectangle cellBounds = cellBounds(list, index);
108  57 list.scrollRectToVisible(cellBounds);
109  57 return cellCenter(list, cellBounds);
110    }
111   
 
112    toggle private JListScrollToItemTask() {}
113    }