Statistics
| Revision:

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

History | View | Annotate | Download (34.1 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
import java.util.Iterator;
58

    
59
import javax.print.attribute.PrintRequestAttributeSet;
60

    
61
import org.cresques.cts.IProjection;
62

    
63
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.messages.NotificationManager;
66
import com.iver.cit.gvsig.ProjectExtension;
67
import com.iver.cit.gvsig.fmap.ColorEvent;
68
import com.iver.cit.gvsig.fmap.ExtentEvent;
69
import com.iver.cit.gvsig.fmap.MapContext;
70
import com.iver.cit.gvsig.fmap.ProjectionEvent;
71
import com.iver.cit.gvsig.fmap.ViewPort;
72
import com.iver.cit.gvsig.fmap.ViewPortListener;
73
import com.iver.cit.gvsig.fmap.core.FShape;
74
import com.iver.cit.gvsig.fmap.layers.LegendChangedEvent;
75
import com.iver.cit.gvsig.fmap.layers.XMLException;
76
import com.iver.cit.gvsig.fmap.rendering.LegendListener;
77
import com.iver.cit.gvsig.project.Project;
78
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
79
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
80
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
81
import com.iver.cit.gvsig.project.documents.layout.LayoutControl;
82
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
83
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
84
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
85
import com.iver.cit.gvsig.project.documents.view.ProjectView;
86
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
87
import com.iver.utiles.XMLEntity;
88

    
89

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

    
111
    private BufferedImage m_image = null;
112
    private AffineTransform at = null;
113
    private Project project = null;
114
    private double scaleAnt;
115
        private boolean refresh=false;
116
        private Point origin;
117
        private Point2D p1;
118
        private Point2D p2;
119
        private IFFrame grid;
120
        private boolean showGrid=false;
121
        /**
122
     * Creates a new FFrameView object.
123
     */
124
    public FFrameView() {
125
        num++;
126
    }
127

    
128
    /**
129
     * Devuelve una descripci?n del FFrameView.
130
     *
131
     * @return Descripci?n.
132
     */
133
    public String toString() {
134
        if (getView() == null) {
135
            return "FFrameView " + num + ": " + "Vacio";
136
        }
137

    
138
        return "FFrameView " + num + ": " + getView().getName();
139
    }
140

    
141
    /**
142
     * Rellena la escala de la vista que contiene el fframe.
143
     *
144
     * @param d escala de la vista.
145
     */
146
    public void setScale(double d) {
147
        m_Scale = d;
148
    }
149

    
150
    /**
151
     * Inserta el nuevo extent a la FFrameView.
152
     *
153
     * @param r Rect?ngulo a ocupar por el FFrameView.
154
     */
155
    public void setNewExtent(Rectangle2D r) {
156
        getMapContext().getViewPort().setExtent(r);
157
        refresh = true;
158
        m_Scale = FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
159
                getBoundBox().width,
160
                getBoundingBox(null).width / getBoundBox().width);
161
    }
162

    
163
    /**
164
     * Devuelve el FMap de la vista o una clonaci?n de este si se utiliza una
165
     * escala fija.
166
     *
167
     * @return FMap.
168
     */
169
    public MapContext getMapContext() {
170
            return m_fmap;
171
    }
172

    
173
    /**
174
     * Rellena la calidad que se quiere aplicar.
175
     *
176
     * @param q entero que representa la calidad a aplicar.
177
     */
178
    public void setQuality(int q) {
179
        m_quality = q;
180
    }
181

    
182
    /**
183
     * Devuelve un entero que representa la calidad que est? seleccionada.
184
     *
185
     * @return tipo de calidad selccionada.
186
     */
187
    public int getQuality() {
188
        return m_quality;
189
    }
190

    
191
    /**
192
     * Devuelve un entero que representa la forma en que se actualiza la vista.
193
     *
194
     * @return forma que se actualiza la vista.
195
     */
196
    public int getViewing() {
197
        return m_viewing;
198
    }
199

    
200
    /**
201
     * Rellena la forma de actualizar la vista(cuando activo o siempre). De
202
     * momento esta opci?n esta deshabilitada.
203
     *
204
     * @param v entero que representa la forma de actualizar la vista.
205
     */
206
    public void setViewing(int v) {
207
        m_viewing = v;
208
    }
