Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / ProjectMap.java @ 9392

History | View | Annotate | Download (10.1 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.layout;
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.drivers.DriverIOException;
53
import com.iver.cit.gvsig.fmap.layers.XMLException;
54
import com.iver.cit.gvsig.project.Project;
55
import com.iver.cit.gvsig.project.documents.ProjectDocument;
56
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
57
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
58
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
59
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
60
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
61
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
62
import com.iver.cit.gvsig.project.documents.layout.gui.MapProperties;
63
import com.iver.cit.gvsig.project.documents.view.ProjectView;
64
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
65
import com.iver.utiles.XMLEntity;
66

    
67

    
68
/**
69
 * Modelo del Layout.
70
 *
71
 * @author Fernando Gonz?lez Cort?s
72
 */
73
public class ProjectMap extends ProjectDocument {
74
        public static int numMaps = 0;
75
        private Layout model;
76

    
77
        /**
78
         * @see com.iver.cit.gvsig.project.documents.layout.ProjectMap#getModel()
79
         */
80
        public Layout getModel() {
81
                return model;
82
        }
83

    
84
        /**
85
         * @see com.iver.cit.gvsig.project.documents.layout.ProjectMap#setMapContext(com.iver.cit.gvsig.project.castor.XMLEntity)
86
         */
87
        public void setModel(Layout f) {
88
                model = f;
89
                f.setName(getName());
90
        }
91

    
92
        /**
93
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setName(java.lang.String)
94
         */
95
        public void setName(String string) {
96
                super.setName(string);
97

    
98
                Layout m = getModel();
99

    
100
                if (m != null) {
101
                        m.setName(string);
102
                }
103
        }
104

    
105
        /**
106
         * DOCUMENT ME!
107
         *
108
         * @return DOCUMENT ME!
109
         * @throws SaveException
110
         * @throws XMLException
111
         *
112
         * @throws DriverException
113
         */
114
        public XMLEntity getXMLEntity() throws SaveException   {
115
                XMLEntity xml = super.getXMLEntity();
116
                try{
117
                //xml.putProperty("nameClass", this.getClass().getName());
118
                xml.putProperty("numMaps", numMaps);
119
                xml.addChild(model.getXMLEntity());
120
                }catch (Exception e) {
121
                        throw new SaveException(e,this.getClass().getName());
122
                }
123
                return xml;
124
        }
125

    
126
        /**
127
         * @throws OpenException
128
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
129
         */
130
        public void setXMLEntity(XMLEntity xml) throws OpenException {
131
                try {
132
                        super.setXMLEntity(xml);
133
                        numMaps = xml.getIntProperty("numMaps");
134
                        for (int i=0; i<xml.getChildrenCount(); i++)
135
                        {
136
                                XMLEntity child = xml.getChild(i);
137
                                if (child.contains("className")
138
                                                && (child.getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.Layout") || child.getStringProperty("className").equals(Layout.class.getName()))
139
                                                && child.contains("name")
140
                                                && child.getStringProperty("name").equals("layout")) {
141
                                        setModel(Layout.createLayout(child,getProject()));
142
                                }
143
                        }
144
                        this.model.setProjectMap(this);
145
                } catch (Exception e) {
146
                        throw new OpenException(e,this.getClass().getName());
147
                }
148
        }
149

    
150
        /**
151
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
152
         */
153
        public void setXMLEntity03(XMLEntity xml)
154
                throws XMLException, DriverException, DriverIOException{
155

    
156
                super.setXMLEntity03(xml);
157
                numMaps = xml.getIntProperty("numMaps");
158
                model = Layout.createLayout03(xml.getChild(0), getProject());
159
        }
160

    
161

    
162
        public IWindow createWindow() {
163
                Layout l = getModel();
164
        setName(l.getName());
165
        l.setProjectMap(this);
166
                l.getLayoutControl().fullRect();
167
        l.getWindowInfo().setTitle(PluginServices.getText(this,
168
        "Mapa") + " : " +l.getName());
169
                return l;
170
        }
171
        public IWindow getProperties() {
172
                return new MapProperties(this);
173
        }
174

    
175
        public void afterRemove() {
176
                // TODO Auto-generated method stub
177

    
178
        }
179

    
180
        public void afterAdd() {
181
                // TODO Auto-generated method stub
182

    
183
        }
184

    
185
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
186
                XMLEntity mapsRoot = project.getExportXMLTypeRootNode(root,ProjectMapFactory.registerName);
187
                mapsRoot.addChild(this.getXMLEntity());
188
                this.exportToXMLDependencies(root,project);
189
        }
190

    
191
        private void exportToXMLDependencies( XMLEntity root,Project project)
192
                throws SaveException {
193
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
194
                IFFrame[] components = this.getModel().getLayoutContext().getFFrames();
195
                for (int i=0; i < components.length; i++) {
196
                        if (components[i] instanceof FFrameView) {
197
                                ProjectView view = ((FFrameView)components[i]).getView();
198
                                XMLEntity viewXML = viewsRoot.firstChild("name",view.getName());
199
                                if (viewXML==null) {
200
                                        view.exportToXML(root,project);
201
                                }
202
                        }
203
                }
204

    
205
        }
206

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

    
210
                XMLEntity layout = element.getChild(0);
211
                //Cargamos las vistas vinculadas:
212

    
213
                //Recuperamos todos los nombres
214
                int childIndex;
215
                XMLEntity child;
216
                // Lo hacemos en un map por si una vista se usa varias veces
217
                HashMap viewsName = new HashMap();
218
                for (childIndex=0;childIndex<layout.getChildrenCount();childIndex++) {
219
                        child = layout.getChild(childIndex);
220
                        if (child.contains("viewName")) {
221
                                viewsName.put(child.getStringProperty("viewName"),child.getStringProperty("viewName"));
222
                        }
223

    
224
                }
225

    
226

    
227
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
228
                XMLEntity viewXML;
229

    
230
                // Construimos un diccionario ordenado inversamente por el indice
231
                // del elemento (por si se van eliminando elementos al importar) y
232
                // como valor el nombre de la vista
233
                TreeMap viewsToImport = new TreeMap( new Comparator() {
234

    
235
                        public int compare(Object o1, Object o2) {
236

    
237
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
238
                                        return -1; //o1 first
239
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
240
                                        return 1; //o1 second
241
                                }
242
                                return 0;
243
                        }
244

    
245
                });
246
                Iterator iterViewsName = viewsName.keySet().iterator();
247
                int viewIndex;
248
                String viewName;
249
                while (iterViewsName.hasNext()) {
250
                        viewName = (String)iterViewsName.next();
251
                        viewIndex = viewsRoot.firstIndexOfChild("name",viewName);
252
                        viewsToImport.put(new Integer(viewIndex),viewName);
253
                }
254

    
255

    
256
                ProjectView view;
257
                ProjectDocumentFactory viewFactory = project.getProjectDocumentFactory(ProjectViewFactory.registerName);
258

    
259
                Iterator iterViewToImport = viewsToImport.entrySet().iterator();
260
                Entry entry;
261
                // Nos recorremos las vistas a importar
262
                while (iterViewToImport.hasNext()) {
263
                        entry = (Entry)iterViewToImport.next();
264
                        viewName = (String)entry.getValue();
265
                        viewIndex = ((Integer)entry.getKey()).intValue();
266
                        // Si ya existe la vista no la importamos
267
                        view = (ProjectView)project.getProjectDocumentByName(viewName,ProjectViewFactory.registerName);
268
                        if (view == null) {
269
                                view = (ProjectView)viewFactory.create(project);
270
                                view.importFromXML(root,viewsRoot,viewIndex,project,removeDocumentsFromRoot);
271
                        }
272

    
273
                }
274

    
275

    
276
                this.setXMLEntity(element);
277
                project.addDocument(this);
278
                if (removeDocumentsFromRoot) {
279
                        typeRoot.removeChild(elementIndex);
280
                }
281

    
282

    
283
        }
