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

History | View | Annotate | Download (9.8 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
    @Override
94
    public void setName(String name) {
95
        super.setName(name);
96
        this.mapContext.getLayers().setName(name);
97
    }
98

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

    
110
    public void setMapContext(MapContext fmap) {
111
        mapContext = fmap;
112
        fmap.addErrorListener(this);
113
    }
114

    
115
    public void setMapOverViewContext(MapContext fmap) {
116
        mapOverViewContext = fmap;
117
        mapOverViewContext.setProjection(mapContext.getProjection());
118
    }
119

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

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

    
155
        System.out.println(htmlText);
156
        htmlPanel.show(htmlText);
157

    
158
        PluginServices.getMDIManager().addCentredWindow(htmlPanel);
159

    
160
    }
161

    
162
    /**
163
     * HtmlInfoToolPanel that implements IWindow
164
     *
165
     * @author azabala
166
     *
167
     */
168
    class HtmlWindow extends JPanel implements IWindow {
169

    
170
        /**
171
         *
172
         */
173
        private static final long serialVersionUID = 1151465547277478664L;
174

    
175
        private HTMLInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
176
        WindowInfo viewInfo = null;
177

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

    
189
        public void show(String htmlText) {
190
            htmlPanel.show(htmlText);
191
        }
192

    
193
        public WindowInfo getWindowInfo() {
194
            return viewInfo;
195
        }
196

    
197
        public Object getWindowProfile() {
198
            return WindowInfo.PROPERTIES_PROFILE;
199
        }
200

    
201
    }
202

    
203
    public IProjection getProjection() {
204
        return mapContext.getProjection();
205
    }
206

    
207
    public void setProjection(IProjection proj) {
208
        mapContext.setProjection(proj);
209
        mapOverViewContext.setProjection(proj);
210
    }
211

    
212
    public IProjection getOverViewProjection() {
213
        return mapOverViewContext.getProjection();
214
    }
215

    
216
    public void afterRemove() {
217
        // FIXME: Parece que no recorre correctamente el arbol de capas
218

    
219
        FLayers layers = this.getMapContext().getLayers();
220

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

    
232
    public void afterAdd() {
233
        // Do nothing
234
    }
235

    
236
    public void setBackColor(Color c) {
237
        getMapContext().getViewPort().setBackColor(c);
238
    }
239

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

    
245
    }
246

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

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

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

    
284
    public Object getProperty(Object key) {
285
        return this.propertiesHelper.getProperty(key);
286
    }
287

    
288
    public void setProperty(Object key, Object obj) {
289
        this.propertiesHelper.setProperty(key, obj);
290
    }
291

    
292
    public Map getExtendedProperties() {
293
        return this.propertiesHelper.getExtendedProperties();
294
    }
295

    
296
    public Iterator<FLayer> iterator() {
297
        return this.mapContext.iterator();
298
    }
299

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

    
304
}