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