Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / fframes / FFrameView.java @ 31496

History | View | Annotate | Download (35.4 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 org.gvsig.app.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

    
57
import org.cresques.cts.IProjection;
58
import org.gvsig.andami.PluginServices;
59
import org.gvsig.andami.messages.NotificationManager;
60
import org.gvsig.app.project.Project;
61
import org.gvsig.app.project.documents.exceptions.SaveException;
62
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
63
import org.gvsig.app.project.documents.layout.LayoutControl;
64
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameViewDialog;
65
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
66
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
67
import org.gvsig.app.project.documents.view.DefaultViewDocument;
68
import org.gvsig.compat.print.PrintAttributes;
69
import org.gvsig.fmap.dal.exception.ReadException;
70
import org.gvsig.fmap.geom.Geometry;
71
import org.gvsig.fmap.geom.GeometryLocator;
72
import org.gvsig.fmap.geom.GeometryManager;
73
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
74
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
75
import org.gvsig.fmap.geom.primitive.Envelope;
76
import org.gvsig.fmap.mapcontext.MapContext;
77
import org.gvsig.fmap.mapcontext.MapContextException;
78
import org.gvsig.fmap.mapcontext.ViewPort;
79
import org.gvsig.fmap.mapcontext.events.ColorEvent;
80
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
81
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
82
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
83
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
84
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
85
import org.gvsig.utils.XMLEntity;
86
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
88

    
89

    
90

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

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

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

    
140
        return "FFrameView " + num + ": " + getView().getName();
141
    }
142

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

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

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

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

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

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

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

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

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

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

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

    
301
        if (m_fmap.getViewPort().getEnvelope()==null) {
302
                        try {
303
                                return geomManager.createEnvelope(0,0,0,0, SUBTYPES.GEOM2D);
304
                        } catch (CreateEnvelopeException e) {
305
                                logger.error("Error creating the envelope", e);
306
                                return null;
307
                        }
308
                }
309
        double newx = m_fmap.getViewPort().getEnvelope().getCenter(Geometry.DIMENSIONS.X) - (wextent / 2.0);
310
        double newy = m_fmap.getViewPort().getEnvelope().getCenter(Geometry.DIMENSIONS.Y) -  (hextent / 2.0);
311
        IProjection proj=m_fmap.getViewPort().getProjection();
312
                 Envelope r = null;
313
                try {
314
                        r = geomManager.createEnvelope(newx, newy, newx+ wextent,newy+ hextent, SUBTYPES.GEOM2D);
315
                } catch (CreateEnvelopeException e) {
316
                        logger.error("Error creating the envelope", e);
317
                }
318
        if (!proj.isProjected()){
319
                Rectangle2D auxR=new Rectangle2D.Double(newx,newy,wextent,hextent);
320
                         Rectangle2D aux = m_fmap.getViewPort().getProjection().getExtent(auxR,scale,wview,hview,1,100,2.54);
321
                         try {
322
                                r = geomManager.createEnvelope(aux.getX(),aux.getY(), aux.getMaxX(),aux.getMaxY(), SUBTYPES.GEOM2D);
323
                        } catch (CreateEnvelopeException e) {
324
                                logger.error("Error creating the envelope", e);
325
                        }
326
        }
327
        return r;
328
    }
329

    
330
    /**
331
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
332
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
333
         * de dibujar.
334
         *
335
         * @param g
336
         *            Graphics2D
337
         * @param at
338
         *            Transformada af?n.
339
         * @param rv
340
         *            rect?ngulo sobre el que hacer un clip.
341
         * @param imgBase
342
         *            Imagen para acelerar el dibujado.
343
         */
344
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
345
                        BufferedImage imgBase) {
346
                Rectangle2D.Double r = getBoundingBox(at);
347
                if (getRotation() != 0) {
348
                        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
349
                                        + (r.height / 2));
350
                }
