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.GridBagConstraints; |
19 | import java.awt.GridBagLayout; |
20 | import java.awt.event.WindowEvent; |
21 | import java.awt.event.WindowListener; |
22 | import java.awt.image.RenderedImage; |
23 | |
24 | import javax.swing.BorderFactory; |
25 | import javax.swing.JFrame; |
26 | import javax.swing.JPanel; |
27 | import javax.swing.JScrollPane; |
28 | import javax.swing.WindowConstants; |
29 | |
30 | import net.sf.jomic.ui.RenderedImageView; |
31 | import org.apache.commons.logging.Log; |
32 | import org.apache.commons.logging.LogFactory; |
33 | |
34 | /** |
35 | * JFrame to show two images and their differences. |
36 | * |
37 | * @author Thomas Aglassinger |
38 | */ |
39 | public class ImageComparisonViewer extends JFrame implements WindowListener |
40 | { |
41 | private boolean closed; |
42 | private RenderedImage image1; |
43 | private RenderedImage image2; |
44 | private Log logger; |
45 | private Tools tools; |
46 | |
47 | public ImageComparisonViewer(RenderedImage newImage1, RenderedImage newImage2, Class clazz, String id) { |
48 | super("" + clazz.getName() + "." + id); |
49 | // TODO: @pre for assert clazz != null; |
50 | assert id != null; |
51 | assert newImage1 != null; |
52 | assert newImage2 != null; |
53 | |
54 | tools = Tools.instance(); |
55 | logger = LogFactory.getLog(ImageComparisonViewer.class); |
56 | image1 = newImage1; |
57 | image2 = newImage2; |
58 | |
59 | RenderedImage deltaImage = tools.getDeltaImage(image1, image2); |
60 | JScrollPane scrollPane1 = createImagePanel(image1); |
61 | JScrollPane scrollPane2 = createImagePanel(image2); |
62 | JScrollPane deltaScrollPane = createImagePanel(deltaImage); |
63 | JPanel pane = new JPanel(new GridBagLayout()); |
64 | GridBagConstraints c = new GridBagConstraints(); |
65 | |
66 | pane.setBorder(BorderFactory.createEmptyBorder()); |
67 | |
68 | c.anchor = GridBagConstraints.CENTER; |
69 | c.gridx = 0; |
70 | c.gridy = 0; |
71 | pane.add(scrollPane1, c); |
72 | c.gridx = 1; |
73 | pane.add(scrollPane2, c); |
74 | c.gridx = 0; |
75 | c.gridy = 1; |
76 | pane.add(deltaScrollPane, c); |
77 | |
78 | getContentPane().add(pane); |
79 | setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
80 | addWindowListener(this); |
81 | } |
82 | |
83 | public void dispose() { |
84 | removeWindowListener(this); |
85 | super.dispose(); |
86 | } |
87 | |
88 | /** |
89 | * If property "net.sf.jaiunit.showImage" is <code>true</code>, show the images. |
90 | */ |
91 | public void showImages() { |
92 | if (Boolean.getBoolean(TestCase.PROPERTY_SHOW_IMAGE)) { |
93 | try { |
94 | pack(); |
95 | setVisible(true); |
96 | |
97 | int timeout = Integer.getInteger(TestCase.PROPERTY_TIMEOUT, TestCase.DEFAULT_TIMEOUT).intValue(); |
98 | int ticks = Math.max(1, timeout / ImageViewer.TICK); |
99 | |
100 | while ((!closed && (ticks > 0)) || (timeout == 0)) { |
101 | Thread.sleep(ImageViewer.TICK); |
102 | ticks -= 1; |
103 | } |
104 | } catch (InterruptedException error) { |
105 | logger.warn("sleep interrupted: " + error); |
106 | } finally { |
107 | dispose(); |
108 | } |
109 | } else { |
110 | if (logger.isDebugEnabled()) { |
111 | logger.debug("showImage: to show " + getTitle() |
112 | + ", set " + TestCase.PROPERTY_SHOW_IMAGE + " to " + Boolean.TRUE); |
113 | } |
114 | } |
115 | } |
116 | |
117 | public void windowActivated(WindowEvent event) { |
118 | // do nothing |
119 | } |
120 | |
121 | public void windowClosed(WindowEvent event) { |
122 | // do nothing |
123 | } |
124 | |
125 | public void windowClosing(WindowEvent event) { |
126 | if (logger.isDebugEnabled()) { |
127 | logger.debug("received event: WindowClosed"); |
128 | } |
129 | closed = true; |
130 | } |
131 | |
132 | public void windowDeactivated(WindowEvent event) { |
133 | // do nothing |
134 | } |
135 | |
136 | public void windowDeiconified(WindowEvent event) { |
137 | // do nothing |
138 | } |
139 | |
140 | public void windowIconified(WindowEvent event) { |
141 | // do nothing |
142 | } |
143 | |
144 | public void windowOpened(WindowEvent event) { |
145 | // do nothing |
146 | } |
147 | |
148 | private JScrollPane createImagePanel(RenderedImage image) { |
149 | RenderedImageView imageView = new RenderedImageView(); |
150 | JScrollPane scrollPane = new JScrollPane(imageView); |
151 | |
152 | imageView.set(image); |
153 | scrollPane.setBorder(BorderFactory.createEmptyBorder()); |
154 | return scrollPane; |
155 | } |
156 | |
157 | } |