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

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

    
37
import org.cresques.cts.IProjection;
38

    
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
import org.gvsig.andami.ui.mdiManager.WindowInfo;
42
import org.gvsig.app.ApplicationLocator;
43
import org.gvsig.app.project.documents.AbstractDocument;
44
import org.gvsig.app.project.documents.DocumentManager;
45
import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel;
46
import org.gvsig.fmap.dal.DataStore;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.fmap.mapcontext.MapContext;
51
import org.gvsig.fmap.mapcontext.ViewPort;
52
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
53
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
54
import org.gvsig.fmap.mapcontext.layers.CancelationException;
55
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
56
import org.gvsig.fmap.mapcontext.layers.FLayer;
57
import org.gvsig.fmap.mapcontext.layers.FLayers;
58
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
59
import org.gvsig.tools.exception.BaseException;
60
import org.gvsig.tools.persistence.PersistentState;
61
import org.gvsig.tools.persistence.exception.PersistenceException;
62

    
63
/**
64
 *
65
 * @author 2005- Vicente Caballero
66
 * @author 2009- Joaquin del Cerro
67
 *
68
 */
69
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
70
        ViewDocument {
71

    
72
    /**
73
     *
74
     */
75
    private static final long serialVersionUID = -2621709089720665902L;
76

    
77
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
78

    
79
    protected MapContext mapOverViewContext;
80
    protected MapContext mapContext;
81

    
82
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
83

    
84
    public BaseViewDocument() {
85
        super();
86
    }
87

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

    
92
    /**
93
     * Gets the MapContext of the main map in the view.
94
     *
95
     * @return the main MapContext
96
     */
97
    public MapContext getMapContext() {
98
        return mapContext;
99
    }
100

    
101
    @Override
102
    public void setName(String name) {
103
        super.setName(name);
104
        this.mapContext.getLayers().setName(name);
105
    }
106

    
107
    
108
    /**
109
     * Gets the MapContext from the locator, which is the small map in the
110
     * left-bottom corner of the View.
111
     *
112
     * @return the locator MapContext
113
     */
114
    public MapContext getMapOverViewContext() {
115
        return mapOverViewContext;
116
    }
117

    
118
    public void setMapContext(MapContext fmap) {
119
        mapContext = fmap;
120
        fmap.addErrorListener(this);
121
    }
122

    
123
    public void setMapOverViewContext(MapContext fmap) {
124
        mapOverViewContext = fmap;
125
        mapOverViewContext.setProjection(mapContext.getProjection());
126
    }
127

    
128
    public void showErrors() {
129
        if (mapContext.getLayersError().size() > 0) {
130
            String layersError = "";
131
            for (int i = 0; i < mapContext.getLayersError().size(); i++) {
132
                layersError = layersError + "\n" + (String) mapContext.getLayersError().get(i);
133
            }
134
            JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
135
                    PluginServices.getText(this, "fallo_capas") + " : \n"
136
                    + layersError);
137
        }
138
    }
139

    
140
    /**
141
     * Reports to the user a bundle of driver exceptions produced in the same
142
     * atomic MapContext transaction
143
     */
144
    @SuppressWarnings("rawtypes")
145
    public void reportDriverExceptions(String introductoryText, List driverExceptions) {
146
        HtmlWindow htmlPanel = new HtmlWindow(570, 600, PluginServices.getText(this, "driver_error"));
147
        String htmlText = "";
148
        if (introductoryText == null) {
149
            htmlText += "<h2 text=\"#000080\">" + PluginServices.getText(this, "se_han_producido_los_siguientes_errores_durante_la_carga_de_las_capas") + "</h2>";
150
        } else {
151
            htmlText += introductoryText;
152
        }
153
        int numErrors = driverExceptions.size();
154
        for (int i = 0; i < numErrors; i++) {
155
            //htmlText += "<br>\n";
156
            BaseException exception = (BaseException) driverExceptions.get(i);
157
            htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
158
            htmlText += "<h3>" + PluginServices.getText(this, exception.getMessageKey()) + "</h3>";
159
            htmlText += "<p>" + exception.getMessage() + "</p>";
160
            htmlText += "<p text=\"#550080\">_________________________________________________________________________________________</p>";
161
        }
162

    
163
        System.out.println(htmlText);
164
        htmlPanel.show(htmlText);
165

    
166
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
167

    
168
    }