351
                if (intersects(rv, r)) {
352
                        if (getMapContext() == null) {
353
                                drawEmpty(g);
354
                        } else {
355
                                if (rv != null) {
356
                                        // Dibujamos en pantalla
357
                                        Rectangle rclip = null;
358
                                        if (g.getClipBounds()!=null) {
359
                                                rclip = (Rectangle)g.getClipBounds().clone();
360
                                        }
361
                                        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r
362
                                                        .getWidth(), (int) r.getHeight());
363

    
364
                                        if (getQuality() == PRESENTACION) {
365
                                                if (rv.intersects(r)) {
366
                                                        ViewPort viewPort = this.getMapContext()
367
                                                        .getViewPort();
368
                                                        Color theBackColor = viewPort.getBackColor();
369

    
370
                                                        if (origin != null
371
                                                                        && origin.equals(getLayout().getLayoutControl().getRectOrigin())
372
                                                                        && getLayout() != null
373
                                                                        && getLayout().getLayoutControl().getAT().getScaleX() == scaleAnt
374
                                                                        && m_image != null && !refresh && !(r.getWidth() > getLayout().getWidth()
375
                                                                                        || r.getHeight() > getLayout().getHeight())) {
376
                                                                if (theBackColor != null) {
377
                                                                        g.setColor(theBackColor);
378
                                                                        g.fillRect((int) r.x, (int) r.y, viewPort
379
                                                                                        .getImageWidth(), viewPort
380
                                                                                        .getImageHeight());
381
                                                                }
382
                                                                g.translate(r.getX(), r.getY());
383
                                                                g.drawImage(m_image, 0, 0, getLayout().getLayoutControl());
384
                                                                g.translate(-r.getX(), -r.getY());
385
                                                                scaleAnt = getLayout().getLayoutControl().getAT().getScaleX();
386
                                                                origin = getLayout().getLayoutControl().getRectOrigin();//.clone();
387
                                                        } else {
388

    
389
//                                                                System.err.println("r : " + r);
390
                                                                if (r.getWidth() > getLayout().getWidth()
391
                                                                                || r.getHeight() > getLayout().getHeight()) {
392
                                                                        viewPort.setOffset(
393
                                                                                        new Point2D.Double(r.getX(), r
394
                                                                                                        .getY()));
395
                                                                        viewPort.setImageSize(
396
                                                                                        new Dimension((int) r.getWidth(),
397
                                                                                                        (int) r.getHeight()));
398
                                                                        MapContext fmap = getMapContext().cloneToDraw();
399
                                                                        ViewPort viewp = viewPort.cloneViewPort();
400
                                                                        viewp.setImageSize(new Dimension(
401
                                                                                        getLayout().getWidth(),
402
                                                                                        getLayout().getHeight()));
403
                                                                        Envelope r1 = calculateEnvelope();
404
                                                                        double width = getLayout().getLayoutContext().getAttributes()
405
                                                                                        .getSizeInUnits().getAncho();
406
                                                                        double scale = FLayoutUtilities
407
                                                                                        .getScaleView(viewp, width, r1
408
                                                                                                        .getLength(0)
409
                                                                                                        / width);
410
                                                                        viewp.setEnvelope(r1);
411

    
412
                                                                        fmap.setViewPort(viewp);
413
                                                                        g.translate(-r.getX(), -r.getY());
414
                                                                        if (theBackColor != null) {
415
                                                                                g.setColor(theBackColor);
416
                                                                                g.fillRect((int) r.x, (int) r.y, viewp
417
                                                                                                .getImageWidth(), viewp
418
                                                                                                .getImageHeight());
419
                                                                        }
420
                                                                        try {
421
                                                                                fmap.draw(imgBase, g, scale);
422
                                                                        } catch (ReadException e) {
423
                                                                                e.printStackTrace();
424
                                                                        } catch (MapContextException mpex) {
425
                                        mpex.printStackTrace();
426
                                                                        }
427

    
428
                                                                        g.translate(r.getX(), r.getY());
429

    
430
                                                                } else {
431
                                                                        viewPort.setOffset(
432
                                                                                        new Point2D.Double(r.x, r.y));
433
                                                                        viewPort.setImageSize(
434
                                                                                        new Dimension((int) r.width,
435
                                                                                                        (int) r.height));
436
                                                                        viewPort.refreshExtent();
437
                                                                        m_image = new BufferedImage((int) r
438
                                                                                        .getWidth(), (int) r.getHeight(),
439
                                                                                        BufferedImage.TYPE_INT_ARGB);
440
                                                                        Graphics2D gimg = (Graphics2D) m_image
441
                                                                                        .getGraphics();
442
                                                                        gimg.translate(-((int) r.getX()), -((int) r
443
                                                                                        .getY()));
444
                                                                        try {
445
                                                                                getMapContext().draw(m_image, gimg, getScale());
446
                                                                        } catch (ReadException e) {
447
                                                                                e.printStackTrace();
448
                                                                        } catch (MapContextException mpex) {
449
                                        mpex.printStackTrace();
450
                                    }
451
                                                                        gimg.translate(((int) r.getX()), ((int) r
452
                                                                                        .getY()));
453
                                                                        if (theBackColor != null) {
454
                                                                                g.setColor(theBackColor);
455
                                                                                g.fillRect((int) r.x, (int) r.y, viewPort
456
                                                                                                .getImageWidth(), viewPort
457
                                                                                                .getImageHeight());
458
                                                                        }
459
                                                                        g.drawImage(m_image, (int) r.getX(),
460
                                                                                        (int) r.getY(), getLayout());
461
                                                                }
462
                                                                scaleAnt = getLayout().getLayoutControl().getAT().getScaleX();
463
                                                                origin = getLayout().getLayoutControl().getRectOrigin();//.clone();
464
                                                                refresh = false;
465
                                                        }
466
                                                }
467
                                        } else {
468
                                                drawDraft(g);
469
                                        }
470
                                        if (rclip != null) {
471
                                                g.setClip(rclip.x, rclip.y, rclip.width,
472
                                                                rclip.height);
473
                                        }
474
                                } else {
475
                                        printX(g, at);
476
                                }
477
                        }
