Statistics
| Revision:

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

History | View | Annotate | Download (10.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 org.gvsig.fmap.mapcontext.MapContext;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.fmap.mapcontext.layers.FLayers;
52
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
53
import org.gvsig.tools.exception.DriverException;
54

    
55
import com.iver.andami.PluginServices;
56
import com.iver.andami.ui.mdiManager.IWindow;
57
import com.iver.cit.gvsig.project.Project;
58
import com.iver.cit.gvsig.project.documents.ProjectDocument;
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
import com.iver.utiles.XMLException;
67

    
68

    
69
/**
70
 * Clase que representa una vista del proyecto
71
 *
72
 * @author Fernando Gonz?lez Cort?s
73
 */
74
public class ProjectView extends ProjectViewBase {
75
        //public static int numViews = 0;
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
                int numViews=(ProjectDocument.NUMS.get(ProjectViewFactory.registerName)).intValue();
94

    
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
         * @throws OpenException
126
         *
127
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
128
         */
129
        public void setXMLEntity(XMLEntity xml)
130
                throws XMLException {
131
                try{
132
                        super.setXMLEntity(xml);
133
                        int currentChild=0;
134
                        int numViews = xml.getIntProperty("numViews");
135
                        ProjectDocument.NUMS.put(ProjectViewFactory.registerName,new Integer(numViews));
136

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

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

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

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

    
178
        public IWindow createWindow() {
179
                com.iver.cit.gvsig.project.documents.view.gui.View view = new com.iver.cit.gvsig.project.documents.view.gui.View();
180
                if (windowData != null) {
181
                        view.setWindowData(windowData);
182
                }
183
                view.initialize();
184
                view.setModel(this);
185
                return view;
186
        }
187

    
188
        public IWindow getProperties() {
189
                return new ViewProperties(this);
190
        }
191

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

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

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

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

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

    
233

    
234

    
235

    
236
                //Cargamos las tables vinculadas:
237

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

    
250

    
251
                XMLEntity tableXML;
252

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

    
258
                        public int compare(Object o1, Object o2) {
259

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

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

    
278
                ProjectTable table;
279
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(ProjectTableFactory.registerName);
280

    
281
                Iterator iterTablesToImport = tablesToImport.entrySet().iterator();
282
                Entry entry;
283
                // Nos recorremos las vistas a importar
284
                while (iterTablesToImport.hasNext()) {
285
                        entry = (Entry)iterTablesToImport.next();
286
                        tableName = (String)entry.getValue();
287
                        tableIndex = ((Integer)entry.getKey()).intValue();
288
                        table = (ProjectTable)tableFactory.create(project);
289
                        table.importFromXML(root,tablesRoot,tableIndex,project,removeDocumentsFromRoot);
290

    
291

    
292
                }
293

    
294
        }
295

    
296
        public String getClassName() {
297
                // TODO Auto-generated method stub
298
                return null;
299
        }
300

    
301

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