Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / ProjectMap.java @ 28368

History | View | Annotate | Download (10.3 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.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.ui.mdiManager.IWindow;
52
import com.iver.cit.gvsig.fmap.layers.XMLException;
53
import com.iver.cit.gvsig.project.Project;
54
import com.iver.cit.gvsig.project.documents.ProjectDocument;
55
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
56
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
57
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
58
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
59
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
60
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
61
import com.iver.cit.gvsig.project.documents.layout.gui.MapProperties;
62
import com.iver.cit.gvsig.project.documents.view.ProjectView;
63
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
64
import com.iver.utiles.XMLEntity;
65

    
66

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

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

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

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

    
97
                Layout m = getModel();
98

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

    
104
        /**
105
         * DOCUMENT ME!
106
         *
107
         * @return DOCUMENT ME!
108
         * @throws SaveException
109
         * @throws XMLException
110
         *
111
         * @throws ReadDriverException
112
         */
113
        public XMLEntity getXMLEntity() throws SaveException   {
114
                XMLEntity xml = super.getXMLEntity();
115
                try{
116
                //xml.putProperty("nameClass", this.getClass().getName());
117
                int numMaps=((Integer)ProjectDocument.NUMS.get(ProjectMapFactory.registerName)).intValue();
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
                        int numMaps=xml.getIntProperty("numMaps");
134
                        ProjectDocument.NUMS.put(ProjectMapFactory.registerName,new Integer(numMaps));
135
                        for (int i=0; i<xml.getChildrenCount(); i++)
136
                        {
137
                                XMLEntity child = xml.getChild(i);
138
                                if (child.contains("className")
139
                                                && (child.getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.Layout") || child.getStringProperty("className").equals(Layout.class.getName()))
140
                                                && child.contains("name")
141
                                                && child.getStringProperty("name").equals("layout")) {
142
                                        setModel(Layout.createLayout(child,getProject()));
143
                                }
144
                        }
145
                        this.model.setProjectMap(this);
146
                } catch (Exception e) {
147
                        throw new OpenException(e,this.getClass().getName());
148
                }
149
        }
150

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

    
157
                super.setXMLEntity03(xml);
158
                int numMaps=xml.getIntProperty("numMaps");
159
                ProjectDocument.NUMS.put(ProjectMapFactory.registerName,new Integer(numMaps));
160
                model = Layout.createLayout03(xml.getChild(0), getProject());
161
        }
162

    
163

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

    
178
        public void afterRemove() {
179
                // TODO Auto-generated method stub
180

    
181
        }
182

    
183
        public void afterAdd() {
184
                // TODO Auto-generated method stub
185

    
186
        }
187

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

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

    
208
        }
209

    
210
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException, OpenException, ReadDriverException {
211
                XMLEntity element = typeRoot.getChild(elementIndex);
212

    
213
                XMLEntity layout = element.getChild(0);
214
                //Cargamos las vistas vinculadas:
215

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

    
227
                }
228

    
229

    
230
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
231
                XMLEntity viewXML;
232

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

    
238
                        public int compare(Object o1, Object o2) {
239

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

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

    
258

    
259
                ProjectView view;
260
                ProjectDocumentFactory viewFactory = project.getProjectDocumentFactory(ProjectViewFactory.registerName);
261

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

    
276
                }
277

    
278

    
279
                this.setXMLEntity(element);
280
                project.addDocument(this);
281
                if (removeDocumentsFromRoot) {
282
                        typeRoot.removeChild(elementIndex);
283
                }
284

    
285

    
286
        }
287

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