Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / TocItemLeaf.java @ 28368

History | View | Annotate | Download (7.37 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 com.iver.cit.gvsig.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 com.iver.andami.PluginServices;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
65
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
66
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
67
import com.iver.cit.gvsig.project.documents.IContextMenuAction;
68
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
69

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

    
78
        private ISymbol symbol;
79
        private String description;
80
        private int layerType;
81
        private static int w_1symbol = 20;
82
        private static int h_1symbol = 15;
83
        
84
        private Dimension sz;
85
        BufferedImage imgLegend = null;
86
        
87
    final public static DataFlavor INFO_FLAVOR =
88
            new DataFlavor(TocItemLeaf.class, "ItemLeaf");
89

    
90
        static DataFlavor flavors[] = {INFO_FLAVOR };
91
        
92
        
93
        public TocItemLeaf(ISymbol symbol, String description, int layerType)
94
        {
95
                this.symbol = symbol;
96
                this.description = description;
97
                this.layerType = layerType;
98
        }
99
        public TocItemLeaf()
100
        {
101
                
102
        }
103
        public void setImageLegend(Image imageLegend, String descrip, Dimension size) 
104
        {                
105
                this.description = descrip;
106
                this.sz = size;
107
                imgLegend = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
108
                Graphics2D g2 = imgLegend.createGraphics();
109
                // Rectangle r = new Rectangle(sz.width, sz.height);
110
                // TODO: Calcular ratio
111

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

    
115
            boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, sz.height, null);
116
//                if (ratioImage > ratioToc) {
117
//                        int newHeight = (int) (sz.width / ratioImage);
118
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, newHeight, null);
119
//                } else {
120
//                        int newWidth = (int) (sz.height * ratioImage);
121
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, newWidth, sz.height, null);
122
//                }
123
                
124
        }
125
        /* (non-Javadoc)
126
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
127
         */
128
        public String getLabel() {
129
                return description;
130
        }
131

    
132
        /* (non-Javadoc)
133
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
134
         */
135
        public Icon getIcon() {
136
                // System.out.println("Dentro de getIcon: layerType=" + layerType);
137
                if (imgLegend != null)
138
                {
139
//                        Graphics2D g2 = imgLegend.createGraphics();
140
//                        g2.drawString("Prueba", 10, 20);
141
                        return new ImageIcon(imgLegend);
142
                }
143
                
144
                BufferedImage img = null;
145
                Graphics2D g2 = null;
146
                Rectangle r = null;
147
                
148
                switch (layerType)
149
                {
150
                        case FShape.POINT:
151
                        case FShape.LINE:
152
                        case FShape.POLYGON:
153
                        case FShape.POINT | FShape.Z:
154
                        case FShape.LINE  | FShape.Z:
155
                        case FShape.POLYGON | FShape.Z:
156
                        case FShape.POINT | FShape.M:
157
                        case FShape.LINE | FShape.M:
158
                        case FShape.POLYGON | FShape.M:
159
                        case FShape.MULTIPOINT:
160
                                img = new BufferedImage(w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
161
                                g2 = img.createGraphics();
162
                                r = new Rectangle(w_1symbol, h_1symbol);        
163

    
164
//                                // TODO: CAMBIAR PARA QUE NO DEPENDA DE FSYMBOL
165
//                                if (symbol instanceof FSymbol)
166
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
167
//                                // Por ahora, los que no sean FSymbol no se renderizan en el TOC.
168
                                break;                                                                        
169
                        case FShape.MULTI:
170
                                img = new BufferedImage(3*w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
171
                                g2 = img.createGraphics();
172
                                r = new Rectangle(3*w_1symbol, h_1symbol);
173
//                                if (symbol instanceof FSymbol)
174
//                                        FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
175
                                break;                                        
176
                        
177
                }
178
                
179
                if (g2 == null) return null;//TODO tipo de shape no soportado.
180
                try {
181
                        symbol.drawInsideRectangle(g2, AffineTransform.getScaleInstance(0.8,0.8), r, null);
182
                } catch (SymbolDrawingException e) {
183
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
184
                                try {
185
                                        SymbologyFactory.getWarningSymbol(
186
                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
187
                                                        symbol.getDescription(),
188
                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS
189
                                                        ).drawInsideRectangle(g2, g2.getTransform(), r.getBounds(), null);
190
                                } catch (SymbolDrawingException e1) {
191
                                        // IMPOSSIBLE TO REACH THIS
192
                                }
193
                        } else {
194
                                // should be unreachable code
195
                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
196
                        }
197
                }
198
                return new ImageIcon(img);
199
        }
200
        
201
        /* (non-Javadoc)
202
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
203
         */
204
        public DataFlavor[] getTransferDataFlavors() {
205
                return flavors;
206
        }
207

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

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

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