gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.app / org.gvsig.topology.app.mainplugin / src / main / java / org / gvsig / topology / app / mainplugin / AppTopologyServices.java @ 727
History | View | Annotate | Download (14 KB)
1 | 688 | 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 | package org.gvsig.topology.app.mainplugin; |
||
25 | |||
26 | 727 | jjdelcerro | import java.io.File; |
27 | 688 | jjdelcerro | import java.util.ArrayList; |
28 | import java.util.HashMap; |
||
29 | import java.util.List; |
||
30 | import java.util.Map; |
||
31 | 727 | jjdelcerro | import javax.swing.ImageIcon; |
32 | 688 | jjdelcerro | import javax.swing.tree.TreeModel; |
33 | import org.apache.commons.lang3.mutable.Mutable; |
||
34 | import org.apache.commons.lang3.mutable.MutableObject; |
||
35 | 727 | jjdelcerro | import org.gvsig.andami.IconThemeHelper; |
36 | import org.gvsig.andami.PluginsLocator; |
||
37 | 688 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
38 | import org.gvsig.app.ApplicationManager; |
||
39 | import org.gvsig.app.project.Project; |
||
40 | import org.gvsig.app.project.documents.Document; |
||
41 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
42 | import org.gvsig.app.project.documents.view.ViewManager; |
||
43 | 727 | jjdelcerro | import org.gvsig.fmap.dal.EditingNotification; |
44 | import org.gvsig.fmap.dal.EditingNotificationManager; |
||
45 | 688 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
46 | 727 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
47 | import org.gvsig.fmap.geom.Geometry; |
||
48 | import org.gvsig.fmap.geom.aggregate.MultiCurve; |
||
49 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
50 | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
||
51 | import org.gvsig.fmap.geom.primitive.Curve; |
||
52 | 688 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
53 | import org.gvsig.fmap.geom.primitive.Point; |
||
54 | 727 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Surface; |
55 | import org.gvsig.fmap.mapcontext.MapContext; |
||
56 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
57 | 688 | jjdelcerro | import org.gvsig.fmap.mapcontext.ViewPort; |
58 | import org.gvsig.fmap.mapcontext.events.ColorEvent; |
||
59 | import org.gvsig.fmap.mapcontext.events.ExtentEvent; |
||
60 | import org.gvsig.fmap.mapcontext.events.ProjectionEvent; |
||
61 | import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener; |
||
62 | 727 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
63 | 688 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayers; |
64 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
65 | 727 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
66 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
67 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2; |
||
68 | import org.gvsig.fmap.mapcontrol.MapControlLocator; |
||
69 | import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor; |
||
70 | import org.gvsig.tools.dynobject.DynObject; |
||
71 | 688 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
72 | 727 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
73 | import org.gvsig.tools.observer.Observer; |
||
74 | import org.gvsig.tools.swing.api.ListElement; |
||
75 | 688 | jjdelcerro | import org.gvsig.tools.visitor.VisitCanceledException; |
76 | import org.gvsig.tools.visitor.Visitor; |
||
77 | import org.gvsig.topology.lib.api.TopologyDataSet; |
||
78 | import org.gvsig.topology.swing.api.TopologySwingServices; |
||
79 | import org.slf4j.Logger; |
||
80 | import org.slf4j.LoggerFactory; |
||
81 | |||
82 | /**
|
||
83 | *
|
||
84 | * @author jjdelcerro
|
||
85 | */
|
||
86 | @SuppressWarnings("UseSpecificCatch") |
||
87 | public class AppTopologyServices implements TopologySwingServices { |
||
88 | |||
89 | private static final Logger LOGGER = LoggerFactory.getLogger(AppTopologyServices.class); |
||
90 | 727 | jjdelcerro | |
91 | 688 | jjdelcerro | private class WorkingAreaViewPortListener implements ViewPortListener { |
92 | 727 | jjdelcerro | |
93 | 688 | jjdelcerro | private WorkingAreaChangedListener workingAreaChangedListener;
|
94 | private ViewPort viewPort;
|
||
95 | 727 | jjdelcerro | |
96 | 688 | jjdelcerro | public WorkingAreaViewPortListener(ViewPort viewPort, WorkingAreaChangedListener workingAreaChangedListener) {
|
97 | this.workingAreaChangedListener = workingAreaChangedListener;
|
||
98 | this.viewPort = viewPort;
|
||
99 | } |
||
100 | |||
101 | public ViewPort getViewPort() {
|
||
102 | return this.viewPort; |
||
103 | } |
||
104 | 727 | jjdelcerro | |
105 | 688 | jjdelcerro | @Override
|
106 | public void extentChanged(ExtentEvent e) { |
||
107 | this.workingAreaChangedListener.workingAreaChanged(e.getNewExtent());
|
||
108 | } |
||
109 | |||
110 | @Override
|
||
111 | public void backColorChanged(ColorEvent e) { |
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | public void projectionChanged(ProjectionEvent e) { |
||
116 | } |
||
117 | } |
||
118 | |||
119 | 727 | jjdelcerro | private class EditingNotificationObserver implements Observer { |
120 | |||
121 | public EditingNotificationObserver() {
|
||
122 | |||
123 | } |
||
124 | |||
125 | @Override
|
||
126 | public void update(Observable o, Object notification) { |
||
127 | EditingNotification n = (EditingNotification) notification; |
||
128 | if (n.isCancelable() && n.getFeature() != null) { |
||
129 | try {
|
||
130 | DynObject data = n.getFeature().getAsDynObject(); |
||
131 | DynObjectEditor editor = new DynObjectEditor(data);
|
||
132 | editor.setTitle("Topology - " + n.getFeature().getType().getName());
|
||
133 | editor.editObject(true);
|
||
134 | if (editor.isCanceled()) {
|
||
135 | n.cancel(); |
||
136 | } else {
|
||
137 | editor.getData(data); |
||
138 | } |
||
139 | } catch (Exception ex) { |
||
140 | LOGGER.warn("Problems showing the feature in a form.", ex);
|
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | 688 | jjdelcerro | private Map<WorkingAreaChangedListener, WorkingAreaViewPortListener> workingAreaListener; |
147 | 727 | jjdelcerro | private ISymbol errorPolygonSymbol = null; |
148 | private ISymbol errorPointSymbol = null; |
||
149 | private ISymbol errorLineSymbol = null; |
||
150 | private EditingNotificationObserver editingNotificationObserver = null; |
||
151 | |||
152 | 688 | jjdelcerro | public AppTopologyServices() {
|
153 | this.workingAreaListener = new HashMap<>(); |
||
154 | } |
||
155 | 727 | jjdelcerro | |
156 | 688 | jjdelcerro | @Override
|
157 | public TreeModel getDataSetTreeModel() { |
||
158 | ApplicationManager application = ApplicationLocator.getManager(); |
||
159 | ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
160 | return new DataSetsTreeModel(this, view); |
||
161 | } |
||
162 | |||
163 | @Override
|
||
164 | public FeatureStore getFeatureStore(final TopologyDataSet dataSet) { |
||
165 | final Mutable<FeatureStore> store = new MutableObject<>(); |
||
166 | store.setValue(null);
|
||
167 | 727 | jjdelcerro | |
168 | 688 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
169 | Project project = application.getCurrentProject(); |
||
170 | 727 | jjdelcerro | |
171 | 688 | jjdelcerro | List<Document> views = new ArrayList<>(); |
172 | views.add(project.getActiveDocument(ViewManager.TYPENAME)); |
||
173 | // views.addAll(project.getDocuments(ViewManager.TYPENAME));
|
||
174 | |||
175 | for (Document view : views) { |
||
176 | 727 | jjdelcerro | if (view == null) { |
177 | 688 | jjdelcerro | continue;
|
178 | } |
||
179 | 727 | jjdelcerro | FLayers layers = ((ViewDocument) view).getMapContext().getLayers(); |
180 | 688 | jjdelcerro | try {
|
181 | layers.accept(new Visitor() {
|
||
182 | @Override
|
||
183 | public void visit(Object o) throws VisitCanceledException, BaseException { |
||
184 | 727 | jjdelcerro | if (o instanceof FLyrVect) { |
185 | 688 | jjdelcerro | FLyrVect layer = (FLyrVect) o; |
186 | 727 | jjdelcerro | if (dataSet.isThisStore(layer.getFeatureStore())) {
|
187 | 688 | jjdelcerro | store.setValue(layer.getFeatureStore()); |
188 | throw new VisitCanceledException(); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | }); |
||
193 | } catch (VisitCanceledException ex) {
|
||
194 | break;
|
||
195 | } catch (Exception ex) { |
||
196 | throw new RuntimeException(ex); |
||
197 | } |
||
198 | } |
||
199 | return store.getValue();
|
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public void zoomTo(Envelope envelope) { |
||
204 | ApplicationManager application = ApplicationLocator.getManager(); |
||
205 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
206 | 727 | jjdelcerro | if (viewdoc == null) { |
207 | 688 | jjdelcerro | return;
|
208 | } |
||
209 | ViewPort viewPort = viewdoc.getMapContext().getViewPort(); |
||
210 | viewPort.setEnvelope(envelope); |
||
211 | } |
||
212 | |||
213 | @Override
|
||
214 | 726 | jjdelcerro | public void refreshView() { |
215 | try {
|
||
216 | ApplicationManager application = ApplicationLocator.getManager(); |
||
217 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
218 | 727 | jjdelcerro | if (viewdoc == null) { |
219 | 726 | jjdelcerro | return;
|
220 | } |
||
221 | viewdoc.getMapContext().invalidate(); |
||
222 | } catch (Exception ex) { |
||
223 | LOGGER.warn("Cant refresh view", ex);
|
||
224 | } |
||
225 | } |
||
226 | 727 | jjdelcerro | |
227 | 726 | jjdelcerro | @Override
|
228 | 688 | jjdelcerro | public void centerTo(Point point) { |
229 | try {
|
||
230 | ApplicationManager application = ApplicationLocator.getManager(); |
||
231 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
232 | 727 | jjdelcerro | if (viewdoc == null) { |
233 | 688 | jjdelcerro | return;
|
234 | } |
||
235 | ViewPort viewPort = viewdoc.getMapContext().getViewPort(); |
||
236 | Envelope envelope = (Envelope) viewPort.getEnvelope().clone(); |
||
237 | envelope.centerTo(point); |
||
238 | viewPort.setEnvelope(envelope); |
||
239 | } catch (Exception ex) { |
||
240 | LOGGER.warn("Cant center view", ex);
|
||
241 | } |
||
242 | } |
||
243 | |||
244 | @Override
|
||
245 | public Envelope getWorkingArea() {
|
||
246 | ApplicationManager application = ApplicationLocator.getManager(); |
||
247 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
248 | 727 | jjdelcerro | if (viewdoc == null) { |
249 | 688 | jjdelcerro | return null; |
250 | } |
||
251 | ViewPort viewPort = viewdoc.getMapContext().getViewPort(); |
||
252 | return viewPort.getEnvelope();
|
||
253 | } |
||
254 | |||
255 | @Override
|
||
256 | public void addWorkingAreaChangedListener(WorkingAreaChangedListener listener) { |
||
257 | ApplicationManager application = ApplicationLocator.getManager(); |
||
258 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
259 | 727 | jjdelcerro | if (viewdoc == null) { |
260 | 688 | jjdelcerro | return;
|
261 | } |
||
262 | ViewPort viewPort = viewdoc.getMapContext().getViewPort(); |
||
263 | WorkingAreaViewPortListener viewPortListener = this.workingAreaListener.get(listener);
|
||
264 | 727 | jjdelcerro | if (viewPortListener == null) { |
265 | 688 | jjdelcerro | viewPortListener = new WorkingAreaViewPortListener(viewPort, listener);
|
266 | this.workingAreaListener.put(listener, viewPortListener);
|
||
267 | } else {
|
||
268 | 727 | jjdelcerro | if (viewPort != viewPortListener.getViewPort()) {
|
269 | 688 | jjdelcerro | viewPortListener.getViewPort().removeViewPortListener(viewPortListener); |
270 | viewPortListener = new WorkingAreaViewPortListener(viewPort, listener);
|
||
271 | this.workingAreaListener.put(listener, viewPortListener);
|
||
272 | } |
||
273 | } |
||
274 | viewPortListener.getViewPort().addViewPortListener(viewPortListener); |
||
275 | } |
||
276 | |||
277 | @Override
|
||
278 | public void removeWorkingAreaChangedListener(WorkingAreaChangedListener listener) { |
||
279 | WorkingAreaViewPortListener viewPortListener = this.workingAreaListener.get(listener);
|
||
280 | 727 | jjdelcerro | if (viewPortListener == null) { |
281 | 688 | jjdelcerro | return;
|
282 | } |
||
283 | 727 | jjdelcerro | viewPortListener.getViewPort().removeViewPortListener(viewPortListener); |
284 | 688 | jjdelcerro | } |
285 | 727 | jjdelcerro | |
286 | @Override
|
||
287 | public void addError(Geometry geom) { |
||
288 | if (this.errorPointSymbol == null) { |
||
289 | try {
|
||
290 | File pluginfolder = PluginsLocator.getManager().getPlugin(this).getPluginDirectory(); |
||
291 | File folder = new File(pluginfolder, "symbols"); |
||
292 | ISymbol[] symbols = MapContextLocator.getSymbolManager().loadSymbols(folder);
|
||
293 | for (ISymbol symbol : symbols) {
|
||
294 | if (symbol instanceof ISymbol_v2) { |
||
295 | String symbolid = ((ISymbol_v2) symbol).getID();
|
||
296 | switch(symbolid) {
|
||
297 | case "topology-error-polygon": |
||
298 | this.errorPolygonSymbol = symbol;
|
||
299 | break;
|
||
300 | case "topology-error-line": |
||
301 | this.errorLineSymbol = symbol;
|
||
302 | break;
|
||
303 | case "topology-error-point": |
||
304 | this.errorPointSymbol = symbol;
|
||
305 | break;
|
||
306 | } |
||
307 | } |
||
308 | } |
||
309 | } catch (Exception ex) { |
||
310 | } |
||
311 | } |
||
312 | ApplicationManager application = ApplicationLocator.getManager(); |
||
313 | ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
314 | MapContext mapContext = viewdoc.getMapContext(); |
||
315 | GraphicLayer gl = mapContext.getGraphicsLayer(); |
||
316 | gl.removeGraphics("topology-errors");
|
||
317 | if ( geom!=null ) { |
||
318 | ISymbol symbol = null;
|
||
319 | if( geom instanceof Point || geom instanceof MultiPoint) { |
||
320 | symbol = this.errorPointSymbol;
|
||
321 | } else if( geom instanceof Curve || geom instanceof MultiCurve ) { |
||
322 | symbol = this.errorLineSymbol;
|
||
323 | } else if( geom instanceof Surface || geom instanceof MultiSurface ) { |
||
324 | symbol = this.errorPolygonSymbol;
|
||
325 | } |
||
326 | if (symbol != null) { |
||
327 | int symbolid = gl.getSymbolId(symbol);
|
||
328 | if (symbolid < 0) { |
||
329 | gl.addSymbol(symbol); |
||
330 | symbolid = gl.getSymbolId(symbol); |
||
331 | } |
||
332 | gl.addGraphic("topology-errors", geom, symbolid);
|
||
333 | } |
||
334 | } |
||
335 | mapContext.invalidate(); |
||
336 | } |
||
337 | |||
338 | @Override
|
||
339 | public void setShowFormWhenEdit(boolean showFormWhenEdit) { |
||
340 | if (this.editingNotificationObserver == null) { |
||
341 | this.editingNotificationObserver = new EditingNotificationObserver(); |
||
342 | } |
||
343 | EditingNotificationManager editingNotificationManager = DALSwingLocator.getEditingNotificationManager(); |
||
344 | if (showFormWhenEdit) {
|
||
345 | editingNotificationManager.addObserver(this.editingNotificationObserver);
|
||
346 | } else {
|
||
347 | editingNotificationManager.deleteObserver(this.editingNotificationObserver);
|
||
348 | } |
||
349 | } |
||
350 | |||
351 | @Override
|
||
352 | public ImageIcon getTreeIcon(TreeModel model, Object item) { |
||
353 | return ((DataSetsTreeModel)model).getTreeIcon(item);
|
||
354 | } |
||
355 | |||
356 | 688 | jjdelcerro | } |