Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / BaseViewDocument.java @ 33654

History | View | Annotate | Download (10.8 KB)

1 31496 jjdelcerro
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2009 Infrastructures and Transports Department
4 31544 cordinyana
* of the Valencian Government (CIT)
5 31496 jjdelcerro
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2004-2009 IVER TI
26
*
27
*/
28
29 29598 jpiera
package org.gvsig.app.project.documents.view;
30 7392 sbayarri
31
import java.awt.Color;
32
import java.awt.Component;
33 10626 caballero
import java.awt.Dimension;
34 7476 azabala
import java.util.List;
35 7392 sbayarri
36
import javax.swing.JOptionPane;
37 10626 caballero
import javax.swing.JPanel;
38
import javax.swing.JScrollPane;
39 7392 sbayarri
40
import org.cresques.cts.IProjection;
41 29598 jpiera
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.ui.mdiManager.IWindow;
43
import org.gvsig.andami.ui.mdiManager.WindowInfo;
44
import org.gvsig.app.extension.ProjectExtension;
45
import org.gvsig.app.project.Project;
46 32880 jjdelcerro
import org.gvsig.app.project.documents.AbstractDocument;
47 31496 jjdelcerro
import org.gvsig.app.project.documents.Document;
48
import org.gvsig.app.project.documents.DocumentManager;
49
import org.gvsig.app.project.documents.table.TableDocument;
50
import org.gvsig.app.project.documents.table.TableManager;
51 29598 jpiera
import org.gvsig.app.project.documents.view.info.gui.HTMLInfoToolPanel;
52 20994 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
53
import org.gvsig.fmap.mapcontext.events.ErrorEvent;
54
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener;
55
import org.gvsig.fmap.mapcontext.layers.CancelationException;
56 28066 fdiaz
import org.gvsig.fmap.mapcontext.layers.FLayer;
57 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayers;
58 21893 vcaballero
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59 33275 jjdelcerro
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dynobject.DynStruct;
61 23214 jmvivo
import org.gvsig.tools.exception.BaseException;
62 33275 jjdelcerro
import org.gvsig.tools.persistence.PersistenceManager;
63 31496 jjdelcerro
import org.gvsig.tools.persistence.PersistentState;
64 32880 jjdelcerro
import org.gvsig.tools.persistence.exception.PersistenceException;
65 7392 sbayarri
66 31496 jjdelcerro
/**
67
 *
68
 * @author 2005-         Vicente Caballero
69
 * @author 2009-         Joaquin del Cerro
70
 *
71
 */
72 7392 sbayarri
73 31496 jjdelcerro
public abstract class BaseViewDocument extends AbstractDocument implements ErrorListener,
74
                ViewDocument {
75 7392 sbayarri
76 31496 jjdelcerro
        /**
77
         *
78
         */
79
        private static final long serialVersionUID = -2621709089720665902L;
80
81 33275 jjdelcerro
        public static final String PERSISTENCE_DEFINITION_NAME = "BaseViewDocument";
82
83 7392 sbayarri
        protected MapContext mapOverViewContext;
84
        protected MapContext mapContext;
85 31496 jjdelcerro
86 33275 jjdelcerro
//        protected int m_typeLink = LinkListener.TYPELINKIMAGE;
87
//        protected String m_extLink;
88
//        protected String m_selectedField = null;
89 7392 sbayarri
90 31496 jjdelcerro
        public BaseViewDocument() {
91
                super();
92
        }
93
94
        public BaseViewDocument(DocumentManager factory) {
95
                super(factory);
96
        }
97
98 7392 sbayarri
        /**
99 31496 jjdelcerro
         * Gets the MapContext of the main map in the view.
100 7392 sbayarri
         *
101 31496 jjdelcerro
         * @return the  main MapContext
102 7392 sbayarri
         */
103
        public MapContext getMapContext() {
104
                return mapContext;
105
        }
106
107
        /**
108 31496 jjdelcerro
         * Gets the MapContext from the locator, which is the
109 7392 sbayarri
         * small map in the left-bottom corner of the View.
110
         *
111 31496 jjdelcerro
         * @return the locator MapContext
112 7392 sbayarri
         */
113
        public MapContext getMapOverViewContext() {
114
                return mapOverViewContext;
115
        }
116
117
        public void setMapContext(MapContext fmap) {
118
                mapContext = fmap;
119
                fmap.addErrorListener(this);
120
        }
121
122
        public void setMapOverViewContext(MapContext fmap) {
123
                mapOverViewContext = fmap;
124
                mapOverViewContext.setProjection(mapContext.getProjection());
125
        }
126
127
        public void showErrors(){
128
                if (mapContext.getLayersError().size()>0){
129
                        String layersError="";
130
                        for (int i=0;i<mapContext.getLayersError().size();i++){
131
                                layersError=layersError+"\n"+(String)mapContext.getLayersError().get(i);
132
                        }
133
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
134
                                        PluginServices.getText(this,"fallo_capas")+" : \n"+
135
                                        layersError);
136
                }
137
        }
