|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JFCUtilities.java | 20% | 42.9% | 50% | 37.1% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.tools;
|
|
| 2 |
|
|
| 3 |
import java.awt.Component;
|
|
| 4 |
import java.awt.Dimension;
|
|
| 5 |
import java.awt.Toolkit;
|
|
| 6 |
import java.awt.Window;
|
|
| 7 |
|
|
| 8 |
import javax.swing.SwingUtilities;
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
/**
|
|
| 12 |
* JFCUtilities provides convenience methods for frequently needed GUI programming operations.
|
|
| 13 |
*
|
|
| 14 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 15 |
*/
|
|
| 16 |
public final class JFCUtilities { |
|
| 17 |
/**
|
|
| 18 |
* Hide the constructor.
|
|
| 19 |
*/
|
|
| 20 | 0 |
private JFCUtilities() {
|
| 21 |
} |
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* A utility method to center a <code>Window</code>.
|
|
| 25 |
*
|
|
| 26 |
* @param win The Window to be centered.
|
|
| 27 |
*/
|
|
| 28 | 199 |
public static void center(final Window win) { |
| 29 | 199 |
center(win, null);
|
| 30 |
} |
|
| 31 |
|
|
| 32 |
/**
|
|
| 33 |
* A utility method to center a <code>Window</code> with respect to its parent <code>Window</code>.
|
|
| 34 |
*
|
|
| 35 |
* @param win The Window to be centered.
|
|
| 36 |
* @param comp The parent <code>Component</code> of the above <code>Window</code>.
|
|
| 37 |
*/
|
|
| 38 | 199 |
public static void center(final Window win, final Component comp) { |
| 39 | 199 |
Window parentWin = null;
|
| 40 |
|
|
| 41 | 199 |
if (comp != null) { |
| 42 | 0 |
parentWin = SwingUtilities.windowForComponent(comp); |
| 43 |
} |
|
| 44 |
|
|
| 45 | 199 |
Dimension winSize = win.getSize(); |
| 46 | 199 |
Dimension parentSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| 47 | 199 |
int startX = 0;
|
| 48 | 199 |
int startY = 0;
|
| 49 |
|
|
| 50 | 199 |
if (parentWin != null) { |
| 51 | 0 |
startX = parentWin.getLocation().x; |
| 52 | 0 |
startY = parentWin.getLocation().y; |
| 53 | 0 |
parentSize = parentWin.getSize(); |
| 54 |
} |
|
| 55 |
|
|
| 56 |
// If child window is larger than parent window, startX or startY could be negative
|
|
| 57 |
// If the calculated value is negative, use 0 instead
|
|
| 58 | 199 |
win.setLocation( |
| 59 |
Math.max(0, startX + ((parentSize.width - winSize.width) / 2)), |
|
| 60 |
Math.max(0, startY + ((parentSize.height - winSize.height) / 2))); |
|
| 61 |
} |
|
| 62 |
|
|
| 63 |
/**
|
|
| 64 |
* A utility method to find a Component in the component's parent hierarchy which is of the type
|
|
| 65 |
* specified.
|
|
| 66 |
*
|
|
| 67 |
* @param comp The Component whose hierarchy has to be searched for the JViewport.
|
|
| 68 |
* @param clazz The type of the required parent
|
|
| 69 |
* @return The parent that contains the component or one of its parents, null if not found.
|
|
| 70 |
*/
|
|
| 71 | 0 |
public static Component findComponentInHierarchy(final Component comp, |
| 72 |
final Class clazz) {
|
|
| 73 | 0 |
if (comp == null) { |
| 74 | 0 |
return null; |
| 75 |
} |
|
| 76 |
|
|
| 77 | 0 |
Component parent = comp.getParent(); |
| 78 |
|
|
| 79 | 0 |
while ((parent != null) && !(clazz.isInstance(parent))) { |
| 80 | 0 |
parent = parent.getParent(); |
| 81 |
} |
|
| 82 |
|
|
| 83 | 0 |
if (clazz.isInstance(parent)) {
|
| 84 | 0 |
return parent;
|
| 85 |
} |
|
| 86 |
|
|
| 87 | 0 |
return null; |
| 88 |
} |
|
| 89 |
} |
|
| 90 |
|
|
||||||||||