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