138 10165 caballero
139 7476 azabala
        /**
140
         * Reports to the user a bundle of driver exceptions produced in the
141
         * same atomic MapContext transaction
142
         */
143 33275 jjdelcerro
        @SuppressWarnings("rawtypes")
144 7476 azabala
        public void reportDriverExceptions(String introductoryText, List driverExceptions) {
145 15843 vcaballero
                HtmlWindow htmlPanel = new HtmlWindow(570, 600, PluginServices.getText(this,"driver_error"));
146 9214 fdiaz
                String htmlText = "";
147 7476 azabala
                if(introductoryText == null){
148 15843 vcaballero
                        htmlText += "<h2 text=\"#000080\">"+PluginServices.getText(this,"se_han_producido_los_siguientes_errores_durante_la_carga_de_las_capas")+"</h2>";
149 7476 azabala
                }else{
150
                        htmlText += introductoryText;
151
                }
152
                int numErrors = driverExceptions.size();
153
                for(int i = 0; i < numErrors; i++){
154 10626 caballero
                        //htmlText += "<br>\n";
155
                        BaseException exception = (BaseException) driverExceptions.get(i);
156 15843 vcaballero
                        htmlText +="<p text=\"#550080\">_________________________________________________________________________________________</p>";
157
                        htmlText += "<h3>"+PluginServices.getText(this,exception.getMessageKey())+"</h3>";
158 10626 caballero
                        htmlText += "<p>"+exception.getMessage()+"</p>";
159 15843 vcaballero
                        htmlText +="<p text=\"#550080\">_________________________________________________________________________________________</p>";
160 7476 azabala
                }
161 10626 caballero
162 7476 azabala
                System.out.println(htmlText);
163
                htmlPanel.show(htmlText);
164 10165 caballero
165 7476 azabala
                PluginServices.getMDIManager().addCentredWindow(htmlPanel);
166 10165 caballero
167 7476 azabala
        }
168 10165 caballero
169 7476 azabala
        /**
170
         * HtmlInfoToolPanel that implements IWindow
171
         * @author azabala
172
         *
173
         */
174 10626 caballero
        class HtmlWindow extends JPanel implements IWindow {
175 31496 jjdelcerro
                /**
176
                 *
177
                 */
178
                private static final long serialVersionUID = 1151465547277478664L;
179
180 10626 caballero
                private HTMLInfoToolPanel htmlPanel=new HTMLInfoToolPanel();
181 7476 azabala
                WindowInfo viewInfo = null;
182
                public HtmlWindow(int width, int height, String windowTitle){
183 15843 vcaballero
                        htmlPanel.setBackground(Color.white);
184 10626 caballero
                        JScrollPane scrollPane=new JScrollPane(htmlPanel);
185
                        scrollPane.setPreferredSize(new Dimension(width-30,height-30));
186
                        this.add(scrollPane);
187 7476 azabala
                        viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
188
                        viewInfo.setTitle(windowTitle);
189
                        viewInfo.setWidth(width);
190
                        viewInfo.setHeight(height);
191
                }
192 10165 caballero
193 10626 caballero
                public void show(String htmlText) {
194
                        htmlPanel.show(htmlText);
195
                }
196
197 7476 azabala
                public WindowInfo getWindowInfo() {
198
                        return viewInfo;
199
                }
200 26449 jmvivo
                public Object getWindowProfile() {
201
                        return WindowInfo.PROPERTIES_PROFILE;
202
                }
203 10165 caballero
204 7476 azabala
        }
205 7392 sbayarri
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 31496 jjdelcerro
        public IProjection getOverViewProjection() {
216
                return mapOverViewContext.getProjection();
217
        }
