Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart7.png 95% of files have more coverage
36   123   17   6
22   63   0.47   6
6     2.83  
1    
 
  JToolBarLocation       Line # 36 36 0% 17 20 68.8% 0.6875
 
No Tests
 
1    /*
2    * Created on Feb 2, 2008
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 @2008-2010 the original author or authors.
14    */
15    package org.fest.swing.driver;
16   
17    import static java.awt.BorderLayout.*;
18    import static java.lang.Math.max;
19    import static javax.swing.SwingConstants.HORIZONTAL;
20    import static org.fest.util.Arrays.format;
21    import static org.fest.util.Strings.concat;
22    import static org.fest.util.Strings.quote;
23   
24    import java.awt.*;
25   
26    import javax.swing.JToolBar;
27   
28    import org.fest.swing.annotation.RunsInCurrentThread;
29   
30    /**
31    * Understands a visible location on a <code>{@link JToolBar}</code>.
32    *
33    * @author Yvonne Wang
34    * @author Alex Ruiz
35    */
 
36    public final class JToolBarLocation {
37   
38    private static String[] VALID_CONSTRAINTS = { NORTH, EAST, SOUTH, WEST };
39   
40    /**
41    * Returns the point where to grab the given <code>{@link JToolBar}</code>.
42    * <p>
43    * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
44    * responsible for calling this method from the EDT.
45    * </p>
46    * @param toolBar the target <code>JToolBar</code>.
47    * @return the point where to grab the given <code>JToolBar</code>.
48    */
 
49  10 toggle @RunsInCurrentThread
50    public Point pointToGrab(JToolBar toolBar) {
51  10 Insets insets = toolBar.getInsets();
52  10 int width = toolBar.getWidth();
53  10 int height = toolBar.getHeight();
54  10 if (max(max(max(insets.left, insets.top), insets.right), insets.bottom) == insets.left)
55  10 return new Point(insets.left / 2, height / 2);
56  0 if (max(max(insets.top, insets.right), insets.bottom) == insets.top)
57  0 return new Point(width / 2, insets.top / 2);
58  0 if (max(insets.right, insets.bottom) == insets.right)
59  0 return new Point(width - insets.right / 2, height / 2);
60  0 return new Point(width / 2, height - insets.bottom / 2);
61    }
62   
63    /**
64    * Returns the location where to dock the given <code>{@link JToolBar}</code>, at the given constraint position.
65    * The constraint position must be one of the constants <code>{@link BorderLayout#NORTH NORTH}</code>,
66    * <code>{@link BorderLayout#EAST EAST}</code>, <code>{@link BorderLayout#SOUTH SOUTH}</code>, or
67    * <code>{@link BorderLayout#WEST WEST}</code>.
68    * <p>
69    * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
70    * responsible for calling this method from the EDT.
71    * </p>
72    * @param toolBar the target <code>JToolBar</code>.
73    * @param dock the container where to dock the <code>JToolBar</code> to.
74    * @param constraint the constraint position.
75    * @return the location where to dock the given <code>JToolBar</code>.
76    * @throws IllegalArgumentException if the constraint has an invalid value.
77    */
 
78  4 toggle @RunsInCurrentThread
79    public Point dockLocation(JToolBar toolBar, Container dock, String constraint) {
80  4 validate(constraint);
81  4 Insets insets = dock.getInsets();
82    // BasicToolBarUI prioritizes location N/E/W/S by proximity to the respective border. Close to top border is N, even
83    // if close to the left or right border.
84  4 int offset = isHorizontal(toolBar) ? toolBar.getHeight() : toolBar.getWidth();
85  4 Dimension dockSize = dock.getSize();
86  4 if (NORTH.equals(constraint))
87  1 return new Point(dockSize.width / 2, insets.top);
88  3 if (EAST.equals(constraint))
89  1 return new Point(dockSize.width - insets.right - 1, verticalDockingYCoordinate(dockSize.height, insets, offset));
90  2 if (WEST.equals(constraint))
91  1 return new Point(insets.left, verticalDockingYCoordinate(dockSize.height, insets, offset));
92  1 int x = dockSize.width / 2;
93    // Make sure we don't get mistaken for EAST or WEST
94  1 if (x < insets.left + offset)
95  0 x = insets.left + offset;
96  1 else if (x > dockSize.width - insets.right - offset - 1)
97  0 x = dockSize.width - insets.right - offset - 1;
98  1 return new Point(x, dockSize.height - insets.bottom - 1);
99    }
100   
 
101  4 toggle @RunsInCurrentThread
102    private boolean isHorizontal(JToolBar toolBar) {
103  4 return toolBar.getOrientation() == HORIZONTAL;
104    }
105   
 
106  4 toggle private void validate(String constraint) {
107  4 for (String validConstraint : VALID_CONSTRAINTS)
108  4 if (validConstraint.equals(constraint)) return;
109  0 throw invalidConstraint(constraint);
110    }
111   
 
112  0 toggle private IllegalArgumentException invalidConstraint(String constraint) {
113  0 throw new IllegalArgumentException(
114    concat(quote(constraint), " is not a valid constraint. Valid constraints are ", format(VALID_CONSTRAINTS)));
115    }
116   
 
117  2 toggle private int verticalDockingYCoordinate(int dockHeight, Insets insets, int offset) {
118  2 int y = dockHeight / 2;
119    // Make sure we don't get mistaken for NORTH
120  2 if (y < insets.top + offset) y = insets.top + offset;
121  2 return y;
122    }
123    }