Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / ProjectView.java @ 6888

History | View | Annotate | Download (11.2 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;
42

    
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.lang.reflect.Field;
46
import java.text.DateFormat;
47
import java.util.Date;
48

    
49
import javax.swing.JOptionPane;
50

    
51
import org.cresques.cts.IProjection;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.andami.ui.mdiManager.WindowInfo;
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.ErrorEvent;
57
import com.iver.cit.gvsig.fmap.ErrorListener;
58
import com.iver.cit.gvsig.fmap.MapContext;
59
import com.iver.cit.gvsig.fmap.ViewPort;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.layers.XMLException;
62
import com.iver.cit.gvsig.gui.project.OpenException;
63
import com.iver.cit.gvsig.gui.project.SaveException;
64
import com.iver.utiles.XMLEntity;
65

    
66

    
67
/**
68
 * Clase que representa una vista del proyecto
69
 *
70
 * @author Fernando Gonz?lez Cort?s
71
 */
72
public class ProjectView extends ProjectElement implements ErrorListener {
73
        public static int numViews = 0;
74

    
75
        //public static int METROS = 0;
76
        //public static int KILOMETROS = 1;
77
        //public static int[] unidades = new int[] { METROS, KILOMETROS };
78
        ///private Color backgroundColor = new Color(255, 255, 255);
79
        private MapContext mapOverViewContext;
80
        private MapContext mapContext;
81
        private int m_typeLink = 0;
82
        private String m_extLink;
83
        private String m_selectedField = null;
84

    
85
        /**
86
         * Gets the FMap's contexts of the main map in the view.
87
         *
88
         * @return
89
         */
90
        public MapContext getMapContext() {
91
                return mapContext;
92
        }
93

    
94
        /**
95
         * Gets the FMap's context from the locator, which is the
96
         * small map in the left-bottom corner of the View.
97
         *
98
         * @return
99
         */
100
        public MapContext getMapOverViewContext() {
101
                return mapOverViewContext;
102
        }
103

    
104
        /**
105
         * Crea una ProjectView, que es el modelo que hay detr?s de una vista.
106
         *
107
         * @param viewName
108
         *
109
         * @return
110
         */
111
        public static ProjectView createView(String viewName) {
112
                ProjectView v = new ProjectView();
113
                v.mapContext = new MapContext(new ViewPort(Project.getProjection()));
114
                v.mapOverViewContext = new MapContext(null);
115
                v.mapOverViewContext.setProjection(v.mapContext.getProjection());
116

    
117
                //v.mapOverViewContext = null;
118
                v.setName(viewName);
119
                v.setCreationDate(DateFormat.getInstance().format(new Date()));
120

    
121
                return v;
122
        }
123

    
124
        /**
125
         * @see com.iver.cit.gvsig.project.ProjectView#setMapContext(com.iver.cit.gvsig.fmap.MapContext)
126
         */
127
        public void setMapContext(MapContext fmap) {
128
                mapContext = fmap;
129
                fmap.addErrorListener(this);
130
        }
131

    
132
        /**
133
         * DOCUMENT ME!
134
         *
135
         * @param fmap DOCUMENT ME!
136
         */
137
        public void setMapOverViewContext(MapContext fmap) {
138
                mapOverViewContext = fmap;
139
                mapOverViewContext.setProjection(mapContext.getProjection());
140
        }
141

    
142
        /**
143
         * DOCUMENT ME!
144
         *
145
         * @return DOCUMENT ME!
146
         * @throws XMLException
147
         * @throws SaveException
148
         */
149
        public XMLEntity getXMLEntity() throws SaveException {
150
                XMLEntity xml = super.getXMLEntity();
151
                //xml.putProperty("nameClass", this.getClass().getName());
152
                try{
153
                xml.putProperty("numViews", numViews);
154
                xml.putProperty("m_selectedField", m_selectedField);
155
                xml.putProperty("m_typeLink", m_typeLink);
156
                xml.putProperty("m_extLink", m_extLink);
157
                xml.addChild(mapContext.getXMLEntity());
158

    
159
                if (mapOverViewContext != null) {
160
                        if (mapOverViewContext.getViewPort() != null) {
161
                                xml.putProperty("mapOverView", true);
162
                                xml.addChild(mapOverViewContext.getXMLEntity());
163
                        } else {
164
                                xml.putProperty("mapOverView", false);
165
                        }
166
                } else {
167
                        xml.putProperty("mapOverView", false);
168
                }
169
                XMLEntity viewProperties = this.getViewInfoXMLEntity();
170
                if (viewProperties!=null) { //store the properties of the window
171
                        xml.addChild(viewProperties);
172
                }
173
                }catch (Exception e) {
174
                        throw new SaveException(e,this.getClass().getName());
175
                }
176
                return xml;
177
        }
178

    
179
        /**
180
         * DOCUMENT ME!
181
         *
182
         * @param xml DOCUMENT ME!
183
         * @param p DOCUMENT ME!
184
         * @throws XMLException
185
         * @throws DriverException
186
         * @throws DriverIOException
187
         *
188
         * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
189
         */
190
        public void setXMLEntity03(XMLEntity xml, Project p)
191
                throws XMLException, DriverException, DriverIOException {
192
                numViews = xml.getIntProperty("numViews");
193
                m_selectedField = xml.getStringProperty("m_selectedField");
194
                m_typeLink = xml.getIntProperty("m_typeLink");
195
                m_extLink = xml.getStringProperty("m_extLink");
196
                setMapContext(MapContext.createFromXML03(xml.getChild(0)));
197

    
198
                if (xml.getBooleanProperty("mapOverView")) {
199
                        setMapOverViewContext(MapContext.createFromXML03(xml.getChild(1)));
200
                }
201

    
202
        }
203

    
204
        /**
205
         * DOCUMENT ME!
206
         *
207
         * @param xml DOCUMENT ME!
208
         * @param p DOCUMENT ME!
209
         * @throws XMLException
210
         * @throws DriverException
211
         * @throws DriverIOException
212
         * @throws OpenException
213
         *
214
         * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
215
         */
216
        public void setXMLEntity(XMLEntity xml, Project p)
217
                throws XMLException, DriverException, DriverIOException, OpenException {
218
                try{
219
                        int currentChild=0;
220
                        numViews = xml.getIntProperty("numViews");
221
                        m_selectedField = xml.getStringProperty("m_selectedField");
222
                        m_typeLink = xml.getIntProperty("m_typeLink");
223
                        m_extLink = xml.getStringProperty("m_extLink");
224

    
225
                        setMapContext(MapContext.createFromXML(xml.getChild(currentChild)));
226
                        currentChild++;
227
                        if (xml.getBooleanProperty("mapOverView")) {
228
                                setMapOverViewContext(MapContext.createFromXML(xml.getChild(currentChild)));
229
                                currentChild++;
230
                        }
231
                        if (currentChild<xml.getChildrenCount()) {
232
                                XMLEntity child = xml.getChild(currentChild);
233
                                if (child.contains("className") && child.getStringProperty("className").equals(this.getClass().getName()) && child.contains("name") && child.getStringProperty("name").equals("ViewInfoProperties")) {
234
                                        seedViewInfo = createViewInfoFromXMLEntity(child);
235
                                        //currentChild++;
236
                                }
237
                        }
238
                        showErrors();
239
                }catch (Exception e) {
240
                        throw new OpenException(e,this.getClass().getName());
241
                }
242
        }
243

    
244
        public void showErrors(){
245
                if (mapContext.getLayersError().size()>0){
246
                        String layersError="";
247
                        for (int i=0;i<mapContext.getLayersError().size();i++){
248
                                layersError=layersError+"\n"+(String)mapContext.getLayersError().get(i);
249
                        }
250
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
251
                                        PluginServices.getText(this,"fallo_capas")+" : \n"+
252
                                        layersError);
253
                }
254
        }
255
        /**
256
         * DOCUMENT ME!
257
         *
258
         * @param p DOCUMENT ME!
259
         *
260
         * @return DOCUMENT ME!
261
         * @throws XMLException
262
         * @throws DriverException
263
         * @throws DriverIOException
264
         * @throws OpenException
265
         */
266
        /*public ProjectView cloneProjectView(Project p)
267
                throws XMLException, DriverException, DriverIOException, OpenException {
268
                return (ProjectView) createFromXML(getXMLEntity(), p);
269
        }
270
*/
271
        /**
272
         * DOCUMENT ME!
273
         *
274
         * @return DOCUMENT ME!
275
         */
276
        public IProjection getProjection() {
277
                return mapContext.getProjection();
278
        }
279

    
280
        /**
281
         * DOCUMENT ME!
282
         *
283
         * @return DOCUMENT ME!
284
         */
285
        public IProjection getOverViewProjection() {
286
                return mapOverViewContext.getProjection();
287
        }
288

    
289
        public void setProjection (IProjection proj) {
290
                mapContext.setProjection(proj);
291
                mapOverViewContext.setProjection(proj);
292
        }
293

    
294
        /**
295
         * DOCUMENT ME!
296
         *
297
         * @return DOCUMENT ME!
298
         */
299
        public String getExtLink() {
300
                return m_extLink;
301
        }
302

    
303
        /**
304
         * DOCUMENT ME!
305
         *
306
         * @return DOCUMENT ME!
307
         */
308
        public int getTypeLink() {
309
                return m_typeLink;
310
        }
311

    
312
        /**
313
     * Se selecciona la extensi?n para realizar cuando se quiera el link.
314
     *
315
     * @param s nombre del campo.
316
     */
317
        public void setExtLink(String s) {
318
                m_extLink = s;
319
        }
320

    
321
        /**
322
     * Se selecciona el tipo de fichero para realizar cuando se quiera el link.
323
     *
324
     * @param i tipo de fichero.
325
     */
326
        public void setTypeLink(int i) {
327
                m_typeLink = i;
328
        }
329

    
330
        /**
331
         * DOCUMENT ME!
332
         *
333
         * @param c DOCUMENT ME!
334
         */
335
        public void setBackColor(Color c) {
336
//                getMapContext().getViewPort().addViewPortListener(getMapContext()
337
        //                                                                                                           .getEventBuffer());
338
                getMapContext().getViewPort().setBackColor(c);
339
                //getMapContext().getViewPort().removeViewPortListener(getMapContext()
340
                        //                                                                                                  .getEventBuffer());
341
        }
342

    
343
        /**
344
         * Se selecciona el nombre del campo para realizar cuando se quiera el
345
         * link.
346
         *
347
         * @param s nombre del campo.
348
         */
349
        public void setSelectedField(String s) {
350
                m_selectedField = s;
351
        }
352

    
353
        /**
354
         * DOCUMENT ME!
355
         *
356
         * @return DOCUMENT ME!
357
         */
358
        public String getSelectedField() {
359
                return m_selectedField;
360
        }
361

    
362
        public void errorThrown(ErrorEvent e) {
363
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
364
                                        PluginServices.getText(this,"fallo_capas")+" : \n"+
365
                                        e.getMessage());
366

    
367
        }
