Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / ProjectView.java @ 8767

History | View | Annotate | Download (11.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents.view;
42

    
43
import java.util.Comparator;
44
import java.util.HashMap;
45
import java.util.Iterator;
46
import java.util.TreeMap;
47
import java.util.Map.Entry;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.ui.mdiManager.IWindow;
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.MapContext;
53
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
54
import com.iver.cit.gvsig.fmap.layers.FLayer;
55
import com.iver.cit.gvsig.fmap.layers.FLayers;
56
import com.iver.cit.gvsig.fmap.layers.XMLException;
57
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
58
import com.iver.cit.gvsig.project.Project;
59
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
60
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
61
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
62
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
63
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
64
import com.iver.cit.gvsig.project.documents.view.gui.ViewProperties;
65
import com.iver.utiles.XMLEntity;
66

    
67

    
68
/**
69
 * Clase que representa una vista del proyecto
70
 *
71
 * @author Fernando Gonz?lez Cort?s
72
 */
73
public class ProjectView extends ProjectViewBase {
74
        public static int numViews = 0;
75

    
76
        //public static int METROS = 0;
77
        //public static int KILOMETROS = 1;
78
        //public static int[] unidades = new int[] { METROS, KILOMETROS };
79
        ///private Color backgroundColor = new Color(255, 255, 255);
80

    
81
        /**
82
         * DOCUMENT ME!
83
         *
84
         * @return DOCUMENT ME!
85
         * @throws XMLException
86
         * @throws SaveException
87
         */
88
        public XMLEntity getXMLEntity() throws SaveException {
89
                XMLEntity xml = super.getXMLEntity();
90
                //xml.putProperty("nameClass", this.getClass().getName());
91
                try{
92
                xml.putProperty("numViews", numViews);
93
                xml.putProperty("m_selectedField", m_selectedField);
94
                xml.putProperty("m_typeLink", m_typeLink);
95
                xml.putProperty("m_extLink", m_extLink);
96
                xml.addChild(mapContext.getXMLEntity());
97

    
98
                if (mapOverViewContext != null) {
99
                        if (mapOverViewContext.getViewPort() != null) {
100
                                xml.putProperty("mapOverView", true);
101
                                xml.addChild(mapOverViewContext.getXMLEntity());
102
                        } else {
103
                                xml.putProperty("mapOverView", false);
104
                        }
105
                } else {
106
                        xml.putProperty("mapOverView", false);
107
                }
108
                }catch (Exception e) {
109
                        throw new SaveException(e,this.getClass().getName());
110
                }
111
                return xml;
112
        }
113

    
114
        /**
115
         * DOCUMENT ME!
116
         *
117
         * @param xml DOCUMENT ME!
118
         * @param p DOCUMENT ME!
119
         * @throws XMLException
120
         * @throws DriverException
121
         * @throws DriverIOException
122
         *
123
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
124
         */
125
        public void setXMLEntity03(XMLEntity xml)
126
                throws XMLException, DriverException, DriverIOException {
127
                super.setXMLEntity03(xml);
128
                numViews = xml.getIntProperty("numViews");
129
                m_selectedField = xml.getStringProperty("m_selectedField");
130
                m_typeLink = xml.getIntProperty("m_typeLink");
131
                m_extLink = xml.getStringProperty("m_extLink");
132
                setMapContext(MapContext.createFromXML03(xml.getChild(0)));
133

    
134
                if (xml.getBooleanProperty("mapOverView")) {
135
                        setMapOverViewContext(MapContext.createFromXML03(xml.getChild(1)));
136
                }
137

    
138
        }
139

    
140
        /**
141
         * DOCUMENT ME!
142
         *
143
         * @param xml DOCUMENT ME!
144
         * @param p DOCUMENT ME!
145
         * @throws XMLException
146
         * @throws DriverException
147
         * @throws DriverIOException
148
         * @throws OpenException
149
         *
150
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
151
         */
152
        public void setXMLEntity(XMLEntity xml)
153
                throws XMLException, DriverException, DriverIOException, OpenException {
154
                try{
155
                        super.setXMLEntity(xml);
156
                        int currentChild=0;
157
                        numViews = xml.getIntProperty("numViews");
158
                        m_selectedField = xml.getStringProperty("m_selectedField");
159
                        m_typeLink = xml.getIntProperty("m_typeLink");
160
                        m_extLink = xml.getStringProperty("m_extLink");
161

    
162
                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
163
                        currentChild++;
164
                        if (xml.getBooleanProperty("mapOverView")) {
165
                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
166
                                currentChild++;
167
                        }
168
                        showErrors();
169
                }catch (Exception e) {
170
                        throw new OpenException(e,this.getClass().getName());
171
                }
172
        }
173

    
174
        /**
175
         * DOCUMENT ME!
176
         *
177
         * @param p DOCUMENT ME!
178
         *
179
         * @return DOCUMENT ME!
180
         * @throws XMLException
181
         * @throws DriverException
182
         * @throws DriverIOException
183
         * @throws OpenException
184
         */
185
        /*public ProjectView cloneProjectView(Project p)
186
                throws XMLException, DriverException, DriverIOException, OpenException {
187
                return (ProjectView) createFromXML(getXMLEntity(), p);
188
        }
189
*/
190

    
191
        public String getFrameName() {
192
                return PluginServices.getText(this,"Vista");
193
        }
194
        public IWindow createWindow() {
195
                com.iver.cit.gvsig.project.documents.view.gui.View view = new com.iver.cit.gvsig.project.documents.view.gui.View();
196
                view.setModel(this);
197
                return view;
198
        }
199

    
200
        public IWindow getProperties() {
201
                return new ViewProperties(this);
202
        }
203

    
204
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
205
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);                
206
                viewsRoot.addChild(this.getXMLEntity());
207
                this.exportToXMLLayerDependencies(this.getMapContext().getLayers(),root,project);
208
                if (this.getMapOverViewContext() != null) {
209
                        this.exportToXMLLayerDependencies(this.getMapOverViewContext().getLayers(),root,project);
210
                }
211
        }
