Statistics
| Revision:

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 @ 47790

History | View | Annotate | Download (15.8 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 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
 *
11
 * 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
 *
16
 * 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
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.project.documents.view;
24

    
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.geom.Point2D;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.Objects;
33
import javax.swing.JOptionPane;
34
import javax.swing.JPanel;
35
import javax.swing.JScrollPane;
36
import org.cresques.cts.IProjection;
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.andami.ui.mdiManager.WindowInfo;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.project.Project;
42
import org.gvsig.app.project.documents.AbstractDocument;
43
import org.gvsig.app.project.documents.DocumentManager;
44
import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel;
45
import org.gvsig.fmap.dal.DataStore;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.GeometryLocator;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.fmap.mapcontext.MapContext;
52
import org.gvsig.fmap.mapcontext.ViewPort;
53
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
54
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
55
import org.gvsig.fmap.mapcontext.layers.CancelationException;
56
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
57
import org.gvsig.fmap.mapcontext.layers.FLayer;
58
import org.gvsig.fmap.mapcontext.layers.FLayers;
59
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61
import org.gvsig.tools.dispose.DisposeUtils;
62
import org.gvsig.tools.exception.BaseException;
63
import org.gvsig.tools.persistence.PersistentState;
64
import org.gvsig.tools.persistence.exception.PersistenceException;
65
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
67

    
68
/**
69
 *
70
 * @author 2005- Vicente Caballero
71
 * @author 2009- Joaquin del Cerro
72
 *
73
 */
74
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
75
        ViewDocument {
76

    
77
    /**
78
     *
79
     */
80
    private static final long serialVersionUID = -2621709089720665902L;
81
    
82
    protected static final Logger LOGGER = LoggerFactory.getLogger(BaseViewDocument.class);
83

    
84
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
85

    
86
    protected MapContext mapOverViewContext;
87
    protected MapContext mapContext;
88

    
89
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
90

    
91
    public BaseViewDocument() {
92
        super();
93
    }
94

    
95
    public BaseViewDocument(DocumentManager factory) {
96
        super(factory);
97
    }
98

    
99
    @Override
100
    public void setProject(Project project) {
101
        super.setProject(project);
102
        if(mapContext != null) {
103
            mapContext.setSelectionColor(project.getSelectionColor());
104
        }
105
    }
106
    
107
    
108

    
109
    /**
110
     * Gets the MapContext of the main map in the view.
111
     *
112
     * @return the main MapContext
113
     */
114
    @Override
115
    public MapContext getMapContext() {
116
        if( this.mapContext!=null && DisposeUtils.isNullOrDisposed(this.mapContext) ) {
117
            try {
118
                throw new RuntimeException();
119
            } catch(Exception ex) {
120
                LOGGER.info("MapContext of view '"+Objects.toString(this.getName())+"' is disposed", ex);
121
            }        
122
        }
123
        return mapContext;
124
    }
125

    
126
    @Override
127
    public void setName(String name) {
128
        super.setName(name);
129
        this.mapContext.getLayers().setName(name);
130
    }
131

    
132
    
133
    /**
134
     * Gets the MapContext from the locator, which is the small map in the
135
     * left-bottom corner of the View.
136
     *
137
     * @return the locator MapContext
138
     */
139
    @Override
140
    public MapContext getMapOverViewContext() {
141
        return mapOverViewContext;
142
    }
143

    
144
    @Override
145
    public void setMapContext(MapContext fmap) {
146
        mapContext = fmap;
147
        Project p = this.getProject();
148
        if(p != null){
149
            mapContext.setSelectionColor(p.getSelectionColor());
150
        }
151
        fmap.addErrorListener(this);
152
    }
153

    
154
    @Override
155
    public void setMapOverViewContext(MapContext fmap) {
156
        mapOverViewContext = fmap;
157
        mapOverViewContext.setProjection(mapContext.getProjection());
158
    }
159

    
160
    public void showErrors() {
161
        if (!mapContext.getLayersError().isEmpty()) {
162
            String layersError = "";
163
            for (int i = 0; i < mapContext.getLayersError().size(); i++) {
164
                layersError = layersError + "\n" + (String) mapContext.getLayersError().get(i);
165
            }
166
            JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
167
                    PluginServices.getText(this, "fallo_capas") + " : \n"
168
                    + layersError);
169
        }
170
    }
171

    
172
    /**
173
     * Reports to the user a bundle of driver exceptions produced in the same
174
     * atomic MapContext transaction
175
     */
176
    @SuppressWarnings("rawtypes")
177
    @Override
178
    public void reportDriverExceptions(String introductoryText, List driverExceptions) {
179
        HtmlWindow htmlPanel = new HtmlWindow(570, 600, PluginServices.getText(this, "driver_error"));
180
        String htmlText = "";
181
        if (introductoryText == null) {
182
            htmlText += "<h2 text=\"#000080\">" + PluginServices.getText(this, "se_han_producido_los_siguientes_errores_durante_la_carga_de_las_capas") + "</h2>";
183
        } else {
184
            htmlText += introductoryText;
185
        }
186
        int numErrors = driverExceptions.size();
187
        for (int i = 0; i < numErrors; i++) {
188
            //htmlText += "<br>\n";
189
            BaseException exception = (BaseException) driverExceptions.get(i);
190
            htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
191
            htmlText += "<h3>" + PluginServices.getText(this, exception.getMessageKey()) + "</h3>";
192
            htmlText += "<p>" + exception.getMessage() + "</p>";
193
            htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
194
        }
195

    
196
        System.out.println(htmlText);
197
        htmlPanel.show(htmlText);
198

    
199
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
200

    
201
    }
202

    
203
    /**
204
     * HtmlInfoToolPanel that implements IWindow
205
     *
206
     * @author azabala
207
     *
208
     */
209
    class HtmlWindow extends JPanel implements IWindow {
210

    
211
        /**
212
         *
213
         */
214
        private static final long serialVersionUID = 1151465547277478664L;
215

    
216
        private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
217
        WindowInfo viewInfo = null;
218

    
219
        public HtmlWindow(int width, int height, String windowTitle) {
220
            htmlPanel.setBackground(Color.white);
221
            JScrollPane scrollPane = new JScrollPane(htmlPanel);
222
            scrollPane.setPreferredSize(new Dimension(width - 30, height - 30));
223
            this.add(scrollPane);
224
            viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
225
            viewInfo.setTitle(windowTitle);
226
            viewInfo.setWidth(width);
227
            viewInfo.setHeight(height);
228
        }
229

    
230
        public void show(String htmlText) {
231
            htmlPanel.show(htmlText);
232
        }
233

    
234
        @Override
235
        public WindowInfo getWindowInfo() {
236
            return viewInfo;
237
        }
238

    
239
        @Override
240
        public Object getWindowProfile() {
241
            return WindowInfo.PROPERTIES_PROFILE;
242
        }
243

    
244
    }
245

    
246
    @Override
247
    public IProjection getProjection() {
248
        if( this.mapContext == null ) {
249
            return null;
250
        }
251
        return mapContext.getProjection();
252
    }
253

    
254
    @Override
255
    public void setProjection(IProjection proj) {
256
        mapContext.setProjection(proj);
257
        mapOverViewContext.setProjection(proj);
258
    }
259

    
260
    public IProjection getOverViewProjection() {
261
        return mapOverViewContext.getProjection();
262
    }
263

    
264
    @Override
265
    public void afterRemove() {
266
        // FIXME: Parece que no recorre correctamente el arbol de capas
267

    
268
        FLayers layers = this.getMapContext().getLayers();
269

    
270
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
271
            try {
272
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
273
            } catch (CancelationException e1) {
274
                LOGGER.warn("Can't remove layer");
275
            }
276
        }
277
        getMapContext().dispose();
278
        getMapOverViewContext().dispose();
279
    }
