Statistics
| Revision:

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

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.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
        private boolean isModified=false;
77

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

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

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

    
99
                Layout m = getModel();
100

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

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

    
127
        /**
128
         * @throws OpenException
129
         * @see com.iver.cit.gvsig.project.documents.ProjectDocument#setXMLEntity(com.iver.utiles.XMLEntity)
130
         */
131
        public void setXMLEntity(XMLEntity xml) throws OpenException {
132
                try {
133
                        super.setXMLEntity(xml);
134
                        numMaps = xml.getIntProperty("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, DriverException, DriverIOException{
156

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

    
162

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

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

    
179
        }
180

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

    
184
        }
185

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

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

    
206
        }
207

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

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

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

    
225
                }
226

    
227

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

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

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

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

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

    
256

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

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

    
274
                }
275

    
276

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

    
283

    
284
        }
285

    
286
        public boolean isModified() {
287
                return isModified;
288
        }
289

    
290
        public void setModified(boolean modified) {
291
                isModified=modified;
292
        }
293

    
294

    
295

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