Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameView.java @ 28368

History | View | Annotate | Download (34.3 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
                Rectangle2D.Double r = getBoundingBox(at);
330
                if (getRotation() != 0)
331
                        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
332
                                        + (r.height / 2));
333
                if (intersects(rv, r)) {
334
                        if (getMapContext() == null) {
335
                                drawEmpty(g);
336
                        } else {
337
                                if (rv != null) {
338
                                        // Dibujamos en pantalla
339
                                        Rectangle rclip = (Rectangle)g.getClipBounds().clone();
340
                                        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r
341
                                                        .getWidth(), (int) r.getHeight());
342

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

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

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

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

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

    
457
                if (getMapContext() != null) {
458
                        setATMap(getMapContext().getViewPort()
459
                                        .getAffineTransform());
460

    
461
                }
462
                if (showGrid && grid!=null){
463
                        ((FFrameGrid)grid).setFFrameDependence(this);
464
                        grid.draw(g,at,rv,imgBase);
465
                }
466
        }
467

    
468
    private Rectangle2D calculateExtent() {
469
            Rectangle2D.Double r = new Rectangle2D.Double();
470
                if (p1==null||p2==null)
471
                        return r;
472
            r.setFrameFromDiagonal(p1, p2);
473
                return r;
474
    }
475

    
476
    public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet prroperties) {
477
        draw(g, at, null, null);
478
    }
479

    
480
    private void printX(Graphics2D g, AffineTransform at) {
481
        Rectangle2D.Double r = getBoundingBox(at);
482

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

    
491
        try {
492
                this.getMapContext().print(g, getScale(),getLayout().getLayoutContext().getAtributes().toPrintAttributes());
493
        } catch (ReadDriverException e) {
494
            NotificationManager.addError(e.getMessage(), e);
495
        }
496

    
497
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
498
    }
499

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

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

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

    
540
        return (long) m_Scale;
541
    }
542

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

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

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

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

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

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

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

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

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

    
625
            boolean hasIndex = false;
626

    
627

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

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

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

    
653
                xml.addChild(getMapContext().getXMLEntity());
654
            }
655
            xml.putProperty("showGrid", showGrid);
656
            if (grid!=null)
657
                    xml.addChild(grid.getXMLEntity());
658
        } catch (Exception e) {
659
            throw new SaveException(e, this.getClass().getName());
660
        }
661

    
662
        return xml;
663
    }
664

    
665
    /**
666
     * Inserta la imagen para repintar el FFrameView.
667
     *
668
     * @param bi Imagen para repintar.
669
     */
670
    public void setBufferedImage(BufferedImage bi) {
671
        m_image = bi;
672
    }
673

    
674
    /**
675
     * Devuelve la imagen para repintar.
676
     *
677
     * @return Imagen para repintar.
678
     */
679
    public BufferedImage getBufferedImage() {
680
        return m_image;
681
    }
682

    
683
    /**
684
     * Devuelve la MAtriz de transformaci?n utilizada por la FFrameView.
685
     *
686
     * @return MAtriz de transformaci?n.
687
     */
688
    public AffineTransform getATMap() {
689
        return at;
690
    }
691

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

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

    
720
        this.setName(xml.getStringProperty("m_name"));
721
        this.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
722
                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
723
                xml.getDoubleProperty("h")));
724

    
725
        this.m_Mode = xml.getIntProperty("m_Mode");
726
        this.m_typeScale = xml.getIntProperty("m_typeScale");
727
        this.m_extension = xml.getIntProperty("m_extension");
728
        this.m_quality = xml.getIntProperty("m_quality");
729
        this.m_viewing = xml.getIntProperty("m_viewing");
730
        this.m_bLinked = xml.getBooleanProperty("m_bLinked");
731
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
732

    
733
        //ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
734
        this.m_Scale = xml.getDoubleProperty("m_Scale");
735

    
736
        int indice = xml.getIntProperty("indice");
737

    
738
        if (indice != -1) {
739
                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
740

    
741
            ProjectView view = (ProjectView) views.get(indice);
742
            this.m_fmap = view.getMapContext();
743
            this.setView(view);
744

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

    
762
    /**
763
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame()
764
     */
765
    public String getNameFFrame() {
766
        return PluginServices.getText(this, "Vista")+ num;
767
    }
768

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

    
782
    /**
783
     * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
784
     */
785
    public void backColorChanged(ColorEvent e) {
786
        if (getLinked()) {
787
            m_fmap.getViewPort().setBackColor(e.getNewColor());
788
            getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
789

    
790
            //setBufferedImage(null);
791
        }
792
    }
793

    
794
    /**
795
     * @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
796
     */
797
        public void projectionChanged(ProjectionEvent e) {
798
        if (getTypeScale() == AUTOMATICO) {
799
            m_fmap.getViewPort().setProjection(e.getNewProjection());
800

    
801
            if (getLayout() != null) {
802
                    getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
803
            }
804
        }
805
        }
806

    
807
    /**
808
     * DOCUMENT ME!
809
     *
810
     * @param e DOCUMENT ME!
811
     */
812
    public void legendChanged(LegendChangedEvent e) {
813
        if (getLinked()) {
814
                getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
815
            refresh=true;
816
            //setBufferedImage(null);
817
        }
818
    }
819

    
820
    /**
821
     * DOCUMENT ME!
822
     *
823
     * @param xml DOCUMENT ME!
824
     */
825
    public void setXMLEntity(XMLEntity xml) {
826
        if (xml.getIntProperty("m_Selected") != 0) {
827
            this.setSelected(true);
828
        } else {
829
            this.setSelected(false);
830
        }
831

    
832
        this.setName(xml.getStringProperty("m_name"));
833
        this.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
834
                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
835
                xml.getDoubleProperty("h")));
