svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / marker / impl / PictureMarkerSymbol.java @ 45526
History | View | Annotate | Download (12 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.symbology.fmap.mapcontext.rendering.symbol.marker.impl; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.geom.AffineTransform; |
30 |
import java.awt.geom.Point2D; |
31 |
import java.io.IOException; |
32 |
import java.net.URL; |
33 |
|
34 |
import org.gvsig.compat.print.PrintAttributes; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.fmap.mapcontext.MapContext; |
43 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
46 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
47 |
import org.gvsig.i18n.Messages; |
48 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol; |
49 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.BackgroundFileStyle; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.dynobject.DynStruct; |
52 |
import org.gvsig.tools.persistence.PersistenceManager; |
53 |
import org.gvsig.tools.persistence.PersistentState; |
54 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
55 |
import org.gvsig.tools.task.Cancellable; |
56 |
import org.gvsig.tools.util.Callable; |
57 |
import org.slf4j.Logger; |
58 |
import org.slf4j.LoggerFactory; |
59 |
|
60 |
/**
|
61 |
* PictureMarkerSymbol allows to use an image file as a definition to be painted
|
62 |
* instead of a marker symbol.
|
63 |
*/
|
64 |
@SuppressWarnings("UseSpecificCatch") |
65 |
public class PictureMarkerSymbol extends AbstractMarkerSymbol implements IPictureMarkerSymbol { |
66 |
|
67 |
private final Logger LOG = LoggerFactory.getLogger(PictureMarkerSymbol.class); |
68 |
|
69 |
public static final String PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME |
70 |
= "PictureMarkerSymbol";
|
71 |
private static final String SELECTED = "selected"; |
72 |
private static final String SELECTION_SYMBOL = "selectionSym"; |
73 |
private static final String BACKGROUND_IMAGE = "bgImage"; |
74 |
private static final String BACKGROUND_SELECTION_IMAGE = "bgSelImage"; |
75 |
|
76 |
// private static final float SELECTION_OPACITY_FACTOR = .3F;
|
77 |
private boolean selected; |
78 |
private PictureMarkerSymbol selectionSym;
|
79 |
|
80 |
private BackgroundFileStyle bgImage;
|
81 |
private BackgroundFileStyle bgSelImage;
|
82 |
|
83 |
public PictureMarkerSymbol() {
|
84 |
super();
|
85 |
setSize(18);
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* Constructor method
|
90 |
*
|
91 |
* @param imageURL , URL of the normal image
|
92 |
* @param selImageURL , URL of the image when it is selected in the map
|
93 |
* @throws IOException
|
94 |
*/
|
95 |
public PictureMarkerSymbol(URL imageURL, URL selImageURL) throws IOException { |
96 |
setImage(imageURL); |
97 |
if (selImageURL != null) { |
98 |
setSelImage(selImageURL); |
99 |
} else {
|
100 |
setSelImage(imageURL); |
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Sets the file for the image to be used as a marker symbol
|
107 |
*
|
108 |
* @param imageUrl
|
109 |
* @throws IOException
|
110 |
*/
|
111 |
@Override
|
112 |
public void setImage(URL imageUrl) throws IOException { |
113 |
bgImage = BackgroundFileStyle.createStyleByURL(imageUrl); |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Sets the file for the image to be used as a marker symbol (when it is
|
118 |
* selected in the map)
|
119 |
*
|
120 |
* @param imageFileUrl
|
121 |
* @throws IOException
|
122 |
*/
|
123 |
@Override
|
124 |
public void setSelImage(URL imageFileUrl) throws IOException { |
125 |
bgSelImage = BackgroundFileStyle.createStyleByURL(imageFileUrl); |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public ISymbol getSymbolForSelection() {
|
130 |
if (selectionSym == null) { |
131 |
try {
|
132 |
selectionSym = (PictureMarkerSymbol) this.clone();
|
133 |
} catch (CloneNotSupportedException e) { |
134 |
LOG.warn("Error creating the selection symbol for the symbol "+ this, e); |
135 |
} |
136 |
selectionSym.selected = true;
|
137 |
selectionSym.selectionSym = selectionSym; // avoid too much lazy creations
|
138 |
} else {
|
139 |
selectionSym.setColor(MapContext.getSelectionColor()); |
140 |
} |
141 |
return selectionSym;
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public void draw(Graphics2D g, |
146 |
AffineTransform affineTransform,
|
147 |
Geometry geom, |
148 |
Feature f, |
149 |
Cancellable cancel) { |
150 |
Point p;
|
151 |
try {
|
152 |
p = geom.centroid(); |
153 |
} catch (Exception ex) { |
154 |
return;
|
155 |
} |
156 |
if (affineTransform != null) { |
157 |
p.transform(affineTransform); |
158 |
} |
159 |
double x, y;
|
160 |
int size = (int) Math.round(getSize()); |
161 |
double halfSize = getSize() / 2; |
162 |
x = p.getX() - halfSize; |
163 |
y = p.getY() - halfSize; |
164 |
Point2D theOffset = this.getEfectiveOffset(f); |
165 |
int xOffset = (int) theOffset.getX(); |
166 |
int yOffset = (int) theOffset.getY(); |
167 |
|
168 |
if (size > 0) { |
169 |
BackgroundFileStyle bg = (!selected) ? bgImage : bgSelImage; |
170 |
Rectangle rect = new Rectangle(size, size); |
171 |
g.translate(x + xOffset, y + yOffset); |
172 |
double auxRotation = getEfectiveRotation(f);
|
173 |
g.rotate(auxRotation, halfSize, halfSize); |
174 |
if (bg != null) { |
175 |
try {
|
176 |
bg.drawInsideRectangle(g, rect); |
177 |
} catch (SymbolDrawingException e) {
|
178 |
LOG.warn(Messages.getText("label_style_could_not_be_painted")
|
179 |
+ ": " + bg.getSource().toString(),
|
180 |
e); |
181 |
} |
182 |
} else {
|
183 |
LOG.warn(Messages.getText("label_style_could_not_be_painted")
|
184 |
+ ": bg is Null");
|
185 |
} |
186 |
g.rotate(-auxRotation, halfSize, halfSize); |
187 |
g.translate(-(x + xOffset), -(y + yOffset)); |
188 |
|
189 |
if( isDrawLineToOffset() ) {
|
190 |
g.setColor(this.getLineToOffsetColor());
|
191 |
g.drawLine((int)p.getX(), (int)p.getY(), (int)p.getX() + xOffset, (int)p.getY() + yOffset); |
192 |
} |
193 |
} |
194 |
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
199 |
|
200 |
/*
|
201 |
* Marker symbols which are not simple (images, etc)
|
202 |
* are resized when drawn inside a rectangle
|
203 |
*/
|
204 |
double saved_size = this.getSize(); |
205 |
this.setSize(r.getHeight());
|
206 |
|
207 |
super.drawInsideRectangle(g, scaleInstance, r, properties);
|
208 |
|
209 |
this.setSize(saved_size);
|
210 |
} |
211 |
|
212 |
public String getClassName() { |
213 |
return getClass().getName();
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* Returns the URL of the image that is used as a marker symbol
|
218 |
*
|
219 |
* @return imagePath,URL
|
220 |
*/
|
221 |
@Override
|
222 |
public URL getSource() { |
223 |
return bgImage.getSource();
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Returns the URL of the image that is used as a marker symbol (when it is
|
228 |
* selected in the map)
|
229 |
*
|
230 |
* @return selimagePath,URL
|
231 |
*/
|
232 |
@Override
|
233 |
public URL getSelectedSource() { |
234 |
return bgSelImage.getSource();
|
235 |
} |
236 |
|
237 |
@Override
|
238 |
public PictureMarkerSymbol clone() throws CloneNotSupportedException { |
239 |
PictureMarkerSymbol copy = (PictureMarkerSymbol) super.clone();
|
240 |
|
241 |
// clone selection
|
242 |
if (selectionSym != null) { |
243 |
//to avoid an infinite loop
|
244 |
if (this == selectionSym) { |
245 |
copy.selectionSym = copy; |
246 |
} else {
|
247 |
copy.selectionSym = (PictureMarkerSymbol) selectionSym.clone(); |
248 |
} |
249 |
} |
250 |
|
251 |
// clone brackground image
|
252 |
if (bgImage != null) { |
253 |
copy.bgImage = (BackgroundFileStyle) bgImage.clone(); |
254 |
} |
255 |
|
256 |
// clone selection brackground image
|
257 |
if (bgSelImage != null) { |
258 |
copy.bgSelImage = (BackgroundFileStyle) bgSelImage.clone(); |
259 |
} |
260 |
return copy;
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void loadFromState(PersistentState state) throws PersistenceException { |
265 |
// Set parent style properties
|
266 |
super.loadFromState(state);
|
267 |
|
268 |
this.selected = (Boolean) state.get(SELECTED); |
269 |
this.selectionSym = (PictureMarkerSymbol) state.get(SELECTION_SYMBOL);
|
270 |
this.bgImage = (BackgroundFileStyle) state.get(BACKGROUND_IMAGE);
|
271 |
this.bgSelImage = (BackgroundFileStyle) state.get(BACKGROUND_SELECTION_IMAGE);
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public void saveToState(PersistentState state) throws PersistenceException { |
276 |
// Save parent fill symbol properties
|
277 |
super.saveToState(state);
|
278 |
|
279 |
// Save own properties
|
280 |
state.set(SELECTED, this.selected);
|
281 |
state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
|
282 |
state.set(BACKGROUND_IMAGE, this.bgImage);
|
283 |
state.set(BACKGROUND_SELECTION_IMAGE, this.bgSelImage);
|
284 |
} |
285 |
|
286 |
public static class RegisterPersistence implements Callable { |
287 |
|
288 |
@Override
|
289 |
public Object call() throws Exception { |
290 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
291 |
if (manager.getDefinition(PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) { |
292 |
DynStruct definition |
293 |
= manager.addDefinition(PictureMarkerSymbol.class, |
294 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
295 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME |
296 |
+ " Persistence definition",
|
297 |
null,
|
298 |
null);
|
299 |
|
300 |
// Extend the Style base definition
|
301 |
definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
302 |
|
303 |
definition.addDynFieldBoolean(SELECTED).setMandatory(false);
|
304 |
definition.addDynFieldObject(SELECTION_SYMBOL).setMandatory(false)
|
305 |
.setClassOfValue(PictureMarkerSymbol.class).setMandatory(false);
|
306 |
definition.addDynFieldObject(BACKGROUND_IMAGE) |
307 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
|
308 |
definition.addDynFieldObject(BACKGROUND_SELECTION_IMAGE) |
309 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
|
310 |
} |
311 |
return true; |
312 |
} |
313 |
} |
314 |
|
315 |
public static class RegisterSymbol implements Callable { |
316 |
|
317 |
@Override
|
318 |
public Object call() throws Exception { |
319 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
320 |
|
321 |
manager.registerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
322 |
PictureMarkerSymbol.class); |
323 |
|
324 |
return true; |
325 |
} |
326 |
|
327 |
} |
328 |
|
329 |
@Override
|
330 |
public String[] getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException { |
331 |
// By default only need the default Geometry
|
332 |
FeatureType ftype = featureStore.getDefaultFeatureTypeQuietly(); |
333 |
String[] res = new String[ftype.size()]; |
334 |
int cont = 0; |
335 |
for (FeatureAttributeDescriptor attr : ftype) {
|
336 |
res[cont++]=attr.getName(); |
337 |
} |
338 |
return res;
|
339 |
} |
340 |
|
341 |
|
342 |
} |