Statistics
| Revision:

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

History | View | Annotate | Download (12.9 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.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.hardcode.gdbms.engine.data.driver.DriverException;
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.ui.mdiManager.IWindow;
53
import com.iver.cit.gvsig.fmap.MapContext;
54
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
55
import com.iver.cit.gvsig.fmap.layers.FLayer;
56
import com.iver.cit.gvsig.fmap.layers.FLayers;
57
import com.iver.cit.gvsig.fmap.layers.LayersIterator;
58
import com.iver.cit.gvsig.fmap.layers.XMLException;
59
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
60
import com.iver.cit.gvsig.project.Project;
61
import com.iver.cit.gvsig.project.documents.ProjectDocument;
62
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
63
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
64
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
65
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
66
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
67
import com.iver.cit.gvsig.project.documents.view.gui.ViewProperties;
68
import com.iver.utiles.XMLEntity;
69

    
70

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

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

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

    
99
                xml.putProperty("numViews", numViews);
100
                
101
                // remove old hyperlink persistence
102
//                xml.putProperty("m_selectedField", m_selectedField);
103
//                xml.putProperty("m_typeLink", m_typeLink);
104
//                xml.putProperty("m_extLink", m_extLink);
105
                xml.addChild(mapContext.getXMLEntity());
106

    
107
                if (mapOverViewContext != null) {
108
                        if (mapOverViewContext.getViewPort() != null) {
109
                                xml.putProperty("mapOverView", true);
110
                                xml.addChild(mapOverViewContext.getXMLEntity());
111
                        } else {
112
                                xml.putProperty("mapOverView", false);
113
                        }
114
                } else {
115
                        xml.putProperty("mapOverView", false);
116
                }
117
                }catch (Exception e) {
118
                        throw new SaveException(e,this.getClass().getName());
119
                }
120
                return xml;
121
        }
122

    
123
        /**
124
         * DOCUMENT ME!
125
         *
126
         * @param xml DOCUMENT ME!
127
         * @param p DOCUMENT ME!
128
         * @throws XMLException
129
         * @throws DriverException
130
         * @throws DriverIOException
131
         *
132
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
133
         */
134
        public void setXMLEntity03(XMLEntity xml)
135
                throws XMLException, ReadDriverException {
136
                super.setXMLEntity03(xml);
137
                int numViews = xml.getIntProperty("numViews");
138
                ProjectDocument.NUMS.put(ProjectViewFactory.registerName,new Integer(numViews));
139

    
140
                m_selectedField = xml.getStringProperty("m_selectedField");
141
                m_typeLink = xml.getIntProperty("m_typeLink");
142
                m_extLink = xml.getStringProperty("m_extLink");
143
                setMapContext(MapContext.createFromXML03(xml.getChild(0)));
144

    
145
                if (xml.getBooleanProperty("mapOverView")) {
146
                        setMapOverViewContext(MapContext.createFromXML03(xml.getChild(1)));
147
                }
148

    
149
        }
150

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

    
171
                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
172
                        currentChild++;
173
                        if (xml.getBooleanProperty("mapOverView")) {
174
                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
175
                                currentChild++;
176
                        }
177
                        
178
                        // legacy hyperlink stuff
179
                        if (xml.contains("m_selectedField")) {
180
                                String selectedField, extension=null;
181
                                int type=-1;
182
                                selectedField = xml.getStringProperty("m_selectedField");
183
                                if (xml.contains("m_typeLink")) {
184
                                        type = xml.getIntProperty("m_typeLink");                                        
185
                                }
186
                                if (xml.contains("m_extLink")) {
187
                                        extension = xml.getStringProperty("m_extLink");
188
                                }
189
                                applyHyperlinkToLayers(selectedField, type, extension);
190
                        }
191
                        
192
                        
193
                        
194
                        showErrors();
195
                }catch (Exception e) {
196
                        throw new OpenException(e,this.getClass().getName());
197
                }
198
        }
199

    
200
        /**
201
         * <p>Legacy code to keep compatibility with old 1.1.x hyperlink.</p>
202
         * 
203
         * @param selectedField
204
         * @param type
205
         * @param extension
206
         */
