KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ImagePreviewAccessory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.File JavaDoc;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JFileChooser JavaDoc;
32
33 public class ImagePreviewAccessory extends JComponent JavaDoc implements PropertyChangeListener JavaDoc {
34     /**
35      *
36      */

37     ImageIcon JavaDoc thumbnail = null;
38     File JavaDoc file = null;
39
40     public ImagePreviewAccessory(JFileChooser JavaDoc fc) {
41         setBorder(BorderFactory.createTitledBorder("Image preview"));
42         setPreferredSize(new Dimension JavaDoc(100, 50));
43         fc.addPropertyChangeListener(this);
44     }
45
46     public void loadImage() {
47         if (file == null) {
48             return;
49         }
50         ImageIcon JavaDoc tmpIcon = new ImageIcon JavaDoc(file.getPath());
51         if (tmpIcon.getIconWidth() > 90) {
52             thumbnail = new ImageIcon JavaDoc(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
53         } else {
54             thumbnail = tmpIcon;
55         }
56     }
57
58     public void propertyChange(PropertyChangeEvent JavaDoc e) {
59         String JavaDoc prop = e.getPropertyName();
60         if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
61             file = (File JavaDoc) e.getNewValue();
62             loadImage();
63             repaint();
64         }
65     }
66
67     public void paintComponent(Graphics JavaDoc g) {
68         if (thumbnail == null) {
69             loadImage();
70         }
71         if (thumbnail != null) {
72             int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
73             int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;
74             if (y < 0) {
75                 y = 0;
76             }
77             if (x < 5) {
78                 x = 5;
79             }
80             thumbnail.paintIcon(this, g, x, y);
81         }
82     }
83 }
Popular Tags