Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / ProjectView.java @ 30011

History | View | Annotate | Download (10.7 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 org.gvsig.app.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 org.gvsig.andami.PluginServices;
50
import org.gvsig.andami.ui.mdiManager.IWindow;
51
import org.gvsig.app.project.Project;
52
import org.gvsig.app.project.documents.ProjectDocument;
53
import org.gvsig.app.project.documents.ProjectDocumentFactory;
54
import org.gvsig.app.project.documents.exceptions.OpenException;
55
import org.gvsig.app.project.documents.exceptions.SaveException;
56
import org.gvsig.app.project.documents.table.FeatureTableDocument;
57
import org.gvsig.app.project.documents.table.FeatureTableDocumentFactory;
58
import org.gvsig.app.project.documents.view.gui.ViewProperties;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.fmap.mapcontext.MapContext;
61
import org.gvsig.fmap.mapcontext.layers.FLayer;
62
import org.gvsig.fmap.mapcontext.layers.FLayers;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64
import org.gvsig.tools.exception.DriverException;
65
import org.gvsig.utils.XMLEntity;
66
import org.gvsig.utils.XMLException;
67

    
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=(ProjectDocument.NUMS.get(ProjectViewFactory.registerName)).intValue();
95

    
96
                xml.putProperty("numViews", numViews);
97
                xml.putProperty("m_selectedField", m_selectedField);
98
                xml.putProperty("m_typeLink", m_typeLink);
99
                xml.putProperty("m_extLink", m_extLink);
100
                xml.addChild(mapContext.getXMLEntity());
101

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

    
118
        /**
119
         * DOCUMENT ME!
120
         *
121
         * @param xml DOCUMENT ME!
122
         * @param p DOCUMENT ME!
123
         * @throws XMLException
124
         * @throws DriverException
125
         * @throws DriverIOException
126
         * @throws OpenException
127
         *
128
         * @see org.gvsig.app.project.documents.ProjectDocument#setXMLEntity(org.gvsig.utils.XMLEntity)
129
         */
130
        public void setXMLEntity(XMLEntity xml)
131
                throws XMLException {
132
                try{
133
                        super.setXMLEntity(xml);
134
                        int currentChild=0;
135
                        int numViews = xml.getIntProperty("numViews");
136
                        ProjectDocument.NUMS.put(ProjectViewFactory.registerName,new Integer(numViews));
137

    
138
                        if (xml.contains("m_selectedField")) {
139
                                m_selectedField = xml.getStringProperty("m_selectedField");
140
                        }
141
                        m_typeLink = xml.getIntProperty("m_typeLink");
142
                        if (xml.contains("m_extLink")) {
143
                                m_extLink = xml.getStringProperty("m_extLink");
144
                        }
145

    
146
                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
147
                        currentChild++;
148
                        if (xml.getBooleanProperty("mapOverView")) {
149
                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
150
                                currentChild++;
151
                        }
152
                        showErrors();
153
                }catch (Exception e) {
154
                        throw new OpenException(e,this.getClass().getName());
155
                }
156
        }
157

    
158
        /**
159
         * DOCUMENT ME!
160
         *
161
         * @param p DOCUMENT ME!
162
         *
163
         * @return DOCUMENT ME!
164
         * @throws XMLException
165
         * @throws DriverException
166
         * @throws DriverIOException
167
         * @throws OpenException
168
         */
169
        /*public ProjectView cloneProjectView(Project p)
170
                throws XMLException, DriverException, DriverIOException, OpenException {
171
                return (ProjectView) createFromXML(getXMLEntity(), p);
172
        }
173
*/
174

    
175
        public String getFrameName() {
176
                return PluginServices.getText(this,"Vista");
177
        }
178

    
179
        public IWindow createWindow() {
180
                org.gvsig.app.project.documents.view.gui.View view = new org.gvsig.app.project.documents.view.gui.View();
181
                if (windowData != null) {
182
                        view.setWindowData(windowData);
183
                }
184
                view.initialize();
185
                view.setModel(this);
186
                callCreateWindow(view);
187
                return view;
188
        }
189

    
190
        public IWindow getProperties() {
191
                return new ViewProperties(this);
192
        }
193

    
194
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
195
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
196
                viewsRoot.addChild(this.getXMLEntity());
197
                this.exportToXMLLayerDependencies(this.getMapContext().getLayers(),root,project);
198
                if (this.getMapOverViewContext() != null) {
199
                        this.exportToXMLLayerDependencies(this.getMapOverViewContext().getLayers(),root,project);
200
                }
201
        }
