Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / styling / LayerPreview.java @ 40911

History | View | Annotate | Download (4.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
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 License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*/
19
package org.gvsig.labeling.gui.styling;
20

    
21
import java.awt.Dimension;
22
import java.awt.Graphics2D;
23
import java.awt.image.BufferedImage;
24
import java.util.Stack;
25

    
26
import org.gvsig.fmap.dal.exception.ReadException;
27
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
28
import org.gvsig.fmap.geom.GeometryLocator;
29
import org.gvsig.fmap.geom.GeometryManager;
30
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
31
import org.gvsig.fmap.geom.primitive.Envelope;
32
import org.gvsig.fmap.mapcontext.MapContext;
33
import org.gvsig.fmap.mapcontext.ViewPort;
34
import org.gvsig.fmap.mapcontext.layers.FLayer;
35
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
36
import org.gvsig.gui.beans.imagenavigator.IClientImageNavigator;
37
import org.gvsig.gui.beans.imagenavigator.ImageNavigator;
38
import org.gvsig.gui.beans.imagenavigator.ImageUnavailableException;
39
import org.gvsig.tools.task.Cancellable;
40

    
41
public class LayerPreview extends ImageNavigator implements IClientImageNavigator {
42
        
43
        private static final long serialVersionUID = -7554322442540714114L;
44
        private FLayer layer = null;
45
        private ViewPort vp;
46
        private Stack<Cancellable> cancels = new Stack<Cancellable>();
47
        private static GeometryManager geoman = GeometryLocator.getGeometryManager();
48

    
49
        public LayerPreview() {
50
                super(null);
51
                setClientImageNavigator(this);
52
        }
53

    
54
        public void setLayer(FLayer layer) throws ReadException {
55
                
56
                this.layer = layer;
57
                if (layer != null) {
58
                        vp = new ViewPort(layer.getProjection());
59
                        Envelope env = layer.getFullEnvelope();
60
                        setViewDimensions(
61
                                        env.getMinimum(0),
62
                                        env.getMaximum(1),
63
                                        env.getMaximum(0),
64
                                        env.getMinimum(1));
65
                }
66
                updateBuffer();
67
                setEnabled(true);
68
        }
69

    
70
        /*
71
        public static void main(String[] args) {
72
                JFrame jFrame = new JFrame("test");
73
                String fwAndamiDriverPath = "../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers";
74
                IProjection PROJ = CRSFactory.getCRS("EPSG:23030");
75
                try {
76
                        LayerFactory.setDriversPath(new File(fwAndamiDriverPath).getAbsolutePath());
77
                        LayerPreview prev = new LayerPreview();
78

79
                        prev.setLayer(LayerFactory.
80
                                        createLayer(
81
                                                        "line",
82
                                                        "gvSIG shp driver",
83
                                                        new File("/home/jaume/Documents/Cartografia/cv_300_polygons.shp"),
84
                                                        PROJ));
85

86
                        jFrame.setSize(new Dimension(598, 167));
87

88
                        jFrame.setContentPane(prev);
89

90
                        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
91
                        jFrame.setVisible(true);
92
                } catch (Exception e) {
93
                        e.printStackTrace();
94
                }
95
        }
96
        */
97

    
98
        public void drawImage(
99
                        Graphics2D g,
100
                        double left, double top, double right, double bottom,
101
                        double zoom, int width, int height)
102
        throws ImageUnavailableException {
103
                
104
                Cancellable c = new PreviewCancellable();
105
                while (!cancels.isEmpty()) cancels.pop().setCanceled(true);
106

    
107
                cancels.push(c);
108
                if (layer == null || width <= 0 || height <= 0 || vp == null)
109
                        return;
110
                
111
                Envelope env = null;
112
                try {
113
                        env = geoman.createEnvelope(
114
                                        left,
115
                                        bottom,
116
                                        right,
117
                                        top,
118
                                        SUBTYPES.GEOM2D);
119
                } catch (CreateEnvelopeException e1) {
120
                        
121
                        throw new ImageUnavailableException(
122
                                        "Error while initializing layer preview", e1);
123
                }
124
                vp.setEnvelope(env);
125
                vp.setImageSize(new Dimension(width, height));
126
                BufferedImage bi = new BufferedImage(
127
                                width, height, BufferedImage.TYPE_4BYTE_ABGR);
128

    
129
//                double scale = vp.getScale();
130
                double scale = layer.getMapContext().getScaleView();
131
                try {
132
                        layer.draw(bi, g, vp, c, scale);
133
                        if (layer instanceof ILabelable && ((ILabelable) layer).isLabeled()) {
134
                                ((ILabelable) layer).drawLabels(bi, g, vp, c, scale,
135
                                                MapContext.getScreenDPI());
136
                        }
137
                } catch (ReadException e) {
138
                        
139
                        throw new ImageUnavailableException(
140
                                        "Error while drawing layer '" + layer.getName() + "'", e);
141
                }
142
        }
143
        
144
        private class PreviewCancellable implements Cancellable {
145

    
146
                private boolean canc = false;
147
                
148
                public PreviewCancellable() {
149
                        
150
                }
151
                
152
                public boolean isCanceled() {
153
                        return canc;
154
                }
155

    
156
                public void setCanceled(boolean c) {
157
                        canc = c;
158
                }
159
                
160
        }
161
        
162

    
163

    
164
}