Revision 45517
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 | ||
---|---|---|
55 | 55 |
* PictureMarkerSymbol allows to use an image file as a definition to be painted |
56 | 56 |
* instead of a marker symbol. |
57 | 57 |
*/ |
58 |
@SuppressWarnings("UseSpecificCatch") |
|
58 | 59 |
public class PictureMarkerSymbol extends AbstractMarkerSymbol implements IPictureMarkerSymbol { |
59 | 60 |
|
60 |
private final Logger LOG = |
|
61 |
LoggerFactory.getLogger(PictureMarkerSymbol.class); |
|
61 |
private final Logger LOG = LoggerFactory.getLogger(PictureMarkerSymbol.class); |
|
62 | 62 |
|
63 |
public static final String PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME =
|
|
64 |
"PictureMarkerSymbol"; |
|
63 |
public static final String PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME |
|
64 |
= "PictureMarkerSymbol";
|
|
65 | 65 |
private static final String SELECTED = "selected"; |
66 | 66 |
private static final String SELECTION_SYMBOL = "selectionSym"; |
67 | 67 |
private static final String BACKGROUND_IMAGE = "bgImage"; |
68 | 68 |
private static final String BACKGROUND_SELECTION_IMAGE = "bgSelImage"; |
69 | 69 |
|
70 | 70 |
// private static final float SELECTION_OPACITY_FACTOR = .3F; |
71 |
|
|
72 | 71 |
private boolean selected; |
73 | 72 |
private PictureMarkerSymbol selectionSym; |
74 | 73 |
|
75 | 74 |
private BackgroundFileStyle bgImage; |
76 | 75 |
private BackgroundFileStyle bgSelImage; |
77 | 76 |
|
78 |
/** |
|
79 |
* Constructor method |
|
80 |
*/ |
|
81 | 77 |
public PictureMarkerSymbol() { |
82 | 78 |
super(); |
83 | 79 |
setSize(18); |
... | ... | |
86 | 82 |
/** |
87 | 83 |
* Constructor method |
88 | 84 |
* |
89 |
* @param imageURL |
|
90 |
* , URL of the normal image |
|
91 |
* @param selImageURL |
|
92 |
* , URL of the image when it is selected in the map |
|
85 |
* @param imageURL , URL of the normal image |
|
86 |
* @param selImageURL , URL of the image when it is selected in the map |
|
93 | 87 |
* @throws IOException |
94 | 88 |
*/ |
95 | 89 |
public PictureMarkerSymbol(URL imageURL, URL selImageURL) throws IOException { |
96 | 90 |
setImage(imageURL); |
97 |
if (selImageURL != null) |
|
91 |
if (selImageURL != null) {
|
|
98 | 92 |
setSelImage(selImageURL); |
99 |
else
|
|
93 |
} else {
|
|
100 | 94 |
setSelImage(imageURL); |
95 |
} |
|
101 | 96 |
|
102 | 97 |
} |
103 | 98 |
|
104 | 99 |
/** |
105 | 100 |
* Sets the file for the image to be used as a marker symbol |
106 | 101 |
* |
107 |
* @param imageFile |
|
108 |
* , File |
|
102 |
* @param imageUrl |
|
109 | 103 |
* @throws IOException |
110 | 104 |
*/ |
105 |
@Override |
|
111 | 106 |
public void setImage(URL imageUrl) throws IOException { |
112 |
|
|
113 | 107 |
bgImage = BackgroundFileStyle.createStyleByURL(imageUrl); |
114 | 108 |
} |
115 | 109 |
|
... | ... | |
117 | 111 |
* Sets the file for the image to be used as a marker symbol (when it is |
118 | 112 |
* selected in the map) |
119 | 113 |
* |
120 |
* @param imageFile |
|
121 |
* , File |
|
114 |
* @param imageFileUrl |
|
122 | 115 |
* @throws IOException |
123 | 116 |
*/ |
117 |
@Override |
|
124 | 118 |
public void setSelImage(URL imageFileUrl) throws IOException { |
125 |
|
|
126 | 119 |
bgSelImage = BackgroundFileStyle.createStyleByURL(imageFileUrl); |
127 | 120 |
} |
128 | 121 |
|
122 |
@Override |
|
129 | 123 |
public ISymbol getSymbolForSelection() { |
130 | 124 |
if (selectionSym == null) { |
131 | 125 |
try { |
132 | 126 |
selectionSym = (PictureMarkerSymbol) this.clone(); |
133 | 127 |
} catch (CloneNotSupportedException e) { |
134 |
LOG.error("Error creating the selection symbol for the symbol " |
|
135 |
+ this, e); |
|
128 |
LOG.warn("Error creating the selection symbol for the symbol "+ this, e); |
|
136 | 129 |
} |
137 | 130 |
selectionSym.selected = true; |
138 |
selectionSym.selectionSym = selectionSym; // avoid too much lazy |
|
139 |
// creations |
|
140 |
}else{ |
|
131 |
selectionSym.selectionSym = selectionSym; // avoid too much lazy creations |
|
132 |
} else { |
|
141 | 133 |
selectionSym.setColor(MapContext.getSelectionColor()); |
142 | 134 |
} |
143 | 135 |
return selectionSym; |
144 | 136 |
} |
145 | 137 |
|
138 |
@Override |
|
146 | 139 |
public void draw(Graphics2D g, |
147 |
AffineTransform affineTransform, |
|
148 |
Geometry geom, |
|
149 |
Feature f, |
|
150 |
Cancellable cancel) { |
|
140 |
AffineTransform affineTransform,
|
|
141 |
Geometry geom,
|
|
142 |
Feature f,
|
|
143 |
Cancellable cancel) {
|
|
151 | 144 |
Point p; |
152 | 145 |
try { |
153 | 146 |
p = geom.centroid(); |
154 |
} catch(Exception ex) { |
|
147 |
} catch (Exception ex) {
|
|
155 | 148 |
return; |
156 | 149 |
} |
157 |
if (affineTransform!=null) {
|
|
158 |
p.transform(affineTransform);
|
|
159 |
}
|
|
150 |
if (affineTransform != null) {
|
|
151 |
p.transform(affineTransform);
|
|
152 |
}
|
|
160 | 153 |
double x, y; |
161 | 154 |
int size = (int) Math.round(getSize()); |
162 | 155 |
double halfSize = getSize() / 2; |
... | ... | |
176 | 169 |
bg.drawInsideRectangle(g, rect); |
177 | 170 |
} catch (SymbolDrawingException e) { |
178 | 171 |
LOG.warn(Messages.getText("label_style_could_not_be_painted") |
179 |
+ ": " + bg.getSource().toString(), |
|
180 |
e); |
|
172 |
+ ": " + bg.getSource().toString(),
|
|
173 |
e);
|
|
181 | 174 |
} |
182 | 175 |
} else { |
183 | 176 |
LOG.warn(Messages.getText("label_style_could_not_be_painted") |
184 |
+ ": bg is Null"); |
|
177 |
+ ": bg is Null");
|
|
185 | 178 |
} |
186 | 179 |
g.rotate(-auxRotation, halfSize, halfSize); |
187 | 180 |
g.translate(-(x + xOffset), -(y + yOffset)); |
... | ... | |
190 | 183 |
|
191 | 184 |
} |
192 | 185 |
|
193 |
|
|
186 |
@Override |
|
194 | 187 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
195 | 188 |
|
196 | 189 |
/* |
... | ... | |
202 | 195 |
|
203 | 196 |
super.drawInsideRectangle(g, scaleInstance, r, properties); |
204 | 197 |
|
205 |
// =================== Restoring size |
|
206 | 198 |
this.setSize(saved_size); |
207 | 199 |
} |
208 | 200 |
|
209 |
|
|
210 | 201 |
public String getClassName() { |
211 | 202 |
return getClass().getName(); |
212 | 203 |
} |
213 | 204 |
|
214 |
// public void print(Graphics2D g, AffineTransform at, FShape shape) |
|
215 |
// throws ReadDriverException { |
|
216 |
// // TODO Implement it |
|
217 |
// throw new Error("Not yet implemented!"); |
|
218 |
// |
|
219 |
// } |
|
220 |
/** |
|
221 |
* Returns the URL of the image that is used as a marker symbol |
|
222 |
* @return imagePath,URL |
|
223 |
*/ |
|
205 |
/** |
|
206 |
* Returns the URL of the image that is used as a marker symbol |
|
207 |
* |
|
208 |
* @return imagePath,URL |
|
209 |
*/ |
|
210 |
@Override |
|
224 | 211 |
public URL getSource() { |
225 |
return bgImage.getSource();
|
|
212 |
return bgImage.getSource();
|
|
226 | 213 |
} |
214 |
|
|
227 | 215 |
/** |
228 |
* Returns the URL of the image that is used as a marker symbol (when it |
|
229 |
is selected in the map) |
|
216 |
* Returns the URL of the image that is used as a marker symbol (when it is |
|
217 |
* selected in the map) |
|
218 |
* |
|
230 | 219 |
* @return selimagePath,URL |
231 | 220 |
*/ |
232 |
public URL getSelectedSource(){ |
|
233 |
return bgSelImage.getSource(); |
|
221 |
@Override |
|
222 |
public URL getSelectedSource() { |
|
223 |
return bgSelImage.getSource(); |
|
234 | 224 |
} |
235 | 225 |
|
236 |
public Object clone() throws CloneNotSupportedException { |
|
226 |
@Override |
|
227 |
public PictureMarkerSymbol clone() throws CloneNotSupportedException { |
|
237 | 228 |
PictureMarkerSymbol copy = (PictureMarkerSymbol) super.clone(); |
238 | 229 |
|
239 | 230 |
// clone selection |
240 | 231 |
if (selectionSym != null) { |
241 |
//to avoid an infinite loop
|
|
242 |
if (this == selectionSym){
|
|
243 |
copy.selectionSym = copy;
|
|
244 |
} else {
|
|
245 |
copy.selectionSym = (PictureMarkerSymbol) selectionSym.clone();
|
|
246 |
}
|
|
232 |
//to avoid an infinite loop
|
|
233 |
if (this == selectionSym) {
|
|
234 |
copy.selectionSym = copy;
|
|
235 |
} else {
|
|
236 |
copy.selectionSym = (PictureMarkerSymbol) selectionSym.clone();
|
|
237 |
}
|
|
247 | 238 |
} |
248 | 239 |
|
249 | 240 |
// clone brackground image |
... | ... | |
258 | 249 |
return copy; |
259 | 250 |
} |
260 | 251 |
|
252 |
@Override |
|
261 | 253 |
public void loadFromState(PersistentState state) throws PersistenceException { |
262 | 254 |
// Set parent style properties |
263 | 255 |
super.loadFromState(state); |
... | ... | |
265 | 257 |
this.selected = (Boolean) state.get(SELECTED); |
266 | 258 |
this.selectionSym = (PictureMarkerSymbol) state.get(SELECTION_SYMBOL); |
267 | 259 |
this.bgImage = (BackgroundFileStyle) state.get(BACKGROUND_IMAGE); |
268 |
this.bgSelImage = |
|
269 |
(BackgroundFileStyle) state.get(BACKGROUND_SELECTION_IMAGE); |
|
260 |
this.bgSelImage = (BackgroundFileStyle) state.get(BACKGROUND_SELECTION_IMAGE); |
|
270 | 261 |
} |
271 | 262 |
|
263 |
@Override |
|
272 | 264 |
public void saveToState(PersistentState state) throws PersistenceException { |
273 | 265 |
// Save parent fill symbol properties |
274 | 266 |
super.saveToState(state); |
... | ... | |
282 | 274 |
|
283 | 275 |
public static class RegisterPersistence implements Callable { |
284 | 276 |
|
277 |
@Override |
|
285 | 278 |
public Object call() throws Exception { |
286 | 279 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
287 | 280 |
if (manager.getDefinition(PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) { |
288 |
DynStruct definition =
|
|
289 |
manager.addDefinition(PictureMarkerSymbol.class, |
|
290 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
|
291 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME |
|
292 |
+ " Persistence definition", |
|
293 |
null, |
|
294 |
null); |
|
281 |
DynStruct definition |
|
282 |
= manager.addDefinition(PictureMarkerSymbol.class,
|
|
283 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
|
|
284 |
PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME
|
|
285 |
+ " Persistence definition",
|
|
286 |
null,
|
|
287 |
null);
|
|
295 | 288 |
|
296 | 289 |
// Extend the Style base definition |
297 | 290 |
definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
298 | 291 |
|
299 | 292 |
definition.addDynFieldBoolean(SELECTED).setMandatory(false); |
300 | 293 |
definition.addDynFieldObject(SELECTION_SYMBOL).setMandatory(false) |
301 |
.setClassOfValue(PictureMarkerSymbol.class).setMandatory(false); |
|
294 |
.setClassOfValue(PictureMarkerSymbol.class).setMandatory(false);
|
|
302 | 295 |
definition.addDynFieldObject(BACKGROUND_IMAGE) |
303 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false); |
|
296 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
|
|
304 | 297 |
definition.addDynFieldObject(BACKGROUND_SELECTION_IMAGE) |
305 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false); |
|
298 |
.setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
|
|
306 | 299 |
} |
307 |
return Boolean.TRUE;
|
|
300 |
return true;
|
|
308 | 301 |
} |
309 | 302 |
} |
310 | 303 |
|
311 |
public static class RegisterSymbol implements Callable {
|
|
304 |
public static class RegisterSymbol implements Callable {
|
|
312 | 305 |
|
313 |
public Object call() throws Exception { |
|
314 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
|
306 |
@Override |
|
307 |
public Object call() throws Exception { |
|
308 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
|
315 | 309 |
|
316 |
manager.registerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
|
|
317 |
PictureMarkerSymbol.class);
|
|
310 |
manager.registerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
|
|
311 |
PictureMarkerSymbol.class);
|
|
318 | 312 |
|
319 |
return Boolean.TRUE;
|
|
320 |
}
|
|
313 |
return true;
|
|
314 |
}
|
|
321 | 315 |
|
322 |
}
|
|
316 |
}
|
|
323 | 317 |
|
324 | 318 |
} |
Also available in: Unified diff