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.comic; |
17 | |
18 | import java.awt.datatransfer.DataFlavor; |
19 | import java.awt.datatransfer.Transferable; |
20 | import java.awt.datatransfer.UnsupportedFlavorException; |
21 | import java.awt.image.RenderedImage; |
22 | import java.io.IOException; |
23 | |
24 | import javax.swing.JComponent; |
25 | |
26 | import net.sf.jomic.tools.ImageTools; |
27 | |
28 | import org.apache.commons.logging.Log; |
29 | import org.apache.commons.logging.LogFactory; |
30 | |
31 | /** |
32 | * Transferable to transfer the currently visible image from a ComicView. |
33 | * |
34 | * @see ComicView |
35 | */ |
36 | public class ComicViewTransferable implements Transferable |
37 | { |
38 | private static Log logger = LogFactory.getLog(ComicViewTransferable.class); |
39 | private ComicView comicView; |
40 | private ImageTools imageTools; |
41 | |
42 | public ComicViewTransferable(JComponent component) { |
43 | assert component instanceof ComicView; |
44 | imageTools = ImageTools.instance(); |
45 | comicView = (ComicView) component; |
46 | } |
47 | |
48 | public Object getTransferData(DataFlavor flavor) |
49 | throws IOException, UnsupportedFlavorException { |
50 | Object result = null; |
51 | |
52 | if (DataFlavor.imageFlavor.equals(flavor)) { |
53 | RenderedImage renderedImage = comicView.getRenderedImage(); |
54 | |
55 | if (renderedImage != null) { |
56 | result = imageTools.getAsBufferedImage(renderedImage); |
57 | } else { |
58 | logger.warn("ComicView has no image to transfer"); |
59 | } |
60 | } else { |
61 | throw new UnsupportedFlavorException(flavor); |
62 | } |
63 | return result; |
64 | } |
65 | |
66 | public DataFlavor[] getTransferDataFlavors() { |
67 | return new DataFlavor[]{DataFlavor.imageFlavor}; |
68 | } |
69 | |
70 | public boolean isDataFlavorSupported(DataFlavor flavor) { |
71 | return DataFlavor.imageFlavor.equals(flavor); |
72 | } |
73 | } |