478
                }
479
                if (getRotation() != 0) {
480
                        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
481
                                        + (r.height / 2));
482
                }
483

    
484
                if (getMapContext() != null) {
485
                        setATMap(getMapContext().getViewPort()
486
                                        .getAffineTransform());
487

    
488
                }
489
                if (showGrid && grid!=null){
490
                        ((FFrameGrid)grid).setFFrameDependence(this);
491
                        grid.draw(g,at,rv,imgBase);
492
                }
493
        }
494

    
495
    private Envelope calculateEnvelope() {
496
            if (p1==null||p2==null) {
497
                        try {
498
                                return geomManager.createEnvelope(0,0,0,0, SUBTYPES.GEOM2D);
499
                        } catch (CreateEnvelopeException e) {
500
                                logger.error("Error creating the envelope", e);
501
                        }
502
                }
503
            Envelope r = null;
504
                try {
505
                        r = geomManager.createEnvelope(p1.getX(),p1.getY(),p2.getX(),p2.getY(), SUBTYPES.GEOM2D);
506
                } catch (CreateEnvelopeException e) {
507
                        logger.error("Error creating the envelope", e);
508
                }
509
                return r;
510
    }
511

    
512
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
513
                        PrintAttributes prroperties) {
514
        draw(g, at, null, null);
515
    }
516

    
517
    private void printX(Graphics2D g, AffineTransform at) {
518
        Rectangle2D.Double r = getBoundingBox(at);
519

    
520
        // Dibujamos en impresora
521
        Rectangle rclip = g.getClipBounds();
522
        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r.getWidth(),
523
            (int) r.getHeight());
524
        this.getMapContext().getViewPort().setOffset(new Point2D.Double(r.x, r.y));
525
        this.getMapContext().getViewPort().setImageSize(new Dimension((int) r.width,
526
                (int) r.height));
527

    
528
        try {
529
                this.getMapContext().print(g, getScale(),getLayout().getLayoutContext().getAttributes().toPrintAttributes());
530
        } catch (ReadException e) {
531
            NotificationManager.addError(e.getMessage(), e);
532
        } catch (MapContextException e) {
533
            NotificationManager.addError(e.getMessage(), e);
534
        }
535

    
536
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
537
    }
538

    
539
    /**
540
     * Rellena la unidad de medida en la que est? la vista.
541
     *
542
     * @param i entero que representa la unidad de medida de la vista.
543
     */
544
    public void setMapUnits(int i) {
545
        m_mapUnits = i;
546
    }
547

    
548
    /**
549
     * Obtiene la unidad de medida en la que est? la vista.
550
     *
551
     * @return Unidad de medida.
552
     */
553
    public int getMapUnits() {
554
        return m_mapUnits;
555
    }
556

    
557
    /**
558
     * Devuelve la escala seg?n el tipo de escala que se haya seleccionado al
559
     * a?adida la vista.
560
     *
561
     * @return escala.
562
     */
