| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.jaiunit; |
| 17 | |
| 18 | import java.awt.event.WindowEvent; |
| 19 | import java.awt.event.WindowListener; |
| 20 | import java.awt.image.RenderedImage; |
| 21 | |
| 22 | import javax.swing.BorderFactory; |
| 23 | import javax.swing.JFrame; |
| 24 | import javax.swing.JScrollPane; |
| 25 | import javax.swing.WindowConstants; |
| 26 | |
| 27 | import net.sf.jomic.ui.RenderedImageView; |
| 28 | import org.apache.commons.logging.Log; |
| 29 | import org.apache.commons.logging.LogFactory; |
| 30 | |
| 31 | /** |
| 32 | * JFrame to show an image during testing. The window title identifies the class and test that |
| 33 | * created the image. Call <code>showImage()</code> to conveniently show the image only if |
| 34 | * requested and with a timeout. |
| 35 | * |
| 36 | * @see #showImage() |
| 37 | * @author Thomas Aglassinger |
| 38 | */ |
| 39 | public class ImageViewer extends JFrame implements WindowListener |
| 40 | { |
| 41 | // TODO: Move to JAIUnit, JAIUnitConstants or where ever. |
| 42 | public static final int TICK = 50; |
| 43 | private boolean closed; |
| 44 | private RenderedImage image; |
| 45 | private Log logger; |
| 46 | |
| 47 | public ImageViewer(RenderedImage newImage, Class clazz, String id) { |
| 48 | super("" + clazz.getName() + "." + id); |
| 49 | // TODO: @pre for assert clazz != null; |
| 50 | assert id != null; |
| 51 | assert newImage != null; |
| 52 | logger = LogFactory.getLog(ImageViewer.class); |
| 53 | image = newImage; |
| 54 | |
| 55 | RenderedImageView icon = new RenderedImageView(); |
| 56 | JScrollPane scrollPane = new JScrollPane(icon); |
| 57 | |
| 58 | icon.set(image); |
| 59 | scrollPane.setBorder(BorderFactory.createEmptyBorder()); |
| 60 | getContentPane().add(scrollPane); |
| 61 | setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 62 | addWindowListener(this); |
| 63 | } |
| 64 | |
| 65 | public void dispose() { |
| 66 | removeWindowListener(this); |
| 67 | super.dispose(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * If property "net.sf.jaiunit.showImage" is <code>true</code>, show <code>image</code>. The |
| 72 | * window closes automatically after <code>PROPERTY_TIMEOUT</code> milliseconds, or when the |
| 73 | * user clicks the close button. |
| 74 | */ |
| 75 | public void showImage() { |
| 76 | if (Boolean.getBoolean(TestCase.PROPERTY_SHOW_IMAGE)) { |
| 77 | try { |
| 78 | pack(); |
| 79 | setResizable(false); |
| 80 | setVisible(true); |
| 81 | |
| 82 | int timeout = Integer.getInteger(TestCase.PROPERTY_TIMEOUT, TestCase.DEFAULT_TIMEOUT).intValue(); |
| 83 | int ticks = Math.max(1, timeout / TICK); |
| 84 | |
| 85 | while ((!closed && (ticks > 0)) || (timeout == 0)) { |
| 86 | Thread.sleep(TICK); |
| 87 | ticks -= 1; |
| 88 | } |
| 89 | } catch (InterruptedException error) { |
| 90 | logger.warn("sleep interrupted: " + error); |
| 91 | } finally { |
| 92 | dispose(); |
| 93 | } |
| 94 | } else { |
| 95 | if (logger.isDebugEnabled()) { |
| 96 | logger.debug("showImage: to show " + getTitle() |
| 97 | + ", set " + TestCase.PROPERTY_SHOW_IMAGE + " to " + Boolean.TRUE); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public void windowActivated(WindowEvent event) { |
| 103 | // do nothing |
| 104 | } |
| 105 | |
| 106 | public void windowClosed(WindowEvent event) { |
| 107 | // do nothing |
| 108 | } |
| 109 | |
| 110 | public void windowClosing(WindowEvent event) { |
| 111 | if (logger.isDebugEnabled()) { |
| 112 | logger.debug("received event: WindowClosed"); |
| 113 | } |
| 114 | closed = true; |
| 115 | } |
| 116 | |
| 117 | public void windowDeactivated(WindowEvent event) { |
| 118 | // do nothing |
| 119 | } |
| 120 | |
| 121 | public void windowDeiconified(WindowEvent event) { |
| 122 | // do nothing |
| 123 | } |
| 124 | |
| 125 | public void windowIconified(WindowEvent event) { |
| 126 | // do nothing |
| 127 | } |
| 128 | |
| 129 | public void windowOpened(WindowEvent event) { |
| 130 | // do nothing |
| 131 | } |
| 132 | } |