Statistics
| Revision:

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

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().clone();
367
                                                        } else {
368

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

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

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

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

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

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

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

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

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

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

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

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

    
533
        return (long) m_Scale;
534
    }
535

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

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

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

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

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

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

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

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

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

    
618
            boolean hasIndex = false;
619

    
620

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

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

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

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

    
655
        return xml;
656
    }
657

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
843
        ProjectView view = null;
844

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

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

    
860

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
998

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

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

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

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

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