| 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.ui; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.Dimension; |
| 20 | import java.awt.Graphics; |
| 21 | import java.awt.Graphics2D; |
| 22 | import java.awt.geom.AffineTransform; |
| 23 | import java.awt.image.RenderedImage; |
| 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | import javax.swing.JComponent; |
| 28 | |
| 29 | import net.sf.jomic.comic.ComicCache; |
| 30 | import net.sf.jomic.tools.ImageInCacheListener; |
| 31 | import net.sf.jomic.tools.ImageTools; |
| 32 | |
| 33 | import org.apache.commons.logging.Log; |
| 34 | import org.apache.commons.logging.LogFactory; |
| 35 | |
| 36 | /** |
| 37 | * JComponent to show a title image for a comic. |
| 38 | * |
| 39 | * @author Thomas Aglassinger |
| 40 | */ |
| 41 | class TitleImageView extends JComponent implements ImageInCacheListener |
| 42 | { |
| 43 | private ComicCache comicCache; |
| 44 | private File comicFile; |
| 45 | private RenderedImage image; |
| 46 | private ImageTools imageTools; |
| 47 | private Log logger; |
| 48 | private Object setMutex; |
| 49 | |
| 50 | public TitleImageView() { |
| 51 | logger = LogFactory.getLog(TitleImageView.class); |
| 52 | comicCache = ComicCache.instance(); |
| 53 | imageTools = ImageTools.instance(); |
| 54 | |
| 55 | int preferredWidth = comicCache.getThumbWidth(); |
| 56 | int preferredHeight = comicCache.getThumbHeight(); |
| 57 | |
| 58 | setMutex = new Object(); |
| 59 | setPreferredSize(new Dimension(preferredWidth, preferredHeight)); |
| 60 | } |
| 61 | |
| 62 | public void setComicFile(File newComicFile) { |
| 63 | synchronized (setMutex) { |
| 64 | comicFile = newComicFile; |
| 65 | if (comicFile == null) { |
| 66 | image = null; |
| 67 | } else { |
| 68 | try { |
| 69 | image = comicCache.getTitleCache().get(comicFile, this); |
| 70 | } catch (IOException error) { |
| 71 | logger.warn("cannot read title thumbnail for " + comicFile, error); |
| 72 | image = imageTools.createBrokenImage(getPreferredSize().width, getPreferredSize().height, |
| 73 | Color.RED, Color.WHITE); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | repaint(); |
| 78 | } |
| 79 | |
| 80 | public void imageCached(File baseComicFile) { |
| 81 | synchronized (setMutex) { |
| 82 | if (baseComicFile.equals(comicFile)) { |
| 83 | setComicFile(baseComicFile); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | protected void paintComponent(Graphics g) { |
| 89 | super.paintComponents(g); |
| 90 | synchronized (setMutex) { |
| 91 | if (image != null) { |
| 92 | Graphics2D g2d = (Graphics2D) g; |
| 93 | int topX = (getWidth() - image.getWidth()) / 2; |
| 94 | int topY = (getHeight() - image.getHeight()) / 2; |
| 95 | |
| 96 | g2d.drawRenderedImage(image, AffineTransform.getTranslateInstance(topX, topY)); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |