Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameView.java @ 9956

History | View | Annotate | Download (32.8 KB)

1
/*
2
 * Created on 20-feb-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout.fframes;
46

    
47
import java.awt.Color;
48
import java.awt.Dimension;
49
import java.awt.Graphics2D;
50
import java.awt.Point;
51
import java.awt.Rectangle;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.Point2D;
54
import java.awt.geom.Rectangle2D;
55
import java.awt.image.BufferedImage;
56
import java.util.ArrayList;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.cit.gvsig.ProjectExtension;
61
import com.iver.cit.gvsig.fmap.ColorEvent;
62
import com.iver.cit.gvsig.fmap.DriverException;
63
import com.iver.cit.gvsig.fmap.ExtentEvent;
64
import com.iver.cit.gvsig.fmap.MapContext;
65
import com.iver.cit.gvsig.fmap.ProjectionEvent;
66
import com.iver.cit.gvsig.fmap.ViewPort;
67
import com.iver.cit.gvsig.fmap.ViewPortListener;
68
import com.iver.cit.gvsig.fmap.core.FShape;
69
import com.iver.cit.gvsig.fmap.layers.LegendListener;
70
import com.iver.cit.gvsig.fmap.layers.XMLException;
71
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
72
import com.iver.cit.gvsig.project.Project;
73
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
74
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
75
import com.iver.cit.gvsig.project.documents.layout.LayoutControl;
76
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
77
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
78
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
79
import com.iver.cit.gvsig.project.documents.view.ProjectView;
80
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
81
import com.iver.utiles.XMLEntity;
82

    
83

    
84
/**
85
 * FFrame para introducir una vista en el Layout.
86
 *
87
 * @author Vicente Caballero Navarro
88
 */
89
public class FFrameView extends FFrame implements ViewPortListener,
90
    LegendListener, IFFrameUseProject, IFFrameUseFMap {
91
    //private static Logger logger = Logger.getLogger(FFrameView.class.getName());
92
    public static final int PRESENTACION = 0;
93
    public static final int BORRADOR = 1;
94
    protected int m_Mode;
95
    protected int m_typeScale = AUTOMATICO;
96
    protected int m_extension = 0;
97
    protected int m_quality = PRESENTACION;
98
    protected int m_viewing = 0;
99
    protected boolean m_bLinked = true;
100
    protected ProjectView view = null;
101
    protected MapContext m_fmap = null;
102
    protected double m_Scale = 0;
103
    protected int m_mapUnits = 1; //Metros.
104

    
105
    private BufferedImage m_image = null;
106
    private AffineTransform at = null;
107
    private Project project = null;
108
    private double scaleAnt;
109
        private boolean refresh=false;
110
        private Point origin;
111
        private Point2D p1;
112
        private Point2D p2;
113
        /**
114
     * Creates a new FFrameView object.
115
     */
116
    public FFrameView() {
117
        num++;
118
    }
119

    
120
    /**
121
     * Devuelve una descripci?n del FFrameView.
122
     *
123
     * @return Descripci?n.
124
     */
125
    public String toString() {
126
        if (getView() == null) {
127
            return "FFrameView " + num + ": " + "Vacio";
128
        }
129

    
130
        return "FFrameView " + num + ": " + getView().getName();
131
    }
132

    
133
    /**
134
     * Rellena la escala de la vista que contiene el fframe.
135
     *
136
     * @param d escala de la vista.
137
     */
138
    public void setScale(double d) {
139
        m_Scale = d;
140
    }
141

    
142
    /**
143
     * Inserta el nuevo extent a la FFrameView.
144
     *
145
     * @param r Rect?ngulo a ocupar por el FFrameView.
146
     */
147
    public void setNewExtent(Rectangle2D r) {
148
        getMapContext().getViewPort().setExtent(r);
149
        refresh = true;
150
        m_Scale = FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
151
                getBoundBox().width,
152
                getBoundingBox(null).width / getBoundBox().width);
153
    }
154

    
155
    /**
156
     * Devuelve el FMap de la vista o una clonaci?n de este si se utiliza una
157
     * escala fija.
158
     *
159
     * @return FMap.
160
     */
161
    public MapContext getMapContext() {
162
            return m_fmap;
163
    }
164

    
165
    /**
166
     * Rellena la calidad que se quiere aplicar.
167
     *
168
     * @param q entero que representa la calidad a aplicar.
169
     */
170
    public void setQuality(int q) {
171
        m_quality = q;
172
    }
