Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / ProjectView.java @ 9532

History | View | Annotate | Download (11.4 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
        private boolean isModified=false;
76

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

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

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

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

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

    
139
        }
140

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

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

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

    
192
        public String getFrameName() {
193
                return PluginServices.getText(this,"Vista");
194
        }
195

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

    
205
        public IWindow getProperties() {
206
                return new ViewProperties(this);
207
        }
208

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

    
218
        private void exportToXMLLayerDependencies(FLayer layer, XMLEntity root,Project project)
219
                throws SaveException  {
220

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

    
234
                                ProjectTable pt = project.getTable((AlphanumericData) layer);
235
                                if (pt != null) {
236
                                        pt.exportToXML(root,project);
237
                                }
238
                        }
239
                }
240
        }
241

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

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

    
255

    
256

    
257

    
258
                //Cargamos las tables vinculadas:
259

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

    
272

    
273
                XMLEntity tableXML;
274

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

    
280
                        public int compare(Object o1, Object o2) {
281

    
282
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
283
                                        return -1; //o1 first
284
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
285
                                        return 1; //o1 second
286
                                }
287
                                return 0;
288
                        }
289

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

    
300
                ProjectTable table;
301
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(ProjectTableFactory.registerName);
302

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

    
313

    
314
                }
315

    
316
        }
317

    
318
        public boolean isModified() {
319
                return isModified;
320
        }
321

    
322
        public void setModified(boolean modified) {
323
                isModified=modified;
324
        }
325

    
326

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