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

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

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

    
35
import org.cresques.cts.IProjection;
36

    
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.andami.ui.mdiManager.WindowInfo;
40
import org.gvsig.app.project.documents.AbstractDocument;
41
import org.gvsig.app.project.documents.DocumentManager;
42
import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel;
43
import org.gvsig.fmap.mapcontext.MapContext;
44
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
45
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
46
import org.gvsig.fmap.mapcontext.layers.CancelationException;
47
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
48
import org.gvsig.fmap.mapcontext.layers.FLayer;
49
import org.gvsig.fmap.mapcontext.layers.FLayers;
50
import org.gvsig.tools.exception.BaseException;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53

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

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

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

    
70
    protected MapContext mapOverViewContext;
71
    protected MapContext mapContext;
72

    
73
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
74

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

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

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

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

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

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

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

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

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

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

    
152
    }
153

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

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

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

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

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

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

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

    
193
    }
194

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

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

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

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

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

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

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

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

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

    
237
    }
238

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

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

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

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

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

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

    
288
}