368

    
369
//        public int computeSignature() {
370
//                int result = 17;
371
//
372
//                Class clazz = getClass();
373
//                Field[] fields = clazz.getDeclaredFields();
374
//                for (int i = 0; i < fields.length; i++) {
375
//                        try {
376
//                                String type = fields[i].getType().getName();
377
//                                if (type.equals("boolean")) {
378
//                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
379
//                                } else if (type.equals("java.lang.String")) {
380
//                                        Object v = fields[i].get(this);
381
//                                        if (v == null) {
382
//                                                result += 37;
383
//                                                continue;
384
//                                        }
385
//                                        char[] chars = ((String) v).toCharArray();
386
//                                        for (int j = 0; j < chars.length; j++) {
387
//                                                result += 37 + (int) chars[i];
388
//                                        }
389
//                                } else if (type.equals("byte")) {
390
//                                        result += 37 + (int) fields[i].getByte(this);
391
//                                } else if (type.equals("char")) {
392
//                                        result += 37 + (int) fields[i].getChar(this);
393
//                                } else if (type.equals("short")) {
394
//                                        result += 37 + (int) fields[i].getShort(this);
395
//                                } else if (type.equals("int")) {
396
//                                        result += 37 + fields[i].getInt(this);
397
//                                } else if (type.equals("long")) {
398
//                                        long f = fields[i].getLong(this) ;
399
//                                        result += 37 + (f ^ (f >>> 32));
400
//                                } else if (type.equals("float")) {
401
//                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
402
//                                } else if (type.equals("double")) {
403
//                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
404
//                                        result += 37 + (f ^ (f >>> 32));
405
//                                } else {
406
//                                        Object obj = fields[i].get(this);
407
//                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
408
//                                }
409
//                        } catch (Exception e) { e.printStackTrace(); }
410
//
411
//                }
412
//                return result;
413
//        }
414
}