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
18   102   10   3.6
8   42   0.56   5
5     2  
1    
3.1% of code in this file is excluded from these metrics.
 
  Scrolling       Line # 37 18 3.1% 10 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Jan 13, 2009
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * 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 is distributed on
10    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11    * specific language governing permissions and limitations under the License.
12    *
13    * Copyright @2009-2010 the original author or authors.
14    */
15    package org.fest.swing.core;
16   
17    import static javax.swing.SwingUtilities.convertRectangle;
18    import static org.fest.swing.edt.GuiActionRunner.execute;
19   
20    import java.awt.Component;
21    import java.awt.Container;
22    import java.awt.Rectangle;
23   
24    import javax.swing.JComponent;
25    import javax.swing.JInternalFrame;
26    import javax.swing.JViewport;
27   
28    import org.fest.swing.edt.GuiTask;
29   
30    /**
31    * Understands utility methods related to scrolling.
32    *
33    * @author Juhos Csaba-Zsolt
34    *
35    * @since 1.2
36    */
 
37    public final class Scrolling {
38   
39    /**
40    * Scrolls a <code>{@link JComponent}</code> into view within a container.
41    * @param robot simulates user input.
42    * @param c the given component.
43    */
 
44  106 toggle public static void scrollToVisible(Robot robot, JComponent c) {
45  106 JComponent root = findClosestValidatingRootAncestor(c);
46    // scroll the component to view within each validating root ancestor, starting from the nearest
47  205 while (root != null) {
48  99 scrollToVisible(robot, root, c);
49    // find the next validating root
50  99 root = findClosestValidatingRootAncestor(root);
51    }
52    }
53   
54    /**
55    * Returns a component's closest validating root ancestor in the AWT containment hierarchy.
56    * @param c the given component.
57    * @return the found ancestor or <code>null</code> if there isn't one.
58    */
 
59  205 toggle private static JComponent findClosestValidatingRootAncestor(JComponent c) {
60    // the candidate validating root at every iteration (candidate = not necessarily a root)
61  205 Container root = c;
62    // we go up to the top of the hierarchy
63  960 while (root != null) {
64  854 Container parent = root.getParent();
65    // the new candidate root becomes the parent of the previous one
66  854 root = parent;
67    // if the candidate isn't a JComponent, we're not interested in it (we need JComponent#scrollRectToVisible)
68  228 if (!(root instanceof JComponent)) continue;
69    // we don't have to take JFrame into account, it's not a JComponent (ant it's a top-level container anyway)
70  99 if (root instanceof JViewport || root instanceof JInternalFrame) return (JComponent) root;
71    }
72  106 return null;
73    }
74   
75    /**
76    * Scrolls a component into view within a container.
77    * @param robot simulates user input.
78    * @param container the given container.
79    * @param target the given component.
80    */
 
81  99 toggle private static void scrollToVisible(Robot robot, JComponent container, Component target) {
82  99 Rectangle r = convertRectangle(target.getParent(), target.getBounds(), container);
83  99 scrollToVisible(robot, container, r);
84    }
85   
86    /**
87    * Scrolls a rectangular region of a component into view.
88    * @param robot simulates user input.
89    * @param c the component.
90    * @param rectangle the rectangular region.
91    */
 
92  99 toggle private static void scrollToVisible(Robot robot, final JComponent c, final Rectangle rectangle) {
93  99 execute(new GuiTask() {
 
94  99 toggle protected void executeInEDT() {
95  99 c.scrollRectToVisible(rectangle);
96    }
97    });
98  99 robot.waitForIdle();
99    }
100   
 
101    toggle private Scrolling() {}
102    }