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

History | View | Annotate | Download (11 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.project.documents.AbstractDocument;
42
import org.gvsig.app.project.documents.DocumentManager;
43
import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel;
44
import org.gvsig.fmap.dal.DataStore;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.fmap.mapcontext.MapContext;
47
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
48
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
49
import org.gvsig.fmap.mapcontext.layers.CancelationException;
50
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
51
import org.gvsig.fmap.mapcontext.layers.FLayer;
52
import org.gvsig.fmap.mapcontext.layers.FLayers;
53
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
54
import org.gvsig.tools.exception.BaseException;
55
import org.gvsig.tools.persistence.PersistentState;
56
import org.gvsig.tools.persistence.exception.PersistenceException;
57

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

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

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

    
74
    protected MapContext mapOverViewContext;
75
    protected MapContext mapContext;
76

    
77
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
78

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

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

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

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

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

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

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

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

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

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

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

    
163
    }
164

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

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

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

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

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

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

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

    
204
    }
205

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

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

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

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

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

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

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

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

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

    
248
    }
249

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

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

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

    
287
    public Object getProperty(Object key) {
288
        return this.propertiesHelper.getProperty(key);
289
    }
290

    
291
    public void setProperty(Object key, Object obj) {
292
        this.propertiesHelper.setProperty(key, obj);
293
    }
294

    
295
    public Map getExtendedProperties() {
296
        return this.propertiesHelper.getExtendedProperties();
297
    }
298

    
299
    public Iterator<FLayer> iterator() {
300
        return this.mapContext.iterator();
301
    }
302

    
303
    public Iterator<FLayer> deepiterator() {
304
        return this.mapContext.deepiterator();
305
    }
306

    
307
    public boolean contains(FLayer layer) {
308
        if( !(layer instanceof SingleLayer) ) {
309
            return false;
310
        }
311
        return this.isInLayers(this.getMapContext().getLayers(), ((SingleLayer)layer).getDataStore());
312
    }
313
    
314
    public boolean contains(DataStore store) {
315
        return this.isInLayers(this.getMapContext().getLayers(), store);
316
    }
317
    
318
    private boolean isInLayers(FLayers layers, DataStore store) {
319
        for (int i = 0; i < layers.getLayersCount(); i++) {
320
            if (layers.getLayer(i) instanceof FLayers) {
321
                if (isInLayers((FLayers) layers.getLayer(i), store)) {
322
                    return true;
323
                }
324
            }
325
            FLayer layer = layers.getLayer(i);
326
            if( layer instanceof SingleLayer ) {
327
                if (((SingleLayer)layer).getDataStore() == store ) {
328
                    return true;
329
                }
330
            }
331
        }
332
        return false;
333
    }
334
    
335
    public void center(Envelope envelope) {
336
        this.getMapContext().getViewPort().setEnvelope(envelope);
337
    }
338
}