836

    
837
        this.m_Mode = xml.getIntProperty("m_Mode");
838
        this.m_typeScale = xml.getIntProperty("m_typeScale");
839
        this.m_extension = xml.getIntProperty("m_extension");
840
        this.m_quality = xml.getIntProperty("m_quality");
841
        this.m_viewing = xml.getIntProperty("m_viewing");
842
        this.m_bLinked = xml.getBooleanProperty("m_bLinked");
843
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
844
        setRotation(xml.getDoubleProperty("m_rotation"));
845

    
846
        this.m_Scale = xml.getDoubleProperty("m_Scale");
847

    
848
        int indice = xml.getIntProperty("indice");
849

    
850
        ProjectView view = null;
851

    
852
        if (xml.contains("viewName")){
853
                view = (ProjectView)project.getProjectDocumentByName(xml.getStringProperty("viewName"),ProjectViewFactory.registerName);
854
        }else {
855
                if (indice != -1) {
856
                        try {
857
                                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
858

    
859
                                view = (ProjectView) views.get(indice);
860
                        } catch (IndexOutOfBoundsException e) {
861
                                NotificationManager.addError("No se ha encontrado la vista de indice "+ indice,
862
                                            e);
863
                        }
864
                }
865
        }
866

    
867

    
868
        if (view != null) {
869
                this.setView(view);
870

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

    
907
    /**
908
     * DOCUMENT ME!
909
     *
910
     * @param arg0 DOCUMENT ME!
911
     *
912
     * @return DOCUMENT ME!
913
     */
914
    public boolean compare(Object arg0) {
915
        if (!(arg0 instanceof FFrameView)) {
916
            return false;
917
        }
918

    
919
        if (!this.getName().equals(((FFrameView) arg0).getName())) {
920
            return false;
921
        }
922

    
923
        if (Math.abs(this.getBoundBox().getWidth()-(((FFrameView) arg0).getBoundBox().getWidth()))>0.05) {
924
            return false;
925
        }
926
        if (Math.abs(this.getBoundBox().getHeight()-(((FFrameView) arg0).getBoundBox().getHeight()))>0.05) {
927
            return false;
928
        }
929

    
930
        if (!this.toString().equals(((FFrameView) arg0).toString())) {
931
            return false;
932
        }
933

    
934
        if (this.getMapContext()!=null && !this.getMapContext().equals(((FFrameView) arg0).getMapContext())) {
935
            return false;
936
        }
937

    
938
        if (this.getRotation()!=((FFrameView)arg0).getRotation()){
939
                return false;
940
        }
941
        return true;
942
    }
943

    
944
    /**
945
     * DOCUMENT ME!
946
     *
947
     * @param arg0 DOCUMENT ME!
948
     *
949
     * @return DOCUMENT ME!
950
     */
951
    public boolean equals(Object arg0) {
952
        return this.compare(arg0);
953
    }
954

    
955
        public void refresh() {
956
                if (view!=null && (getTypeScale() == MANUAL || getTypeScale() == CONSTANTE ))
957
                        getMapContext().getViewPort().setExtent(getNewExtent(getScale()));
958
                refresh=true;
959
        }
960

    
961
        public void fullExtent() throws ReadDriverException {
962
                setNewExtent(getMapContext().getFullExtent());
963
        }
964

    
965
        public void setPointsToZoom(Point2D px1, Point2D px2) {
966
                p1=px1;
967
                p2=px2;
968
        }
969

    
970
        public void movePoints(Point2D px1, Point2D px2) {
971
                double difX=-px2.getX()+px1.getX();
972
                double difY=-px2.getY()+px1.getY();
973
                if (p1!=null) {
974
                        p1.setLocation(p1.getX()+difX,p1.getY()+difY);
975
                        p2.setLocation(p2.getX()+difX,p2.getY()+difY);
976
                }
977
        }
978

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

    
1005

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

    
1023
        frame.m_Scale = this.m_Scale;
1024
        frame.view=this.getView();
1025
        frame.m_fmap = this.getMapContext();
1026
        frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
1027
        frame.setLayout(layout);
1028

    
1029
        if (frame instanceof IFFrameViewDependence) {
1030
            ((IFFrameViewDependence) frame).initDependence(layout.getLayoutContext().getAllFFrames());
1031
        }
1032
        if (grid!=null){
1033
                FFrameGrid newGrid=(FFrameGrid)this.grid.cloneFFrame(layout);
1034
                newGrid.setFFrameDependence(frame);
1035
                frame.setGrid(newGrid);
1036
        }
1037
        frame.showGrid=showGrid;
1038
        cloneActions(frame);
1039
        return frame;
1040
    }
1041
        public void setGrid(IFFrame grid) {
1042
                this.grid=grid;
1043
                this.grid.setRotation(this.getRotation());
1044
        }
1045
        public IFFrame getGrid(){
1046
                return this.grid;
1047
        }
1048

    
1049
        public void setRotation(double rotation) {
1050
                super.setRotation(rotation);
1051
                if (grid!=null)
1052
                        grid.setRotation(rotation);
1053
        }
1054
        public IFFrameDialog getPropertyDialog() {
1055
                return new FFrameViewDialog(getLayout(),this);
1056
        }
1057
        public void showGrid(boolean b){
1058
                showGrid=b;
1059
        }
1060

    
1061
        public boolean isShowGrid() {
1062
                return showGrid;
1063
        }
1064
}