207
        private void applyHyperlinkToLayers(String selectedField, int type, String extension) {
208
                final String LAYERPROPERTYNAME = "org.gvsig.hyperlink.config";
209
                
210
                LayersIterator iterator = new LayersIterator(getMapContext().getLayers());
211
                while (iterator.hasNext()) {
212
                        FLayer layer = iterator.nextLayer();
213
                        // don't apply 1.1.x compatibility if a property from 1.9 alpha hyperlink is found
214
                        String auxFieldName = (String) layer.getProperty("legacy.hyperlink.selectedField");
215
                        
216
                        if (auxFieldName == null && selectedField!=null) {
217
                                layer.setProperty("legacy.hyperlink.selectedField", selectedField);
218
                                layer.setProperty("legacy.hyperlink.type", new Integer(type));
219
                                if (extension!=null) {
220
                                        layer.setProperty("legacy.hyperlink.extension", extension);
221
                                }
222
                        }
223
                }
224
                
225
        }
226

    
227
        /**
228
         * DOCUMENT ME!
229
         *
230
         * @param p DOCUMENT ME!
231
         *
232
         * @return DOCUMENT ME!
233
         * @throws XMLException
234
         * @throws DriverException
235
         * @throws DriverIOException
236
         * @throws OpenException
237
         */
238
        /*public ProjectView cloneProjectView(Project p)
239
                throws XMLException, DriverException, DriverIOException, OpenException {
240
                return (ProjectView) createFromXML(getXMLEntity(), p);
241
        }
242
*/
243

    
244
        public String getFrameName() {
245
                return PluginServices.getText(this,"Vista");
246
        }
247

    
248
        public IWindow createWindow() {
249
                com.iver.cit.gvsig.project.documents.view.gui.View view = new com.iver.cit.gvsig.project.documents.view.gui.View();
250
                if (windowData != null)
251
                        view.setWindowData(windowData);
252
                view.initialize();
253
                view.setModel(this);
254
                callCreateWindow(view);
255
                return view;
256
        }
257

    
258
        public IWindow getProperties() {
259
                return new ViewProperties(this);
260
        }
261

    
262
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
263
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
264
                viewsRoot.addChild(this.getXMLEntity());
265
                this.exportToXMLLayerDependencies(this.getMapContext().getLayers(),root,project);
266
                if (this.getMapOverViewContext() != null) {
267
                        this.exportToXMLLayerDependencies(this.getMapOverViewContext().getLayers(),root,project);
268
                }
269
        }
270

    
271
        private void exportToXMLLayerDependencies(FLayer layer, XMLEntity root,Project project)
272
                throws SaveException  {
273

    
274
                if (layer instanceof FLayers) {
275
                        FLayers layers = (FLayers)layer;
276
                        for (int i=0;i< layers.getLayersCount();i++) {
277
                                this.exportToXMLLayerDependencies(layers.getLayer(i),root,project);
278
                        }
279
                } else {
280
                        if (layer instanceof AlphanumericData) {
281
                                try {
282
                                        project.exportToXMLDataSource(root,((AlphanumericData)layer).getRecordset().getName());
283
                                } catch (ReadDriverException e) {
284
                                        throw new SaveException(e,layer.getName());
285
                                }
286

    
287
                                ProjectTable pt = project.getTable((AlphanumericData) layer);
288
                                if (pt != null) {
289
                                        pt.exportToXML(root,project);
290
                                }
291
                        }
292
                }
293
        }