218
219 33275 jjdelcerro
//        public String getExtLink() {
220
//                return m_extLink;
221
//        }
222
//
223
//        public int getTypeLink() {
224
//                return m_typeLink;
225
//        }
226
//
227
//        /**
228
//     * Se selecciona la extensi?n para realizar cuando se quiera el link.
229
//     *
230
//     * @param s nombre del campo.
231
//     */
232
//        public void setExtLink(String s) {
233
//                m_extLink = s;
234
//        }
235
//
236
//        /**
237
//     * Se selecciona el tipo de fichero para realizar cuando se quiera el link.
238
//     *
239
//     * @param i tipo de fichero.
240
//     */
241
//        public void setTypeLink(int i) {
242
//                m_typeLink = i;
243
//        }
244 7392 sbayarri
245
        public void afterRemove() {
246 31496 jjdelcerro
                // FIXME: Parece que no recorre correctamente el arbol de capas
247
248 7392 sbayarri
                FLayers layers=this.getMapContext().getLayers();
249
250
                for (int i = layers.getLayersCount()-1; i>=0; i--){
251
                try {
252 21893 vcaballero
                        if (layers.getLayer(i) instanceof FLyrVect){
253 7392 sbayarri
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
254 31496 jjdelcerro
                    TableDocument pt = project.getTable((FLyrVect) layers.getLayer(i));
255 7392 sbayarri
256 31496 jjdelcerro
                    List<Document> tables = project.getDocuments(TableManager.TYPENAME);
257 7392 sbayarri
                    for (int j = 0; j < tables.size(); j++) {
258
                        if (tables.get(j) == pt){
259 31496 jjdelcerro
                            project.remove(tables.get(j));
260 7392 sbayarri
                            break;
261
                        }
262
                    }
263
264
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
265
                }
266
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
267
                        } catch (CancelationException e1) {
268
                            e1.printStackTrace();
269
                    }
270
            }
271 31284 cordinyana
                getMapContext().dispose();
272
                getMapOverViewContext().dispose();
273 7392 sbayarri
        }
274
275
        public void afterAdd() {
276 31496 jjdelcerro
                // Do nothing
277 7392 sbayarri
        }
278
279
280
        public void setBackColor(Color c) {
281
                getMapContext().getViewPort().setBackColor(c);
282
        }
283
284 33275 jjdelcerro
//        /**
285
//         * Se selecciona el nombre del campo para realizar cuando se quiera el
286
//         * link.
287
//         *
288
//         * @param s nombre del campo.
289
//         */
290
//        public void setSelectedField(String s) {
291
//                m_selectedField = s;
292
//        }
293
//
294
//        public String getSelectedField() {
295
//                return m_selectedField;
296
//        }
297 7392 sbayarri
298
        public void errorThrown(ErrorEvent e) {
299
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
300
                                        PluginServices.getText(this,"fallo_capas")+" : \n"+
301
                                        e.getMessage());
302
303
        }
304
305 28066 fdiaz
        public boolean isLocked() {
306
                if(super.isLocked()) return true;
307
                FLayers layers = getMapContext().getLayers();
308
                for(int i=0; i<layers.getLayersCount(); i++){
309
                        FLayer layer = layers.getLayer(i);
310
                        if (layer.isEditing()){
311
                                return true;
312
                        }
313
                }
314
                return false;
315
        }
316 31496 jjdelcerro
317
        public void saveToState(PersistentState state) throws PersistenceException {
318
                super.saveToState(state);
319 33275 jjdelcerro
//                state.set("linkType", this.getTypeLink());
320
//                state.set("linkExtension",this.getExtLink());
321
//                state.set("selectedField", this.getSelectedField());
322 31496 jjdelcerro
                state.set("mainMapContext", this.getMapContext());
323
                if( this.getMapOverViewContext() != null ) {
324
                        state.set("useMapOverview", true);
325
                } else {
326
                        state.set("useMapOverview", false);
327
                }
328
                state.set("overviewMapContext", this.getMapOverViewContext());
329
        }
330
331
        public void loadFromState(PersistentState state) throws PersistenceException {
332
                super.loadFromState(state);
333 33275 jjdelcerro
//                this.setTypeLink( state.getInt("linkType"));
334
//                this.setExtLink(state.getString("linkExtension"));
335
//                this.setSelectedField(state.getString("selectedField"));
336 31496 jjdelcerro
337
                this.mapContext = (MapContext) state.get("mainMapContext");
338 33275 jjdelcerro
                if( state.getBoolean("useMapOverview") ) {
339
                        this.mapOverViewContext = (MapContext) state.get("overviewMapContext");
340
                } else {
341
                        this.mapOverViewContext = null;
342
                }
343 31496 jjdelcerro
        }
344 33275 jjdelcerro
345
        public static void registerPersistent() {
346
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
347
                DynStruct definition = manager.addDefinition(
348
                                BaseViewDocument.class,
349
                                PERSISTENCE_DEFINITION_NAME,
350
                                "Base view document persistence definition",
351
                                null,
352
                                null
353
                );
354
                definition.extend(manager.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
355 31496 jjdelcerro
356 33275 jjdelcerro
                definition.addDynFieldBoolean("useMapOverview").setMandatory(true);
357
                definition.addDynFieldObject("mainMapContext").setClassOfValue(MapContext.class).setMandatory(true);
358
                definition.addDynFieldObject("overviewMapContext").setClassOfValue(MapContext.class).setMandatory(false);
359
        }
360
361 7392 sbayarri
}