563
    public long getScale() {
564
        /*if (m_bLinked){
565
           return getScaleView1(METROS);
566
               }
567
         */
568
            if (getMapContext()==null) {
569
                        return 0;
570
                }
571
        if (getTypeScale() == AUTOMATICO) {
572
            return FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
573
                getBoundBox().width, getBoundingBox(null).width);
574
        } else if (getTypeScale() == CONSTANTE) {
575
            return (long) m_Scale;
576
        } else if (getTypeScale() == MANUAL) {
577
            return (long) m_Scale;
578
        }
579

    
580
        return (long) m_Scale;
581
    }
582

    
583
    /**
584
     * Seleccionar si la vista esta relacionada o no con la original.
585
     *
586
     * @param b true si est? ligada y false si no lo est?.
587
     */
588
    public void setLinked(boolean b) {
589
        m_bLinked = b;
590
    }
591

    
592
    /**
593
     * Devuelve si est? ligada o no el FFrameView con la vista.
594
     *
595
     * @return True si la vista est? ligada.
596
     */
597
    public boolean getLinked() {
598
        return m_bLinked;
599
    }
600

    
601
    /**
602
     * Devuelve la opci?n seleccionada:Rellenar marco de la vista o recorte a
603
     * la vista.
604
     *
605
     * @return entero que representa la opci?n elegida.
606
     */
607
    public int getExtension() {
608
        return m_extension;
609
    }
610

    
611
    /**
612
     * Devuelve el tipo de escala que est? seleccionada AUTOMATICO,CONSTANTE o
613
     * MANUAL.
614
     *
615
     * @return entero que representa el tipo seleccionado.
616
     */
617
    public int getTypeScale() {
618
        return m_typeScale;
619
    }
620

    
621
    /**
622
     * Rellenar si se quiere:Rellenar marco de la vista o recorte a la vista.
623
     *
624
     * @param i entero que representa la opci?n elegida.
625
     */
626
    public void setExtension(int i) {
627
        m_extension = i;
628
    }
629

    
630
    /**
631
     * Rellenar el tipo de escala que se desea.
632
     *
633
     * @param i entero que representa el tipo de escala.
634
     */
635
    public void setTypeScale(int i) {
636
        m_typeScale = i;
637
    }
638

    
639
    /**
640
     * DOCUMENT ME!
641
     *
642
     * @return DOCUMENT ME!
643
     *
644
     * @throws SaveException
645
     *
646
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getXMLEntity()
647
     */
648
    public XMLEntity getXMLEntity() throws SaveException {
649
        XMLEntity xml = super.getXMLEntity();
650

    
651
//        try {
652
////            xml.putProperty("type", Layout.RECTANGLEVIEW);
653
//            xml.putProperty("m_Mode", m_Mode);
654
//            xml.putProperty("m_typeScale", m_typeScale);
655
//            xml.putProperty("m_extension", m_extension);
656
//            xml.putProperty("m_quality", m_quality);
657
//            xml.putProperty("m_viewing", m_viewing);
658
//            xml.putProperty("m_bLinked", m_bLinked);
659
//            xml.putProperty("m_mapUnits", m_mapUnits);
660
//            xml.putProperty("m_Scale", m_Scale);
661
//
662
//            ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
663
//            ArrayList views = pe.getProject().getDocumentsByType(ProjectViewFactory.registerName);
664
//
665
//            boolean hasIndex = false;
666
//
667
//
668
//            if (view != null) {
669
//                    xml.putProperty("viewName",view.getName());
670
//                    for (int i = 0; i < views.size(); i++) {
671
//                        if (view.getName().equals(((ProjectView) views.get(i)).getName())) {
672
//                            xml.putProperty("indice", i);
673
//                            hasIndex = true;
674
//                            break;
675
//                        }
676
//                    }
677
//            }
678
//
679
//            if (!hasIndex) {
680
//                xml.putProperty("indice", -1);
681
//            }
682
//
683
//            if (getMapContext() != null && getMapContext().getViewPort().getExtent()!=null) {
684
//                xml.putProperty("extentX",
685
//                    getMapContext().getViewPort().getExtent().getX());
686
//                xml.putProperty("extentY",
687
//                    getMapContext().getViewPort().getExtent().getY());
688
//                xml.putProperty("extentW",
689
//                    getMapContext().getViewPort().getExtent().getWidth());
690
//                xml.putProperty("extentH",
691
//                    getMapContext().getViewPort().getExtent().getHeight());
692
//
693
//                xml.addChild(getMapContext().getXMLEntity());
694
//            }
695
//            xml.putProperty("showGrid", showGrid);
696
//            if (grid!=null) {
697
//                                xml.addChild(grid.getXMLEntity());
698
//                        }
699
//        } catch (Exception e) {
700
//            throw new SaveException(e, this.getClass().getName());
701
//        }
702

    
703
        return xml;
704
    }
