Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / TocItemLeaf.java @ 30011

History | View | Annotate | Download (7.3 KB)

1
/*
2
 * Created on 03-dic-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.gvsig.app.project.documents.view.toc;
48

    
49
import java.awt.Dimension;
50
import java.awt.Graphics2D;
51
import java.awt.Image;
52
import java.awt.Rectangle;
53
import java.awt.datatransfer.DataFlavor;
54
import java.awt.datatransfer.UnsupportedFlavorException;
55
import java.awt.geom.AffineTransform;
56
import java.awt.image.BufferedImage;
57
import java.io.IOException;
58

    
59
import javax.swing.Icon;
60
import javax.swing.ImageIcon;
61

    
62
import org.gvsig.andami.PluginServices;
63
import org.gvsig.app.project.documents.IContextMenuAction;
64
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
65
import org.gvsig.fmap.geom.Geometry;
66
import org.gvsig.fmap.mapcontext.MapContextLocator;
67
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
69

    
70

    
71
/**
72
 * @author FJP
73
 *
74
 * TODO To change the template for this generated type comment go to
75
 * Window - Preferences - Java - Code Generation - Code and Comments
76
 */
77
public class TocItemLeaf implements ITocItem {
78

    
79
        private ISymbol symbol;
80
        private String description;
81
        private int layerType;
82
        private static int w_1symbol = 20;
83
        private static int h_1symbol = 15;
84

    
85
        private Dimension sz;
86
        BufferedImage imgLegend = null;
87

    
88
    final public static DataFlavor INFO_FLAVOR =
89
            new DataFlavor(TocItemLeaf.class, "ItemLeaf");
90

    
91
        static DataFlavor flavors[] = {INFO_FLAVOR };
92

    
93

    
94
        public TocItemLeaf(ISymbol symbol, String description, int layerType)
95
        {
96
                this.symbol = symbol;
97
                this.description = description;
98
                this.layerType = layerType;
99
        }
100
        public TocItemLeaf()
101
        {
102

    
103
        }
104
        public void setImageLegend(Image imageLegend, String descrip, Dimension size)
105
        {
106
                this.description = descrip;
107
                this.sz = size;
108
                imgLegend = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
109
                Graphics2D g2 = imgLegend.createGraphics();
110
                // Rectangle r = new Rectangle(sz.width, sz.height);
111
                // TODO: Calcular ratio
112

    
113
                double ratioImage =  (double) imageLegend.getWidth(null) / (double) imageLegend.getHeight(null);
114
            double ratioToc  = (double) sz.width / (double) sz.height;
115

    
116
            boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, sz.height, null);
117
//                if (ratioImage > ratioToc) {
118
//                        int newHeight = (int) (sz.width / ratioImage);
119
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, newHeight, null);
120
//                } else {
121
//                        int newWidth = (int) (sz.height * ratioImage);
122
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, newWidth, sz.height, null);
123
//                }
124

    
125
        }
126
        /* (non-Javadoc)
127
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
128
         */
129
        public String getLabel() {
130
                return description;
131
        }
132

    
133
        /* (non-Javadoc)
134
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
135
         */
136
        public Icon getIcon() {
137
                // System.out.println("Dentro de getIcon: layerType=" + layerType);
138
                if (imgLegend != null)
139
                {
140
//                        Graphics2D g2 = imgLegend.createGraphics();
141
//                        g2.drawString("Prueba", 10, 20);
142
                        return new ImageIcon(imgLegend);
143
                }
144

    
145
                BufferedImage img = null;
146
                Graphics2D g2 = null;
147
                Rectangle r = null;
148

    
149
                switch (layerType)
150
                {
151
                        case Geometry.TYPES.POINT:
152
                        case Geometry.TYPES.CURVE:
153
                        case Geometry.TYPES.SURFACE:
154
                        case Geometry.TYPES.MULTICURVE:
155
                        case Geometry.TYPES.MULTISURFACE:
156
                        case Geometry.TYPES.MULTIPOINT:
157
                                img = new BufferedImage(w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
158
                                g2 = img.createGraphics();
159
                                r = new Rectangle(w_1symbol, h_1symbol);
160

    
161
//                                // TODO: CAMBIAR PARA QUE NO DEPENDA DE FSYMBOL
162
//                                if (symbol instanceof FSymbol)
163
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
164
//                                // Por ahora, los que no sean FSymbol no se renderizan en el TOC.
165
                                break;
166
                        case Geometry.TYPES.GEOMETRY:
167
                                img = new BufferedImage(3*w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
168
                                g2 = img.createGraphics();
169
                                r = new Rectangle(3*w_1symbol, h_1symbol);
170
//                                if (symbol instanceof FSymbol)
171
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
172
                                break;
173

    
174
                }
175

    
176
                if (g2 == null) {
177
                        return null;//TODO tipo de shape no soportado.
178
                }
179
                try {
180
                        symbol.drawInsideRectangle(g2, AffineTransform.getScaleInstance(0.8,0.8), r,null);
181
                } catch (SymbolDrawingException e) {
182
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
183
                                try {
184
                                        MapContextLocator
185
                                                        .getMapContextManager()
186
                                                        .getWarningSymbol(
187
                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
188
                                                                        symbol.getDescription(),
189
                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
190
                                                        .drawInsideRectangle(g2, g2.getTransform(),
191
                                                                        r.getBounds(), null);
192
                                } catch (SymbolDrawingException e1) {
193
                                        // IMPOSSIBLE TO REACH THIS
194
                                }
195
                        } else {
196
                                // should be unreachable code
197
                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
198
                        }
199
                }
200
                return new ImageIcon(img);
201
        }
202

    
203
        /* (non-Javadoc)
204
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
205
         */
206
        public DataFlavor[] getTransferDataFlavors() {
207
                return flavors;
208
        }
209

    
210
        /* (non-Javadoc)
211
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
212
         */
213
        public boolean isDataFlavorSupported(DataFlavor dF) {
214
                return dF.equals(INFO_FLAVOR);
215
        }
216

    
217
        /* (non-Javadoc)
218
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
219
         */
220
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
221
            if (dF.equals(INFO_FLAVOR)) {
222
                return this;
223
              } else {
224
                        throw new UnsupportedFlavorException(dF);
225
                }
226
        }
227
        /* (non-Javadoc)
228
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
229
         */
230
        public Dimension getSize() {
231
                return sz;
232
        }
233
        /* (non-Javadoc)
234
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
235
         */
236
        public void setSize(Dimension sz) {
237
                this.sz = sz;
238

    
239
        }
240

    
241

    
242
        /**
243
         * @return Returns the symbol.
244
         */
245
        public ISymbol getSymbol() {
246
                return symbol;
247
        }
248
        public IContextMenuAction getDoubleClickAction() {
249
                return new ChangeSymbolTocMenuEntry();
250
        }
251
}