Better support for multiple images/windows in test code.

Code style update.
This commit is contained in:
Harald Kuhr 2011-10-30 20:37:48 +01:00
parent fe25b48804
commit 7ddc2c991e
2 changed files with 29 additions and 26 deletions

View File

@ -325,21 +325,12 @@ public abstract class ImageReaderBase extends ImageReader {
} }
}); });
frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "window-close"); frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "window-close");
frame.addWindowListener(new ExitIfNoWindowPresentHandler());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
Window[] windows = Window.getWindows();
if (windows == null || windows.length == 0) {
System.exit(0);
}
}
});
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true); frame.setLocationByPlatform(true);
JPanel pane = new JPanel(new BorderLayout()); JPanel pane = new JPanel(new BorderLayout());
JScrollPane scroll = new JScrollPane(new ImageLabel(pImage)); JScrollPane scroll = new JScrollPane(pImage != null ? new ImageLabel(pImage) : new JLabel("(no image data)", JLabel.CENTER));
scroll.setBorder(null); scroll.setBorder(null);
pane.add(scroll); pane.add(scroll);
frame.setContentPane(pane); frame.setContentPane(pane);
@ -361,22 +352,22 @@ public abstract class ImageReaderBase extends ImageReader {
} }
private static class ImageLabel extends JLabel { private static class ImageLabel extends JLabel {
Paint mBackground; Paint backgroundPaint;
final Paint mCheckeredBG; final Paint checkeredBG;
final Color mDefaultBG; final Color defaultBG;
public ImageLabel(final BufferedImage pImage) { public ImageLabel(final BufferedImage pImage) {
super(new BufferedImageIcon(pImage)); super(new BufferedImageIcon(pImage));
setOpaque(false); setOpaque(false);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
mCheckeredBG = createTexture(); checkeredBG = createTexture();
// For indexed color, default to the color of the transparent pixel, if any // For indexed color, default to the color of the transparent pixel, if any
mDefaultBG = getDefaultBackground(pImage); defaultBG = getDefaultBackground(pImage);
mBackground = mDefaultBG != null ? mDefaultBG : mCheckeredBG; backgroundPaint = defaultBG != null ? defaultBG : checkeredBG;
JPopupMenu popup = createBackgroundPopup(); JPopupMenu popup = createBackgroundPopup();
@ -395,7 +386,7 @@ public abstract class ImageReaderBase extends ImageReader {
JPopupMenu popup = new JPopupMenu(); JPopupMenu popup = new JPopupMenu();
ButtonGroup group = new ButtonGroup(); ButtonGroup group = new ButtonGroup();
addCheckBoxItem(new ChangeBackgroundAction("Checkered", mCheckeredBG), popup, group); addCheckBoxItem(new ChangeBackgroundAction("Checkered", checkeredBG), popup, group);
popup.addSeparator(); popup.addSeparator();
addCheckBoxItem(new ChangeBackgroundAction("White", Color.WHITE), popup, group); addCheckBoxItem(new ChangeBackgroundAction("White", Color.WHITE), popup, group);
addCheckBoxItem(new ChangeBackgroundAction("Light", Color.LIGHT_GRAY), popup, group); addCheckBoxItem(new ChangeBackgroundAction("Light", Color.LIGHT_GRAY), popup, group);
@ -403,7 +394,7 @@ public abstract class ImageReaderBase extends ImageReader {
addCheckBoxItem(new ChangeBackgroundAction("Dark", Color.DARK_GRAY), popup, group); addCheckBoxItem(new ChangeBackgroundAction("Dark", Color.DARK_GRAY), popup, group);
addCheckBoxItem(new ChangeBackgroundAction("Black", Color.BLACK), popup, group); addCheckBoxItem(new ChangeBackgroundAction("Black", Color.BLACK), popup, group);
popup.addSeparator(); popup.addSeparator();
addCheckBoxItem(new ChooseBackgroundAction("Choose...", mDefaultBG != null ? mDefaultBG : Color.BLUE), popup, group); addCheckBoxItem(new ChooseBackgroundAction("Choose...", defaultBG != null ? defaultBG : Color.BLUE), popup, group);
return popup; return popup;
} }
@ -447,21 +438,21 @@ public abstract class ImageReaderBase extends ImageReader {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
Graphics2D gr = (Graphics2D) g; Graphics2D gr = (Graphics2D) g;
gr.setPaint(mBackground); gr.setPaint(backgroundPaint);
gr.fillRect(0, 0, getWidth(), getHeight()); gr.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g); super.paintComponent(g);
} }
private class ChangeBackgroundAction extends AbstractAction { private class ChangeBackgroundAction extends AbstractAction {
protected Paint mPaint; protected Paint paint;
public ChangeBackgroundAction(final String pName, final Paint pPaint) { public ChangeBackgroundAction(final String pName, final Paint pPaint) {
super(pName); super(pName);
mPaint = pPaint; paint = pPaint;
} }
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
mBackground = mPaint; backgroundPaint = paint;
repaint(); repaint();
} }
} }
@ -472,7 +463,7 @@ public abstract class ImageReaderBase extends ImageReader {
putValue(Action.SMALL_ICON, new Icon() { putValue(Action.SMALL_ICON, new Icon() {
public void paintIcon(Component c, Graphics pGraphics, int x, int y) { public void paintIcon(Component c, Graphics pGraphics, int x, int y) {
Graphics g = pGraphics.create(); Graphics g = pGraphics.create();
g.setColor((Color) mPaint); g.setColor((Color) paint);
g.fillRect(x, y, 16, 16); g.fillRect(x, y, 16, 16);
g.dispose(); g.dispose();
} }
@ -489,12 +480,23 @@ public abstract class ImageReaderBase extends ImageReader {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Color selected = JColorChooser.showDialog(ImageLabel.this, "Choose background", (Color) mPaint); Color selected = JColorChooser.showDialog(ImageLabel.this, "Choose background", (Color) paint);
if (selected != null) { if (selected != null) {
mPaint = selected; paint = selected;
super.actionPerformed(e); super.actionPerformed(e);
} }
} }
} }
} }
private static class ExitIfNoWindowPresentHandler extends WindowAdapter {
@Override
public void windowClosed(final WindowEvent e) {
Window[] windows = Window.getWindows();
if (windows == null || windows.length == 0) {
System.exit(0);
}
}
}
} }

View File

@ -694,6 +694,7 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
catch (IOException e) { catch (IOException e) {
fail(e.getMessage()); fail(e.getMessage());
} }
assertTrue(num > 0); assertTrue(num > 0);
} }