173

    
174
    /**
175
     * Devuelve un entero que representa la calidad que est? seleccionada.
176
     *
177
     * @return tipo de calidad selccionada.
178
     */
179
    public int getQuality() {
180
        return m_quality;
181
    }
182

    
183
    /**
184
     * Devuelve un entero que representa la forma en que se actualiza la vista.
185
     *
186
     * @return forma que se actualiza la vista.
187
     */
188
    public int getViewing() {
189
        return m_viewing;
190
    }
191

    
192
    /**
193
     * Rellena la forma de actualizar la vista(cuando activo o siempre). De
194
     * momento esta opci?n esta deshabilitada.
195
     *
196
     * @param v entero que representa la forma de actualizar la vista.
197
     */
198
    public void setViewing(int v) {
199
        m_viewing = v;
200
    }
201

    
202
    /**
203
     * Inserta el ProjectView de donde obtener las propiedades de la vista a
204
     * mostrar.
205
     *
206
     * @param v Modelo de la vista.
207
     */
208
    public void setView(ProjectView v) {
209
            view=v;
210
            ViewPort vp =null;
211
            if (getMapContext()!=null){
212
                vp=getMapContext().getViewPort();
213
        }else{
214
                vp = v.getMapContext().getViewPort().cloneViewPort();
215
        }
216
        vp.setImageSize(new Dimension((int) getBoundingBox(null).width,
217
                (int) getBoundingBox(null).height));
218

    
219
        if (m_bLinked) {
220
            if (getTypeScale() == AUTOMATICO) {
221
                m_fmap = v.getMapContext().createNewFMap(v.getMapContext()
222
                                                          .getViewPort()
223
                                                          .cloneViewPort());
224
                m_fmap.getViewPort().setImageSize(new Dimension(
225
                        (int) getBoundingBox(null).width,
226
                        (int) getBoundingBox(null).height));
227
                v.getMapContext().getViewPort().addViewPortListener(this);
228
                v.getMapContext().addLayerListener(this);
229
            } else if (getTypeScale() == CONSTANTE) {
230
                    m_fmap = v.getMapContext().createNewFMap(vp);
231
                    vp.setExtent(getNewExtent(getScale()));
232
                v.getMapContext().getViewPort().removeViewPortListener(this);
233
                v.getMapContext().addLayerListener(this);
234
            } else if (getTypeScale() == MANUAL) {
235
                m_fmap = v.getMapContext().createNewFMap(vp);
236
                vp.setExtent(getNewExtent(getScale()));
237
                v.getMapContext().getViewPort().removeViewPortListener(this);
238
                v.getMapContext().addLayerListener(this);
239
            }
240
        } else if (!m_bLinked) {
241
            try {
242
                if (getTypeScale() == AUTOMATICO) {
243
                    m_fmap = v.getMapContext().cloneFMap(); //(v.getMapContext().getViewPort().cloneViewPort());
244
                    m_fmap.setViewPort(v.getMapContext().getViewPort()
245
                                        .cloneViewPort());
246
                    m_fmap.getViewPort().setImageSize(new Dimension(
247
                            (int) getBoundingBox(null).width,
248
                            (int) getBoundingBox(null).height));
249
                    v.getMapContext().getViewPort().addViewPortListener(this);
250
                } else if (getTypeScale() == CONSTANTE) {
251
                        m_fmap = v.getMapContext().cloneFMap();
252
                        vp.setExtent(getNewExtent(getScale()));
253
                    m_fmap.setViewPort(vp);
254
                    v.getMapContext().getViewPort().removeViewPortListener(this);
255
                } else if (getTypeScale() == MANUAL) {
256
                    m_fmap = v.getMapContext().cloneFMap();
257
                    vp.setExtent(getNewExtent(getScale()));
258
                    m_fmap.setViewPort(vp);
259
                    v.getMapContext().getViewPort().removeViewPortListener(this);
260
                }
261
            } catch (XMLException e1) {
262
                NotificationManager.addError("Cuando se a?ade una vista al Layout",
263
                    e1);
264
            }
265
        }
266
    }
267

    
268
    /**
269
     * Devuelve el modelo de la vista.
270
     *
271
     * @return Modelo de la vista.
272
     */
273
    public ProjectView getView() {
274
        return view;
275
    }
276

    
277
    /**
278
     * Devuelve un Rect?ngulo que representa  el extent de la vista que se
279
     * requiere a partir  de una escala.
280
     *
281
     * @param scale Escala a mostrar.
282
     *
283
     * @return Rect?ngulo.
284
     */
285
    protected Rectangle2D.Double getNewExtent(long scale) {
286
        double hview = getBoundBox().getHeight();
287
        double wview = getBoundBox().getWidth();
288
        double hextent = (scale * hview) / 100.0;
289
        double wextent = (scale * wview) / 100.0;
290

    
291
        if (m_fmap.getViewPort().getExtent()==null)
292
                return new Rectangle2D.Double();
293
        double newx = m_fmap.getViewPort().getExtent().getCenterX() -
294
            (wextent / 2.0);
295
        double newy = m_fmap.getViewPort().getExtent().getCenterY() -
296
            (hextent / 2.0);
297

    
298
        return new Rectangle2D.Double(newx, newy, wextent, hextent);
299
    }
300

    
301
    /**
302
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
303
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
304
         * de dibujar.
305
         *
306
         * @param g
307
         *            Graphics2D
308
         * @param at
309
         *            Transformada af?n.
310
         * @param rv
311
         *            rect?ngulo sobre el que hacer un clip.
312
         * @param imgBase
313
         *            Imagen para acelerar el dibujado.
314
         *
315
         * @throws com.iver.cit.gvsig.fmap.DriverException
316
         */
317
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
318
                        BufferedImage imgBase)
319
                        throws com.iver.cit.gvsig.fmap.DriverException {
320
                Rectangle2D.Double r = getBoundingBox(at);
321
                if (getRotation() != 0)
322
                        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
323
                                        + (r.height / 2));
324
                if (intersects(rv, r)) {
325
                        if (getMapContext() == null) {
326
                                drawEmpty(g);
327
                        } else {
328
                                if (rv != null) {
329
                                        // Dibujamos en pantalla
330
                                        Rectangle rclip = (Rectangle)g.getClipBounds().clone();
331
                                        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r
332
                                                        .getWidth(), (int) r.getHeight());
333

    
334
                                        if (getQuality() == PRESENTACION) {
335
                                                if (rv.intersects(r)) {
336
                                                        ViewPort viewPort = this.getMapContext()
337
                                                        .getViewPort();
338
                                                        Color theBackColor = viewPort.getBackColor();
339

    
340
                                                        if (origin != null
341
                                                                        && origin.equals(getLayout().getLayoutControl().getRectOrigin())
342
                                                                        && getLayout() != null
343
                                                                        && getLayout().getLayoutControl().getAT().getScaleX() == scaleAnt
344
                                                                        && m_image != null && !refresh && !(r.getWidth() > getLayout().getWidth()
345
                                                                                        || r.getHeight() > getLayout().getHeight())) {
346
                                                                if (theBackColor != null) {
347
                                                                        g.setColor(theBackColor);
348
                                                                        g.fillRect((int) r.x, (int) r.y, viewPort
349
                                                                                        .getImageWidth(), viewPort
350
                                                                                        .getImageHeight());
351
                                                                }
352
                                                                g.translate(r.getX(), r.getY());
353
                                                                g.drawImage(m_image, 0, 0, getLayout().getLayoutControl());
354
                                                                g.translate(-r.getX(), -r.getY());
355
                                                                scaleAnt = getLayout().getLayoutControl().getAT().getScaleX();
356
                                                                origin = (Point) getLayout().getLayoutControl().getRectOrigin().clone();
357
                                                        } else {
358

    
359
                                                                System.err.println("r : " + r);
360
                                                                if (r.getWidth() > getLayout().getWidth()
361
                                                                                || r.getHeight() > getLayout().getHeight()) {
362
                                                                        getMapContext().getViewPort().setOffset(
363
                                                                                        new Point2D.Double(r.getX(), r
364
                                                                                                        .getY()));
365
                                                                        getMapContext().getViewPort().setImageSize(
366
                                                                                        new Dimension((int) r.getWidth(),
367
                                                                                                        (int) r.getHeight()));
368
                                                                        MapContext fmap = getMapContext().cloneToDraw();
369
                                                                        ViewPort viewp = viewPort.cloneViewPort();
370
                                                                        viewp.setImageSize(new Dimension(
371
                                                                                        getLayout().getWidth(),
372
                                                                                        getLayout().getHeight()));
373
                                                                        Rectangle2D r1 = calculateExtent();
374
                                                                        double width = getLayout().getLayoutContext().getAtributes()
375
                                                                                        .getSizeInUnits().getAncho();
376
                                                                        double scale = FLayoutUtilities
377
                                                                                        .getScaleView(viewp, width, r1
378
                                                                                                        .getWidth()
379
                                                                                                        / width);
380
                                                                        viewp.setExtent(r1);
381

    
382
                                                                        fmap.setViewPort(viewp);
383
                                                                        g.translate(-r.getX(), -r.getY());
384
                                                                        if (theBackColor != null) {
385
                                                                                g.setColor(theBackColor);
386
                                                                                g.fillRect((int) r.x, (int) r.y, viewp
387
                                                                                                .getImageWidth(), viewp
388
                                                                                                .getImageHeight());
389
                                                                        }
390
                                                                        fmap.draw(imgBase, g, scale);
391
                                                                        g.translate(r.getX(), r.getY());
392

    
393
                                                                } else {
394
                                                                        getMapContext().getViewPort().setOffset(
395
                                                                                        new Point2D.Double(r.x, r.y));
396
                                                                        getMapContext().getViewPort().setImageSize(
397
                                                                                        new Dimension((int) r.width,
398
                                                                                                        (int) r.height));
399
                                                                        m_image = new BufferedImage((int) r
400
                                                                                        .getWidth(), (int) r.getHeight(),
401
                                                                                        BufferedImage.TYPE_INT_ARGB);
402
                                                                        Graphics2D gimg = (Graphics2D) m_image
403
                                                                                        .getGraphics();
404
                                                                        gimg.translate(-((int) r.getX()), -((int) r
405
                                                                                        .getY()));
406
                                                                        getMapContext().draw(m_image, gimg, getScale());
407
                                                                        gimg.translate(((int) r.getX()), ((int) r
408
                                                                                        .getY()));
409
                                                                        if (theBackColor != null) {
410
                                                                                g.setColor(theBackColor);
411
                                                                                g.fillRect((int) r.x, (int) r.y, viewPort
412
                                                                                                .getImageWidth(), viewPort
413
                                                                                                .getImageHeight());
414
                                                                        }
415
                                                                        g.drawImage(m_image, (int) r.getX(),
416
                                                                                        (int) r.getY(), getLayout());
417
                                                                }
418
                                                                scaleAnt = getLayout().getLayoutControl().getAT().getScaleX();
419
                                                                origin = (Point) getLayout().getLayoutControl().getRectOrigin().clone();
420
                                                                refresh = false;
421
                                                        }
422
                                                }
423
                                        } else {
424
                                                drawDraft(g);
425
                                        }
426
                                        if (rclip != null) {
427
                                                g.setClip(rclip.x, rclip.y, rclip.width,
428
                                                                rclip.height);
429
                                        }
430
                                } else {
431
                                        printX(g, at);
432
                                }
433
                        }
434
                }
435
                if (getRotation() != 0)
436
                        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
437
                                        + (r.height / 2));
438

    
439
                if (getMapContext() != null) {
440
                        setATMap(getMapContext().getViewPort()
441
                                        .getAffineTransform());
442

    
443
                }
444
        }
445
    private Rectangle2D calculateExtent() {
446
            Rectangle2D.Double r = new Rectangle2D.Double();
447
                if (p1==null||p2==null)
448
                        return r;
449
            r.setFrameFromDiagonal(p1, p2);
450
                return r;
451
    }
452
    /**
453
         * DOCUMENT ME!
454
         *
455
         * @param g
456
         *            DOCUMENT ME!
457
         * @param at
458
         *            DOCUMENT ME!
459
         *
460
         * @throws DriverException
461
         *
462
         * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
463
         *      java.awt.geom.AffineTransform)
464
         */
465
    public void print(Graphics2D g, AffineTransform at, FShape shape)
466
        throws DriverException {
467
        draw(g, at, null, null);
468
    }
469

    
470
    /**
471
         * DOCUMENT ME!
472
         *
473
         * @param g
474
         *            DOCUMENT ME!
475
         * @param at
476
         *            DOCUMENT ME!
477
         */
478
    private void printX(Graphics2D g, AffineTransform at) {
479
        Rectangle2D.Double r = getBoundingBox(at);
480

    
481
        // Dibujamos en impresora
482
        Rectangle rclip = g.getClipBounds();
483
        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r.getWidth(),
484
            (int) r.getHeight());
485
        this.getMapContext().getViewPort().setOffset(new Point2D.Double(r.x, r.y));
486
        this.getMapContext().getViewPort().setImageSize(new Dimension((int) r.width,
487
                (int) r.height));
488

    
489
        try {
490
                this.getMapContext().print(g, getScale(),getLayout().getLayoutContext().getAtributes().toPrintAttributes());
491
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
492
            NotificationManager.addError(e.getMessage(), e);
493
        }
494

    
495
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
496
    }
