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 @ 43496

History | View | Annotate | Download (6.91 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
package org.gvsig.app.project.documents.view.toc;
26

    
27
import java.awt.Dimension;
28
import java.awt.Graphics2D;
29
import java.awt.Image;
30
import java.awt.Rectangle;
31
import java.awt.datatransfer.DataFlavor;
32
import java.awt.datatransfer.UnsupportedFlavorException;
33
import java.awt.geom.AffineTransform;
34
import java.awt.image.BufferedImage;
35
import java.io.IOException;
36
import java.util.logging.Level;
37

    
38
import javax.swing.Icon;
39
import javax.swing.ImageIcon;
40

    
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.andami.PluginServices;
45
import org.gvsig.app.project.documents.view.IContextMenuAction;
46
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.type.GeometryType;
50
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
51
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
52
import org.gvsig.fmap.mapcontext.MapContextLocator;
53
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
54
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
55

    
56

    
57
/**
58
 * @author FJP
59
 *
60
 * TODO To change the template for this generated type comment go to
61
 * Window - Preferences - Java - Code Generation - Code and Comments
62
 */
63
public class TocItemLeaf implements ITocItem {
64

    
65
    private static Logger logger = LoggerFactory.getLogger(TocItemLeaf.class);
66

    
67
        private ISymbol symbol;
68
        private String description;
69
        private int layerType;
70
        private static int w_1symbol = 20;
71
        private static int h_1symbol = 15;
72

    
73
        private Dimension sz;
74
        BufferedImage imgLegend = null;
75

    
76
    final public static DataFlavor INFO_FLAVOR =
77
            new DataFlavor(TocItemLeaf.class, "ItemLeaf");
78

    
79
        static DataFlavor flavors[] = {INFO_FLAVOR };
80

    
81

    
82
        public TocItemLeaf(ISymbol symbol, String description, int layerType)
83
        {
84
                this.symbol = symbol;
85
                this.description = description;
86
                this.layerType = layerType;
87
        }
88
        public TocItemLeaf()
89
        {
90

    
91
        }
92
        public void setImageLegend(Image imageLegend, String descrip, Dimension size)
93
        {
94
                this.description = descrip;
95
                this.sz = size;
96
                imgLegend = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
97
                Graphics2D g2 = imgLegend.createGraphics();
98
                // Rectangle r = new Rectangle(sz.width, sz.height);
99
                // TODO: Calcular ratio
100

    
101
                double ratioImage =  (double) imageLegend.getWidth(null) / (double) imageLegend.getHeight(null);
102
            double ratioToc  = (double) sz.width / (double) sz.height;
103

    
104
            boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, sz.height, null);
105
//                if (ratioImage > ratioToc) {
106
//                        int newHeight = (int) (sz.width / ratioImage);
107
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, newHeight, null);
108
//                } else {
109
//                        int newWidth = (int) (sz.height * ratioImage);
110
//                        boolean resul = g2.drawImage(imageLegend, 0, 0, newWidth, sz.height, null);
111
//                }
112

    
113
        }
114
        /* (non-Javadoc)
115
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
116
         */
117
        public String getLabel() {
118
                return description;
119
        }
120

    
121
        /* (non-Javadoc)
122
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
123
         */
124
        public Icon getIcon() {
125
                // System.out.println("Dentro de getIcon: layerType=" + layerType);
126
                if (imgLegend != null)
127
                {
128
//                        Graphics2D g2 = imgLegend.createGraphics();
129
//                        g2.drawString("Prueba", 10, 20);
130
                        return new ImageIcon(imgLegend);
131
                }
132

    
133
                BufferedImage img = null;
134
                Graphics2D g2 = null;
135
                Rectangle r = null;
136

    
137
        switch (layerType) {
138
            case Geometry.TYPES.GEOMETRY:
139
                                img = new BufferedImage(3*w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
140
                                g2 = img.createGraphics();
141
                                r = new Rectangle(3*w_1symbol, h_1symbol);
142
                                break;
143

    
144
            case Geometry.TYPES.NULL:
145
                break;
146

    
147
            default:
148
                                img = new BufferedImage(w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB);
149
                                g2 = img.createGraphics();
150
                                r = new Rectangle(w_1symbol, h_1symbol);
151
                                break;
152

    
153
                }
154

    
155
                if (g2 == null) {
156
                        return null;
157
                }
158
                try {
159
                    ISymbol clonesym = (ISymbol) symbol.clone();
160
                    clonesym.drawInsideRectangle(g2, AffineTransform.getScaleInstance(1,1), r,null);
161
                } catch (SymbolDrawingException e) {
162
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
163
                                try {
164
                                        MapContextLocator.getSymbolManager()
165
                                                        .getWarningSymbol(
166
                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
167
                                                                        symbol.getDescription(),
168
                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
169
                                                        .drawInsideRectangle(g2, g2.getTransform(),
170
                                                                        r.getBounds(), null);
171
                                } catch (SymbolDrawingException e1) {
172
                                        // IMPOSSIBLE TO REACH THIS
173
                                }
174
                        } else {
175
                    logger.warn("Symbol type mismatch", e);
176
                    return null;
177
                        }
178
                } catch (CloneNotSupportedException e) {
179
                    logger.warn("Symbol cannot be cloned (?)", e);
180
                    return null;
181
        }
182
                return new ImageIcon(img);
183
        }
184

    
185
        /* (non-Javadoc)
186
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
187
         */
188
        public DataFlavor[] getTransferDataFlavors() {
189
                return flavors;
190
        }
191

    
192
        /* (non-Javadoc)
193
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
194
         */
195
        public boolean isDataFlavorSupported(DataFlavor dF) {
196
                return dF.equals(INFO_FLAVOR);
197
        }
198

    
199
        /* (non-Javadoc)
200
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
201
         */
202
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
203
            if (dF.equals(INFO_FLAVOR)) {
204
                return this;
205
              } else {
206
                        throw new UnsupportedFlavorException(dF);
207
                }
208
        }
209
        /* (non-Javadoc)
210
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
211
         */
212
        public Dimension getSize() {
213
                return sz;
214
        }
215
        /* (non-Javadoc)
216
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
217
         */
218
        public void setSize(Dimension sz) {
219
                this.sz = sz;
220

    
221
        }
222

    
223

    
224
        /**
225
         * @return Returns the symbol.
226
         */
227
        public ISymbol getSymbol() {
228
                return symbol;
229
        }
230
        public IContextMenuAction getDoubleClickAction() {
231
            if(symbol==null){
232
                return null;
233
            }
234
                return new ChangeSymbolTocMenuEntry();
235
        }
236
}