Commit | Line | Data |
---|---|---|
6ce136da JL |
1 | /* |
2 | GNU Lesser General Public License | |
3 | ||
4 | ImageFileChooserPreview | |
5 | Copyright (C) 2000 Frits Jalvingh & Howard Kistler | |
6 | ||
7 | This library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | This library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with this library; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | package com.hexidec.ekit.component; | |
23 | ||
24 | import java.awt.Dimension; | |
25 | import java.awt.Graphics; | |
26 | import java.awt.Image; | |
27 | import java.io.File; | |
28 | import javax.swing.ImageIcon; | |
29 | import javax.swing.JComponent; | |
30 | import javax.swing.JFileChooser; | |
31 | import java.beans.PropertyChangeEvent; | |
32 | import java.beans.PropertyChangeListener; | |
33 | ||
34 | /** Class provides a preview window for the selected image file | |
35 | */ | |
36 | class ImageFileChooserPreview extends JComponent implements PropertyChangeListener | |
37 | { | |
38 | private static final int previewWidth = 100; | |
39 | private static final int previewHeight = 100; | |
40 | ||
41 | private ImageIcon imageThumb = null; | |
42 | private File imageFile = null; | |
43 | ||
44 | /** This class requires a file chooser to register with so this class will | |
45 | * be notified when a new file is selected in the browser. | |
46 | * @param JFileChooser that this preview window is used in. | |
47 | */ | |
48 | public ImageFileChooserPreview(JFileChooser parent) | |
49 | { | |
50 | setPreferredSize(new Dimension(previewWidth , previewHeight)); | |
51 | parent.addPropertyChangeListener(this); | |
52 | } | |
53 | ||
54 | /** Loads a new image into the preview window, and scales it if necessary. | |
55 | */ | |
56 | public void loadImage() | |
57 | { | |
58 | if(imageFile == null) | |
59 | { | |
60 | imageThumb = null; | |
61 | return; | |
62 | } | |
63 | imageThumb = new ImageIcon(imageFile.getPath()); | |
64 | ||
65 | // Check if thumb requires scaling | |
66 | if(imageThumb.getIconHeight() < previewHeight && imageThumb.getIconWidth() < previewWidth) | |
67 | { | |
68 | return; | |
69 | } | |
70 | int w = previewWidth; | |
71 | int h = previewHeight; | |
72 | if(imageThumb.getIconHeight() > imageThumb.getIconWidth()) | |
73 | { | |
74 | w = -1; | |
75 | } | |
76 | else | |
77 | { | |
78 | h = -1; | |
79 | } | |
80 | imageThumb = new ImageIcon(imageThumb.getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT)); | |
81 | } | |
82 | ||
83 | /** Callback (event handler) to indicate that a property of the | |
84 | * JFileChooser has changed. If the selected file has changed cause a new | |
85 | * thumbnail to load. | |
86 | */ | |
87 | public void propertyChange(PropertyChangeEvent e) | |
88 | { | |
89 | if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) | |
90 | { | |
91 | imageFile = (File)e.getNewValue(); | |
92 | if(isShowing()) | |
93 | { | |
94 | loadImage(); | |
95 | repaint(); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | /** Paints the icon of the current image, if one's present.. | |
101 | * @param Graphics object to use when painting the component. | |
102 | */ | |
103 | public void paintComponent(Graphics g) | |
104 | { | |
105 | if(imageThumb == null) | |
106 | { | |
107 | loadImage(); | |
108 | } | |
109 | if(imageThumb == null) | |
110 | { | |
111 | return; | |
112 | } | |
113 | int x = (getWidth() - imageThumb.getIconWidth()) / 2; | |
114 | int y = (getHeight() - imageThumb.getIconHeight()) / 2; | |
115 | if(y < 0) | |
116 | { | |
117 | y = 0; | |
118 | } | |
119 | if(x < 5) | |
120 | { | |
121 | x = 5; | |
122 | } | |
123 | imageThumb.paintIcon(this, g, x, y); | |
124 | } | |
125 | } | |
126 |