497

    
498
    /**
499
     * Rellena la unidad de medida en la que est? la vista.
500
     *
501
     * @param i entero que representa la unidad de medida de la vista.
502
     */
503
    public void setMapUnits(int i) {
504
        m_mapUnits = i;
505
    }
506

    
507
    /**
508
     * Obtiene la unidad de medida en la que est? la vista.
509
     *
510
     * @return Unidad de medida.
511
     */
512
    public int getMapUnits() {
513
        return m_mapUnits;
514
    }
515

    
516
    /**
517
     * Devuelve la escala seg?n el tipo de escala que se haya seleccionado al
518
     * a?adida la vista.
519
     *
520
     * @return escala.
521
     */
522
    public long getScale() {
523
        /*if (m_bLinked){
524
           return getScaleView1(METROS);
525
               }
526
         */
527
            if (getMapContext()==null)
528
                    return 0;
529
        if (getTypeScale() == AUTOMATICO) {
530
            return FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
531
                getBoundBox().width, getBoundingBox(null).width);
532
        } else if (getTypeScale() == CONSTANTE) {
533
            return (long) m_Scale;
534
        } else if (getTypeScale() == MANUAL) {
535
            return (long) m_Scale;
536
        }
537

    
538
        return (long) m_Scale;
539
    }
540

    
541
    /**
542
     * Seleccionar si la vista esta relacionada o no con la original.
543
     *
544
     * @param b true si est? ligada y false si no lo est?.
545
     */
546
    public void setLinked(boolean b) {
547
        m_bLinked = b;
548
    }
549

    
550
    /**
551
     * Devuelve si est? ligada o no el FFrameView con la vista.
552
     *
553
     * @return True si la vista est? ligada.
554
     */
555
    public boolean getLinked() {
556
        return m_bLinked;
557
    }
558

    
559
    /**
560
     * Devuelve la opci?n seleccionada:Rellenar marco de la vista o recorte a
561
     * la vista.
562
     *
563
     * @return entero que representa la opci?n elegida.
564
     */
565
    public int getExtension() {
566
        return m_extension;
567
    }
568

    
569
    /**
570
     * Devuelve el tipo de escala que est? seleccionada AUTOMATICO,CONSTANTE o
571
     * MANUAL.
572
     *
573
     * @return entero que representa el tipo seleccionado.
574
     */
575
    public int getTypeScale() {
576
        return m_typeScale;
577
    }
578

    
579
    /**
580
     * Rellenar si se quiere:Rellenar marco de la vista o recorte a la vista.
581
     *
582
     * @param i entero que representa la opci?n elegida.
583
     */
584
    public void setExtension(int i) {
585
        m_extension = i;
586
    }
587

    
588
    /**
589
     * Rellenar el tipo de escala que se desea.
590
     *
591
     * @param i entero que representa el tipo de escala.
592
     */
593
    public void setTypeScale(int i) {
594
        m_typeScale = i;
595
    }
596

    
597
    /**
598
     * DOCUMENT ME!
599
     *
600
     * @return DOCUMENT ME!
601
     *
602
     * @throws SaveException
603
     *
604
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getXMLEntity()
605
     */
606
    public XMLEntity getXMLEntity() throws SaveException {
607
        XMLEntity xml = super.getXMLEntity();
608

    
609
        try {
610
//            xml.putProperty("type", Layout.RECTANGLEVIEW);
611
            xml.putProperty("m_Mode", m_Mode);
612
            xml.putProperty("m_typeScale", m_typeScale);
613
            xml.putProperty("m_extension", m_extension);
614
            xml.putProperty("m_quality", m_quality);
615
            xml.putProperty("m_viewing", m_viewing);
616
            xml.putProperty("m_bLinked", m_bLinked);
617
            xml.putProperty("m_mapUnits", m_mapUnits);
618
            xml.putProperty("m_Scale", m_Scale);
619

    
620
            ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
621
            ArrayList views = pe.getProject().getDocumentsByType(ProjectViewFactory.registerName);
622

    
623
            boolean hasIndex = false;
624

    
625

    
626
            if (view != null) {
627
                    xml.putProperty("viewName",view.getName());
628
                    for (int i = 0; i < views.size(); i++) {
629
                        if (view.getName().equals(((ProjectView) views.get(i)).getName())) {
630
                            xml.putProperty("indice", i);
631
                            hasIndex = true;
632
                            break;
633
                        }
634
                    }
635
            }
636

    
637
            if (!hasIndex) {
638
                xml.putProperty("indice", -1);
639
            }
640

    
641
            if (getMapContext() != null && getMapContext().getViewPort().getExtent()!=null) {
642
                xml.putProperty("extentX",
643
                    getMapContext().getViewPort().getExtent().getX());
644
                xml.putProperty("extentY",
645
                    getMapContext().getViewPort().getExtent().getY());
646
                xml.putProperty("extentW",
647
                    getMapContext().getViewPort().getExtent().getWidth());
648
                xml.putProperty("extentH",
649
                    getMapContext().getViewPort().getExtent().getHeight());
650

    
651
                xml.addChild(getMapContext().getXMLEntity());
652
            }
653
        } catch (Exception e) {
654
            throw new SaveException(e, this.getClass().getName());
655
        }
656

    
657
        return xml;
658
    }