212
        
213
        private void exportToXMLLayerDependencies(FLayer layer, XMLEntity root,Project project)
214
                throws SaveException  {
215
                
216
                if (layer instanceof FLayers) {
217
                        FLayers layers = (FLayers)layer;
218
                        for (int i=0;i< layers.getLayersCount();i++) {
219
                                this.exportToXMLLayerDependencies(layers.getLayer(i),root,project);
220
                        }
221
                } else {
222
                        if (layer instanceof AlphanumericData) {
223
                                try {
224
                                        project.exportToXMLDataSource(root,((AlphanumericData)layer).getRecordset().getName());
225
                                } catch (DriverException e) {
226
                                        throw new SaveException(e,layer.getName());
227
                                }
228
                                
229
                                ProjectTable pt = project.getTable((AlphanumericData) layer);
230
                                if (pt != null) {
231
                                        pt.exportToXML(root,project);
232
                                }
233
                        }
234
                }
235
        }
236

    
237
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException, DriverException, OpenException {
238
                XMLEntity element = typeRoot.getChild(elementIndex);
239
                
240
                try {
241
                        this.setXMLEntity(element);
242
                } catch (DriverIOException e) {
243
                        throw new OpenException(e,"Open error");
244
                }                
245
                project.addDocument(this);
246
                if (removeDocumentsFromRoot) {
247
                        typeRoot.removeChild(elementIndex);
248
                }
249

    
250
                
251
                
252
                
253
                //Cargamos las tables vinculadas:
254
                                
255
                //Recuperamos todos los nombres
256
                XMLEntity tablesRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
257
                int childIndex;
258
                XMLEntity child;
259
                // Lo hacemos en un map por si una vista se usa varias veces
260
                HashMap tablesName = new HashMap();
261
                Iterator iterTables = tablesRoot.findChildren("viewName",this.getName());
262
                while (iterTables.hasNext()){
263
                        child = (XMLEntity)iterTables.next();
264
                        tablesName.put(child.getStringProperty("name"),child.getStringProperty("name"));                        
265
                }
266

    
267
                
268
                XMLEntity tableXML;
269
                
270
                // Construimos un diccionario ordenado inversamente por el indice
271
                // del elemento (por si se van eliminando elementos al importar) y
272
                // como valor el nombre de la vista
273
                TreeMap tablesToImport = new TreeMap( new Comparator() {
274

    
275
                        public int compare(Object o1, Object o2) {
276
                                
277
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
278
                                        return -1; //o1 first
279
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
280
                                        return 1; //o1 second
281
                                }
282
                                return 0;
283
                        }
284
                        
285
                });