209

    
210
    /**
211
     * Inserta el ProjectView de donde obtener las propiedades de la vista a
212
     * mostrar.
213
     *
214
     * @param v Modelo de la vista.
215
     */
216
    public void setView(ProjectView v) {
217
            view=v;
218
            ViewPort vp =null;
219
            if (getMapContext()!=null){
220
                vp=getMapContext().getViewPort();
221
        }else{
222
                vp = v.getMapContext().getViewPort().cloneViewPort();
223
        }
224
        vp.setImageSize(new Dimension((int) getBoundingBox(null).width,
225
                (int) getBoundingBox(null).height));
226

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

    
276
    /**
277
     * Devuelve el modelo de la vista.
278
     *
279
     * @return Modelo de la vista.
280
     */
281
    public ProjectView getView() {
282
        return view;
283
    }
284

    
285
    /**
286
     * Devuelve un Rect?ngulo que representa  el extent de la vista que se
287
     * requiere a partir  de una escala.
288
     *
289
     * @param scale Escala a mostrar.
290
     *
291
     * @return Rect?ngulo.
292
     */
293
    protected Rectangle2D getNewExtent(long scale) {
294
        double hview = getBoundBox().getHeight();
295
        double wview = getBoundBox().getWidth();
296
        double hextent = (scale * hview) / 100.0;
297
        double wextent = (scale * wview) / 100.0;
298

    
299
        if (m_fmap.getViewPort().getExtent()==null)
300
                return new Rectangle2D.Double();
301
        double newx = m_fmap.getViewPort().getExtent().getCenterX() -
302
            (wextent / 2.0);
303
        double newy = m_fmap.getViewPort().getExtent().getCenterY() -
304
            (hextent / 2.0);
305
        IProjection proj=m_fmap.getViewPort().getProjection();
306
                 Rectangle2D r = new Rectangle2D.Double(newx, newy, wextent, hextent);
307
        if (!proj.isProjected()){
308
                         r = m_fmap.getViewPort().getProjection().getExtent(r,scale,wview,hview,1,100,2.54);
309
                }
310
        return r;
311
    }
312

    
313
    /**
314
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
315
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
316
         * de dibujar.
317
         *
318
         * @param g
319
         *            Graphics2D
320
         * @param at
321
         *            Transformada af?n.
322
         * @param rv
323
         *            rect?ngulo sobre el que hacer un clip.
324
         * @param imgBase
325
         *            Imagen para acelerar el dibujado.
326
         */
327
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
328
                        BufferedImage imgBase)
329
                        throws ReadDriverException {
330
                Rectangle2D.Double r = getBoundingBox(at);
331
                if (getRotation() != 0)
332
                        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
333
                                        + (r.height / 2));
334
                if (intersects(rv, r)) {
335
                        if (getMapContext() == null) {
336
                                drawEmpty(g);
337
                        } else {
338
                                if (rv != null) {
339
                                        // Dibujamos en pantalla
340
                                        Rectangle rclip = (Rectangle)g.getClipBounds().clone();
341
                                        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r
342
                                                        .getWidth(), (int) r.getHeight());
343

    
344
                                        if (getQuality() == PRESENTACION) {
345
                                                if (rv.intersects(r)) {
346
                                                        ViewPort viewPort = this.getMapContext()
347
                                                        .getViewPort();
348
                                                        Color theBackColor = viewPort.getBackColor();
349

    
350
                                                        if (origin != null
351
                                                                        && origin.equals(getLayout().getLayoutControl().getRectOrigin())
352
                                                                        && getLayout() != null
353
                                                                        && getLayout().getLayoutControl().getAT().getScaleX() == scaleAnt
354
                                                                        && m_image != null && !refresh && !(r.getWidth() > getLayout().getWidth()
355
                                                                                        || r.getHeight() > getLayout().getHeight())) {
356
                                                                if (theBackColor != null) {
357
                                                                        g.setColor(theBackColor);
358
                                                                        g.fillRect((int) r.x, (int) r.y, viewPort
359
                                                                                        .getImageWidth(), viewPort
360
                                                                                        .getImageHeight());
361
                                                                }
362
                                                                g.translate(r.getX(), r.getY());
363
                                                                g.drawImage(m_image, 0, 0, getLayout().getLayoutControl());
364
                                                                g.translate(-r.getX(), -r.getY());
365
                                                                scaleAnt = getLayout().getLayoutControl().getAT().getScaleX();
366
                                                                origin = (Point) getLayout().getLayoutControl().getRectOrigin();
367
                                                        } else {
368

    
369
//                                                                System.err.println("r : " + r);
370
                                                                if (r.getWidth() > getLayout().getWidth()
371
                                                                                || r.getHeight() > getLayout().getHeight()) {
372
                                                                        viewPort.setOffset(
373
                                                                                        new Point2D.Double(r.getX(), r
374
                                                                                                        .getY()));
375
                                                                        viewPort.setImageSize(
376
                                                                                        new Dimension((int) r.getWidth(),
377
                                                                                                        (int) r.getHeight()));
378
                                                                        MapContext fmap = getMapContext().cloneToDraw();
379
                                                                        ViewPort viewp = viewPort.cloneViewPort();
380
                                                                        viewp.setImageSize(new Dimension(
381
                                                                                        getLayout().getWidth(),
382
                                                                                        getLayout().getHeight()));
383
                                                                        Rectangle2D r1 = calculateExtent();
384
                                                                        double width = getLayout().getLayoutContext().getAtributes()
385
                                                                                        .getSizeInUnits().getAncho();
386
                                                                        double scale = FLayoutUtilities
387
                                                                                        .getScaleView(viewp, width, r1
388
                                                                                                        .getWidth()
389
                                                                                                        / width);
390
                                                                        viewp.setExtent(r1);
391

    
392
                                                                        fmap.setViewPort(viewp);
393
                                                                        g.translate(-r.getX(), -r.getY());
394
                                                                        if (theBackColor != null) {
395
                                                                                g.setColor(theBackColor);
396
                                                                                g.fillRect((int) r.x, (int) r.y, viewp
397
                                                                                                .getImageWidth(), viewp
398
                                                                                                .getImageHeight());
399
                                                                        }
400
                                                                        fmap.draw(imgBase, g, scale);
401
                                                                        g.translate(r.getX(), r.getY());
402

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

    
450
                if (getMapContext() != null) {
451
                        setATMap(getMapContext().getViewPort()
452
                                        .getAffineTransform());
453

    
454
                }
455
                if (showGrid && grid!=null){
456
                        ((FFrameGrid)grid).setFFrameDependence(this);
457
                        grid.draw(g,at,rv,imgBase);
458
                }
459
        }
460

    
461
    private Rectangle2D calculateExtent() {
462
            Rectangle2D.Double r = new Rectangle2D.Double();
463
                if (p1==null||p2==null)
464
                        return r;
465
            r.setFrameFromDiagonal(p1, p2);
466
                return r;
467
    }
468

    
469
    public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet prroperties)
470
        throws ReadDriverException {
471
        draw(g, at, null, null);
472
    }
473

    
474
    private void printX(Graphics2D g, AffineTransform at) {
475
        Rectangle2D.Double r = getBoundingBox(at);
476

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

    
485
        try {
486
                this.getMapContext().print(g, getScale(),getLayout().getLayoutContext().getAtributes().toPrintAttributes());
487
        } catch (ReadDriverException e) {
488
            NotificationManager.addError(e.getMessage(), e);
489
        }
490

    
491
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
492
    }
493

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

    
503
    /**
504
     * Obtiene la unidad de medida en la que est? la vista.
505
     *
506
     * @return Unidad de medida.
507
     */
508
    public int getMapUnits() {
509
        return m_mapUnits;
510
    }
511

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

    
534
        return (long) m_Scale;
535
    }
536

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

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

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

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

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

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

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

    
605
        try {
606
//            xml.putProperty("type", Layout.RECTANGLEVIEW);
607
            xml.putProperty("m_Mode", m_Mode);
608
            xml.putProperty("m_typeScale", m_typeScale);
609
            xml.putProperty("m_extension", m_extension);
610
            xml.putProperty("m_quality", m_quality);
611
            xml.putProperty("m_viewing", m_viewing);
612
            xml.putProperty("m_bLinked", m_bLinked);
613
            xml.putProperty("m_mapUnits", m_mapUnits);
614
            xml.putProperty("m_Scale", m_Scale);
615

    
616
            ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
617
            ArrayList views = pe.getProject().getDocumentsByType(ProjectViewFactory.registerName);
618

    
619
            boolean hasIndex = false;
620

    
621

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

    
633
            if (!hasIndex) {
634
                xml.putProperty("indice", -1);
635
            }
636

    
637
            if (getMapContext() != null && getMapContext().getViewPort().getExtent()!=null) {
638
                xml.putProperty("extentX",
639
                    getMapContext().getViewPort().getExtent().getX());
640
                xml.putProperty("extentY",
641
                    getMapContext().getViewPort().getExtent().getY());
642
                xml.putProperty("extentW",
643
                    getMapContext().getViewPort().getExtent().getWidth());
644
                xml.putProperty("extentH",
645
                    getMapContext().getViewPort().getExtent().getHeight());
646

    
647
                xml.addChild(getMapContext().getXMLEntity());
648
            }
649
            xml.putProperty("showGrid", showGrid);
650
            if (grid!=null)
651
                    xml.addChild(grid.getXMLEntity());
652
        } catch (Exception e) {
653
            throw new SaveException(e, this.getClass().getName());
654
        }
655

    
656
        return xml;
657
    }
658

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
844
        ProjectView view = null;
845

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

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

    
861

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

    
865
                try {
866
                        if (xml.contains("extentX")) {
867
                                if (m_bLinked) {
868
                                        this.getMapContext().getViewPort().setExtent(new Rectangle2D.Double(
869
                                                        xml.getDoubleProperty("extentX"),
870
                                                        xml.getDoubleProperty("extentY"),
871
                                                        xml.getDoubleProperty("extentW"),
872
                                                        xml.getDoubleProperty("extentH")));
873
                                } else if (!m_bLinked) {
874
                                        this.m_fmap = MapContext.createFromXML(xml.getChild(0));
875
                                }
876
                        }
877
                } catch (XMLException e) {
878
                        NotificationManager.addError("Pasando las propiedades del XMLEntity al objeto",
879
                                        e);
880
                }
881
        } else if (!m_bLinked) {
882
            try {
883
                this.m_fmap = MapContext.createFromXML(xml.getChild(0));
884
            } catch (XMLException e) {
885
                e.printStackTrace();
886
            }
887
        }
888
        if (xml.contains("showGrid")){
889
                showGrid=xml.getBooleanProperty("showGrid");
890
        }
891
        Iterator iterator=xml.findChildren("fframegrid","fframegrid");
892
        while (iterator.hasNext()) {
893
                        XMLEntity element = (XMLEntity) iterator.next();
894
                        FFrameGridFactory gridFactory=new FFrameGridFactory();
895
                        grid=gridFactory.createFrame();//FFrameGridFactory.createFrameFromName("grid : "+this.getName());//element,project,getLayout());
896
                        grid.setXMLEntity(element);
897
                        grid.setLayout(getLayout());
898
                }
899
    }