659

    
660
    /**
661
     * Inserta la imagen para repintar el FFrameView.
662
     *
663
     * @param bi Imagen para repintar.
664
     */
665
    public void setBufferedImage(BufferedImage bi) {
666
        m_image = bi;
667
    }
668

    
669
    /**
670
     * Devuelve la imagen para repintar.
671
     *
672
     * @return Imagen para repintar.
673
     */
674
    public BufferedImage getBufferedImage() {
675
        return m_image;
676
    }
677

    
678
    /**
679
     * Devuelve la MAtriz de transformaci?n utilizada por la FFrameView.
680
     *
681
     * @return MAtriz de transformaci?n.
682
     */
683
    public AffineTransform getATMap() {
684
        return at;
685
    }
686

    
687
    /**
688
     * Inserta la matriz de transformaci?n.
689
     *
690
     * @param transform Matriz de transformaci?n.
691
     */
692
    public void setATMap(AffineTransform transform) {
693
        at = transform;
694
    }
695
    /**
696
     * Inserta el proyecto.
697
     *
698
     * @param p Proyecto.
699
     */
700
    public void setProject(Project p) {
701
        project = p;
702
    }
703

    
704
    /**
705
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
706
     *      com.iver.cit.gvsig.project.Project)
707
     */
708
    public void setXMLEntity03(XMLEntity xml, Layout l) {
709
        if (xml.getIntProperty("m_Selected") != 0) {
710
            this.setSelected(true);
711
        } else {
712
            this.setSelected(false);
713
        }
714

    
715
        this.setName(xml.getStringProperty("m_name"));
716
        this.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
717
                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
718
                xml.getDoubleProperty("h")));
719

    
720
        this.m_Mode = xml.getIntProperty("m_Mode");
721
        this.m_typeScale = xml.getIntProperty("m_typeScale");
722
        this.m_extension = xml.getIntProperty("m_extension");
723
        this.m_quality = xml.getIntProperty("m_quality");
724
        this.m_viewing = xml.getIntProperty("m_viewing");
725
        this.m_bLinked = xml.getBooleanProperty("m_bLinked");
726
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
727

    
728
        //ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
729
        this.m_Scale = xml.getDoubleProperty("m_Scale");
730

    
731
        int indice = xml.getIntProperty("indice");
732

    
733
        if (indice != -1) {
734
                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
735

    
736
            ProjectView view = (ProjectView) views.get(indice);
737
            this.m_fmap = view.getMapContext();
738
            this.setView(view);
739

    
740
            try {
741
                if (m_bLinked) {
742
                    this.getMapContext().getViewPort().setExtent(new Rectangle2D.Double(
743
                            xml.getDoubleProperty("extentX"),
744
                            xml.getDoubleProperty("extentY"),
745
                            xml.getDoubleProperty("extentW"),
746
                            xml.getDoubleProperty("extentH")));
747
                } else if (!m_bLinked) {
748
                    this.m_fmap = MapContext.createFromXML03(xml.getChild(0));
749
                }
750
            } catch (XMLException e) {
751
                NotificationManager.addError("Pasando las propiedades del XMLEntity al objeto",
752
                    e);
753
            }
754
        }
755
    }
756

    
757
    /**
758
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame()
759
     */
760
    public String getNameFFrame() {
761
        return PluginServices.getText(this, "Vista")+ num;
762
    }
763

    
764
    /**
765
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
766
     */
767
    public void extentChanged(ExtentEvent e) {
768
        if (getTypeScale() == AUTOMATICO) {
769
            m_fmap.getViewPort().setExtent(e.getNewExtent());
770
            if (getLayout() != null) {
771
                    getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
772
            }
773
        }
774
        refresh=true;
775
    }
776

    
777
    /**
778
     * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
779
     */