169

    
170
    /**
171
     * HtmlInfoToolPanel that implements IWindow
172
     *
173
     * @author azabala
174
     *
175
     */
176
    class HtmlWindow extends JPanel implements IWindow {
177

    
178
        /**
179
         *
180
         */
181
        private static final long serialVersionUID = 1151465547277478664L;
182

    
183
        private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
184
        WindowInfo viewInfo = null;
185

    
186
        public HtmlWindow(int width, int height, String windowTitle) {
187
            htmlPanel.setBackground(Color.white);
188
            JScrollPane scrollPane = new JScrollPane(htmlPanel);
189
            scrollPane.setPreferredSize(new Dimension(width - 30, height - 30));
190
            this.add(scrollPane);
191
            viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
192
            viewInfo.setTitle(windowTitle);
193
            viewInfo.setWidth(width);
194
            viewInfo.setHeight(height);
195
        }
196

    
197
        public void show(String htmlText) {
198
            htmlPanel.show(htmlText);
199
        }
200

    
201
        public WindowInfo getWindowInfo() {
202
            return viewInfo;
203
        }
204

    
205
        public Object getWindowProfile() {
206
            return WindowInfo.PROPERTIES_PROFILE;
207
        }
208

    
209
    }
210

    
211
    public IProjection getProjection() {
212
        return mapContext.getProjection();
213
    }
214

    
215
    public void setProjection(IProjection proj) {
216
        mapContext.setProjection(proj);
217
        mapOverViewContext.setProjection(proj);
218
    }
219

    
220
    public IProjection getOverViewProjection() {
221
        return mapOverViewContext.getProjection();
222
    }
223

    
224
    public void afterRemove() {
225
        // FIXME: Parece que no recorre correctamente el arbol de capas
226

    
227
        FLayers layers = this.getMapContext().getLayers();
228

    
229
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
230
            try {
231
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
232
            } catch (CancelationException e1) {
233
                e1.printStackTrace();
234
            }
235
        }
236
        getMapContext().dispose();
237
        getMapOverViewContext().dispose();
238
    }
239

    
240
    public void afterAdd() {
241
        // Do nothing
242
    }
243

    
244
    public void setBackColor(Color c) {
245
        getMapContext().getViewPort().setBackColor(c);
246
    }
247

    
248
    public void errorThrown(ErrorEvent e) {
249
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
250
                PluginServices.getText(this, "fallo_capas") + " : \n"
251
                + e.getMessage());
252

    
253
    }
254

    
255
    public boolean isLocked() {
256
        if (super.isLocked()) {
257
            return true;
258
        }
259
        FLayers layers = getMapContext().getLayers();
260
        for (int i = 0; i < layers.getLayersCount(); i++) {
261
            FLayer layer = layers.getLayer(i);
262
            if (layer.isEditing()) {
263
                return true;
264
            }
265
        }
266
        return false;
267
    }
268

    
269
    public void saveToState(PersistentState state) throws PersistenceException {
270
        super.saveToState(state);
271
        state.set("mainMapContext", this.getMapContext());
272
        if (this.getMapOverViewContext() != null) {
273
            state.set("useMapOverview", true);
274
        } else {
275
            state.set("useMapOverview", false);
276
        }
277
        state.set("overviewMapContext", this.getMapOverViewContext());
278
        state.set("propertiesHelper", propertiesHelper);
279
    }