900

    
901
    /**
902
     * DOCUMENT ME!
903
     *
904
     * @param arg0 DOCUMENT ME!
905
     *
906
     * @return DOCUMENT ME!
907
     */
908
    public boolean compare(Object arg0) {
909
        if (!(arg0 instanceof FFrameView)) {
910
            return false;
911
        }
912

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

    
917
        if (Math.abs(this.getBoundBox().getWidth()-(((FFrameView) arg0).getBoundBox().getWidth()))>0.05) {
918
            return false;
919
        }
920
        if (Math.abs(this.getBoundBox().getHeight()-(((FFrameView) arg0).getBoundBox().getHeight()))>0.05) {
921
            return false;
922
        }
923

    
924
        if (!this.toString().equals(((FFrameView) arg0).toString())) {
925
            return false;
926
        }
927

    
928
        if (this.getMapContext()!=null && !this.getMapContext().equals(((FFrameView) arg0).getMapContext())) {
929
            return false;
930
        }
931

    
932
        if (this.getRotation()!=((FFrameView)arg0).getRotation()){
933
                return false;
934
        }
935
        return true;
936
    }
937

    
938
    /**
939
     * DOCUMENT ME!
940
     *
941
     * @param arg0 DOCUMENT ME!
942
     *
943
     * @return DOCUMENT ME!
944
     */
945
    public boolean equals(Object arg0) {
946
        return this.compare(arg0);
947
    }
948

    
949
        public void refresh() {
950
                if (view!=null && (getTypeScale() == MANUAL || getTypeScale() == CONSTANTE ))
951
                        getMapContext().getViewPort().setExtent(getNewExtent(getScale()));
952
                refresh=true;
953
        }
954

    
955
        public void fullExtent() throws ReadDriverException {
956
                setNewExtent(getMapContext().getFullExtent());
957
        }
