Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / ProjectMap.java @ 29598

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 org.gvsig.app.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.andami.PluginServices;
50
import org.gvsig.andami.ui.mdiManager.IWindow;
51
import org.gvsig.app.project.Project;
52
import org.gvsig.app.project.documents.ProjectDocument;
53
import org.gvsig.app.project.documents.ProjectDocumentFactory;
54
import org.gvsig.app.project.documents.exceptions.OpenException;
55
import org.gvsig.app.project.documents.exceptions.SaveException;
56
import org.gvsig.app.project.documents.layout.fframes.FFrameView;
57
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
58
import org.gvsig.app.project.documents.layout.gui.Layout;
59
import org.gvsig.app.project.documents.layout.gui.MapProperties;
60
import org.gvsig.app.project.documents.view.ProjectView;
61
import org.gvsig.app.project.documents.view.ProjectViewFactory;
62
import org.gvsig.utils.XMLEntity;
63
import org.gvsig.utils.XMLException;
64

    
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 org.gvsig.app.project.documents.layout.ProjectMap#getModel()
78
         */
79
        public Layout getModel() {
80
                return model;
81
        }
82

    
83
        /**
84
         * @see org.gvsig.app.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 org.gvsig.app.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=(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 org.gvsig.app.project.documents.ProjectDocument#setXMLEntity(org.gvsig.utils.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("org.gvsig.app.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
        callCreateWindow(l);
159
                return l;
160
        }
161
        public IWindow getProperties() {
162
                return new MapProperties(this);
163
        }
164

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

    
168
        }
169

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

    
173
        }
174

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

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

    
195
        }
196

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

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

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

    
214
                }
215

    
216

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

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

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

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

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

    
245

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

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

    
267
                }
268

    
269

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

    
276

    
277
        }
278

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