705

    
706
    /**
707
     * Inserta la imagen para repintar el FFrameView.
708
     *
709
     * @param bi Imagen para repintar.
710
     */
711
    public void setBufferedImage(BufferedImage bi) {
712
        m_image = bi;
713
    }
714

    
715
    /**
716
     * Devuelve la imagen para repintar.
717
     *
718
     * @return Imagen para repintar.
719
     */
720
    public BufferedImage getBufferedImage() {
721
        return m_image;
722
    }
723

    
724
    /**
725
     * Devuelve la MAtriz de transformaci?n utilizada por la FFrameView.
726
     *
727
     * @return MAtriz de transformaci?n.
728
     */
729
    public AffineTransform getATMap() {
730
        return at;
731
    }
732

    
733
    /**
734
     * Inserta la matriz de transformaci?n.
735
     *
736
     * @param transform Matriz de transformaci?n.
737
     */
738
    public void setATMap(AffineTransform transform) {
739
        at = transform;
740
    }
741
    /**
742
     * Inserta el proyecto.
743
     *
744
     * @param p Proyecto.
745
     */
746
    public void setProject(Project p) {
747
        project = p;
748
    }
749

    
750

    
751
    /**
752
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
753
     */
754
    public String getNameFFrame() {
755
        return PluginServices.getText(this, "Vista")+ num;
756
    }
757

    
758
    /**
759
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
760
     */
761
    public void extentChanged(ExtentEvent e) {
762
        if (getTypeScale() == AUTOMATICO) {
763
            m_fmap.getViewPort().setEnvelope(e.getNewExtent());
764
            if (getLayout() != null) {
765
                    getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
766
            }
767
        }else if (getTypeScale() == MANUAL) {
768
                Envelope oldExtent = m_fmap.getViewPort().getEnvelope();
769
                Envelope newExtent=e.getNewExtent();
770
                double xDif=newExtent.getCenter(0)-oldExtent.getCenter(0);
771
                double yDif=newExtent.getCenter(1)-oldExtent.getCenter(1);
772
                try {
773
                                m_fmap.getViewPort().setEnvelope(geomManager.createEnvelope(oldExtent.getMinimum(0)+xDif,oldExtent.getMinimum(1)+yDif,oldExtent.getMaximum(0)+xDif,oldExtent.getMaximum(1)+yDif, SUBTYPES.GEOM2D));
774
                        } catch (CreateEnvelopeException e1) {
775
                                e1.printStackTrace();
776
                        }
777
            if (getLayout() != null) {
778
                    getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
779
            }
780
        }
781
        refresh=true;
782
    }
783

    
784
    /**
785
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
786
     */
787
    public void backColorChanged(ColorEvent e) {
788
        if (getLinked()) {
789
            m_fmap.getViewPort().setBackColor(e.getNewColor());
790
            getLayout().getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
791

    
792
            //setBufferedImage(null);
793
        }
794
    }
795

    
796
    /**
797
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#projectionChanged(org.gvsig.fmap.mapcontext.events.ProjectionEvent)
798
     */
