svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / ProjectPreviewPanel.java @ 43983
History | View | Annotate | Download (4.4 KB)
1 |
/*
|
---|---|
2 |
* Copyright (C) 2017 gvSIG Association
|
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 |
*/
|
17 |
package org.gvsig.app.gui; |
18 |
|
19 |
import java.awt.Color; |
20 |
import java.awt.Dimension; |
21 |
import java.awt.Graphics; |
22 |
import java.awt.Image; |
23 |
import java.beans.PropertyChangeEvent; |
24 |
import java.beans.PropertyChangeListener; |
25 |
import java.io.File; |
26 |
import java.util.zip.ZipEntry; |
27 |
import java.util.zip.ZipFile; |
28 |
import javax.imageio.ImageIO; |
29 |
import javax.swing.JFileChooser; |
30 |
import javax.swing.JPanel; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
public class ProjectPreviewPanel extends JPanel |
35 |
implements PropertyChangeListener { |
36 |
|
37 |
private static final Logger LOG = LoggerFactory.getLogger(ProjectPreviewPanel.class); |
38 |
private static final long serialVersionUID = -8314273002829710953L; |
39 |
|
40 |
private int width, height; |
41 |
// private ImageIcon icon;
|
42 |
private Image image; |
43 |
private static final int ACCSIZE = 155; |
44 |
private final Color bg; |
45 |
|
46 |
public ProjectPreviewPanel() {
|
47 |
setPreferredSize(new Dimension(ACCSIZE, -1)); |
48 |
bg = getBackground(); |
49 |
} |
50 |
|
51 |
@Override
|
52 |
public void propertyChange(PropertyChangeEvent e) { |
53 |
String propertyName = e.getPropertyName();
|
54 |
|
55 |
// Make sure we are responding to the right event.
|
56 |
if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { |
57 |
File selection = (File)e.getNewValue(); |
58 |
if (selection == null) { |
59 |
return;
|
60 |
} |
61 |
String name = selection.getAbsolutePath();
|
62 |
if ( name != null ) { |
63 |
try {
|
64 |
ZipFile zfile = new ZipFile(name); |
65 |
ZipEntry entry = zfile.getEntry("preview.jpg"); |
66 |
if( entry != null ) { |
67 |
image = ImageIO.read(zfile.getInputStream(entry));
|
68 |
scaleImage(); |
69 |
} else {
|
70 |
image = null;
|
71 |
} |
72 |
repaint(); |
73 |
} catch(Throwable ex) { |
74 |
LOG.warn("Can't load project preview. Ignore preview.",ex);
|
75 |
} |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
private void scaleImage() { |
81 |
width = image.getWidth(this);
|
82 |
height = image.getHeight(this);
|
83 |
double ratio = 1.0; |
84 |
|
85 |
/*
|
86 |
* Determine how to scale the image. Since the accessory can expand
|
87 |
* vertically make sure we don't go larger than 150 when scaling
|
88 |
* vertically.
|
89 |
*/
|
90 |
if (width >= height) {
|
91 |
ratio = (double)(ACCSIZE-5) / width; |
92 |
width = ACCSIZE-5;
|
93 |
height = (int)(height * ratio);
|
94 |
} |
95 |
else {
|
96 |
if (getHeight() > 150) { |
97 |
ratio = (double)(ACCSIZE-5) / height; |
98 |
height = ACCSIZE-5;
|
99 |
width = (int)(width * ratio);
|
100 |
} |
101 |
else {
|
102 |
ratio = (double)getHeight() / height;
|
103 |
height = getHeight(); |
104 |
width = (int)(width * ratio);
|
105 |
} |
106 |
} |
107 |
|
108 |
image = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void paintComponent(Graphics g) { |
113 |
g.setColor(bg); |
114 |
|
115 |
/*
|
116 |
* If we don't do this, we will end up with garbage from previous
|
117 |
* images if they have larger sizes than the one we are currently
|
118 |
* drawing. Also, it seems that the file list can paint outside
|
119 |
* of its rectangle, and will cause odd behavior if we don't clear
|
120 |
* or fill the rectangle for the accessory before drawing. This might
|
121 |
* be a bug in JFileChooser.
|
122 |
*/
|
123 |
g.fillRect(0, 0, ACCSIZE, getHeight()); |
124 |
if( image != null ) { |
125 |
g.drawImage(image, getWidth() / 2 - width / 2 + 5, |
126 |
getHeight() / 2 - height / 2, this); |
127 |
} |
128 |
} |
129 |
|
130 |
} |