780
    public void backColorChanged(ColorEvent e) {
781
        if (getLinked()) {
782
            m_fmap.getViewPort().setBackColor(e.getNewColor());
783
            getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
784

    
785
            //setBufferedImage(null);
786
        }
787
    }
788

    
789
    /**
790
     * @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
791
     */
792
        public void projectionChanged(ProjectionEvent e) {
793
        if (getTypeScale() == AUTOMATICO) {
794
            m_fmap.getViewPort().setProjection(e.getNewProjection());
795

    
796
            if (getLayout() != null) {
797
                    getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
798
            }
799
        }
800
        }
801

    
802
    /**
803
     * DOCUMENT ME!
804
     *
805
     * @param e DOCUMENT ME!
806
     */
807
    public void legendChanged(LegendChangedEvent e) {
808
        if (getLinked()) {
809
                getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
810
            refresh=true;
811
            //setBufferedImage(null);
812
        }
813
    }
814

    
815
    /**
816
     * DOCUMENT ME!
817
     *
818
     * @param xml DOCUMENT ME!
819
     */
820
    public void setXMLEntity(XMLEntity xml) {
821
        if (xml.getIntProperty("m_Selected") != 0) {
822
            this.setSelected(true);
823
        } else {
824
            this.setSelected(false);
825
        }
826

    
827
        this.setName(xml.getStringProperty("m_name"));
828
        this.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
829
                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
830
                xml.getDoubleProperty("h")));
831

    
832
        this.m_Mode = xml.getIntProperty("m_Mode");
833
        this.m_typeScale = xml.getIntProperty("m_typeScale");
834
        this.m_extension = xml.getIntProperty("m_extension");
835
        this.m_quality = xml.getIntProperty("m_quality");
836
        this.m_viewing = xml.getIntProperty("m_viewing");
837
        this.m_bLinked = xml.getBooleanProperty("m_bLinked");
838
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
839
        setRotation(xml.getDoubleProperty("m_rotation"));
840

    
841
        this.m_Scale = xml.getDoubleProperty("m_Scale");
842

    
843
        int indice = xml.getIntProperty("indice");
844

    
845
        ProjectView view = null;
846

    
847
        if (xml.contains("viewName")){
848
                view = (ProjectView)project.getProjectDocumentByName(xml.getStringProperty("viewName"),ProjectViewFactory.registerName);
849
        }else {
850
                if (indice != -1) {
851
                        try {
852
                                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
853

    
854
                                view = (ProjectView) views.get(indice);
855
                        } catch (IndexOutOfBoundsException e) {
856
                                NotificationManager.addError("No se ha encontrado la vista de indice "+ indice,
857
                                            e);
858
                        }
859
                }
860
        }
861

    
862

    
863
        if (view != null) {
864
                this.setView(view);
865

    
866
                try {
867
                        if (xml.contains("extentX")) {
868
                                if (m_bLinked) {
869
                                        this.getMapContext().getViewPort().setExtent(new Rectangle2D.Double(
870
                                                        xml.getDoubleProperty("extentX"),
871
                                                        xml.getDoubleProperty("extentY"),
872
                                                        xml.getDoubleProperty("extentW"),
873
                                                        xml.getDoubleProperty("extentH")));
874
                                } else if (!m_bLinked) {
875
                                        this.m_fmap = MapContext.createFromXML(xml.getChild(0));
876
                                }
877
                        }
878
                } catch (XMLException e) {
879
                        NotificationManager.addError("Pasando las propiedades del XMLEntity al objeto",
880
                                        e);
881
                }
882
        } else if (!m_bLinked) {
883
            try {
884
                this.m_fmap = MapContext.createFromXML(xml.getChild(0));
885
            } catch (XMLException e) {
886
                e.printStackTrace();
887
            }
888
        }
889
    }
890

    
891
    /**
892
     * DOCUMENT ME!
893
     *
894
     * @param arg0 DOCUMENT ME!
895
     *
896
     * @return DOCUMENT ME!
897
     */
898
    public boolean compare(Object arg0) {
899
        if (!(arg0 instanceof FFrameView)) {
900
            return false;
901
        }
902

    
903
        if (!this.getName().equals(((FFrameView) arg0).getName())) {
904
            return false;
905
        }
906

    
907
        if (Math.abs(this.getBoundBox().getWidth()-(((FFrameView) arg0).getBoundBox().getWidth()))>0.05) {
908
            return false;
909
        }
910
        if (Math.abs(this.getBoundBox().getHeight()-(((FFrameView) arg0).getBoundBox().getHeight()))>0.05) {
911
            return false;
912
        }
913

    
914
        if (!this.toString().equals(((FFrameView) arg0).toString())) {
915
            return false;
916
        }
917

    
918
        if (this.getMapContext()!=null && !this.getMapContext().equals(((FFrameView) arg0).getMapContext())) {
919
            return false;
920
        }
921

    
922
        return true;
923
    }
