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 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 42293 jjdelcerro
import java.util.Iterator;
29 40435 jjdelcerro
import java.util.List;
30 41350 jjdelcerro
import java.util.Map;
31 40435 jjdelcerro
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 43439 jjdelcerro
import org.gvsig.app.ApplicationLocator;
42 40435 jjdelcerro
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 42682 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
46 42775 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
47 40435 jjdelcerro
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 41350 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
52 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontext.layers.FLayers;
54 42682 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
55 40435 jjdelcerro
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 42285 jjdelcerro
 * @author 2005- Vicente Caballero
62
 * @author 2009- Joaquin del Cerro
63
 *
64 40435 jjdelcerro
 */
65
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
66 42285 jjdelcerro
        ViewDocument {
67 40435 jjdelcerro
68 42285 jjdelcerro
    /**
69
     *
70
     */
71
    private static final long serialVersionUID = -2621709089720665902L;
72 40435 jjdelcerro
73 42285 jjdelcerro
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
74 40435 jjdelcerro
75 42285 jjdelcerro
    protected MapContext mapOverViewContext;
76
    protected MapContext mapContext;
77 40435 jjdelcerro
78 42285 jjdelcerro
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
79 40435 jjdelcerro
80 42285 jjdelcerro
    public BaseViewDocument() {
81
        super();
82
    }
83 40435 jjdelcerro
84 42285 jjdelcerro
    public BaseViewDocument(DocumentManager factory) {
85
        super(factory);
86
    }
87 40435 jjdelcerro
88 42285 jjdelcerro
    /**
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 40435 jjdelcerro
97 42651 jjdelcerro
    @Override
98
    public void setName(String name) {
99
        super.setName(name);
100
        this.mapContext.getLayers().setName(name);
101
    }
102
103
104 42285 jjdelcerro
    /**
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 40435 jjdelcerro
114 42285 jjdelcerro
    public void setMapContext(MapContext fmap) {
115
        mapContext = fmap;
116
        fmap.addErrorListener(this);
117
    }
118 40435 jjdelcerro
119 42285 jjdelcerro
    public void setMapOverViewContext(MapContext fmap) {
120
        mapOverViewContext = fmap;
121
        mapOverViewContext.setProjection(mapContext.getProjection());
122
    }
123 40435 jjdelcerro
124 42285 jjdelcerro
    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 40435 jjdelcerro
136 42285 jjdelcerro
    /**
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 40435 jjdelcerro
159 42285 jjdelcerro
        System.out.println(htmlText);
160
        htmlPanel.show(htmlText);
161 40435 jjdelcerro
162 42285 jjdelcerro
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
163 40435 jjdelcerro
164 42285 jjdelcerro
    }
165 40435 jjdelcerro
166 42285 jjdelcerro
    /**
167
     * HtmlInfoToolPanel that implements IWindow
168
     *
169
     * @author azabala
170
     *
171
     */
172
    class HtmlWindow extends JPanel implements IWindow {
173 40435 jjdelcerro
174 42285 jjdelcerro
        /**
175
         *
176
         */
177
        private static final long serialVersionUID = 1151465547277478664L;
178 40435 jjdelcerro
179 42285 jjdelcerro
        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 40435 jjdelcerro
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
228 42285 jjdelcerro
            } catch (CancelationException e1) {
229
                e1.printStackTrace();
230
            }
231
        }
232
        getMapContext().dispose();
233
        getMapOverViewContext().dispose();
234
    }
235 40435 jjdelcerro
236 42285 jjdelcerro
    public void afterAdd() {
237
        // Do nothing
238
    }
239 40435 jjdelcerro
240 42285 jjdelcerro
    public void setBackColor(Color c) {
241
        getMapContext().getViewPort().setBackColor(c);
242
    }
243 40435 jjdelcerro
244 42285 jjdelcerro
    public void errorThrown(ErrorEvent e) {
245
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
246
                PluginServices.getText(this, "fallo_capas") + " : \n"
247
                + e.getMessage());
248 40435 jjdelcerro
249 42285 jjdelcerro
    }
250 40435 jjdelcerro
251 42285 jjdelcerro
    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 40435 jjdelcerro
265 42285 jjdelcerro
    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 41350 jjdelcerro
        }
273 42285 jjdelcerro
        state.set("overviewMapContext", this.getMapOverViewContext());
274
        state.set("propertiesHelper", propertiesHelper);
275
    }
276 41350 jjdelcerro
277 42285 jjdelcerro
    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 41350 jjdelcerro
        }
285 42285 jjdelcerro
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
286 43439 jjdelcerro
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 42285 jjdelcerro
    }
317 41350 jjdelcerro
318 42285 jjdelcerro
    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 43987 jjdelcerro
    @Override
331
    public FLayer getLayer(String name) {
332
        FLayer layer = this.getMapContext().getLayers().getLayer(name);
333
        return layer;
334
    }
335
336 42293 jjdelcerro
    public Iterator<FLayer> iterator() {
337
        return this.mapContext.iterator();
338
    }
339
340
    public Iterator<FLayer> deepiterator() {
341
        return this.mapContext.deepiterator();
342
    }
343
344 42682 jjdelcerro
    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 42775 jjdelcerro
372
    public void center(Envelope envelope) {
373
        this.getMapContext().getViewPort().setEnvelope(envelope);
374
    }
375 40435 jjdelcerro
}