Revision 43506

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/gui/ProjectPreviewPanel.java
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.net.URL;
27
import java.util.zip.ZipEntry;
28
import java.util.zip.ZipFile;
29
import javax.imageio.ImageIO;
30
import javax.swing.JFileChooser;
31
import javax.swing.JPanel;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

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

  
26 26
import java.awt.Component;
27
import java.awt.geom.AffineTransform;
28
import java.awt.image.AffineTransformOp;
29
import java.awt.image.BufferedImage;
27 30
import java.io.File;
31
import java.io.FileOutputStream;
32
import java.io.IOException;
28 33
import java.text.MessageFormat;
29 34
import java.util.ArrayList;
30 35
import java.util.Iterator;
31 36
import java.util.List;
32 37
import java.util.Set;
33 38
import java.util.prefs.Preferences;
39
import java.util.zip.ZipEntry;
40
import java.util.zip.ZipOutputStream;
41
import javax.imageio.ImageIO;
34 42

  
35 43
import javax.swing.JOptionPane;
36 44
import javax.swing.SwingUtilities;
37 45
import org.apache.commons.collections.CollectionUtils;
46
import org.apache.commons.io.IOUtils;
38 47

  
39 48
import org.slf4j.Logger;
40 49
import org.slf4j.LoggerFactory;
......
61 70
import org.gvsig.andami.ui.wizard.UnsavedDataPanel;
62 71
import org.gvsig.app.ApplicationLocator;
63 72
import org.gvsig.app.ApplicationManager;
73
import org.gvsig.app.gui.ProjectPreviewPanel;
64 74
import org.gvsig.app.project.Project;
65 75
import org.gvsig.app.project.ProjectManager;
66 76
import org.gvsig.app.project.documents.gui.ProjectWindow;
......
83 93
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos en
84 94
 * esta clase.
85 95
 *
86
 * @author Fernando Gonz?lez Cort?s
87 96
 */
88 97
public class ProjectExtension extends Extension implements IExtensionStatus {
89 98
	private static final Logger LOG = LoggerFactory
......
348 357
            if (projectFile == null) {
349 358
                Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
350 359
                JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID, prefs.get("ProjectsFolder", null));
360
                ProjectPreviewPanel preview = new ProjectPreviewPanel();
361
                jfc.setAccessory(preview);
362
                jfc.addPropertyChangeListener(preview);                
351 363

  
352 364
                GenericFileFilter projExtensionFilter =
353 365
                    new GenericFileFilter(Project.FILE_EXTENSION, PluginServices.getText(this, "tipo_fichero_proyecto"));
......
462 474
		try {
463 475
			fireBeforeSavingFileEvent(new SaveEvent(this,
464 476
					SaveEvent.BEFORE_SAVING, file));
465
			p.saveState(file);
477
            
478
            FileOutputStream fout = new FileOutputStream(file);
479
            ZipOutputStream zout = new ZipOutputStream(fout);
480
            p.saveState(zout);
481
            
482
            zout.putNextEntry(new ZipEntry("preview.jpg"));
483
            BufferedImage img = ApplicationLocator.getManager().getUIManager().getImagePreview();
484
            img = scale(img, 0.40);
485
            try {
486
                ImageIO.write(img, "jpg", zout);
487
            } catch (IOException ex) {
488
                LOG.warn("Can't save preview image'.",ex);
489
            }
490
            
491
            IOUtils.closeQuietly(zout);
492
            IOUtils.closeQuietly(fout);
493
            
466 494
			fireAfterSavingFileEvent(new SaveEvent(this,
467 495
					SaveEvent.AFTER_SAVING, file));
468 496

  
......
486 514
		return true;
487 515
	}
488 516

  
517
    private BufferedImage scale(BufferedImage before, double factor) {
518
        int w = (int) (before.getWidth()*factor);
519
        int h = (int) (before.getHeight()*factor);
520
        BufferedImage after = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
521
        AffineTransform at = new AffineTransform();
522
        at.scale(factor, factor);
523
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
524
        after = scaleOp.filter(before, after);        
525
        return after;
526
    }
527
    
489 528
	public Project readProject(String path) {
490 529
		Project project = ProjectManager.getInstance().createProject();
491 530

  

Also available in: Unified diff