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 / BaseViewDocument.java @ 47430
History | View | Annotate | Download (14.8 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 42285 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40558 | jjdelcerro | *
|
11 | 42285 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40558 | jjdelcerro | *
|
16 | 42285 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40558 | jjdelcerro | *
|
20 | 42285 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40558 | jjdelcerro | */
|
23 | 40435 | jjdelcerro | package org.gvsig.app.project.documents.view; |
24 | |||
25 | import java.awt.Color; |
||
26 | import java.awt.Component; |
||
27 | import java.awt.Dimension; |
||
28 | 45486 | fdiaz | import java.awt.geom.Point2D; |
29 | 42293 | jjdelcerro | import java.util.Iterator; |
30 | 40435 | jjdelcerro | import java.util.List; |
31 | 41350 | jjdelcerro | import java.util.Map; |
32 | 40435 | jjdelcerro | |
33 | import javax.swing.JOptionPane; |
||
34 | import javax.swing.JPanel; |
||
35 | import javax.swing.JScrollPane; |
||
36 | 45532 | jjdelcerro | import org.apache.commons.lang.StringUtils; |
37 | 40435 | jjdelcerro | |
38 | import org.cresques.cts.IProjection; |
||
39 | |||
40 | import org.gvsig.andami.PluginServices; |
||
41 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
42 | import org.gvsig.andami.ui.mdiManager.WindowInfo; |
||
43 | 43439 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
44 | 40435 | jjdelcerro | import org.gvsig.app.project.documents.AbstractDocument; |
45 | import org.gvsig.app.project.documents.DocumentManager; |
||
46 | import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel; |
||
47 | 42682 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
48 | 46338 | fdiaz | import org.gvsig.fmap.dal.DataStoreParameters; |
49 | 45532 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
50 | 45486 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
51 | import org.gvsig.fmap.geom.GeometryLocator; |
||
52 | 42775 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
53 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
54 | 45486 | fdiaz | import org.gvsig.fmap.mapcontext.ViewPort; |
55 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.events.ErrorEvent; |
56 | import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener; |
||
57 | import org.gvsig.fmap.mapcontext.layers.CancelationException; |
||
58 | 41350 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper; |
59 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
60 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
61 | 42682 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
62 | 45532 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
63 | 40435 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
64 | import org.gvsig.tools.persistence.PersistentState; |
||
65 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
66 | |||
67 | /**
|
||
68 | *
|
||
69 | 42285 | jjdelcerro | * @author 2005- Vicente Caballero
|
70 | * @author 2009- Joaquin del Cerro
|
||
71 | *
|
||
72 | 40435 | jjdelcerro | */
|
73 | public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener, |
||
74 | 42285 | jjdelcerro | ViewDocument { |
75 | 40435 | jjdelcerro | |
76 | 42285 | jjdelcerro | /**
|
77 | *
|
||
78 | */
|
||
79 | private static final long serialVersionUID = -2621709089720665902L; |
||
80 | 40435 | jjdelcerro | |
81 | 42285 | jjdelcerro | public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument"; |
82 | 40435 | jjdelcerro | |
83 | 42285 | jjdelcerro | protected MapContext mapOverViewContext;
|
84 | protected MapContext mapContext;
|
||
85 | 40435 | jjdelcerro | |
86 | 42285 | jjdelcerro | private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper(); |
87 | 40435 | jjdelcerro | |
88 | 42285 | jjdelcerro | public BaseViewDocument() {
|
89 | super();
|
||
90 | } |
||
91 | 40435 | jjdelcerro | |
92 | 42285 | jjdelcerro | public BaseViewDocument(DocumentManager factory) {
|
93 | super(factory);
|
||
94 | } |
||
95 | 40435 | jjdelcerro | |
96 | 42285 | jjdelcerro | /**
|
97 | * Gets the MapContext of the main map in the view.
|
||
98 | *
|
||
99 | * @return the main MapContext
|
||
100 | */
|
||
101 | public MapContext getMapContext() {
|
||
102 | return mapContext;
|
||
103 | } |
||
104 | 40435 | jjdelcerro | |
105 | 42651 | jjdelcerro | @Override
|
106 | public void setName(String name) { |
||
107 | super.setName(name);
|
||
108 | this.mapContext.getLayers().setName(name);
|
||
109 | } |
||
110 | |||
111 | |||
112 | 42285 | jjdelcerro | /**
|
113 | * Gets the MapContext from the locator, which is the small map in the
|
||
114 | * left-bottom corner of the View.
|
||
115 | *
|
||
116 | * @return the locator MapContext
|
||
117 | */
|
||
118 | public MapContext getMapOverViewContext() {
|
||
119 | return mapOverViewContext;
|
||
120 | } |
||
121 | 40435 | jjdelcerro | |
122 | 42285 | jjdelcerro | public void setMapContext(MapContext fmap) { |
123 | mapContext = fmap; |
||
124 | fmap.addErrorListener(this);
|
||
125 | } |
||
126 | 40435 | jjdelcerro | |
127 | 42285 | jjdelcerro | public void setMapOverViewContext(MapContext fmap) { |
128 | mapOverViewContext = fmap; |
||
129 | mapOverViewContext.setProjection(mapContext.getProjection()); |
||
130 | } |
||
131 | 40435 | jjdelcerro | |
132 | 42285 | jjdelcerro | public void showErrors() { |
133 | if (mapContext.getLayersError().size() > 0) { |
||
134 | String layersError = ""; |
||
135 | for (int i = 0; i < mapContext.getLayersError().size(); i++) { |
||
136 | layersError = layersError + "\n" + (String) mapContext.getLayersError().get(i); |
||
137 | } |
||
138 | JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), |
||
139 | PluginServices.getText(this, "fallo_capas") + " : \n" |
||
140 | + layersError); |
||
141 | } |
||
142 | } |
||
143 | 40435 | jjdelcerro | |
144 | 42285 | jjdelcerro | /**
|
145 | * Reports to the user a bundle of driver exceptions produced in the same
|
||
146 | * atomic MapContext transaction
|
||
147 | */
|
||
148 | @SuppressWarnings("rawtypes") |
||
149 | public void reportDriverExceptions(String introductoryText, List driverExceptions) { |
||
150 | HtmlWindow htmlPanel = new HtmlWindow(570, 600, PluginServices.getText(this, "driver_error")); |
||
151 | String htmlText = ""; |
||
152 | if (introductoryText == null) { |
||
153 | htmlText += "<h2 text=\"#000080\">" + PluginServices.getText(this, "se_han_producido_los_siguientes_errores_durante_la_carga_de_las_capas") + "</h2>"; |
||
154 | } else {
|
||
155 | htmlText += introductoryText; |
||
156 | } |
||
157 | int numErrors = driverExceptions.size();
|
||
158 | for (int i = 0; i < numErrors; i++) { |
||
159 | //htmlText += "<br>\n";
|
||
160 | BaseException exception = (BaseException) driverExceptions.get(i); |
||
161 | htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
|
||
162 | htmlText += "<h3>" + PluginServices.getText(this, exception.getMessageKey()) + "</h3>"; |
||
163 | htmlText += "<p>" + exception.getMessage() + "</p>"; |
||
164 | htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
|
||
165 | } |
||
166 | 40435 | jjdelcerro | |
167 | 42285 | jjdelcerro | System.out.println(htmlText);
|
168 | htmlPanel.show(htmlText); |
||
169 | 40435 | jjdelcerro | |
170 | 42285 | jjdelcerro | PluginServices.getMDIManager().addCentredWindow(htmlPanel); |
171 | 40435 | jjdelcerro | |
172 | 42285 | jjdelcerro | } |
173 | 40435 | jjdelcerro | |
174 | 42285 | jjdelcerro | /**
|
175 | * HtmlInfoToolPanel that implements IWindow
|
||
176 | *
|
||
177 | * @author azabala
|
||
178 | *
|
||
179 | */
|
||
180 | class HtmlWindow extends JPanel implements IWindow { |
||
181 | 40435 | jjdelcerro | |
182 | 42285 | jjdelcerro | /**
|
183 | *
|
||
184 | */
|
||
185 | private static final long serialVersionUID = 1151465547277478664L; |
||
186 | 40435 | jjdelcerro | |
187 | 42285 | jjdelcerro | private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel(); |
188 | WindowInfo viewInfo = null;
|
||
189 | |||
190 | public HtmlWindow(int width, int height, String windowTitle) { |
||
191 | htmlPanel.setBackground(Color.white);
|
||
192 | JScrollPane scrollPane = new JScrollPane(htmlPanel); |
||
193 | scrollPane.setPreferredSize(new Dimension(width - 30, height - 30)); |
||
194 | this.add(scrollPane);
|
||
195 | viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
|
||
196 | viewInfo.setTitle(windowTitle); |
||
197 | viewInfo.setWidth(width); |
||
198 | viewInfo.setHeight(height); |
||
199 | } |
||
200 | |||
201 | public void show(String htmlText) { |
||
202 | htmlPanel.show(htmlText); |
||
203 | } |
||
204 | |||
205 | public WindowInfo getWindowInfo() {
|
||
206 | return viewInfo;
|
||
207 | } |
||
208 | |||
209 | public Object getWindowProfile() { |
||
210 | return WindowInfo.PROPERTIES_PROFILE;
|
||
211 | } |
||
212 | |||
213 | } |
||
214 | |||
215 | public IProjection getProjection() {
|
||
216 | 47430 | jjdelcerro | if( this.mapContext == null ) { |
217 | return null; |
||
218 | } |
||
219 | 42285 | jjdelcerro | return mapContext.getProjection();
|
220 | } |
||
221 | |||
222 | public void setProjection(IProjection proj) { |
||
223 | mapContext.setProjection(proj); |
||
224 | mapOverViewContext.setProjection(proj); |
||
225 | } |
||
226 | |||
227 | public IProjection getOverViewProjection() {
|
||
228 | return mapOverViewContext.getProjection();
|
||
229 | } |
||
230 | |||
231 | public void afterRemove() { |
||
232 | // FIXME: Parece que no recorre correctamente el arbol de capas
|
||
233 | |||
234 | FLayers layers = this.getMapContext().getLayers();
|
||
235 | |||
236 | for (int i = layers.getLayersCount() - 1; i >= 0; i--) { |
||
237 | try {
|
||
238 | 40435 | jjdelcerro | layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i)); |
239 | 42285 | jjdelcerro | } catch (CancelationException e1) {
|
240 | e1.printStackTrace(); |
||
241 | } |
||
242 | } |
||
243 | getMapContext().dispose(); |
||
244 | getMapOverViewContext().dispose(); |
||
245 | } |
||
246 | 40435 | jjdelcerro | |
247 | 42285 | jjdelcerro | public void afterAdd() { |
248 | // Do nothing
|
||
249 | } |
||
250 | 40435 | jjdelcerro | |
251 | 42285 | jjdelcerro | public void setBackColor(Color c) { |
252 | getMapContext().getViewPort().setBackColor(c); |
||
253 | } |
||
254 | 40435 | jjdelcerro | |
255 | 42285 | jjdelcerro | public void errorThrown(ErrorEvent e) { |
256 | JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), |
||
257 | PluginServices.getText(this, "fallo_capas") + " : \n" |
||
258 | + e.getMessage()); |
||
259 | 40435 | jjdelcerro | |
260 | 42285 | jjdelcerro | } |
261 | 40435 | jjdelcerro | |
262 | 42285 | jjdelcerro | public boolean isLocked() { |
263 | if (super.isLocked()) { |
||
264 | return true; |
||
265 | } |
||
266 | FLayers layers = getMapContext().getLayers(); |
||
267 | for (int i = 0; i < layers.getLayersCount(); i++) { |
||
268 | FLayer layer = layers.getLayer(i); |
||
269 | if (layer.isEditing()) {
|
||
270 | return true; |
||
271 | } |
||
272 | } |
||
273 | return false; |
||
274 | } |
||
275 | 40435 | jjdelcerro | |
276 | 42285 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
277 | super.saveToState(state);
|
||
278 | state.set("mainMapContext", this.getMapContext()); |
||
279 | if (this.getMapOverViewContext() != null) { |
||
280 | state.set("useMapOverview", true); |
||
281 | } else {
|
||
282 | state.set("useMapOverview", false); |
||
283 | 41350 | jjdelcerro | } |
284 | 42285 | jjdelcerro | state.set("overviewMapContext", this.getMapOverViewContext()); |
285 | state.set("propertiesHelper", propertiesHelper);
|
||
286 | } |
||
287 | 41350 | jjdelcerro | |
288 | 42285 | jjdelcerro | public void loadFromState(PersistentState state) throws PersistenceException { |
289 | super.loadFromState(state);
|
||
290 | this.mapContext = (MapContext) state.get("mainMapContext"); |
||
291 | if (state.getBoolean("useMapOverview")) { |
||
292 | this.mapOverViewContext = (MapContext) state.get("overviewMapContext"); |
||
293 | } else {
|
||
294 | this.mapOverViewContext = null; |
||
295 | 41350 | jjdelcerro | } |
296 | 42285 | jjdelcerro | this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper"); |
297 | 43439 | jjdelcerro | |
298 | |||
299 | IProjection proj = ApplicationLocator.getProjectManager().getProjectPreferences().getDefaultProjection(); |
||
300 | if( this.mapContext!=null ) { |
||
301 | if( this.mapContext.getProjection()==null ) { |
||
302 | try {
|
||
303 | this.mapContext.setProjection(proj);
|
||
304 | } catch(Throwable ex) { |
||
305 | |||
306 | } |
||
307 | } |
||
308 | } |
||
309 | for( FLayer layer : mapContext ) {
|
||
310 | if( layer.getProjection()==null ) { |
||
311 | try {
|
||
312 | layer.setProjection(proj); |
||
313 | } catch(Throwable ex) { |
||
314 | |||
315 | } |
||
316 | } |
||
317 | } |
||
318 | for( FLayer layer : mapOverViewContext ) {
|
||
319 | if( layer.getProjection()==null ) { |
||
320 | try {
|
||
321 | layer.setProjection(proj); |
||
322 | } catch(Throwable ex) { |
||
323 | |||
324 | } |
||
325 | } |
||
326 | } |
||
327 | 42285 | jjdelcerro | } |
328 | 41350 | jjdelcerro | |
329 | 42285 | jjdelcerro | public Object getProperty(Object key) { |
330 | return this.propertiesHelper.getProperty(key); |
||
331 | } |
||
332 | |||
333 | public void setProperty(Object key, Object obj) { |
||
334 | this.propertiesHelper.setProperty(key, obj);
|
||
335 | } |
||
336 | |||
337 | public Map getExtendedProperties() { |
||
338 | return this.propertiesHelper.getExtendedProperties(); |
||
339 | } |
||
340 | |||
341 | 43987 | jjdelcerro | @Override
|
342 | public FLayer getLayer(String name) { |
||
343 | FLayer layer = this.getMapContext().getLayers().getLayer(name);
|
||
344 | return layer;
|
||
345 | } |
||
346 | |||
347 | 44281 | jjdelcerro | @Override
|
348 | 42293 | jjdelcerro | public Iterator<FLayer> iterator() { |
349 | return this.mapContext.iterator(); |
||
350 | } |
||
351 | |||
352 | 44281 | jjdelcerro | @Override
|
353 | 42293 | jjdelcerro | public Iterator<FLayer> deepiterator() { |
354 | return this.mapContext.deepiterator(); |
||
355 | } |
||
356 | |||
357 | 44281 | jjdelcerro | @Override
|
358 | public Iterable<FLayer> layers() { |
||
359 | return new Iterable<FLayer>() { |
||
360 | @Override
|
||
361 | public Iterator<FLayer> iterator() { |
||
362 | return deepiterator();
|
||
363 | } |
||
364 | }; |
||
365 | } |
||
366 | |||
367 | @Override
|
||
368 | 42682 | jjdelcerro | public boolean contains(FLayer layer) { |
369 | if( !(layer instanceof SingleLayer) ) { |
||
370 | return false; |
||
371 | } |
||
372 | return this.isInLayers(this.getMapContext().getLayers(), ((SingleLayer)layer).getDataStore()); |
||
373 | } |
||
374 | |||
375 | 44281 | jjdelcerro | @Override
|
376 | 42682 | jjdelcerro | public boolean contains(DataStore store) { |
377 | return this.isInLayers(this.getMapContext().getLayers(), store); |
||
378 | } |
||
379 | |||
380 | private boolean isInLayers(FLayers layers, DataStore store) { |
||
381 | for (int i = 0; i < layers.getLayersCount(); i++) { |
||
382 | if (layers.getLayer(i) instanceof FLayers) { |
||
383 | if (isInLayers((FLayers) layers.getLayer(i), store)) {
|
||
384 | return true; |
||
385 | } |
||
386 | } |
||
387 | FLayer layer = layers.getLayer(i); |
||
388 | if( layer instanceof SingleLayer ) { |
||
389 | if (((SingleLayer)layer).getDataStore() == store ) {
|
||
390 | return true; |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | return false; |
||
395 | } |
||
396 | 42775 | jjdelcerro | |
397 | 45486 | fdiaz | @Override
|
398 | 42775 | jjdelcerro | public void center(Envelope envelope) { |
399 | 45486 | fdiaz | ViewPort viewPort = this.getMapContext().getViewPort();
|
400 | |||
401 | try {
|
||
402 | Envelope oldExtent = viewPort.getAdjustedEnvelope(); |
||
403 | double oldCenterX = oldExtent.getCenter(0); |
||
404 | double oldCenterY = oldExtent.getCenter(1); |
||
405 | double centerX = envelope.getCenter(0); |
||
406 | double centerY = envelope.getCenter(1); |
||
407 | Point2D.Double center = new Point2D.Double(centerX, centerY); |
||
408 | double movX = centerX - oldCenterX;
|
||
409 | double movY = centerY - oldCenterY;
|
||
410 | |||
411 | double minx = oldExtent.getMinimum(0) + movX; |
||
412 | double miny = oldExtent.getMinimum(1) + movY; |
||
413 | double maxX = oldExtent.getMaximum(0) + movX; |
||
414 | double maxY = oldExtent.getMaximum(1) + movY; |
||
415 | Envelope extent = GeometryLocator.getGeometryManager().createEnvelope( |
||
416 | minx, miny, |
||
417 | maxX, maxY, |
||
418 | Geometry.SUBTYPES.GEOM2D); |
||
419 | viewPort.setEnvelope(extent); |
||
420 | } catch (Exception e) { |
||
421 | throw new RuntimeException(e); |
||
422 | } |
||
423 | |||
424 | 42775 | jjdelcerro | } |
425 | 45532 | jjdelcerro | |
426 | @Override
|
||
427 | public boolean contains(Object value) { |
||
428 | if( value == null ) { |
||
429 | return false; |
||
430 | } |
||
431 | 46338 | fdiaz | DataStoreParameters params; |
432 | |||
433 | if( (value instanceof FeatureStore) ) { |
||
434 | params = ((FeatureStore) value).getParameters(); |
||
435 | } else if(value instanceof DataStoreParameters){ |
||
436 | params = (DataStoreParameters) value; |
||
437 | } else {
|
||
438 | 45532 | jjdelcerro | return false; |
439 | } |
||
440 | 46338 | fdiaz | |
441 | 45532 | jjdelcerro | for (Iterator<FLayer> it = this.deepiterator(); it.hasNext();) { |
442 | FLayer layer = it.next(); |
||
443 | if(layer instanceof FLyrVect){ |
||
444 | 46470 | jjdelcerro | FeatureStore store = ((FLyrVect) layer).getFeatureStore(); |
445 | if( store!=null && store.getParameters().isTheSameStore(params)){ |
||
446 | 45532 | jjdelcerro | return true; |
447 | } |
||
448 | } |
||
449 | } |
||
450 | return false; |
||
451 | } |
||
452 | |||
453 | @Override
|
||
454 | public void refresh() { |
||
455 | this.getMapContext().invalidate();
|
||
456 | } |
||
457 | |||
458 | 40435 | jjdelcerro | } |