294

    
295
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException, ReadDriverException, OpenException {
296
                XMLEntity element = typeRoot.getChild(elementIndex);
297
                this.setXMLEntity(element);
298
                project.addDocument(this);
299
                if (removeDocumentsFromRoot) {
300
                        typeRoot.removeChild(elementIndex);
301
                }
302

    
303

    
304

    
305

    
306
                //Cargamos las tables vinculadas:
307

    
308
                //Recuperamos todos los nombres
309
                XMLEntity tablesRoot = project.getExportXMLTypeRootNode(root,ProjectTableFactory.registerName);
310
                int childIndex;
311
                XMLEntity child;
312
                // Lo hacemos en un map por si una vista se usa varias veces
313
                HashMap tablesName = new HashMap();
314
                Iterator iterTables = tablesRoot.findChildren("viewName",this.getName());
315
                while (iterTables.hasNext()){
316
                        child = (XMLEntity)iterTables.next();
317
                        tablesName.put(child.getStringProperty("name"),child.getStringProperty("name"));
318
                }
319

    
320

    
321
                XMLEntity tableXML;
322

    
323
                // Construimos un diccionario ordenado inversamente por el indice
324
                // del elemento (por si se van eliminando elementos al importar) y
325
                // como valor el nombre de la vista
326
                TreeMap tablesToImport = new TreeMap( new Comparator() {
327

    
328
                        public int compare(Object o1, Object o2) {
329

    
330
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
331
                                        return -1; //o1 first
332
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
333
                                        return 1; //o1 second
334
                                }
335
                                return 0;
336
                        }
337

    
338
                });
339
                Iterator iterTablesName = tablesName.keySet().iterator();
340
                int tableIndex;
341
                String tableName;
342
                while (iterTablesName.hasNext()) {
343
                        tableName = (String)iterTablesName.next();
344
                        tableIndex = tablesRoot.firstIndexOfChild("name",tableName);
345
                        tablesToImport.put(new Integer(tableIndex),tableName);
346
                }
347

    
348
                ProjectTable table;
349
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(ProjectTableFactory.registerName);
350

    
351
                Iterator iterTablesToImport = tablesToImport.entrySet().iterator();
352
                Entry entry;
353
                // Nos recorremos las vistas a importar
354
                while (iterTablesToImport.hasNext()) {
355
                        entry = (Entry)iterTablesToImport.next();
356
                        tableName = (String)entry.getValue();
357
                        tableIndex = ((Integer)entry.getKey()).intValue();
358
                        table = (ProjectTable)tableFactory.create(project);
359
                        table.importFromXML(root,tablesRoot,tableIndex,project,removeDocumentsFromRoot);
360

    
361

    
362
                }
363

    
364
        }
365

    
366
//        public int computeSignature() {
367
//                int result = 17;
368
//
369
//                Class clazz = getClass();
370
//                Field[] fields = clazz.getDeclaredFields();
371
//                for (int i = 0; i < fields.length; i++) {
372
//                        try {
373
//                                String type = fields[i].getType().getName();
374
//                                if (type.equals("boolean")) {
375
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
376
//                                } else if (type.equals("java.lang.String")) {
377
//                                        Object v = fields[i].get(this);
378
//                                        if (v == null) {
379
//                                                result += 37;
380
//                                                continue;
381
//                                        }
382
//                                        char[] chars = ((String) v).toCharArray();
383
//                                        for (int j = 0; j < chars.length; j++) {
384
//                                                result += 37 + (int) chars[i];
385
//                                        }
386
//                                } else if (type.equals("byte")) {
387
//                                        result += 37 + (int) fields[i].getByte(this);
388
//                                } else if (type.equals("char")) {
389
//                                        result += 37 + (int) fields[i].getChar(this);
390
//                                } else if (type.equals("short")) {
391
//                                        result += 37 + (int) fields[i].getShort(this);
392
//                                } else if (type.equals("int")) {
393
//                                        result += 37 + fields[i].getInt(this);
394
//                                } else if (type.equals("long")) {
395
//                                        long f = fields[i].getLong(this) ;
396
//                                        result += 37 + (f ^ (f >>> 32));
397
//                                } else if (type.equals("float")) {
398
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
399
//                                } else if (type.equals("double")) {
400
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
401
//                                        result += 37 + (f ^ (f >>> 32));
402
//                                } else {
403
//                                        Object obj = fields[i].get(this);
404
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
405
//                                }
406
//                        } catch (Exception e) { e.printStackTrace(); }
407
//
408
//                }
409
//                return result;
410
//        }
411
}