Tests - fixed test failing due to invalid swing access

This commit is contained in:
dragonmacher 2021-07-12 14:59:31 -04:00
parent 3c1e51d0c0
commit d67659fc36

View file

@ -26,28 +26,55 @@ import java.text.AttributedCharacterIterator;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import javax.swing.JPanel;
/** /**
* Graphics used to render copied text data. This class is not a true graphics object, but is * Graphics used to render copied text data. This class is not a true graphics object, but is
* instead used to grab text being painted so that clients can later use that text. * instead used to grab text being painted so that clients can later use that text.
*/ */
public class TextLayoutGraphics extends Graphics2D { public class TextLayoutGraphics extends Graphics2D {
private static final Component COMPONENT = new JPanel();
private int transX; private int transX;
private int transY; private int transY;
private Shape clip; private Shape clip;
private StringBuilder buffer = new StringBuilder(); private StringBuilder buffer = new StringBuilder();
private Font lastFont = new Font("SansSerif", Font.PLAIN, 12); private Font lastFont = new Font("SansSerif", Font.PLAIN, 12);
private FontMetrics fontMetrics = getFontMetricsForFont(lastFont); private FontMetrics fontMetrics = createFontMetrics(lastFont);
private List<TextInfo> textInfos = new ArrayList<>(); private List<TextInfo> textInfos = new ArrayList<>();
@SuppressWarnings("deprecation") // Java still uses it, so we still use it private Comparator<TextInfo> pointComparator = (o1, o2) -> {
private static FontMetrics getFontMetricsForFont(Font font) { TextInfo t1 = o1;
return Toolkit.getDefaultToolkit().getFontMetrics(font); TextInfo t2 = o2;
int diff = t1.point.y - t2.point.y;
if (diff != 0) {
return diff;
}
diff = t1.point.x - t2.point.x;
return diff;
};
private Comparator<TextInfo> rowComparator = (o1, o2) -> {
TextInfo t1 = o1;
TextInfo t2 = o2;
int diff = t1.row - t2.row;
if (diff != 0) {
return diff;
}
diff = t1.point.x - t2.point.x;
return diff;
};
private static FontMetrics createFontMetrics(Font font) {
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = image.getGraphics();
FontMetrics fm = g.getFontMetrics(font);
g.dispose();
return fm;
} }
@Override @Override
@ -71,6 +98,7 @@ public class TextLayoutGraphics extends Graphics2D {
newTextInfo.point = new Point(x + transX, y + transY); newTextInfo.point = new Point(x + transX, y + transY);
newTextInfo.text = str; newTextInfo.text = str;
newTextInfo.font = lastFont; newTextInfo.font = lastFont;
newTextInfo.fontMetrics = fontMetrics;
textInfos.add(newTextInfo); textInfos.add(newTextInfo);
} }
@ -89,34 +117,6 @@ public class TextLayoutGraphics extends Graphics2D {
return; return;
} }
Comparator<TextInfo> pointComparator = (o1, o2) -> {
TextInfo t1 = o1;
TextInfo t2 = o2;
int diff = t1.point.y - t2.point.y;
if (diff != 0) {
return diff;
}
diff = t1.point.x - t2.point.x;
return diff;
};
Comparator<TextInfo> rowComparator = (o1, o2) -> {
TextInfo t1 = o1;
TextInfo t2 = o2;
int diff = t1.row - t2.row;
if (diff != 0) {
return diff;
}
diff = t1.point.x - t2.point.x;
return diff;
};
TextInfo[] sortedTextInfos = new TextInfo[textInfos.size()]; TextInfo[] sortedTextInfos = new TextInfo[textInfos.size()];
textInfos.toArray(sortedTextInfos); textInfos.toArray(sortedTextInfos);
@ -156,7 +156,7 @@ public class TextLayoutGraphics extends Graphics2D {
lastRow = sortedTextInfo.row; lastRow = sortedTextInfo.row;
//Insert spaces to account for distance past last field in row //Insert spaces to account for distance past last field in row
FontMetrics metrics = COMPONENT.getFontMetrics(sortedTextInfo.font); FontMetrics metrics = sortedTextInfo.fontMetrics;
int spaceWidth = metrics.charWidth(' '); int spaceWidth = metrics.charWidth(' ');
if (spaceWidth == 0) { if (spaceWidth == 0) {
// some environments report 0 for some fonts // some environments report 0 for some fonts
@ -538,11 +538,11 @@ public class TextLayoutGraphics extends Graphics2D {
} }
class TextInfo { class TextInfo {
public String text; String text;
public Point point; Font font;
public Font font; FontMetrics fontMetrics;
Point point;
public int row; int row;
@Override @Override
public String toString() { public String toString() {