284

    
285

    
286

    
287
//        public int computeSignature() {
288
//                int result = 17;
289
//
290
//                Class clazz = getClass();
291
//                Field[] fields = clazz.getDeclaredFields();
292
//                for (int i = 0; i < fields.length; i++) {
293
//                        try {
294
//                                String type = fields[i].getType().getName();
295
//                                if (type.equals("boolean")) {
296
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
297
//                                } else if (type.equals("java.lang.String")) {
298
//                                        Object v = fields[i].get(this);
299
//                                        if (v == null) {
300
//                                                result += 37;
301
//                                                continue;
302
//                                        }
303
//                                        char[] chars = ((String) v).toCharArray();
304
//                                        for (int j = 0; j < chars.length; j++) {
305
//                                                result += 37 + (int) chars[i];
306
//                                        }
307
//                                } else if (type.equals("byte")) {
308
//                                        result += 37 + (int) fields[i].getByte(this);
309
//                                } else if (type.equals("char")) {
310
//                                        result += 37 + (int) fields[i].getChar(this);
311
//                                } else if (type.equals("short")) {
312
//                                        result += 37 + (int) fields[i].getShort(this);
313
//                                } else if (type.equals("int")) {
314
//                                        result += 37 + fields[i].getInt(this);
315
//                                } else if (type.equals("long")) {
316
//                                        long f = fields[i].getLong(this) ;
317
//                                        result += 37 + (f ^ (f >>> 32));
318
//                                } else if (type.equals("float")) {
319
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
320
//                                } else if (type.equals("double")) {
321
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
322
//                                        result += 37 + (f ^ (f >>> 32));
323
//                                } else {
324
//                                        Object obj = fields[i].get(this);
325
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
326
//                                }
327
//                        } catch (Exception e) { e.printStackTrace(); }
328
//
329
//                }
330
//                return result;
331
//        }
332
}