958

    
959
        public void setPointsToZoom(Point2D px1, Point2D px2) {
960
                p1=px1;
961
                p2=px2;
962
        }
963

    
964
        public void movePoints(Point2D px1, Point2D px2) {
965
                double difX=-px2.getX()+px1.getX();
966
                double difY=-px2.getY()+px1.getY();
967
                if (p1!=null) {
968
                        p1.setLocation(p1.getX()+difX,p1.getY()+difY);
969
                        p2.setLocation(p2.getX()+difX,p2.getY()+difY);
970
                }
971
        }
972

    
973
        public void cloneActions(IFFrame frame) {
974
                if (view ==null || view.getMapContext()==null)
975
                        return;
976
                if (m_bLinked) {
977
            if (getTypeScale() == AUTOMATICO) {
978
                    view.getMapContext().getViewPort().addViewPortListener(this);
979
                view.getMapContext().addLayerListener(this);
980
            } else if (getTypeScale() == CONSTANTE) {
981
                view.getMapContext().getViewPort().removeViewPortListener(this);
982
                view.getMapContext().addLayerListener(this);
983
            } else if (getTypeScale() == MANUAL) {
984
                view.getMapContext().getViewPort().removeViewPortListener(this);
985
                view.getMapContext().addLayerListener(this);
986
            }
987
        } else if (!m_bLinked) {
988
                if (getTypeScale() == AUTOMATICO) {
989
                view.getMapContext().getViewPort().addViewPortListener(this);
990
            } else if (getTypeScale() == CONSTANTE) {
991
                view.getMapContext().getViewPort().removeViewPortListener(this);
992
            } else if (getTypeScale() == MANUAL) {
993
                view.getMapContext().getViewPort().removeViewPortListener(this);
994
            }
995
        }
996
                ((FFrameView)frame).view.getMapContext().removeLayerListener((FFrameView)frame);
997
            ((FFrameView)frame).view.getMapContext().getViewPort().removeViewPortListener((FFrameView)frame);
998

    
999

    
1000
        }
1001
        public IFFrame cloneFFrame(Layout layout) {
1002
                FFrameView frame =(FFrameView)FrameFactory.createFrameFromName(FFrameViewFactory.registerName);
1003
        frame.setLevel(this.getLevel());
1004
        frame.setNum(this.num);
1005
        frame.setName(this.getName());
1006
        frame.setBoundBox(this.getBoundBox());
1007
        frame.setTag(this.getTag());
1008
        frame.m_Mode = this.m_Mode;
1009
        frame.m_typeScale = this.m_typeScale;
1010
        frame.m_extension = this.m_extension;
1011
        frame.m_quality = this.m_quality;
1012
        frame.m_viewing = this.m_viewing;
1013
        frame.m_bLinked = this.m_bLinked;
1014
        frame.m_mapUnits = this.m_mapUnits;
1015
        frame.setRotation(this.getRotation());
1016

    
1017
        frame.m_Scale = this.m_Scale;
1018
        frame.view=this.getView();
1019
        frame.m_fmap = this.getMapContext();
1020
        frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
1021
        frame.setLayout(layout);
1022

    
1023
        if (frame instanceof IFFrameViewDependence) {
1024
            ((IFFrameViewDependence) frame).initDependence(layout.getLayoutContext().getAllFFrames());
1025
        }
1026
        if (grid!=null){
1027
                FFrameGrid newGrid=(FFrameGrid)this.grid.cloneFFrame(layout);
1028
                newGrid.setFFrameDependence(frame);
1029
                frame.setGrid(newGrid);
1030
        }
1031
        cloneActions(frame);
1032
        return frame;
1033
    }
1034
        public void setGrid(IFFrame grid) {
1035
                this.grid=grid;
1036
                this.grid.setRotation(this.getRotation());
1037
        }
1038

    
1039
        public void setRotation(double rotation) {
1040
                super.setRotation(rotation);
1041
                if (grid!=null)
1042
                        grid.setRotation(rotation);
1043
        }
1044
        public IFFrameDialog getPropertyDialog() {
1045
                return new FFrameViewDialog(getLayout(),this);
1046
        }
1047
        public void showGrid(boolean b){
1048
                showGrid=b;
1049
        }
1050

    
1051
        public boolean isShowGrid() {
1052
                return showGrid;
1053
        }
1054
}