Statistics
| Revision:

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

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

    
65

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

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

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

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

    
96
                Layout m = getModel();
97

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

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

    
125
        /**
126
         * @throws OpenException
127
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
128
         */
129
        public void setXMLEntity(XMLEntity xml) throws OpenException {
130
                try {
131
                        super.setXMLEntity(xml);
132
                        int numMaps=xml.getIntProperty("numMaps");
133
                        ProjectDocument.NUMS.put(ProjectMapFactory.registerName,new Integer(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
        public IWindow createWindow() {
151
                Layout l = getModel();
152
        setName(l.getName());
153
        l.setProjectMap(this);
154
                l.getLayoutControl().fullRect();
155
        l.getWindowInfo().setTitle(PluginServices.getText(this,
156
        "Mapa") + " : " +l.getName());
157
                return l;
158
        }
159
        public IWindow getProperties() {
160
                return new MapProperties(this);
161
        }
162

    
163
        public void afterRemove() {
164
                // TODO Auto-generated method stub
165

    
166
        }
167

    
168
        public void afterAdd() {
169
                // TODO Auto-generated method stub
170

    
171
        }
172

    
173
        public void exportToXML(XMLEntity root, Project project) throws SaveException {
174
                XMLEntity mapsRoot = project.getExportXMLTypeRootNode(root,ProjectMapFactory.registerName);
175
                mapsRoot.addChild(this.getXMLEntity());
176
                this.exportToXMLDependencies(root,project);
177
        }
178

    
179
        private void exportToXMLDependencies( XMLEntity root,Project project)
180
                throws SaveException {
181
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
182
                IFFrame[] components = this.getModel().getLayoutContext().getFFrames();
183
                for (int i=0; i < components.length; i++) {
184
                        if (components[i] instanceof FFrameView) {
185
                                ProjectView view = ((FFrameView)components[i]).getView();
186
                                XMLEntity viewXML = viewsRoot.firstChild("name",view.getName());
187
                                if (viewXML==null) {
188
                                        view.exportToXML(root,project);
189
                                }
190
                        }
191
                }
192

    
193
        }
194

    
195
        public void importFromXML(XMLEntity root, XMLEntity typeRoot, int elementIndex, Project project, boolean removeDocumentsFromRoot) throws XMLException {
196
                XMLEntity element = typeRoot.getChild(elementIndex);
197

    
198
                XMLEntity layout = element.getChild(0);
199
                //Cargamos las vistas vinculadas:
200

    
201
                //Recuperamos todos los nombres
202
                int childIndex;
203
                XMLEntity child;
204
                // Lo hacemos en un map por si una vista se usa varias veces
205
                HashMap viewsName = new HashMap();
206
                for (childIndex=0;childIndex<layout.getChildrenCount();childIndex++) {
207
                        child = layout.getChild(childIndex);
208
                        if (child.contains("viewName")) {
209
                                viewsName.put(child.getStringProperty("viewName"),child.getStringProperty("viewName"));
210
                        }
211

    
212
                }
213

    
214

    
215
                XMLEntity viewsRoot = project.getExportXMLTypeRootNode(root,ProjectViewFactory.registerName);
216
                XMLEntity viewXML;
217

    
218
                // Construimos un diccionario ordenado inversamente por el indice
219
                // del elemento (por si se van eliminando elementos al importar) y
220
                // como valor el nombre de la vista
221
                TreeMap viewsToImport = new TreeMap( new Comparator() {
222

    
223
                        public int compare(Object o1, Object o2) {
224

    
225
                                if (((Integer)o1).intValue() > ((Integer)o2).intValue()) {
226
                                        return -1; //o1 first
227
                                } else if (((Integer)o1).intValue() < ((Integer)o2).intValue()){
228
                                        return 1; //o1 second
229
                                }
230
                                return 0;
231
                        }
232

    
233
                });
234
                Iterator iterViewsName = viewsName.keySet().iterator();
235
                int viewIndex;
236
                String viewName;
237
                while (iterViewsName.hasNext()) {
238
                        viewName = (String)iterViewsName.next();
239
                        viewIndex = viewsRoot.firstIndexOfChild("name",viewName);
240
                        viewsToImport.put(new Integer(viewIndex),viewName);
241
                }
242

    
243

    
244
                ProjectView view;
245
                ProjectDocumentFactory viewFactory = project.getProjectDocumentFactory(ProjectViewFactory.registerName);
246

    
247
                Iterator iterViewToImport = viewsToImport.entrySet().iterator();
248
                Entry entry;
249
                // Nos recorremos las vistas a importar
250
                while (iterViewToImport.hasNext()) {
251
                        entry = (Entry)iterViewToImport.next();
252
                        viewName = (String)entry.getValue();
253
                        viewIndex = ((Integer)entry.getKey()).intValue();
254
                        // Si ya existe la vista no la importamos
255
                        view = (ProjectView)project.getProjectDocumentByName(viewName,ProjectViewFactory.registerName);
256
                        if (view == null) {
257
                                view = (ProjectView)viewFactory.create(project);
258
//                                try {
259
                                        view.importFromXML(root,viewsRoot,viewIndex,project,removeDocumentsFromRoot);
260
//                                } catch (ReadException e) {
261
//                                        throw new OpenException(e,getName());
262
//                                }
263
                        }
264

    
265
                }
266

    
267

    
268
                this.setXMLEntity(element);
269
                project.addDocument(this);
270
                if (removeDocumentsFromRoot) {
271
                        typeRoot.removeChild(elementIndex);
272
                }
273

    
274

    
275
        }
276

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