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 @ 40596
History | View | Annotate | Download (6.88 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
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 | 40596 | jjdelcerro | |
25 | 40435 | jjdelcerro | 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 | |||
37 | import javax.swing.Icon; |
||
38 | import javax.swing.ImageIcon; |
||
39 | |||
40 | import org.gvsig.andami.PluginServices; |
||
41 | import org.gvsig.app.project.documents.view.IContextMenuAction; |
||
42 | import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry; |
||
43 | import org.gvsig.fmap.geom.Geometry; |
||
44 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
45 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
46 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
||
47 | |||
48 | |||
49 | /**
|
||
50 | * @author FJP
|
||
51 | *
|
||
52 | * TODO To change the template for this generated type comment go to
|
||
53 | * Window - Preferences - Java - Code Generation - Code and Comments
|
||
54 | */
|
||
55 | public class TocItemLeaf implements ITocItem { |
||
56 | |||
57 | private ISymbol symbol;
|
||
58 | private String description; |
||
59 | private int layerType; |
||
60 | private static int w_1symbol = 20; |
||
61 | private static int h_1symbol = 15; |
||
62 | |||
63 | private Dimension sz; |
||
64 | BufferedImage imgLegend = null; |
||
65 | |||
66 | final public static DataFlavor INFO_FLAVOR = |
||
67 | new DataFlavor(TocItemLeaf.class, "ItemLeaf"); |
||
68 | |||
69 | static DataFlavor flavors[] = {INFO_FLAVOR }; |
||
70 | |||
71 | |||
72 | public TocItemLeaf(ISymbol symbol, String description, int layerType) |
||
73 | { |
||
74 | this.symbol = symbol;
|
||
75 | this.description = description;
|
||
76 | this.layerType = layerType;
|
||
77 | } |
||
78 | public TocItemLeaf()
|
||
79 | { |
||
80 | |||
81 | } |
||
82 | public void setImageLegend(Image imageLegend, String descrip, Dimension size) |
||
83 | { |
||
84 | this.description = descrip;
|
||
85 | this.sz = size;
|
||
86 | imgLegend = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
||
87 | Graphics2D g2 = imgLegend.createGraphics();
|
||
88 | // Rectangle r = new Rectangle(sz.width, sz.height);
|
||
89 | // TODO: Calcular ratio
|
||
90 | |||
91 | double ratioImage = (double) imageLegend.getWidth(null) / (double) imageLegend.getHeight(null); |
||
92 | double ratioToc = (double) sz.width / (double) sz.height; |
||
93 | |||
94 | boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, sz.height, null); |
||
95 | // if (ratioImage > ratioToc) {
|
||
96 | // int newHeight = (int) (sz.width / ratioImage);
|
||
97 | // boolean resul = g2.drawImage(imageLegend, 0, 0, sz.width, newHeight, null);
|
||
98 | // } else {
|
||
99 | // int newWidth = (int) (sz.height * ratioImage);
|
||
100 | // boolean resul = g2.drawImage(imageLegend, 0, 0, newWidth, sz.height, null);
|
||
101 | // }
|
||
102 | |||
103 | } |
||
104 | /* (non-Javadoc)
|
||
105 | * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
|
||
106 | */
|
||
107 | public String getLabel() { |
||
108 | return description;
|
||
109 | } |
||
110 | |||
111 | /* (non-Javadoc)
|
||
112 | * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
|
||
113 | */
|
||
114 | public Icon getIcon() { |
||
115 | // System.out.println("Dentro de getIcon: layerType=" + layerType);
|
||
116 | if (imgLegend != null) |
||
117 | { |
||
118 | // Graphics2D g2 = imgLegend.createGraphics();
|
||
119 | // g2.drawString("Prueba", 10, 20);
|
||
120 | return new ImageIcon(imgLegend); |
||
121 | } |
||
122 | |||
123 | BufferedImage img = null; |
||
124 | Graphics2D g2 = null; |
||
125 | Rectangle r = null; |
||
126 | |||
127 | switch (layerType)
|
||
128 | { |
||
129 | case Geometry.TYPES.POINT:
|
||
130 | case Geometry.TYPES.CURVE:
|
||
131 | case Geometry.TYPES.SURFACE:
|
||
132 | case Geometry.TYPES.MULTICURVE:
|
||
133 | case Geometry.TYPES.MULTISURFACE:
|
||
134 | case Geometry.TYPES.MULTIPOINT:
|
||
135 | img = new BufferedImage(w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB); |
||
136 | g2 = img.createGraphics(); |
||
137 | r = new Rectangle(w_1symbol, h_1symbol); |
||
138 | |||
139 | // // TODO: CAMBIAR PARA QUE NO DEPENDA DE FSYMBOL
|
||
140 | // if (symbol instanceof FSymbol)
|
||
141 | // FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
|
||
142 | // // Por ahora, los que no sean FSymbol no se renderizan en el TOC.
|
||
143 | break;
|
||
144 | case Geometry.TYPES.GEOMETRY:
|
||
145 | img = new BufferedImage(3*w_1symbol, h_1symbol, BufferedImage.TYPE_INT_ARGB); |
||
146 | g2 = img.createGraphics(); |
||
147 | r = new Rectangle(3*w_1symbol, h_1symbol); |
||
148 | // if (symbol instanceof FSymbol)
|
||
149 | // FGraphicUtilities.DrawSymbol(g2, AffineTransform.getScaleInstance(0.8,0.8), r, (FSymbol) symbol);
|
||
150 | break;
|
||
151 | |||
152 | } |
||
153 | |||
154 | if (g2 == null) { |
||
155 | return null;//TODO tipo de shape no soportado. |
||
156 | } |
||
157 | try {
|
||
158 | symbol.drawInsideRectangle(g2, AffineTransform.getScaleInstance(1,1), r,null); |
||
159 | } catch (SymbolDrawingException e) {
|
||
160 | if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
|
||
161 | try {
|
||
162 | MapContextLocator.getSymbolManager() |
||
163 | .getWarningSymbol( |
||
164 | SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, |
||
165 | symbol.getDescription(), |
||
166 | SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) |
||
167 | .drawInsideRectangle(g2, g2.getTransform(), |
||
168 | r.getBounds(), null);
|
||
169 | } catch (SymbolDrawingException e1) {
|
||
170 | // IMPOSSIBLE TO REACH THIS
|
||
171 | } |
||
172 | } else {
|
||
173 | // should be unreachable code
|
||
174 | throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch")); |
||
175 | } |
||
176 | } |
||
177 | return new ImageIcon(img); |
||
178 | } |
||
179 | |||
180 | /* (non-Javadoc)
|
||
181 | * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
|
||
182 | */
|
||
183 | public DataFlavor[] getTransferDataFlavors() { |
||
184 | return flavors;
|
||
185 | } |
||
186 | |||
187 | /* (non-Javadoc)
|
||
188 | * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
|
||
189 | */
|
||
190 | public boolean isDataFlavorSupported(DataFlavor dF) { |
||
191 | return dF.equals(INFO_FLAVOR);
|
||
192 | } |
||
193 | |||
194 | /* (non-Javadoc)
|
||
195 | * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
|
||
196 | */
|
||
197 | public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException { |
||
198 | if (dF.equals(INFO_FLAVOR)) {
|
||
199 | return this; |
||
200 | } else {
|
||
201 | throw new UnsupportedFlavorException(dF); |
||
202 | } |
||
203 | } |
||
204 | /* (non-Javadoc)
|
||
205 | * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
|
||
206 | */
|
||
207 | public Dimension getSize() { |
||
208 | return sz;
|
||
209 | } |
||
210 | /* (non-Javadoc)
|
||
211 | * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
|
||
212 | */
|
||
213 | public void setSize(Dimension sz) { |
||
214 | this.sz = sz;
|
||
215 | |||
216 | } |
||
217 | |||
218 | |||
219 | /**
|
||
220 | * @return Returns the symbol.
|
||
221 | */
|
||
222 | public ISymbol getSymbol() {
|
||
223 | return symbol;
|
||
224 | } |
||
225 | public IContextMenuAction getDoubleClickAction() {
|
||
226 | return new ChangeSymbolTocMenuEntry(); |
||
227 | } |
||
228 | } |