202

    
203
        private void exportToXMLLayerDependencies(FLayer layer, XMLEntity root,Project project)
204
                throws SaveException  {
205

    
206
                if (layer instanceof FLayers) {
207
                        FLayers layers = (FLayers)layer;
208
                        for (int i=0;i< layers.getLayersCount();i++) {
209
                                this.exportToXMLLayerDependencies(layers.getLayer(i),root,project);
210
                        }
211
                } else {
212
                        if (layer instanceof FLyrVect) {
213
//                                try {
214
//                                        project.exportToXMLDataSource(root,((FLyrVect)layer).getFeatureStore().getName());
215
//                                } catch (ReadException e) {
216
//                                        throw new SaveException(e,layer.getName());
217
//                                }
218

    
219
                                FeatureTableDocument pt = project.getTable((FLyrVect) layer);
220
                                if (pt != null) {
221
                                        pt.exportToXML(root,project);
222
                                }
223
                        }
224
                }
225
        }
226

    
227
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException {
228
                XMLEntity element = typeRoot.getChild(elementIndex);
229
                this.setXMLEntity(element);
230
                project.addDocument(this);
231
                if (removeDocumentsFromRoot) {
232
                        typeRoot.removeChild(elementIndex);
233
                }
234

    
235

    
236

    
237

    
238
                //Cargamos las tables vinculadas:
239

    
240
                //Recuperamos todos los nombres
241
                XMLEntity tablesRoot = project.getExportXMLTypeRootNode(root,FeatureTableDocumentFactory.registerName);
242
                int childIndex;
243
                XMLEntity child;
244
                // Lo hacemos en un map por si una vista se usa varias veces
245
                HashMap tablesName = new HashMap();
246
                Iterator iterTables = tablesRoot.findChildren("viewName",this.getName());
247
                while (iterTables.hasNext()){
248
                        child = (XMLEntity)iterTables.next();
249
                        tablesName.put(child.getStringProperty("name"),child.getStringProperty("name"));
250
                }
251

    
252

    
253
                XMLEntity tableXML;
254

    
255
                // Construimos un diccionario ordenado inversamente por el indice
256
                // del elemento (por si se van eliminando elementos al importar) y
257
                // como valor el nombre de la vista
258
                TreeMap tablesToImport = new TreeMap( new Comparator() {
259

    
260
                        public int compare(Object o1, Object o2) {
261

    
262
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
263
                                        return -1; //o1 first
264
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
265
                                        return 1; //o1 second
266
                                }
267
                                return 0;
268
                        }
269

    
270
                });
271
                Iterator iterTablesName = tablesName.keySet().iterator();
272
                int tableIndex;
273
                String tableName;
274
                while (iterTablesName.hasNext()) {
275
                        tableName = (String)iterTablesName.next();
276
                        tableIndex = tablesRoot.firstIndexOfChild("name",tableName);
277
                        tablesToImport.put(new Integer(tableIndex),tableName);
278
                }
279

    
280
                FeatureTableDocument table;
281
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(FeatureTableDocumentFactory.registerName);
282

    
283
                Iterator iterTablesToImport = tablesToImport.entrySet().iterator();
284
                Entry entry;
285
                // Nos recorremos las vistas a importar
286
                while (iterTablesToImport.hasNext()) {
287
                        entry = (Entry)iterTablesToImport.next();
288
                        tableName = (String)entry.getValue();
289
                        tableIndex = ((Integer)entry.getKey()).intValue();
290
                        table = (FeatureTableDocument)tableFactory.create(project);
291
                        try {
292
                                table.importFromXML(root,tablesRoot,tableIndex,project,removeDocumentsFromRoot);
293
                        } catch (ReadException e) {
294
                                e.printStackTrace();
295
                        }
296

    
297

    
298
                }
299

    
300
        }
301

    
302
        public String getClassName() {
303
                // TODO Auto-generated method stub
304
                return null;
305
        }
306

    
307

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