| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.awt.Dimension; |
| 18 | import java.awt.Graphics; |
| 19 | import java.awt.Image; |
| 20 | import java.io.File; |
| 21 | |
| 22 | import javax.swing.Icon; |
| 23 | import javax.swing.ImageIcon; |
| 24 | import javax.swing.JComponent; |
| 25 | |
| 26 | /** |
| 27 | * @author Jean-Francois Poilpret |
| 28 | */ |
| 29 | public class Thumbnail extends JComponent |
| 30 | { |
| 31 | public Thumbnail(int size) |
| 32 | { |
| 33 | this(size, true); |
| 34 | } |
| 35 | |
| 36 | public Thumbnail(int size, boolean square) |
| 37 | { |
| 38 | _size = size; |
| 39 | _square = square; |
| 40 | initSize(); |
| 41 | } |
| 42 | |
| 43 | public int getThumbnailSize() |
| 44 | { |
| 45 | return _size; |
| 46 | } |
| 47 | |
| 48 | private void initSize() |
| 49 | { |
| 50 | int width; |
| 51 | int height; |
| 52 | if (_square || _thumbnail == null) |
| 53 | { |
| 54 | width = _size; |
| 55 | height = _size; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | width = _thumbnail.getIconWidth(); |
| 60 | height = _thumbnail.getIconHeight(); |
| 61 | } |
| 62 | Dimension dim = new Dimension(width, height); |
| 63 | setPreferredSize(dim); |
| 64 | setMinimumSize(dim); |
| 65 | setMaximumSize(dim); |
| 66 | } |
| 67 | |
| 68 | public void setFile(File file) |
| 69 | { |
| 70 | if (file == null) |
| 71 | { |
| 72 | _thumbnail = null; |
| 73 | initSize(); |
| 74 | repaint(); |
| 75 | return; |
| 76 | } |
| 77 | setIcon(new ImageIcon(file.getPath())); |
| 78 | } |
| 79 | |
| 80 | public Icon getThumbnailIcon() |
| 81 | { |
| 82 | return _thumbnail; |
| 83 | } |
| 84 | |
| 85 | public void setIcon(Icon icon) |
| 86 | { |
| 87 | _thumbnail = IconTools.scaleIcon(icon, _size); |
| 88 | initSize(); |
| 89 | repaint(); |
| 90 | } |
| 91 | |
| 92 | public void setIcon(Image image) |
| 93 | { |
| 94 | _thumbnail = IconTools.scaleIcon(image, _size); |
| 95 | initSize(); |
| 96 | repaint(); |
| 97 | } |
| 98 | |
| 99 | @Override public void paintComponent(Graphics g) |
| 100 | { |
| 101 | super.paintComponent(g); |
| 102 | if (_thumbnail == null) |
| 103 | { |
| 104 | return; |
| 105 | } |
| 106 | if (_square) |
| 107 | { |
| 108 | int x = (_size - _thumbnail.getIconWidth()) / 2; |
| 109 | int y = (_size - _thumbnail.getIconHeight()) / 2; |
| 110 | _thumbnail.paintIcon(this, g, x, y); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | _thumbnail.paintIcon(this, g, 0, 0); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected Icon _thumbnail = null; |
| 119 | protected boolean _square; |
| 120 | protected int _size; |
| 121 | } |
| 122 | |