280

    
281
    public void loadFromState(PersistentState state) throws PersistenceException {
282
        super.loadFromState(state);
283
        this.mapContext = (MapContext) state.get("mainMapContext");
284
        if (state.getBoolean("useMapOverview")) {
285
            this.mapOverViewContext = (MapContext) state.get("overviewMapContext");
286
        } else {
287
            this.mapOverViewContext = null;
288
        }
289
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
290

    
291
    
292
        IProjection proj = ApplicationLocator.getProjectManager().getProjectPreferences().getDefaultProjection();
293
        if( this.mapContext!=null ) {
294
            if( this.mapContext.getProjection()==null ) {
295
                try {
296
                    this.mapContext.setProjection(proj);
297
                } catch(Throwable ex) {
298
                    
299
                }
300
            }
301
        }
302
        for( FLayer layer : mapContext ) {
303
            if( layer.getProjection()==null ) {
304
                try {
305
                    layer.setProjection(proj);
306
                } catch(Throwable ex) {
307
                    
308
                }
309
            }
310
        }
311
        for( FLayer layer : mapOverViewContext ) {
312
            if( layer.getProjection()==null ) {
313
                try {
314
                    layer.setProjection(proj);
315
                } catch(Throwable ex) {
316
                    
317
                }
318
            }
319
        }
320
    }
321

    
322
    public Object getProperty(Object key) {
323
        return this.propertiesHelper.getProperty(key);
324
    }
325

    
326
    public void setProperty(Object key, Object obj) {
327
        this.propertiesHelper.setProperty(key, obj);
328
    }
329

    
330
    public Map getExtendedProperties() {
331
        return this.propertiesHelper.getExtendedProperties();
332
    }
333

    
334
    @Override
335
    public FLayer getLayer(String name) {
336
        FLayer layer = this.getMapContext().getLayers().getLayer(name);
337
        return layer;            
338
    }
339

    
340
    @Override
341
    public Iterator<FLayer> iterator() {
342
        return this.mapContext.iterator();
343
    }
344

    
345
    @Override
346
    public Iterator<FLayer> deepiterator() {
347
        return this.mapContext.deepiterator();
348
    }
349

    
350
    @Override
351
    public Iterable<FLayer> layers() {
352
        return new Iterable<FLayer>() {
353
            @Override
354
            public Iterator<FLayer> iterator() {
355
                return deepiterator();
356
            }
357
        };
358
    }
359

    
360
    @Override
361
    public boolean contains(FLayer layer) {
362
        if( !(layer instanceof SingleLayer) ) {
363
            return false;
364
        }
365
        return this.isInLayers(this.getMapContext().getLayers(), ((SingleLayer)layer).getDataStore());
366
    }
367
    
368
    @Override
369
    public boolean contains(DataStore store) {
370
        return this.isInLayers(this.getMapContext().getLayers(), store);
371
    }
372
    
373
    private boolean isInLayers(FLayers layers, DataStore store) {
374
        for (int i = 0; i < layers.getLayersCount(); i++) {
375
            if (layers.getLayer(i) instanceof FLayers) {
376
                if (isInLayers((FLayers) layers.getLayer(i), store)) {
377
                    return true;
378
                }
379
            }
380
            FLayer layer = layers.getLayer(i);
381
            if( layer instanceof SingleLayer ) {
382
                if (((SingleLayer)layer).getDataStore() == store ) {
383
                    return true;
384
                }
385
            }
386
        }
387
        return false;
388
    }
389
    
390
    @Override
391
    public void center(Envelope envelope) {
392
        ViewPort viewPort = this.getMapContext().getViewPort();
393

    
394
        try {
395
            Envelope oldExtent = viewPort.getAdjustedEnvelope();
396
            double oldCenterX = oldExtent.getCenter(0);
397
            double oldCenterY = oldExtent.getCenter(1);
398
            double centerX = envelope.getCenter(0);
399
            double centerY = envelope.getCenter(1);
400
            Point2D.Double center = new Point2D.Double(centerX, centerY);
401
            double movX = centerX - oldCenterX;
402
            double movY = centerY - oldCenterY;
403

    
404
            double minx = oldExtent.getMinimum(0) + movX;
405
            double miny = oldExtent.getMinimum(1) + movY;
406
            double maxX = oldExtent.getMaximum(0) + movX;
407
            double maxY = oldExtent.getMaximum(1) + movY;
408
            Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(
409
                    minx, miny,
410
                    maxX, maxY,
411
                    Geometry.SUBTYPES.GEOM2D);
412
            viewPort.setEnvelope(extent);
413
        } catch (Exception e) {
414
            throw new RuntimeException(e);
415
        }
416

    
417
    }
418
}