|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| IconMatcher.java | 95% | 97.4% | 100% | 96.9% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.finder;
|
|
| 2 |
|
|
| 3 |
import java.awt.image.BufferedImage;
|
|
| 4 |
import java.awt.image.PixelGrabber;
|
|
| 5 |
|
|
| 6 |
import java.util.Arrays;
|
|
| 7 |
|
|
| 8 |
import javax.swing.Icon;
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
/**
|
|
| 12 |
* Class for comparing that two {@link javax.swing.Icon}s match.
|
|
| 13 |
*
|
|
| 14 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 15 |
* @author Kevin Wilson
|
|
| 16 |
*/
|
|
| 17 |
public class IconMatcher { |
|
| 18 |
/**
|
|
| 19 |
* The icon of the first component.
|
|
| 20 |
*/
|
|
| 21 |
private Icon m_icon = null; |
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* iconData read from the icon given.
|
|
| 25 |
*/
|
|
| 26 |
private int[] m_iconData = null; |
|
| 27 |
|
|
| 28 |
/**
|
|
| 29 |
* Height of the icon.
|
|
| 30 |
*/
|
|
| 31 |
private int m_ich = 0; |
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* width of the icon.
|
|
| 35 |
*/
|
|
| 36 |
private int m_icw = 0; |
|
| 37 |
|
|
| 38 |
/**
|
|
| 39 |
* Increment for performing spot checks across the icons
|
|
| 40 |
* to speed performance of not equals comparisons.
|
|
| 41 |
*/
|
|
| 42 |
private int m_incr = 1; |
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Size of the icon.
|
|
| 46 |
*/
|
|
| 47 |
private int m_size = 0; |
|
| 48 |
|
|
| 49 |
/**
|
|
| 50 |
* Constructor accepting all arguments needed to filter the component.
|
|
| 51 |
*
|
|
| 52 |
* @param icon The desired pattern for the icon of the component.
|
|
| 53 |
* @throws InterruptedException when the PixelGrabber.grabPixels()
|
|
| 54 |
* is interrupted.
|
|
| 55 |
*/
|
|
| 56 | 58 |
public IconMatcher(final Icon icon) throws InterruptedException { |
| 57 | 58 |
setIcon(icon); |
| 58 |
} |
|
| 59 |
|
|
| 60 |
/**
|
|
| 61 |
* Set the Icon to be matched.
|
|
| 62 |
* @param icon Icon icon to match.
|
|
| 63 |
* @throws InterruptedException may be thrown when
|
|
| 64 |
* converting the icon to an array.
|
|
| 65 |
*/
|
|
| 66 | 58 |
public final void setIcon(final Icon icon) throws InterruptedException { |
| 67 | 58 |
m_icon = icon; |
| 68 |
|
|
| 69 | 58 |
if (icon != null) { |
| 70 | 28 |
m_iconData = iconToArray(icon); |
| 71 | 28 |
m_icw = icon.getIconWidth(); |
| 72 | 28 |
m_ich = icon.getIconHeight(); |
| 73 | 28 |
m_size = m_icw * m_ich; |
| 74 |
|
|
| 75 | 28 |
int min = Math.min(m_icw, m_ich);
|
| 76 |
|
|
| 77 | 28 |
if (min > 32) {
|
| 78 | 25 |
m_incr = min / 8; |
| 79 |
} else {
|
|
| 80 | 3 |
m_incr = min / 4; |
| 81 |
} |
|
| 82 |
} |
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* Get the icon to be matched.
|
|
| 87 |
* @return Icon icon to be matched.
|
|
| 88 |
*/
|
|
| 89 | 4 |
public final Icon getIcon() {
|
| 90 | 4 |
return m_icon;
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Method that returns true if the given icon matches the one in this matcher.
|
|
| 95 |
*
|
|
| 96 |
* @param other The other Icon to be compared
|
|
| 97 |
* @return true if this Icon is a match or the icon which this
|
|
| 98 |
* matcher is created for is null.
|
|
| 99 |
*/
|
|
| 100 | 88 |
public boolean matches(final Icon other) { |
| 101 |
// If the icon on this finder is null, then we treat the
|
|
| 102 |
// finder as a don't care or always match.
|
|
| 103 | 88 |
if (m_icon == null) { |
| 104 | 28 |
return true; |
| 105 |
} |
|
| 106 |
|
|
| 107 | 60 |
if (other == null) { |
| 108 | 44 |
return false; |
| 109 |
} |
|
| 110 |
|
|
| 111 | 16 |
if ((m_icw != other.getIconWidth()) || (m_ich != other.getIconHeight())) {
|
| 112 | 4 |
return false; |
| 113 |
} |
|
| 114 |
|
|
| 115 | 12 |
int[] otherData = null; |
| 116 |
|
|
| 117 | 12 |
try {
|
| 118 | 12 |
otherData = iconToArray(other); |
| 119 |
} catch (InterruptedException ie) {
|
|
| 120 |
// Ignore
|
|
| 121 |
} |
|
| 122 |
|
|
| 123 | 12 |
if ((otherData == null) || (m_iconData.length != otherData.length)) { |
| 124 | 0 |
return false; |
| 125 |
} |
|
| 126 |
|
|
| 127 |
// Spot check
|
|
| 128 | 12 |
for (int i = 0; i < m_size; i += m_incr) { |
| 129 | 5612 |
if (m_iconData[i] != otherData[i]) {
|
| 130 | 2 |
return false; |
| 131 |
} |
|
| 132 |
} |
|
| 133 |
|
|
| 134 |
// If we make it this far then lets compare the whole array.
|
|
| 135 | 10 |
return Arrays.equals(m_iconData, otherData);
|
| 136 |
} |
|
| 137 |
|
|
| 138 |
/**
|
|
| 139 |
* Paint the icon into an array of int.
|
|
| 140 |
* @param icon Icon to be painted.
|
|
| 141 |
* @return int[] containing the painted icon.
|
|
| 142 |
* @throws InterruptedException when
|
|
| 143 |
* PixelGrabber.grabPixels() is interrupted.
|
|
| 144 |
*/
|
|
| 145 | 40 |
private int[] iconToArray(final Icon icon) throws InterruptedException { |
| 146 | 40 |
int width = icon.getIconWidth();
|
| 147 | 40 |
int height = icon.getIconHeight();
|
| 148 |
|
|
| 149 | 40 |
if (width < 0) {
|
| 150 | 2 |
width = 1; |
| 151 |
} |
|
| 152 |
|
|
| 153 | 40 |
if (height < 0) {
|
| 154 | 2 |
height = 1; |
| 155 |
} |
|
| 156 |
|
|
| 157 | 40 |
int[] array = new int[width * height]; |
| 158 | 40 |
BufferedImage bf = new BufferedImage(width, height,
|
| 159 |
BufferedImage.TYPE_INT_ARGB); |
|
| 160 |
|
|
| 161 | 40 |
icon.paintIcon( |
| 162 |
null,
|
|
| 163 |
bf.getGraphics(), |
|
| 164 |
0, |
|
| 165 |
0); |
|
| 166 |
|
|
| 167 | 40 |
PixelGrabber grab = new PixelGrabber(bf, 0, 0, width, height, array, 0,
|
| 168 |
width); |
|
| 169 | 40 |
grab.grabPixels(); |
| 170 |
|
|
| 171 | 40 |
return array;
|
| 172 |
} |
|
| 173 |
} |
|
| 174 |
|
|
||||||||||