Statistics
| Revision:

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

History | View | Annotate | Download (9.81 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 org.gvsig.fmap.mapcontext.layers.XMLException;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.ui.mdiManager.IWindow;
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
        public IWindow createWindow() {
152
                Layout l = getModel();
153
        setName(l.getName());
154
        l.setProjectMap(this);
155
                l.getLayoutControl().fullRect();
156
        l.getWindowInfo().setTitle(PluginServices.getText(this,
157
        "Mapa") + " : " +l.getName());
158
                return l;
159
        }
160
        public IWindow getProperties() {
161
                return new MapProperties(this);
162
        }
163

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

    
167
        }
168

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

    
172
        }
173

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

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

    
194
        }
195

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

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

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

    
213
                }
214

    
215

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

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

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

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

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

    
244

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

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

    
262
                }
263

    
264

    
265
                this.setXMLEntity(element);
266
                project.addDocument(this);
267
                if (removeDocumentsFromRoot) {
268
                        typeRoot.removeChild(elementIndex);
269
                }
270

    
271

    
272
        }
273

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