Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / ProjectView.java @ 10666

History | View | Annotate | Download (11.6 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.ProjectDocument;
60
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
61
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
62
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
63
import com.iver.cit.gvsig.project.documents.gui.WindowData;
64
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
65
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
66
import com.iver.cit.gvsig.project.documents.view.gui.ViewProperties;
67
import com.iver.utiles.XMLEntity;
68

    
69

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

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

    
83
        /**
84
         * DOCUMENT ME!
85
         *
86
         * @return DOCUMENT ME!
87
         * @throws XMLException
88
         * @throws SaveException
89
         */
90
        public XMLEntity getXMLEntity() throws SaveException {
91
                XMLEntity xml = super.getXMLEntity();
92
                //xml.putProperty("nameClass", this.getClass().getName());
93
                try{
94
                int numViews=((Integer)ProjectDocument.NUMS.get(ProjectViewFactory.registerName)).intValue();
95
                xml.putProperty("numViews", numViews);
96
                xml.putProperty("m_selectedField", m_selectedField);
97
                xml.putProperty("m_typeLink", m_typeLink);
98
                xml.putProperty("m_extLink", m_extLink);
99
                xml.addChild(mapContext.getXMLEntity());
100

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

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

    
138
                if (xml.getBooleanProperty("mapOverView")) {
139
                        setMapOverViewContext(MapContext.createFromXML03(xml.getChild(1)));
140
                }
141

    
142
        }
143

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

    
167
                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
168
                        currentChild++;
169
                        if (xml.getBooleanProperty("mapOverView")) {
170
                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
171
                                currentChild++;
172
                        }
173
                        showErrors();
174
                }catch (Exception e) {
175
                        throw new OpenException(e,this.getClass().getName());
176
                }
177
        }
178

    
179
        /**
180
         * DOCUMENT ME!
181
         *
182
         * @param p DOCUMENT ME!
183
         *
184
         * @return DOCUMENT ME!
185
         * @throws XMLException
186
         * @throws DriverException
187
         * @throws DriverIOException
188
         * @throws OpenException
189
         */
190
        /*public ProjectView cloneProjectView(Project p)
191
                throws XMLException, DriverException, DriverIOException, OpenException {
192
                return (ProjectView) createFromXML(getXMLEntity(), p);
193
        }
194
*/
195

    
196
        public String getFrameName() {
197
                return PluginServices.getText(this,"Vista");
198
        }
199

    
200
        public IWindow createWindow() {
201
                com.iver.cit.gvsig.project.documents.view.gui.View view = new com.iver.cit.gvsig.project.documents.view.gui.View();
202
                if (windowData != null)
203
                        view.setWindowData(windowData);
204
                view.initialize();
205
                view.setModel(this);
206
                return view;
207
        }
208

    
209
        public IWindow getProperties() {
210
                return new ViewProperties(this);
211
        }
212

    
213
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
214
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
215
                viewsRoot.addChild(this.getXMLEntity());
216
                this.exportToXMLLayerDependencies(this.getMapContext().getLayers(),root,project);
217
                if (this.getMapOverViewContext() != null) {
218
                        this.exportToXMLLayerDependencies(this.getMapOverViewContext().getLayers(),root,project);
219
                }
220
        }
221

    
222
        private void exportToXMLLayerDependencies(FLayer layer, XMLEntity root,Project project)
223
                throws SaveException  {
224

    
225
                if (layer instanceof FLayers) {
226
                        FLayers layers = (FLayers)layer;
227
                        for (int i=0;i< layers.getLayersCount();i++) {
228
                                this.exportToXMLLayerDependencies(layers.getLayer(i),root,project);
229
                        }
230
                } else {
231
                        if (layer instanceof AlphanumericData) {
232
                                try {
233
                                        project.exportToXMLDataSource(root,((AlphanumericData)layer).getRecordset().getName());
234
                                } catch (DriverException e) {
235
                                        throw new SaveException(e,layer.getName());
236
                                }
237

    
238
                                ProjectTable pt = project.getTable((AlphanumericData) layer);
239
                                if (pt != null) {
240
                                        pt.exportToXML(root,project);
241
                                }
242
                        }
243
                }
244
        }
245

    
246
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException, DriverException, OpenException {
247
                XMLEntity element = typeRoot.getChild(elementIndex);
248

    
249
                try {
250
                        this.setXMLEntity(element);
251
                } catch (DriverIOException e) {
252
                        throw new OpenException(e,"Open error");
253
                }
254
                project.addDocument(this);
255
                if (removeDocumentsFromRoot) {
256
                        typeRoot.removeChild(elementIndex);
257
                }
258

    
259

    
260

    
261

    
262
                //Cargamos las tables vinculadas:
263

    
264
                //Recuperamos todos los nombres
265
                XMLEntity tablesRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
266
                int childIndex;
267
                XMLEntity child;
268
                // Lo hacemos en un map por si una vista se usa varias veces
269
                HashMap tablesName = new HashMap();
270
                Iterator iterTables = tablesRoot.findChildren("viewName",this.getName());
271
                while (iterTables.hasNext()){
272
                        child = (XMLEntity)iterTables.next();
273
                        tablesName.put(child.getStringProperty("name"),child.getStringProperty("name"));
274
                }
275

    
276

    
277
                XMLEntity tableXML;
278

    
279
                // Construimos un diccionario ordenado inversamente por el indice
280
                // del elemento (por si se van eliminando elementos al importar) y
281
                // como valor el nombre de la vista
282
                TreeMap tablesToImport = new TreeMap( new Comparator() {
283

    
284
                        public int compare(Object o1, Object o2) {
285

    
286
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
287
                                        return -1; //o1 first
288
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
289
                                        return 1; //o1 second
290
                                }
291
                                return 0;
292
                        }
293

    
294
                });
295
                Iterator iterTablesName = tablesName.keySet().iterator();
296
                int tableIndex;
297
                String tableName;
298
                while (iterTablesName.hasNext()) {
299
                        tableName = (String)iterTablesName.next();
300
                        tableIndex = tablesRoot.firstIndexOfChild("name",tableName);
301
                        tablesToImport.put(new Integer(tableIndex),tableName);
302
                }
303

    
304
                ProjectTable table;
305
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(ProjectTableFactory.registerName);
306

    
307
                Iterator iterTablesToImport = tablesToImport.entrySet().iterator();
308
                Entry entry;
309
                // Nos recorremos las vistas a importar
310
                while (iterTablesToImport.hasNext()) {
311
                        entry = (Entry)iterTablesToImport.next();
312
                        tableName = (String)entry.getValue();
313
                        tableIndex = ((Integer)entry.getKey()).intValue();
314
                        table = (ProjectTable)tableFactory.create(project);
315
                        table.importFromXML(root,tablesRoot,tableIndex,project,removeDocumentsFromRoot);
316

    
317

    
318
                }
319

    
320
        }
321

    
322

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