Statistics
| Revision:

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

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.utils.XMLEntity;
65
import org.gvsig.utils.XMLException;
66

    
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 DriverIOException
124
         * @throws OpenException
125
         *
126
         * @see org.gvsig.app.project.documents.ProjectDocument#setXMLEntity(org.gvsig.utils.XMLEntity)
127
         */
128
        public void setXMLEntity(XMLEntity xml)
129
                throws XMLException {
130
//                try{
131
//                        super.setXMLEntity(xml);
132
//                        int currentChild=0;
133
//                        int numViews = xml.getIntProperty("numViews");
134
//                        ProjectDocument.NUMS.put(ProjectViewFactory.registerName,new Integer(numViews));
135
//
136
//                        if (xml.contains("m_selectedField")) {
137
//                                m_selectedField = xml.getStringProperty("m_selectedField");
138
//                        }
139
//                        m_typeLink = xml.getIntProperty("m_typeLink");
140
//                        if (xml.contains("m_extLink")) {
141
//                                m_extLink = xml.getStringProperty("m_extLink");
142
//                        }
143
//
144
//                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
145
//                        currentChild++;
146
//                        if (xml.getBooleanProperty("mapOverView")) {
147
//                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
148
//                                currentChild++;
149
//                        }
150
//                        showErrors();
151
//                }catch (Exception e) {
152
//                        throw new OpenException(e,this.getClass().getName());
153
//                }
154
        }
155

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

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

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

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

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

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

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

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

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

    
232

    
233

    
234

    
235
                //Cargamos las tables vinculadas:
236

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

    
249

    
250
                XMLEntity tableXML;
251

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

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

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

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

    
277
                FeatureTableDocument table;
278
                ProjectDocumentFactory tableFactory = project.getProjectDocumentFactory(FeatureTableDocumentFactory.registerName);
279

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

    
294

    
295
                }
296

    
297
        }
298

    
299
        public String getClassName() {
300
                // TODO Auto-generated method stub
301
                return null;
302
        }
303

    
304

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