Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toc / TocItemLeaf.java @ 40558

History | View | Annotate | Download (8.23 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 03-dic-2004
26
 *
27
 * TODO To change the template for this generated file go to
28
 * Window - Preferences - Java - Code Generation - Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.app.project.documents.view.toc;
71

    
72
import java.awt.Dimension;
73
import java.awt.Graphics2D;
74
import java.awt.Image;
75
import java.awt.Rectangle;
76
import java.awt.datatransfer.DataFlavor;
77
import java.awt.datatransfer.UnsupportedFlavorException;
78
import java.awt.geom.AffineTransform;
79
import java.awt.image.BufferedImage;
80
import java.io.IOException;
81

    
82
import javax.swing.Icon;
83
import javax.swing.ImageIcon;
84

    
85
import org.gvsig.andami.PluginServices;
86
import org.gvsig.app.project.documents.view.IContextMenuAction;
87
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
88
import org.gvsig.fmap.geom.Geometry;
89
import org.gvsig.fmap.mapcontext.MapContextLocator;
90
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
91
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
92

    
93

    
94
/**
95
 * @author FJP
96
 *
97
 * TODO To change the template for this generated type comment go to
98
 * Window - Preferences - Java - Code Generation - Code and Comments
99
 */
100
public class TocItemLeaf implements ITocItem {
101

    
102
        private ISymbol symbol;
103
        private String description;
104
        private int layerType;
105
        private static int w_1symbol = 20;
106
        private static int h_1symbol = 15;
107

    
108
        private Dimension sz;
109
        BufferedImage imgLegend = null;
110

    
111
    final public static DataFlavor INFO_FLAVOR =
112
            new DataFlavor(TocItemLeaf.class, "ItemLeaf");
113

    
114
        static DataFlavor flavors[] = {INFO_FLAVOR };
115

    
116

    
117
        public TocItemLeaf(ISymbol symbol, String description, int layerType)
118
        {
119
                this.symbol = symbol;
120
                this.description = description;
121
                this.layerType = layerType;
122
        }
123
        public TocItemLeaf()
124
        {
125

    
126
        }
127
        public void setImageLegend(Image imageLegend, String descrip, Dimension size)
128
        {
129
                this.description = descrip;
130
                this.sz = size;
131
                imgLegend = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
132
                Graphics2D g2 = imgLegend.createGraphics();
133
                // Rectangle r = new Rectangle(sz.width, sz.height);
134
                // TODO: Calcular ratio
135

    
136
                double ratioImage =  (double) imageLegend.getWidth(null) / (double) imageLegend.getHeight(null);
137
            double ratioToc  = (double) sz.width / (double) sz.height;
138

    
139
            boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, sz.height, null);
140
//                if (ratioImage > ratioToc) {
141
//                        int newHeight = (int) (sz.width / ratioImage);
142
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, newHeight, null);
143
//                } else {
144
//                        int newWidth = (int) (sz.height * ratioImage);
145
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, newWidth, sz.height, null);
146
//                }
147

    
148
        }
149
        /* (non-Javadoc)
150
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
151
         */
152
        public String getLabel() {
153
                return description;
154
        }
155

    
156
        /* (non-Javadoc)
157
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
158
         */
159
        public Icon getIcon() {
160
                // System.out.println("Dentro de getIcon: layerType=" + layerType);
161
                if (imgLegend != null)
162
                {
163
//                        Graphics2D g2 = imgLegend.createGraphics();
164
//                        g2.drawString("Prueba", 10, 20);
165
                        return new ImageIcon(imgLegend);
166
                }
167

    
168
                BufferedImage img = null;
169
                Graphics2D g2 = null;
170
                Rectangle r = null;
171

    
172
                switch (layerType)
173
                {
174
                        case Geometry.TYPES.POINT:
175
                        case Geometry.TYPES.CURVE:
176
                        case Geometry.TYPES.SURFACE:
177
                        case Geometry.TYPES.MULTICURVE:
178
                        case Geometry.TYPES.MULTISURFACE:
179
                        case Geometry.TYPES.MULTIPOINT:
180
                                img = new BufferedImage(w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
181
                                g2 = img.createGraphics();
182
                                r = new Rectangle(w_1symbol, h_1symbol);
183

    
184
//                                // TODO: CAMBIAR PARA QUE NO DEPENDA DE FSYMBOL
185
//                                if (symbol instanceof FSymbol)
186
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
187
//                                // Por ahora, los que no sean FSymbol no se renderizan en el TOC.
188
                                break;
189
                        case Geometry.TYPES.GEOMETRY:
190
                                img = new BufferedImage(3*w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
191
                                g2 = img.createGraphics();
192
                                r = new Rectangle(3*w_1symbol, h_1symbol);
193
//                                if (symbol instanceof FSymbol)
194
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
195
                                break;
196

    
197
                }
198

    
199
                if (g2 == null) {
200
                        return null;//TODO tipo de shape no soportado.
201
                }
202
                try {
203
                        symbol.drawInsideRectangle(g2, AffineTransform.getScaleInstance(1,1), r,null);
204
                } catch (SymbolDrawingException e) {
205
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
206
                                try {
207
                                        MapContextLocator.getSymbolManager()
208
                                                        .getWarningSymbol(
209
                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
210
                                                                        symbol.getDescription(),
211
                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
212
                                                        .drawInsideRectangle(g2, g2.getTransform(),
213
                                                                        r.getBounds(), null);
214
                                } catch (SymbolDrawingException e1) {
215
                                        // IMPOSSIBLE TO REACH THIS
216
                                }
217
                        } else {
218
                                // should be unreachable code
219
                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
220
                        }
221
                }
222
                return new ImageIcon(img);
223
        }
224

    
225
        /* (non-Javadoc)
226
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
227
         */
228
        public DataFlavor[] getTransferDataFlavors() {
229
                return flavors;
230
        }
231

    
232
        /* (non-Javadoc)
233
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
234
         */
235
        public boolean isDataFlavorSupported(DataFlavor dF) {
236
                return dF.equals(INFO_FLAVOR);
237
        }
238

    
239
        /* (non-Javadoc)
240
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
241
         */
242
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
243
            if (dF.equals(INFO_FLAVOR)) {
244
                return this;
245
              } else {
246
                        throw new UnsupportedFlavorException(dF);
247
                }
248
        }
249
        /* (non-Javadoc)
250
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
251
         */
252
        public Dimension getSize() {
253
                return sz;
254
        }
255
        /* (non-Javadoc)
256
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
257
         */
258
        public void setSize(Dimension sz) {
259
                this.sz = sz;
260

    
261
        }
262

    
263

    
264
        /**
265
         * @return Returns the symbol.
266
         */
267
        public ISymbol getSymbol() {
268
                return symbol;
269
        }
270
        public IContextMenuAction getDoubleClickAction() {
271
                return new ChangeSymbolTocMenuEntry();
272
        }
273
}