280

    
281
    @Override
282
    public void afterAdd() {
283
        // Do nothing
284
    }
285

    
286
    @Override
287
    public void setBackColor(Color c) {
288
        getMapContext().getViewPort().setBackColor(c);
289
    }
290

    
291
    @Override
292
    public void errorThrown(ErrorEvent e) {
293
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
294
                PluginServices.getText(this, "fallo_capas") + " : \n"
295
                + e.getMessage());
296

    
297
    }
298

    
299
    @Override
300
    public boolean isLocked() {
301
        if (super.isLocked()) {
302
            return true;
303
        }
304
        FLayers layers = getMapContext().getLayers();
305
        for (int i = 0; i < layers.getLayersCount(); i++) {
306
            FLayer layer = layers.getLayer(i);
307
            if (layer.isEditing()) {
308
                return true;
309
            }
310
        }
311
        return false;
312
    }
313

    
314
    @Override
315
    public void saveToState(PersistentState state) throws PersistenceException {
316
        super.saveToState(state);
317
        state.set("mainMapContext", this.getMapContext());
318
        if (this.getMapOverViewContext() != null) {
319
            state.set("useMapOverview", true);
320
        } else {
321
            state.set("useMapOverview", false);
322
        }
323
        state.set("overviewMapContext", this.getMapOverViewContext());
324
        state.set("propertiesHelper", propertiesHelper);
325
    }
326

    
327
    @Override
