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

History | View | Annotate | Download (12.1 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.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31

    
32
import javax.swing.JOptionPane;
33
import javax.swing.JPanel;
34
import javax.swing.JScrollPane;
35

    
36
import org.cresques.cts.IProjection;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.andami.ui.mdiManager.WindowInfo;
41
import org.gvsig.app.ApplicationLocator;
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.geom.primitive.Envelope;
47
import org.gvsig.fmap.mapcontext.MapContext;
48
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
49
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
50
import org.gvsig.fmap.mapcontext.layers.CancelationException;
51
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
52
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontext.layers.FLayers;
54
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
55
import org.gvsig.tools.exception.BaseException;
56
import org.gvsig.tools.persistence.PersistentState;
57
import org.gvsig.tools.persistence.exception.PersistenceException;
58

    
59
/**
60
 *
61
 * @author 2005- Vicente Caballero
62
 * @author 2009- Joaquin del Cerro
63
 *
64
 */
65
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
66
        ViewDocument {
67

    
68
    /**
69
     *
70
     */
71
    private static final long serialVersionUID = -2621709089720665902L;
72

    
73
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
74

    
75
    protected MapContext mapOverViewContext;
76
    protected MapContext mapContext;
77

    
78
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
79

    
80
    public BaseViewDocument() {
81
        super();
82
    }
83

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

    
88
    /**
89
     * Gets the MapContext of the main map in the view.
90
     *
91
     * @return the main MapContext
92
     */
93
    public MapContext getMapContext() {
94
        return mapContext;
95
    }
96

    
97
    @Override
98
    public void setName(String name) {
99
        super.setName(name);
100
        this.mapContext.getLayers().setName(name);
101
    }
102

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

    
114
    public void setMapContext(MapContext fmap) {
115
        mapContext = fmap;
116
        fmap.addErrorListener(this);
117
    }
118

    
119
    public void setMapOverViewContext(MapContext fmap) {
120
        mapOverViewContext = fmap;
121
        mapOverViewContext.setProjection(mapContext.getProjection());
122
    }
123

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

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

    
159
        System.out.println(htmlText);
160
        htmlPanel.show(htmlText);
161

    
162
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
163

    
164
    }
165

    
166
    /**
167
     * HtmlInfoToolPanel that implements IWindow
168
     *
169
     * @author azabala
170
     *
171
     */
172
    class HtmlWindow extends JPanel implements IWindow {
173

    
174
        /**
175
         *
176
         */
177
        private static final long serialVersionUID = 1151465547277478664L;
178

    
179
        private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
180
        WindowInfo viewInfo = null;
181

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

    
193
        public void show(String htmlText) {
194
            htmlPanel.show(htmlText);
195
        }
196

    
197
        public WindowInfo getWindowInfo() {
198
            return viewInfo;
199
        }
200

    
201
        public Object getWindowProfile() {
202
            return WindowInfo.PROPERTIES_PROFILE;
203
        }
204

    
205
    }
206

    
207
    public IProjection getProjection() {
208
        return mapContext.getProjection();
209
    }
210

    
211
    public void setProjection(IProjection proj) {
212
        mapContext.setProjection(proj);
213
        mapOverViewContext.setProjection(proj);
214
    }
215

    
216
    public IProjection getOverViewProjection() {
217
        return mapOverViewContext.getProjection();
218
    }
219

    
220
    public void afterRemove() {
221
        // FIXME: Parece que no recorre correctamente el arbol de capas
222

    
223
        FLayers layers = this.getMapContext().getLayers();
224

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

    
236
    public void afterAdd() {
237
        // Do nothing
238
    }
239

    
240
    public void setBackColor(Color c) {
241
        getMapContext().getViewPort().setBackColor(c);
242
    }
243

    
244
    public void errorThrown(ErrorEvent e) {
245
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
246
                PluginServices.getText(this, "fallo_capas") + " : \n"
247
                + e.getMessage());
248

    
249
    }
250

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

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

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

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

    
318
    public Object getProperty(Object key) {
319
        return this.propertiesHelper.getProperty(key);
320
    }
321

    
322
    public void setProperty(Object key, Object obj) {
323
        this.propertiesHelper.setProperty(key, obj);
324
    }
325

    
326
    public Map getExtendedProperties() {
327
        return this.propertiesHelper.getExtendedProperties();
328
    }
329

    
330
    @Override
331
    public FLayer getLayer(String name) {
332
        FLayer layer = this.getMapContext().getLayers().getLayer(name);
333
        return layer;            
334
    }
335

    
336
    public Iterator<FLayer> iterator() {
337
        return this.mapContext.iterator();
338
    }
339

    
340
    public Iterator<FLayer> deepiterator() {
341
        return this.mapContext.deepiterator();
342
    }
343

    
344
    public boolean contains(FLayer layer) {
345
        if( !(layer instanceof SingleLayer) ) {
346
            return false;
347
        }
348
        return this.isInLayers(this.getMapContext().getLayers(), ((SingleLayer)layer).getDataStore());
349
    }
350
    
351
    public boolean contains(DataStore store) {
352
        return this.isInLayers(this.getMapContext().getLayers(), store);
353
    }
354
    
355
    private boolean isInLayers(FLayers layers, DataStore store) {
356
        for (int i = 0; i < layers.getLayersCount(); i++) {
357
            if (layers.getLayer(i) instanceof FLayers) {
358
                if (isInLayers((FLayers) layers.getLayer(i), store)) {
359
                    return true;
360
                }
361
            }
362
            FLayer layer = layers.getLayer(i);
363
            if( layer instanceof SingleLayer ) {
364
                if (((SingleLayer)layer).getDataStore() == store ) {
365
                    return true;
366
                }
367
            }
368
        }
369
        return false;
370
    }
371
    
372
    public void center(Envelope envelope) {
373
        this.getMapContext().getViewPort().setEnvelope(envelope);
374
    }
375
}