924

    
925
    /**
926
     * DOCUMENT ME!
927
     *
928
     * @param arg0 DOCUMENT ME!
929
     *
930
     * @return DOCUMENT ME!
931
     */
932
    public boolean equals(Object arg0) {
933
        return this.compare(arg0);
934
    }
935

    
936
        public void refresh() {
937
                if (view!=null && (getTypeScale() == MANUAL || getTypeScale() == CONSTANTE ))
938
                        getMapContext().getViewPort().setExtent(getNewExtent(getScale()));
939
                refresh=true;
940
        }
941

    
942
        public void fullExtent() throws DriverException {
943
                setNewExtent(getMapContext().getFullExtent());
944
        }
945

    
946
        public void setPointsToZoom(Point2D px1, Point2D px2) {
947
                p1=px1;
948
                p2=px2;
949
        }
950

    
951
        public void movePoints(Point2D px1, Point2D px2) {
952
                double difX=-px2.getX()+px1.getX();
953
                double difY=-px2.getY()+px1.getY();
954
                if (p1!=null) {
955
                        p1.setLocation(p1.getX()+difX,p1.getY()+difY);
956
                        p2.setLocation(p2.getX()+difX,p2.getY()+difY);
957
                }
958
        }
959

    
960
        public void cloneActions(IFFrame frame) {
961
                if (view ==null || view.getMapContext()==null)
962
                        return;
963
                if (m_bLinked) {
964
            if (getTypeScale() == AUTOMATICO) {
965
                    view.getMapContext().getViewPort().addViewPortListener(this);
966
                view.getMapContext().addLayerListener(this);
967
            } else if (getTypeScale() == CONSTANTE) {
968
                view.getMapContext().getViewPort().removeViewPortListener(this);
969
                view.getMapContext().addLayerListener(this);
970
            } else if (getTypeScale() == MANUAL) {
971
                view.getMapContext().getViewPort().removeViewPortListener(this);
972
                view.getMapContext().addLayerListener(this);
973
            }
974
        } else if (!m_bLinked) {
975
                if (getTypeScale() == AUTOMATICO) {
976
                view.getMapContext().getViewPort().addViewPortListener(this);
977
            } else if (getTypeScale() == CONSTANTE) {
978
                view.getMapContext().getViewPort().removeViewPortListener(this);
979
            } else if (getTypeScale() == MANUAL) {
980
                view.getMapContext().getViewPort().removeViewPortListener(this);
981
            }
982
        }
983
                ((FFrameView)frame).view.getMapContext().removeLayerListener((FFrameView)frame);
984
            ((FFrameView)frame).view.getMapContext().getViewPort().removeViewPortListener((FFrameView)frame);
985

    
986

    
987
        }
988
        public IFFrame cloneFFrame(Layout layout) {
989
                FFrameView frame =(FFrameView)FrameFactory.createFrameFromName(FFrameViewFactory.registerName);
990
        frame.setLevel(this.getLevel());
991
        frame.setNum(this.num);
992
        frame.setName(this.getName());
993
        frame.setBoundBox(this.getBoundBox());
994
        frame.setTag(this.getTag());
995
        frame.m_Mode = this.m_Mode;
996
        frame.m_typeScale = this.m_typeScale;
997
        frame.m_extension = this.m_extension;
998
        frame.m_quality = this.m_quality;
999
        frame.m_viewing = this.m_viewing;
1000
        frame.m_bLinked = this.m_bLinked;
1001
        frame.m_mapUnits = this.m_mapUnits;
1002
        frame.setRotation(this.getRotation());
1003

    
1004
        frame.m_Scale = this.m_Scale;
1005
        frame.view=this.getView();
1006
        frame.m_fmap = this.getMapContext();
1007
        frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
1008
        frame.setLayout(layout);
1009

    
1010
        if (frame instanceof IFFrameViewDependence) {
1011
            ((IFFrameViewDependence) frame).initDependence(layout.getLayoutContext().getAllFFrames());
1012
        }
1013
        cloneActions(frame);
1014
        return frame;
1015
    }
1016

    
1017
        public IFFrameDialog getPropertyDialog() {
1018
                return new FFrameViewDialog(getLayout(),this);
1019
        }
1020
}