328
    public void loadFromState(PersistentState state) throws PersistenceException {
329
        super.loadFromState(state);
330
        this.mapContext = (MapContext) state.get("mainMapContext");
331
        if (state.getBoolean("useMapOverview")) {
332
            this.mapOverViewContext = (MapContext) state.get("overviewMapContext");
333
        } else {
334
            this.mapOverViewContext = null;
335
        }
336
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
337

    
338
    
339
        IProjection proj = ApplicationLocator.getProjectManager().getProjectPreferences().getDefaultProjection();
340
        if( this.mapContext!=null ) {
341
            if( this.mapContext.getProjection()==null ) {
342
                try {
343
                    this.mapContext.setProjection(proj);
344
                } catch(Throwable ex) {
345
                    
346
                }
347
            }
348
        }
349
        for( FLayer layer : mapContext ) {
350
            if( layer.getProjection()==null ) {
351
                try {
352
                    layer.setProjection(proj);
353
                } catch(Throwable ex) {
354
                    
355
                }
356
            }
357
        }
358
        for( FLayer layer : mapOverViewContext ) {
359
            if( layer.getProjection()==null ) {
360
                try {
361
                    layer.setProjection(proj);
362
                } catch(Throwable ex) {
363
                    
364
                }
365
            }
366
        }
367
    }
368

    
369
    @Override
370
    public Object getProperty(Object key) {
371
        return this.propertiesHelper.getProperty(key);
372
    }
373

    
374
    @Override
375
    public void setProperty(Object key, Object obj) {
376
        this.propertiesHelper.setProperty(key, obj);
377
    }
378

    
379
    @Override
380
    public Map getExtendedProperties() {
381
        return this.propertiesHelper.getExtendedProperties();
382
    }
383

    
384
    @Override
385
    public FLayer getLayer(String name) {
386
        FLayer layer = this.getMapContext().getLayers().getLayer(name);
387
        return layer;            
388
    }
389

    
390
    @Override
391
    public Iterator<FLayer> iterator() {
392
        return this.mapContext.iterator();
393
    }
394

    
395
    @Override
396
    public Iterator<FLayer> deepiterator() {
397
        return this.mapContext.deepiterator();
398
    }
399

    
400
    @Override
401
    public Iterable<FLayer> layers() {
402
        return () -> deepiterator();
403
    }
404

    
405
    @Override
406
    public boolean contains(FLayer layer) {
407
        if( !(layer instanceof SingleLayer) ) {
408
            return false;
409
        }
410
        return this.isInLayers(this.getMapContext().getLayers(), ((SingleLayer)layer).getDataStore());
411
    }
412
    
413
    @Override
414
    public boolean contains(DataStore store) {
415
        return this.isInLayers(this.getMapContext().getLayers(), store);
416
    }
417
    
418
    private boolean isInLayers(FLayers layers, DataStore store) {
419
        for (int i = 0; i < layers.getLayersCount(); i++) {
420
            if (layers.getLayer(i) instanceof FLayers) {
421
                if (isInLayers((FLayers) layers.getLayer(i), store)) {
422
                    return true;
423
                }
424
            }
425
            FLayer layer = layers.getLayer(i);
426
            if( layer instanceof SingleLayer ) {
427
                if (((SingleLayer)layer).getDataStore() == store ) {
428
                    return true;
429
                }
430
            }
431
        }
432
        return false;
433
    }
434
    
435
    @Override
436
    public void center(Envelope envelope) {
437
        ViewPort viewPort = this.getMapContext().getViewPort();
438

    
439
        try {
440
            Envelope oldExtent = viewPort.getAdjustedEnvelope();
441
            double oldCenterX = oldExtent.getCenter(0);
442
            double oldCenterY = oldExtent.getCenter(1);
443
            double centerX = envelope.getCenter(0);
444
            double centerY = envelope.getCenter(1);
445
            Point2D.Double center = new Point2D.Double(centerX, centerY);
446
            double movX = centerX - oldCenterX;
447
            double movY = centerY - oldCenterY;
448

    
449
            double minx = oldExtent.getMinimum(0) + movX;
450
            double miny = oldExtent.getMinimum(1) + movY;
451
            double maxX = oldExtent.getMaximum(0) + movX;
452
            double maxY = oldExtent.getMaximum(1) + movY;
453
            Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(
454
                    minx, miny,
455
                    maxX, maxY,
456
                    Geometry.SUBTYPES.GEOM2D);
457
            viewPort.setEnvelope(extent);
458
        } catch (Exception e) {
459
            throw new RuntimeException(e);
460
        }
461

    
462
    }
463

    
464
    @Override
465
    public boolean contains(Object value) {
466
        if( value == null ) {
467
            return false;
468
        }
469
        DataStoreParameters params;
470
        
471
        if( (value instanceof FeatureStore) ) {
472
            params = ((FeatureStore) value).getParameters();
473
        } else if(value instanceof DataStoreParameters){
474
            params = (DataStoreParameters) value;
475
        } else {
476
            return false;
477
        }
478
        
479
        for (Iterator<FLayer> it = this.deepiterator(); it.hasNext();) {
480
            FLayer layer = it.next();
481
            if(layer instanceof FLyrVect){
482
                FeatureStore store = ((FLyrVect) layer).getFeatureStore();
483
                if( store!=null && store.getParameters().isTheSameStore(params)){
484
                    return true;
485
                }
486
            }
487
        }
488
        return false;
489
    }
490

    
491
    @Override
492
    public void refresh() {
493
        this.getMapContext().invalidate();
494
    }
495
    
496
}