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

History | View | Annotate | Download (9.66 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.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
46
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
47
import org.gvsig.fmap.mapcontext.layers.CancelationException;
48
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
49
import org.gvsig.fmap.mapcontext.layers.FLayer;
50
import org.gvsig.fmap.mapcontext.layers.FLayers;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.persistence.PersistentState;
53
import org.gvsig.tools.persistence.exception.PersistenceException;
54

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

    
64
    /**
65
     *
66
     */
67
    private static final long serialVersionUID = -2621709089720665902L;
68

    
69
    public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
70

    
71
    protected MapContext mapOverViewContext;
72
    protected MapContext mapContext;
73

    
74
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
75

    
76
    public BaseViewDocument() {
77
        super();
78
    }
79

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

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

    
93
    /**
94
     * Gets the MapContext from the locator, which is the small map in the
95
     * left-bottom corner of the View.
96
     *
97
     * @return the locator MapContext
98
     */
99
    public MapContext getMapOverViewContext() {
100
        return mapOverViewContext;
101
    }
102

    
103
    public void setMapContext(MapContext fmap) {
104
        mapContext = fmap;
105
        fmap.addErrorListener(this);
106
    }
107

    
108
    public void setMapOverViewContext(MapContext fmap) {
109
        mapOverViewContext = fmap;
110
        mapOverViewContext.setProjection(mapContext.getProjection());
111
    }
112

    
113
    public void showErrors() {
114
        if (mapContext.getLayersError().size() > 0) {
115
            String layersError = "";
116
            for (int i = 0; i < mapContext.getLayersError().size(); i++) {
117
                layersError = layersError + "\n" + (String) mapContext.getLayersError().get(i);
118
            }
119
            JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
120
                    PluginServices.getText(this, "fallo_capas") + " : \n"
121
                    + layersError);
122
        }
123
    }
124

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

    
148
        System.out.println(htmlText);
149
        htmlPanel.show(htmlText);
150

    
151
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
152

    
153
    }
154

    
155
    /**
156
     * HtmlInfoToolPanel that implements IWindow
157
     *
158
     * @author azabala
159
     *
160
     */
161
    class HtmlWindow extends JPanel implements IWindow {
162

    
163
        /**
164
         *
165
         */
166
        private static final long serialVersionUID = 1151465547277478664L;
167

    
168
        private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
169
        WindowInfo viewInfo = null;
170

    
171
        public HtmlWindow(int width, int height, String windowTitle) {
172
            htmlPanel.setBackground(Color.white);
173
            JScrollPane scrollPane = new JScrollPane(htmlPanel);
174
            scrollPane.setPreferredSize(new Dimension(width - 30, height - 30));
175
            this.add(scrollPane);
176
            viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
177
            viewInfo.setTitle(windowTitle);
178
            viewInfo.setWidth(width);
179
            viewInfo.setHeight(height);
180
        }
181

    
182
        public void show(String htmlText) {
183
            htmlPanel.show(htmlText);
184
        }
185

    
186
        public WindowInfo getWindowInfo() {
187
            return viewInfo;
188
        }
189

    
190
        public Object getWindowProfile() {
191
            return WindowInfo.PROPERTIES_PROFILE;
192
        }
193

    
194
    }
195

    
196
    public IProjection getProjection() {
197
        return mapContext.getProjection();
198
    }
199

    
200
    public void setProjection(IProjection proj) {
201
        mapContext.setProjection(proj);
202
        mapOverViewContext.setProjection(proj);
203
    }
204

    
205
    public IProjection getOverViewProjection() {
206
        return mapOverViewContext.getProjection();
207
    }
208

    
209
    public void afterRemove() {
210
        // FIXME: Parece que no recorre correctamente el arbol de capas
211

    
212
        FLayers layers = this.getMapContext().getLayers();
213

    
214
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
215
            try {
216
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
217
            } catch (CancelationException e1) {
218
                e1.printStackTrace();
219
            }
220
        }
221
        getMapContext().dispose();
222
        getMapOverViewContext().dispose();
223
    }
224

    
225
    public void afterAdd() {
226
        // Do nothing
227
    }
228

    
229
    public void setBackColor(Color c) {
230
        getMapContext().getViewPort().setBackColor(c);
231
    }
232

    
233
    public void errorThrown(ErrorEvent e) {
234
        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
235
                PluginServices.getText(this, "fallo_capas") + " : \n"
236
                + e.getMessage());
237

    
238
    }
239

    
240
    public boolean isLocked() {
241
        if (super.isLocked()) {
242
            return true;
243
        }
244
        FLayers layers = getMapContext().getLayers();
245
        for (int i = 0; i < layers.getLayersCount(); i++) {
246
            FLayer layer = layers.getLayer(i);
247
            if (layer.isEditing()) {
248
                return true;
249
            }
250
        }
251
        return false;
252
    }
253

    
254
    public void saveToState(PersistentState state) throws PersistenceException {
255
        super.saveToState(state);
256
        state.set("mainMapContext", this.getMapContext());
257
        if (this.getMapOverViewContext() != null) {
258
            state.set("useMapOverview", true);
259
        } else {
260
            state.set("useMapOverview", false);
261
        }
262
        state.set("overviewMapContext", this.getMapOverViewContext());
263
        state.set("propertiesHelper", propertiesHelper);
264
    }
265

    
266
    public void loadFromState(PersistentState state) throws PersistenceException {
267
        super.loadFromState(state);
268
        this.mapContext = (MapContext) state.get("mainMapContext");
269
        if (state.getBoolean("useMapOverview")) {
270
            this.mapOverViewContext = (MapContext) state.get("overviewMapContext");
271
        } else {
272
            this.mapOverViewContext = null;
273
        }
274
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
275
    }
276

    
277
    public Object getProperty(Object key) {
278
        return this.propertiesHelper.getProperty(key);
279
    }
280

    
281
    public void setProperty(Object key, Object obj) {
282
        this.propertiesHelper.setProperty(key, obj);
283
    }
284

    
285
    public Map getExtendedProperties() {
286
        return this.propertiesHelper.getExtendedProperties();
287
    }
288

    
289
    public Iterator<FLayer> iterator() {
290
        return this.mapContext.iterator();
291
    }
292

    
293
    public Iterator<FLayer> deepiterator() {
294
        return this.mapContext.deepiterator();
295
    }
296

    
297
}