286
                Iterator iterTablesName = tablesName.keySet().iterator();
287
                int tableIndex;
288
                String tableName;
289
                while (iterTablesName.hasNext()) {
290
                        tableName = (String)iterTablesName.next();
291
                        tableIndex = tablesRoot.firstIndexOfChild("name",tableName);
292
                        tablesToImport.put(new Integer(tableIndex),tableName);
293
                }
294
                
295
                ProjectTable table;
296
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(ProjectTableFactory.registerName);
297
                                
298
                Iterator iterTablesToImport = tablesToImport.entrySet().iterator();
299
                Entry entry;
300
                // Nos recorremos las vistas a importar
301
                while (iterTablesToImport.hasNext()) {
302
                        entry = (Entry)iterTablesToImport.next();
303
                        tableName = (String)entry.getValue();
304
                        tableIndex = ((Integer)entry.getKey()).intValue();
305
                        table = (ProjectTable)tableFactory.create(project);
306
                        table.importFromXML(root,tablesRoot,tableIndex,project,removeDocumentsFromRoot);                        
307

    
308
                        
309
                }
310
                
311
        }
312

    
313

    
314
//        public int computeSignature() {
315
//                int result = 17;
316
//
317
//                Class clazz = getClass();
318
//                Field[] fields = clazz.getDeclaredFields();
319
//                for (int i = 0; i < fields.length; i++) {
320
//                        try {
321
//                                String type = fields[i].getType().getName();
322
//                                if (type.equals("boolean")) {
323
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
324
//                                } else if (type.equals("java.lang.String")) {
325
//                                        Object v = fields[i].get(this);
326
//                                        if (v == null) {
327
//                                                result += 37;
328
//                                                continue;
329
//                                        }
330
//                                        char[] chars = ((String) v).toCharArray();
331
//                                        for (int j = 0; j < chars.length; j++) {
332
//                                                result += 37 + (int) chars[i];
333
//                                        }
334
//                                } else if (type.equals("byte")) {
335
//                                        result += 37 + (int) fields[i].getByte(this);
336
//                                } else if (type.equals("char")) {
337
//                                        result += 37 + (int) fields[i].getChar(this);
338
//                                } else if (type.equals("short")) {
339
//                                        result += 37 + (int) fields[i].getShort(this);
340
//                                } else if (type.equals("int")) {
341
//                                        result += 37 + fields[i].getInt(this);
342
//                                } else if (type.equals("long")) {
343
//                                        long f = fields[i].getLong(this) ;
344
//                                        result += 37 + (f ^ (f >>> 32));
345
//                                } else if (type.equals("float")) {
346
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
347
//                                } else if (type.equals("double")) {
348
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
349
//                                        result += 37 + (f ^ (f >>> 32));
350
//                                } else {
351
//                                        Object obj = fields[i].get(this);
352
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
353
//                                }
354
//                        } catch (Exception e) { e.printStackTrace(); }
355
//
356
//                }
357
//                return result;
358
//        }
359
}