svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / impl / DefaultMapControlManager.java @ 42651
History | View | Annotate | Download (12.8 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
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 | 40435 | jjdelcerro | */
|
24 | /*
|
||
25 | * AUTHORS (In addition to CIT):
|
||
26 | * 2009 {Iver T.I.} {Task}
|
||
27 | */
|
||
28 | |||
29 | package org.gvsig.fmap.mapcontrol.impl; |
||
30 | |||
31 | import java.awt.Color; |
||
32 | import java.util.ArrayList; |
||
33 | import java.util.Iterator; |
||
34 | 42146 | fdiaz | import java.util.LinkedHashSet; |
35 | 41743 | jjdelcerro | import java.util.List; |
36 | 40435 | jjdelcerro | import java.util.Map; |
37 | 41743 | jjdelcerro | import java.util.Set; |
38 | 40435 | jjdelcerro | import java.util.prefs.Preferences; |
39 | 42651 | jjdelcerro | import javax.swing.tree.TreeModel; |
40 | import org.gvsig.fmap.mapcontrol.LayersTreeModel; |
||
41 | 40435 | jjdelcerro | |
42 | 41811 | llmarques | import org.slf4j.Logger; |
43 | import org.slf4j.LoggerFactory; |
||
44 | |||
45 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
46 | 41811 | llmarques | import org.gvsig.fmap.mapcontext.MapContext; |
47 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextLocator; |
48 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
49 | 42651 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayers; |
50 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
51 | 42651 | jjdelcerro | import org.gvsig.fmap.mapcontrol.CompoundLayersTreeModel; |
52 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontrol.MapControl; |
53 | import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
||
54 | 41743 | jjdelcerro | import org.gvsig.fmap.mapcontrol.MapControlCreationListener; |
55 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
56 | import org.gvsig.fmap.mapcontrol.MapControlManager; |
||
57 | import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent; |
||
58 | import org.gvsig.fmap.mapcontrol.swing.dynobject.impl.DefaultLayersDynObjectSetComponent; |
||
59 | import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper; |
||
60 | import org.gvsig.tools.ToolsLocator; |
||
61 | import org.gvsig.tools.dynobject.DynObjectSet; |
||
62 | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
||
63 | import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
||
64 | import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
||
65 | |||
66 | /**
|
||
67 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
||
68 | */
|
||
69 | public class DefaultMapControlManager implements MapControlManager{ |
||
70 | private static final Logger logger = LoggerFactory.getLogger(MapControlManager.class); |
||
71 | private static final String MAPCONTROL_MANAGER_EXTENSION_POINT = "MapControlManagerExtensionPoint"; |
||
72 | private static final String DEFAULT_MAPCONTROLMANAGER_NAME = null; |
||
73 | private static final String SNAPPING_EXTENSION_POINT = "Snapper"; |
||
74 | |||
75 | private ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
|
||
76 | private int snappingTolerance = 4; |
||
77 | 42146 | fdiaz | private ISymbol selectionSymbol = null; |
78 | 40435 | jjdelcerro | private ISymbol axisReferencesSymbol = null; |
79 | private ISymbol geometrySelectionSymbol = null; |
||
80 | private ISymbol handlerSymbol = null; |
||
81 | private static MapContextManager mapContextManager = MapContextLocator |
||
82 | .getMapContextManager(); |
||
83 | private Preferences prefs = Preferences.userRoot().node( "cadtooladapter" ); |
||
84 | private static Preferences prefSnappers = Preferences.userRoot().node("snappers"); |
||
85 | 41743 | jjdelcerro | private List<ISnapper> snappers = null; |
86 | 42146 | fdiaz | private Set<MapControlCreationListener> mapControlCreationListeners = new LinkedHashSet<MapControlCreationListener>(); |
87 | 40435 | jjdelcerro | |
88 | 42146 | fdiaz | |
89 | 40435 | jjdelcerro | public DefaultMapControlManager() {
|
90 | super();
|
||
91 | snappers = new ArrayList<ISnapper>(); |
||
92 | } |
||
93 | |||
94 | /* (non-Javadoc)
|
||
95 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#createDefaultMapControlDrawer()
|
||
96 | */
|
||
97 | public MapControlDrawer createDefaultMapControlDrawer() throws MapControlCreationException { |
||
98 | ExtensionPoint ep = extensionPoints.add(MAPCONTROL_MANAGER_EXTENSION_POINT); |
||
99 | try {
|
||
100 | 42146 | fdiaz | return (MapControlDrawer)ep.create(DEFAULT_MAPCONTROLMANAGER_NAME);
|
101 | 40435 | jjdelcerro | } catch (Exception e) { |
102 | throw new MapControlCreationException(e); |
||
103 | 42146 | fdiaz | } |
104 | 40435 | jjdelcerro | } |
105 | |||
106 | /*
|
||
107 | * (non-Javadoc)
|
||
108 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#createMapControlDrawer(java.lang.String)
|
||
109 | */
|
||
110 | public MapControlDrawer createMapControlDrawer(String name) throws MapControlCreationException { |
||
111 | ExtensionPoint ep = extensionPoints.add(MAPCONTROL_MANAGER_EXTENSION_POINT); |
||
112 | try {
|
||
113 | 42146 | fdiaz | return (MapControlDrawer)ep.create(name);
|
114 | 40435 | jjdelcerro | } catch (Exception e) { |
115 | throw new MapControlCreationException(e); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /* (non-Javadoc)
|
||
120 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#registerDefaultMapControlDrawer(java.lang.Class)
|
||
121 | */
|
||
122 | public void registerDefaultMapControlDrawer(Class mapControlDrawerClass) { |
||
123 | if (!MapControlDrawer.class.isAssignableFrom(mapControlDrawerClass)) {
|
||
124 | throw new IllegalArgumentException(mapControlDrawerClass.getName() |
||
125 | + " must implement the MapControlDrawer interface");
|
||
126 | } |
||
127 | |||
128 | ExtensionPoint extensionPoint = extensionPoints.add(MAPCONTROL_MANAGER_EXTENSION_POINT, "");
|
||
129 | 42146 | fdiaz | extensionPoint.append(DEFAULT_MAPCONTROLMANAGER_NAME, "Default MapControl", mapControlDrawerClass);
|
130 | 40435 | jjdelcerro | } |
131 | |||
132 | /* (non-Javadoc)
|
||
133 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#registerMapControlDrawer(int, java.lang.Class)
|
||
134 | */
|
||
135 | public void registerMapControlDrawer(String name, |
||
136 | Class mapControlDrawerClass) {
|
||
137 | |||
138 | if (!MapControlDrawer.class.isAssignableFrom(mapControlDrawerClass)) {
|
||
139 | throw new IllegalArgumentException(mapControlDrawerClass.getName() |
||
140 | + " must implement the MapControlDrawer interface");
|
||
141 | } |
||
142 | |||
143 | ExtensionPoint extensionPoint = extensionPoints.add(MAPCONTROL_MANAGER_EXTENSION_POINT, "");
|
||
144 | 42146 | fdiaz | extensionPoint.append(name, "Default MapControl", mapControlDrawerClass);
|
145 | 40435 | jjdelcerro | } |
146 | |||
147 | /* (non-Javadoc)
|
||
148 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getSnappingTolerance()
|
||
149 | */
|
||
150 | public int getTolerance() { |
||
151 | return snappingTolerance;
|
||
152 | } |
||
153 | |||
154 | /* (non-Javadoc)
|
||
155 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#setSnappingTolerance(int)
|
||
156 | */
|
||
157 | public void setTolerance(int tolerance) { |
||
158 | 42146 | fdiaz | snappingTolerance = tolerance; |
159 | 40435 | jjdelcerro | } |
160 | |||
161 | /* (non-Javadoc)
|
||
162 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#clearSnappers()
|
||
163 | */
|
||
164 | public void clearSnappers() { |
||
165 | 42146 | fdiaz | snappers.clear(); |
166 | 40435 | jjdelcerro | } |
167 | |||
168 | /* (non-Javadoc)
|
||
169 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getAxisReferenceSymbol()
|
||
170 | */
|
||
171 | public ISymbol getAxisReferenceSymbol() {
|
||
172 | if (axisReferencesSymbol == null){ |
||
173 | axisReferencesSymbol = |
||
174 | mapContextManager.getSymbolManager() |
||
175 | .createSymbol(Geometry.TYPES.GEOMETRY, |
||
176 | new Color(100, 100, 100, 100)); |
||
177 | } |
||
178 | return axisReferencesSymbol;
|
||
179 | } |
||
180 | |||
181 | /* (non-Javadoc)
|
||
182 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getGeometrySelectionSymbol()
|
||
183 | */
|
||
184 | public ISymbol getGeometrySelectionSymbol() {
|
||
185 | if (geometrySelectionSymbol == null){ |
||
186 | geometrySelectionSymbol = |
||
187 | mapContextManager.getSymbolManager() |
||
188 | .createSymbol(Geometry.TYPES.GEOMETRY, Color.RED);
|
||
189 | 42146 | fdiaz | } |
190 | 40435 | jjdelcerro | return geometrySelectionSymbol;
|
191 | } |
||
192 | |||
193 | /* (non-Javadoc)
|
||
194 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getHandlerSymbol()
|
||
195 | */
|
||
196 | public ISymbol getHandlerSymbol() {
|
||
197 | if (handlerSymbol == null){ |
||
198 | handlerSymbol = |
||
199 | mapContextManager.getSymbolManager().createSymbol( |
||
200 | Geometry.TYPES.GEOMETRY, Color.ORANGE);
|
||
201 | } |
||
202 | return handlerSymbol;
|
||
203 | } |
||
204 | |||
205 | /* (non-Javadoc)
|
||
206 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getSelectionSymbol()
|
||
207 | */
|
||
208 | public ISymbol getSelectionSymbol() {
|
||
209 | if (selectionSymbol == null){ |
||
210 | selectionSymbol = |
||
211 | mapContextManager.getSymbolManager().createSymbol( |
||
212 | 42146 | fdiaz | Geometry.TYPES.GEOMETRY, new Color(255, 0, 0, 100)); |
213 | 40435 | jjdelcerro | } |
214 | return selectionSymbol;
|
||
215 | } |
||
216 | |||
217 | /*
|
||
218 | * (non-Javadoc)
|
||
219 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#setAxisReferenceSymbol(org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol)
|
||
220 | */
|
||
221 | public void setAxisReferenceSymbol(ISymbol axisReferencesSymbol) { |
||
222 | 42146 | fdiaz | this.axisReferencesSymbol = axisReferencesSymbol;
|
223 | 40435 | jjdelcerro | } |
224 | |||
225 | /*
|
||
226 | * (non-Javadoc)
|
||
227 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#setGeometrySelectionSymbol(org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol)
|
||
228 | */
|
||
229 | public void setGeometrySelectionSymbol(ISymbol geometrySelectionSymbol) { |
||
230 | 42146 | fdiaz | this.geometrySelectionSymbol = geometrySelectionSymbol;
|
231 | 40435 | jjdelcerro | } |
232 | |||
233 | /*
|
||
234 | * (non-Javadoc)
|
||
235 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#setHandlerSymbol(org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol)
|
||
236 | */
|
||
237 | public void setHandlerSymbol(ISymbol handlerSymbol) { |
||
238 | 42146 | fdiaz | this.handlerSymbol = handlerSymbol;
|
239 | 40435 | jjdelcerro | } |
240 | |||
241 | /*
|
||
242 | * (non-Javadoc)
|
||
243 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#setSelectionSymbol(org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol)
|
||
244 | */
|
||
245 | public void setSelectionSymbol(ISymbol selectionSymbol) { |
||
246 | 42146 | fdiaz | this.selectionSymbol = selectionSymbol;
|
247 | 40435 | jjdelcerro | } |
248 | |||
249 | /* (non-Javadoc)
|
||
250 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#addSnapper(org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper)
|
||
251 | */
|
||
252 | public void registerSnapper(String name, Class snapperClass) { |
||
253 | if (!ISnapper.class.isAssignableFrom(snapperClass)) {
|
||
254 | throw new IllegalArgumentException(snapperClass.getName() |
||
255 | + " must implement the ISnapper interface");
|
||
256 | 42146 | fdiaz | } |
257 | 40435 | jjdelcerro | |
258 | ExtensionPoint extensionPoint = extensionPoints.add(SNAPPING_EXTENSION_POINT, "");
|
||
259 | Extension extension = extensionPoint.append(name, "", snapperClass);
|
||
260 | |||
261 | ISnapper snapper; |
||
262 | try {
|
||
263 | snapper = (ISnapper)extension.create(); |
||
264 | snappers.add(snapper); |
||
265 | String nameClass=snapper.getClass().getName();
|
||
266 | nameClass=nameClass.substring(nameClass.lastIndexOf('.'));
|
||
267 | boolean select = prefSnappers.getBoolean("snapper_activated" + nameClass, false); |
||
268 | int priority = prefs.getInt("snapper_priority" + nameClass,3); |
||
269 | snapper.setPriority(priority); |
||
270 | if (select){
|
||
271 | 42146 | fdiaz | snapper.setEnabled(select); |
272 | } |
||
273 | |||
274 | 40435 | jjdelcerro | } catch (Exception e) { |
275 | logger.error("It is not possible to create the snapper");
|
||
276 | 42146 | fdiaz | } |
277 | 40435 | jjdelcerro | } |
278 | |||
279 | /* (non-Javadoc)
|
||
280 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getSnapperAt(int)
|
||
281 | */
|
||
282 | public ISnapper getSnapperAt(int index) { |
||
283 | return snappers.get(index);
|
||
284 | } |
||
285 | |||
286 | /* (non-Javadoc)
|
||
287 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getSnapperCount()
|
||
288 | */
|
||
289 | public int getSnapperCount() { |
||
290 | return snappers.size();
|
||
291 | } |
||
292 | |||
293 | /* (non-Javadoc)
|
||
294 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#disableSnapping()
|
||
295 | */
|
||
296 | public void disableSnapping() { |
||
297 | 42146 | fdiaz | snappers.clear(); |
298 | 40435 | jjdelcerro | } |
299 | |||
300 | /* (non-Javadoc)
|
||
301 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#enableSnapping()
|
||
302 | */
|
||
303 | public void enableSnapping() { |
||
304 | ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
||
305 | ExtensionPoint ep = extensionPoints.get(SNAPPING_EXTENSION_POINT); |
||
306 | Iterator iterator = ep.iterator();
|
||
307 | |||
308 | while (iterator.hasNext()) {
|
||
309 | try {
|
||
310 | Extension obj= (Extension)iterator.next(); |
||
311 | ISnapper snapper = (ISnapper) ep.create(obj.getName()); |
||
312 | snappers.add(snapper); |
||
313 | } catch (Exception e) { |
||
314 | logger.error("Creating a snapper", e);
|
||
315 | } |
||
316 | } |
||
317 | |||
318 | for (int n = 0; n < getSnapperCount(); n++) { |
||
319 | ISnapper snp = getSnapperAt(n); |
||
320 | String nameClass = snp.getClass().getName();
|
||
321 | nameClass = nameClass.substring(nameClass.lastIndexOf('.'));
|
||
322 | boolean select = prefs.getBoolean("snapper_activated" + nameClass, false); |
||
323 | if (select) {
|
||
324 | 42146 | fdiaz | snp.setEnabled(select); |
325 | 40435 | jjdelcerro | } |
326 | int priority = prefs.getInt("snapper_priority" + nameClass, 3); |
||
327 | snp.setPriority(priority); |
||
328 | } |
||
329 | |||
330 | } |
||
331 | |||
332 | /* (non-Javadoc)
|
||
333 | * @see org.gvsig.fmap.mapcontrol.MapControlManager#getEditionPreferences()
|
||
334 | */
|
||
335 | public Preferences getEditionPreferences() { |
||
336 | return prefs;
|
||
337 | } |
||
338 | |||
339 | 41811 | llmarques | public MapControl createJMapControlPanel(MapContext mapContext) throws MapControlCreationException { |
340 | MapControl mapControl = new MapControl(mapContext);
|
||
341 | 40435 | jjdelcerro | mapControl.setMapControlDrawer(this.createDefaultMapControlDrawer());
|
342 | 41743 | jjdelcerro | for( MapControlCreationListener listener : this.mapControlCreationListeners ) { |
343 | if( listener!=null ) { |
||
344 | mapControl = listener.mapControlCreated(mapControl); |
||
345 | } |
||
346 | } |
||
347 | 40435 | jjdelcerro | return mapControl;
|
348 | } |
||
349 | |||
350 | 41743 | jjdelcerro | public void addMapControlCreationListener(MapControlCreationListener listener) { |
351 | this.mapControlCreationListeners.add(listener);
|
||
352 | } |
||
353 | 42146 | fdiaz | |
354 | 40435 | jjdelcerro | public LayersDynObjectSetComponent createLayersDynObjectSetComponent(
|
355 | Map<String, DynObjectSet> layerName2InfoByPoint) { |
||
356 | return createLayersDynObjectSetComponent(layerName2InfoByPoint, false); |
||
357 | } |
||
358 | |||
359 | public LayersDynObjectSetComponent createLayersDynObjectSetComponent(
|
||
360 | Map<String, DynObjectSet> layerName2InfoByPoint, boolean writable) { |
||
361 | return new DefaultLayersDynObjectSetComponent(layerName2InfoByPoint, |
||
362 | writable); |
||
363 | } |
||
364 | 42651 | jjdelcerro | |
365 | public TreeModel createLayersTreeModel(MapContext mapContext) { |
||
366 | return new LayersTreeModel(mapContext.getLayers()); |
||
367 | } |
||
368 | |||
369 | public TreeModel createLayersTreeModel(FLayers layers) { |
||
370 | return new LayersTreeModel(layers); |
||
371 | } |
||
372 | |||
373 | public TreeModel createCompoundLayersTreeModel() { |
||
374 | return new CompoundLayersTreeModel(); |
||
375 | } |
||
376 | |||
377 | |||
378 | 40435 | jjdelcerro | } |