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

History | View | Annotate | Download (14.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

    
33
import javax.swing.JOptionPane;
34
import javax.swing.JPanel;
35
import javax.swing.JScrollPane;
36
import org.apache.commons.lang.StringUtils;
37

    
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
import org.gvsig.app.ApplicationLocator;
44
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
import org.gvsig.fmap.dal.DataStore;
48
import org.gvsig.fmap.dal.DataStoreParameters;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53
import org.gvsig.fmap.mapcontext.MapContext;
54
import org.gvsig.fmap.mapcontext.ViewPort;
55
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
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
59
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.fmap.mapcontext.layers.FLayers;
61
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63
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
 * @author 2005- Vicente Caballero
70
 * @author 2009- Joaquin del Cerro
71
 *
72
 */
73
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
74
        ViewDocument {
75

    
76
    /**
77
     *
78
     */
79
    private static final long serialVersionUID = -2621709089720665902L;
80

    
81
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
82

    
83
    protected MapContext mapOverViewContext;
84
    protected MapContext mapContext;
85

    
86
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
87

    
88
    public BaseViewDocument() {
89
        super();
90
    }
91

    
92
    public BaseViewDocument(DocumentManager factory) {
93
        super(factory);
94
    }
95

    
96
    /**
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

    
105
    @Override
106
    public void setName(String name) {
107
        super.setName(name);
108
        this.mapContext.getLayers().setName(name);
109
    }
110

    
111
    
112
    /**
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

    
122
    public void setMapContext(MapContext fmap) {
123
        mapContext = fmap;
124
        fmap.addErrorListener(this);
125
    }
126

    
127
    public void setMapOverViewContext(MapContext fmap) {
128
        mapOverViewContext = fmap;
129
        mapOverViewContext.setProjection(mapContext.getProjection());
130
    }
131

    
132
    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

    
144
    /**
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

    
167
        System.out.println(htmlText);
168
        htmlPanel.show(htmlText);
169

    
170
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
171

    
172
    }
173

    
174
    /**
175
     * HtmlInfoToolPanel that implements IWindow
176
     *
177
     * @author azabala
178
     *
179
     */
180
    class HtmlWindow extends JPanel implements IWindow {
181

    
182
        /**
183
         *
184
         */
185
        private static final long serialVersionUID = 1151465547277478664L;
186

    
187
        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
        if( this.mapContext == null ) {
217
            return null;
218
        }
219
        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
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
239
            } catch (CancelationException e1) {
240
                e1.printStackTrace();
241
            }
242
        }
243
        getMapContext().dispose();
244
        getMapOverViewContext().dispose();
245
    }
246

    
247
    public void afterAdd() {
248
        // Do nothing
249
    }
250

    
251
    public void setBackColor(Color c) {
252
        getMapContext().getViewPort().setBackColor(c);
253
    }
254

    
255
    public void errorThrown(ErrorEvent e) {
256
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
257
                PluginServices.getText(this, "fallo_capas") + " : \n"
258
                + e.getMessage());
259

    
260
    }
261

    
262
    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

    
276
    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
        }
284
        state.set("overviewMapContext", this.getMapOverViewContext());
285
        state.set("propertiesHelper", propertiesHelper);
286
    }
287

    
288
    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
        }
296
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
297

    
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
    }
328

    
329
    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
    @Override
342
    public FLayer getLayer(String name) {
343
        FLayer layer = this.getMapContext().getLayers().getLayer(name);
344
        return layer;            
345
    }
346

    
347
    @Override
348
    public Iterator<FLayer> iterator() {
349
        return this.mapContext.iterator();
350
    }
351

    
352
    @Override
353
    public Iterator<FLayer> deepiterator() {
354
        return this.mapContext.deepiterator();
355
    }
356

    
357
    @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
    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
    @Override
376
    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
    
397
    @Override
398
    public void center(Envelope envelope) {
399
        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
    }
425

    
426
    @Override
427
    public boolean contains(Object value) {
428
        if( value == null ) {
429
            return false;
430
        }
431
        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
            return false;
439
        }
440
        
441
        for (Iterator<FLayer> it = this.deepiterator(); it.hasNext();) {
442
            FLayer layer = it.next();
443
            if(layer instanceof FLyrVect){
444
                FeatureStore store = ((FLyrVect) layer).getFeatureStore();
445
                if( store!=null && store.getParameters().isTheSameStore(params)){
446
                    return true;
447
                }
448
            }
449
        }
450
        return false;
451
    }
452

    
453
    @Override
454
    public void refresh() {
455
        this.getMapContext().invalidate();
456
    }
457
    
458
}