799
        public void projectionChanged(ProjectionEvent e) {
800
        if (getTypeScale() == AUTOMATICO) {
801
            m_fmap.getViewPort().setProjection(e.getNewProjection());
802

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

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

    
822
    /**
823
     * DOCUMENT ME!
824
     *
825
     * @param xml DOCUMENT ME!
826
     */
827
    public void setXMLEntity(XMLEntity xml) {
828
//        if (xml.getIntProperty("m_Selected") != 0) {
829
//            this.setSelected(true);
830
//        } else {
831
//            this.setSelected(false);
832
//        }
833
//
834
//        this.setName(xml.getStringProperty("m_name"));
835
//        this.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
836
//                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
837
//                xml.getDoubleProperty("h")));
838
//
839
//        this.m_Mode = xml.getIntProperty("m_Mode");
840
//        this.m_typeScale = xml.getIntProperty("m_typeScale");
841
//        this.m_extension = xml.getIntProperty("m_extension");
842
//        this.m_quality = xml.getIntProperty("m_quality");
843
//        this.m_viewing = xml.getIntProperty("m_viewing");
844
//        this.m_bLinked = xml.getBooleanProperty("m_bLinked");
845
//        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
846
//        setRotation(xml.getDoubleProperty("m_rotation"));
847
//
848
//        this.m_Scale = xml.getDoubleProperty("m_Scale");
849
//
850
//        int indice = xml.getIntProperty("indice");
851
//
852
//        ProjectView view = null;
853
//
854
//        if (xml.contains("viewName")){
855
//                view = (ProjectView)project.getProjectDocumentByName(xml.getStringProperty("viewName"),ProjectViewFactory.registerName);
856
//        }else {
857
//                if (indice != -1) {
858
//                        try {
859
//                                ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);
860
//
861
//                                view = (ProjectView) views.get(indice);
862
//                        } catch (IndexOutOfBoundsException e) {
863
//                                NotificationManager.addError("No se ha encontrado la vista de indice "+ indice,
864
//                                            e);
865
//                        }
866
//                }
867
//        }
868
//
869
//
870
//        if (view != null) {
871
//                this.setView(view);
872
//
873
//                try {
874
//                        if (xml.contains("extentX")) {
875
//                                if (m_bLinked) {
876
//                                        double x=xml.getDoubleProperty("extentX");
877
//                                        double y=xml.getDoubleProperty("extentY");
878
//                                        double w=xml.getDoubleProperty("extentW");
879
//                                        double h=xml.getDoubleProperty("extentH");
880
//                                        try {
881
//                                                        this.getMapContext().getViewPort().setEnvelope(geomManager.createEnvelope(x,y,x+w,y+h, SUBTYPES.GEOM2D));
882
//                                                } catch (CreateEnvelopeException e) {
883
//                                                        logger.error("Error creating the envelope", e);
884
//                                                }
885
//                                } else if (!m_bLinked) {
886
//                                        this.m_fmap = MapContext.createFromXML(xml.getChild(0));
887
//                                }
888
//                        }
889
//                } catch (XMLException e) {
890
//                        NotificationManager.addError("Pasando las propiedades del XMLEntity al objeto",
891
//                                        e);
892
//                }
893
//        } else if (!m_bLinked) {
894
//            try {
895
//                this.m_fmap = MapContext.createFromXML(xml.getChild(0));
896
//            } catch (XMLException e) {
897
//                e.printStackTrace();
898
//            }
899
//        }
900
    }
901

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

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

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

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

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

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

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

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

    
957
        public void fullExtent() throws ReadException {
958
                setNewEnvelope(getMapContext().getFullEnvelope());
959
        }
960

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

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

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

    
1002

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

    
1020
        frame.m_Scale = this.m_Scale;
1021
        frame.view=this.getView();
1022
        frame.m_fmap = this.getMapContext();
1023
        frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
1024
        frame.setLayout(layout);
1025
        frame.origin=this.origin;
1026
        frame.scaleAnt=this.scaleAnt;
1027
        frame.m_image=this.m_image;
1028
        frame.refresh=this.refresh;
1029

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

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

    
1063
        public boolean isShowGrid() {
1064
                return showGrid;
1065
        }
1066
        public void refreshOriginalExtent(){
1067
                if (getTypeScale() == AUTOMATICO) {
1068
                        view.getMapContext().getViewPort().setEnvelope(getMapContext().getViewPort().getEnvelope());
1069
                        view.getMapContext().getViewPort().refreshExtent();
1070
                }else if (getTypeScale() == MANUAL){
1071
                        Envelope oldExtent = view.getMapContext().getViewPort().getEnvelope();
1072
                Envelope newExtent=getMapContext().getViewPort().getEnvelope();
1073
                double xDif=newExtent.getCenter(0)-oldExtent.getCenter(0);
1074
                double yDif=newExtent.getCenter(1)-oldExtent.getCenter(1);
1075
                try {
1076
                                view.getMapContext().getViewPort().setEnvelope(geomManager.createEnvelope(oldExtent.getMinimum(0)+xDif,oldExtent.getMinimum(1)+yDif,oldExtent.getMaximum(0)+xDif,oldExtent.getMaximum(1)+yDif, SUBTYPES.GEOM2D));
1077
                        } catch (CreateEnvelopeException e) {
1078
                                e.printStackTrace();
1079
                        }
1080
                view.getMapContext().getViewPort().refreshExtent();
1081
                }
1082
        }
1083
}