Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / fframes / FFrameView.java @ 34936

History | View | Annotate | Download (37.7 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 javax.swing.SwingUtilities;
58

    
59
import org.cresques.cts.IProjection;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
import org.gvsig.andami.PluginServices;
64
import org.gvsig.andami.messages.NotificationManager;
65
import org.gvsig.app.project.Project;
66
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
67
import org.gvsig.app.project.documents.layout.LayoutControl;
68
import org.gvsig.app.project.documents.view.DefaultViewDocument;
69
import org.gvsig.app.project.documents.view.ViewDocument;
70
import org.gvsig.compat.print.PrintAttributes;
71
import org.gvsig.fmap.dal.exception.ReadException;
72
import org.gvsig.fmap.geom.Geometry;
73
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
74
import org.gvsig.fmap.geom.GeometryLocator;
75
import org.gvsig.fmap.geom.GeometryManager;
76
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
77
import org.gvsig.fmap.geom.primitive.Envelope;
78
import org.gvsig.fmap.mapcontext.MapContext;
79
import org.gvsig.fmap.mapcontext.MapContextException;
80
import org.gvsig.fmap.mapcontext.ViewPort;
81
import org.gvsig.fmap.mapcontext.events.ColorEvent;
82
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
83
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
84
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
85
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
86
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
87
import org.gvsig.tools.ToolsLocator;
88
import org.gvsig.tools.dynobject.DynStruct;
89
import org.gvsig.tools.persistence.PersistenceManager;
90
import org.gvsig.tools.persistence.PersistentState;
91
import org.gvsig.tools.persistence.exception.PersistenceException;
92

    
93

    
94

    
95
/**
96
 * FFrame para introducir una vista en el Layout.
97
 *
98
 * @author Vicente Caballero Navarro
99
 */
100
public class FFrameView extends FFrame implements ViewPortListener,
101
LegendListener, IFFrameUseProject, IFFrameUseFMap {
102
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameView";
103

    
104
    private static final String MODE_FIELD = "mode";
105
    private static final String TYPESCALE_FIELD = "typeScale";  
106
    private static final String EXTENSION_FIELD = "extension";
107
    private static final String QUALITY_FIELD  = "quality";  
108
    private static final String VIEWING_FIELD = "viewing";
109
    private static final String BLINKED_FIELD = "bLinked";  
110
    private static final String MAPUNITS_FIELD = "mapUnits";
111
    private static final String SCALE_FIELD = "scale";  
112
    private static final String VIEW_FIELD = "view";
113
    private static final String ENVELOPE_FIELD = "envelope";     
114
    private static final String SHOWGRID_FIELD = "showGrid";  
115
    private static final String GRID_FIELD = "gridview";
116
    private static final String MAPCONTEXT_FIELD = "mapContext";
117
    
118
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
119
    private static final Logger logger = LoggerFactory.getLogger(FFrameView.class);
120
    public static final int PRESENTACION = 0;
121
    public static final int BORRADOR = 1;
122
    protected int m_Mode;
123
    protected int m_typeScale = AUTOMATICO;
124
    protected int m_extension = 0;
125
    protected int m_quality = PRESENTACION;
126
    protected int m_viewing = 0;
127
    protected boolean m_bLinked = true;
128
    protected ViewDocument view = null;
129
    protected MapContext m_fmap = null;
130
    protected double m_Scale = 0;
131
    protected int m_mapUnits = 1; //Metros.
132

    
133
    private BufferedImage m_image = null;
134
    private AffineTransform at = null;
135
    protected Project project = null;
136
    private double scaleAnt;
137
    private boolean refresh=false;
138
    private Point origin;
139
    private Point2D p1;
140
    private Point2D p2;
141
    private IFFrame grid;
142
    private boolean showGrid=false;
143
    /**
144
     * Creates a new FFrameView object.
145
     */
146
    public FFrameView() {
147
        num++;
148
    }
149

    
150
    /**
151
     * Devuelve una descripci?n del FFrameView.
152
     *
153
     * @return Descripci?n.
154
     */
155
    public String toString() {
156
        if (getView() == null) {
157
            return "FFrameView " + num + ": " + "Vacio";
158
        }
159

    
160
        return "FFrameView " + num + ": " + getView().getName();
161
    }
162

    
163
    /**
164
     * Rellena la escala de la vista que contiene el fframe.
165
     *
166
     * @param d escala de la vista.
167
     */
168
    public void setScale(double d) {
169
        m_Scale = d;
170
    }
171

    
172
    /**
173
     * Inserta el nuevo extent a la FFrameView.
174
     *
175
     * @param r Rect?ngulo a ocupar por el FFrameView.
176
     */
177
    public void setNewEnvelope(Envelope r) {
178
        getMapContext().getViewPort().setEnvelope(r);
179
        refresh = true;
180
        m_Scale = FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
181
            getBoundBox().width,
182
            getBoundingBox(null).width / getBoundBox().width);
183
    }
184

    
185
    /**
186
     * Devuelve el FMap de la vista o una clonaci?n de este si se utiliza una
187
     * escala fija.
188
     *
189
     * @return FMap.
190
     */
191
    public MapContext getMapContext() {
192
        return m_fmap;
193
    }
194

    
195
    /**
196
     * Rellena la calidad que se quiere aplicar.
197
     *
198
     * @param q entero que representa la calidad a aplicar.
199
     */
200
    public void setQuality(int q) {
201
        m_quality = q;
202
    }
203

    
204
    /**
205
     * Devuelve un entero que representa la calidad que est? seleccionada.
206
     *
207
     * @return tipo de calidad selccionada.
208
     */
209
    public int getQuality() {
210
        return m_quality;
211
    }
212

    
213
    /**
214
     * Devuelve un entero que representa la forma en que se actualiza la vista.
215
     *
216
     * @return forma que se actualiza la vista.
217
     */
218
    public int getViewing() {
219
        return m_viewing;
220
    }
221

    
222
    /**
223
     * Rellena la forma de actualizar la vista(cuando activo o siempre). De
224
     * momento esta opci?n esta deshabilitada.
225
     *
226
     * @param v entero que representa la forma de actualizar la vista.
227
     */
228
    public void setViewing(int v) {
229
        m_viewing = v;
230
    }
231

    
232
    /**
233
     * Inserta el ProjectView de donde obtener las propiedades de la vista a
234
     * mostrar.
235
     *
236
     * @param v Modelo de la vista.
237
     */
238
    public void setView(DefaultViewDocument v) {
239
        view=v;
240
        ViewPort vp =null;
241
        if (getMapContext()!=null){
242
            vp=getMapContext().getViewPort();
243
        }else{
244
            try {
245
                vp = (ViewPort) v.getMapContext().getViewPort().clone();
246
            } catch (CloneNotSupportedException e1) {
247
                NotificationManager.addError("Excepci?n :", e1);
248
            }
249

    
250
        }
251
        vp.setImageSize(new Dimension((int) getBoundingBox(null).width,
252
            (int) getBoundingBox(null).height));
253

    
254
        if (m_bLinked) {
255
            if (getTypeScale() == AUTOMATICO) {
256
                try {
257
                    m_fmap = v.getMapContext().createNewFMap((ViewPort) v.getMapContext()
258
                        .getViewPort()
259
                        .clone());
260
                } catch (CloneNotSupportedException e1) {
261
                    NotificationManager.addError("Excepci?n :", e1);
262
                }
263

    
264
                m_fmap.getViewPort().setImageSize(new Dimension(
265
                    (int) getBoundingBox(null).width,
266
                    (int) getBoundingBox(null).height));
267
                v.getMapContext().getViewPort().addViewPortListener(this);
268
                v.getMapContext().addLayerListener(this);
269
            } else if (getTypeScale() == CONSTANTE) {
270
                m_fmap = v.getMapContext().createNewFMap(vp);
271
                vp.setEnvelope(getNewEnvelope(getScale()));
272
                v.getMapContext().getViewPort().addViewPortListener(this);
273
                v.getMapContext().addLayerListener(this);
274
            } else if (getTypeScale() == MANUAL) {
275
                m_fmap = v.getMapContext().createNewFMap(vp);
276
                vp.setEnvelope(getNewEnvelope(getScale()));
277
                v.getMapContext().getViewPort().addViewPortListener(this);
278
                v.getMapContext().addLayerListener(this);
279
            }
280
        } else if (!m_bLinked) {
281
          
282
            if (getTypeScale() == AUTOMATICO) {
283
                m_fmap = v.getMapContext().cloneFMap(); //(v.getMapContext().getViewPort().cloneViewPort());
284
                try {
285
                    m_fmap.setViewPort((ViewPort) v.getMapContext().getViewPort().clone());
286
                } catch (CloneNotSupportedException e1) {
287
                    NotificationManager.addError("Excepci?n :", e1);
288
                }
289
                m_fmap.getViewPort().setImageSize(new Dimension(
290
                    (int) getBoundingBox(null).width,
291
                    (int) getBoundingBox(null).height));
292
                v.getMapContext().getViewPort().addViewPortListener(this);
293
            } else if (getTypeScale() == CONSTANTE) {
294
                m_fmap = v.getMapContext().cloneFMap();
295
                vp.setEnvelope(getNewEnvelope(getScale()));
296
                m_fmap.setViewPort(vp);
297
                v.getMapContext().getViewPort().addViewPortListener(this);
298
            } else if (getTypeScale() == MANUAL) {
299
                m_fmap = v.getMapContext().cloneFMap();
300
                vp.setEnvelope(getNewEnvelope(getScale()));
301
                m_fmap.setViewPort(vp);
302
                v.getMapContext().getViewPort().addViewPortListener(this);
303
            }
304
            //            } catch (XMLException e1) {
305
            //                NotificationManager.addError("Cuando se a?ade una vista al Layout",
306
            //                    e1);
307
            //            }
308
        }
309
    }
310

    
311
    /**
312
     * Devuelve el modelo de la vista.
313
     *
314
     * @return Modelo de la vista.
315
     */
316
    public ViewDocument getView() {
317
        return view;
318
    }
319

    
320
    /**
321
     * Devuelve un Rect?ngulo que representa  el extent de la vista que se
322
     * requiere a partir  de una escala.
323
     *
324
     * @param scale Escala a mostrar.
325
     *
326
     * @return Rect?ngulo.
327
     */
328
    protected Envelope getNewEnvelope(long scale) {
329
        double hview = getBoundBox().getHeight();
330
        double wview = getBoundBox().getWidth();
331
        double hextent = (scale * hview) / 100.0;
332
        double wextent = (scale * wview) / 100.0;
333

    
334
        if (m_fmap.getViewPort().getEnvelope()==null) {
335
            try {
336
                return geomManager.createEnvelope(0,0,0,0, SUBTYPES.GEOM2D);
337
            } catch (CreateEnvelopeException e) {
338
                logger.error("Error creating the envelope", e);
339
                return null;
340
            }
341
        }
342
        double newx = m_fmap.getViewPort().getEnvelope().getCenter(Geometry.DIMENSIONS.X) - (wextent / 2.0);
343
        double newy = m_fmap.getViewPort().getEnvelope().getCenter(Geometry.DIMENSIONS.Y) -  (hextent / 2.0);
344
        IProjection proj=m_fmap.getViewPort().getProjection();
345
        Envelope r = null;
346
        try {
347
            r = geomManager.createEnvelope(newx, newy, newx+ wextent,newy+ hextent, SUBTYPES.GEOM2D);
348
        } catch (CreateEnvelopeException e) {
349
            logger.error("Error creating the envelope", e);
350
        }
351
        if (!proj.isProjected()){
352
            Rectangle2D auxR=new Rectangle2D.Double(newx,newy,wextent,hextent);
353
            Rectangle2D aux = m_fmap.getViewPort().getProjection().getExtent(auxR,scale,wview,hview,1,100,2.54);
354
            try {
355
                r = geomManager.createEnvelope(aux.getX(),aux.getY(), aux.getMaxX(),aux.getMaxY(), SUBTYPES.GEOM2D);
356
            } catch (CreateEnvelopeException e) {
357
                logger.error("Error creating the envelope", e);
358
            }
359
        }
360
        return r;
361
    }
362

    
363
    /**
364
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
365
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
366
     * de dibujar.
367
     *
368
     * @param g
369
     *            Graphics2D
370
     * @param at
371
     *            Transformada af?n.
372
     * @param rv
373
     *            rect?ngulo sobre el que hacer un clip.
374
     * @param imgBase
375
     *            Imagen para acelerar el dibujado.
376
     */
377
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
378
        BufferedImage imgBase) {
379
        Rectangle2D.Double r = getBoundingBox(at);
380
        if (getRotation() != 0) {
381
            g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
382
                + (r.height / 2));
383
        }
384
        if (intersects(rv, r)) {
385
            if (getMapContext() == null) {
386
                drawEmpty(g);
387
            } else {
388
                if (rv != null) {
389
                    // Dibujamos en pantalla
390
                    Rectangle rclip = null;
391
                    if (g.getClipBounds()!=null) {
392
                        rclip = (Rectangle)g.getClipBounds().clone();
393
                    }
394
                    g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r
395
                        .getWidth(), (int) r.getHeight());
396

    
397
                    if (getQuality() == PRESENTACION) {
398
                        if (rv.intersects(r)) {
399
                            ViewPort viewPort = this.getMapContext()
400
                            .getViewPort();
401
                            Color theBackColor = viewPort.getBackColor();
402

    
403
                            if (origin != null
404
                                && origin.equals(getLayoutControl().getRectOrigin())                                                                        
405
                                && getLayoutControl().getAT().getScaleX() == scaleAnt
406
                                && m_image != null && !refresh && !(r.getWidth() > getLayoutControl().getWidth()
407
                                    || r.getHeight() > getLayoutControl().getHeight())) {
408
                                if (theBackColor != null) {
409
                                    g.setColor(theBackColor);
410
                                    g.fillRect((int) r.x, (int) r.y, viewPort
411
                                        .getImageWidth(), viewPort
412
                                        .getImageHeight());
413
                                }
414
                                g.translate(r.getX(), r.getY());
415
                                g.drawImage(m_image, 0, 0, getLayoutControl().getComponent());
416
                                g.translate(-r.getX(), -r.getY());
417
                                scaleAnt = getLayoutControl().getAT().getScaleX();
418
                                origin = getLayoutControl().getRectOrigin();//.clone();
419
                            } else {
420

    
421
                                //                                                                System.err.println("r : " + r);
422
                                if (r.getWidth() > getLayoutControl().getWidth()
423
                                    || r.getHeight() > getLayoutControl().getHeight()) {
424
                                    viewPort.setOffset(
425
                                        new Point2D.Double(r.getX(), r
426
                                            .getY()));
427
                                    viewPort.setImageSize(
428
                                        new Dimension((int) r.getWidth(),
429
                                            (int) r.getHeight()));
430
                                    MapContext fmap = getMapContext().cloneToDraw();
431

    
432
                                    ViewPort viewp;
433
                                    try {
434
                                        viewp = (ViewPort) viewPort.clone();                            
435
                                        viewp.setImageSize(new Dimension(
436
                                            getLayoutControl().getWidth(),
437
                                            getLayoutControl().getHeight()));
438
                                        Envelope r1 = calculateEnvelope();
439
                                        double width = getLayoutContext().getAttributes()
440
                                        .getSizeInUnits().getAncho();
441
                                        double scale = FLayoutUtilities
442
                                        .getScaleView(viewp, width, r1
443
                                            .getLength(0)
444
                                            / width);
445
                                        viewp.setEnvelope(r1);
446

    
447

    
448
                                        fmap.setViewPort(viewp);
449
                                        g.translate(-r.getX(), -r.getY());
450
                                        if (theBackColor != null) {
451
                                            g.setColor(theBackColor);
452
                                            g.fillRect((int) r.x,
453
                                                (int) r.y,
454
                                                viewp.getImageWidth(),
455
                                                viewp.getImageHeight());
456
                                        }
457
                                        fmap.draw(imgBase, g, scale);
458
                                    } catch (ReadException e) {
459
                                        e.printStackTrace();
460
                                    } catch (MapContextException mpex) {
461
                                        mpex.printStackTrace();
462
                                    } catch (CloneNotSupportedException e1) {
463
                                        NotificationManager.addError("Excepci?n :", e1);
464
                                    }
465

    
466
                                    g.translate(r.getX(), r.getY());
467

    
468
                                } else {
469
                                    viewPort.setOffset(
470
                                        new Point2D.Double(r.x, r.y));
471
                                    viewPort.setImageSize(
472
                                        new Dimension((int) r.width,
473
                                            (int) r.height));
474
                                    viewPort.refreshExtent();
475
                                    m_image = new BufferedImage((int) r
476
                                        .getWidth(), (int) r.getHeight(),
477
                                        BufferedImage.TYPE_INT_ARGB);
478
                                    Graphics2D gimg = (Graphics2D) m_image
479
                                    .getGraphics();
480
                                    gimg.translate(-((int) r.getX()), -((int) r
481
                                        .getY()));
482
                                    try {
483
                                        getMapContext().draw(m_image, gimg, getScale());
484
                                    } catch (ReadException e) {
485
                                        e.printStackTrace();
486
                                    } catch (MapContextException mpex) {
487
                                        mpex.printStackTrace();
488
                                    }
489
                                    gimg.translate(((int) r.getX()), ((int) r
490
                                        .getY()));
491
                                    if (theBackColor != null) {
492
                                        g.setColor(theBackColor);
493
                                        g.fillRect((int) r.x, (int) r.y, viewPort
494
                                            .getImageWidth(), viewPort
495
                                            .getImageHeight());
496
                                    }
497
                                    g.drawImage(m_image, (int) r.getX(),
498
                                        (int) r.getY(), getLayoutControl().getComponent());
499
                                }
500
                                scaleAnt = getLayoutControl().getAT().getScaleX();
501
                                origin = getLayoutControl().getRectOrigin();//.clone();
502
                                refresh = false;
503
                            }
504
                        }
505
                    } else {
506
                        drawDraft(g);
507
                    }
508
                    if (rclip != null) {
509
                        g.setClip(rclip.x, rclip.y, rclip.width,
510
                            rclip.height);
511
                    }
512
                } else {
513
                    printX(g, at);
514
                }
515
            }
516
        }
517
        if (getRotation() != 0) {
518
            g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
519
                + (r.height / 2));
520
        }
521

    
522
        if (getMapContext() != null) {
523
            setATMap(getMapContext().getViewPort()
524
                .getAffineTransform());
525

    
526
        }
527
        if (showGrid && grid!=null){
528
            ((FFrameGrid)grid).setFFrameDependence(this);
529
            grid.draw(g,at,rv,imgBase);
530
        }
531
    }
532

    
533
    private Envelope calculateEnvelope() {
534
        if (p1==null||p2==null) {
535
            try {
536
                return geomManager.createEnvelope(0,0,0,0, SUBTYPES.GEOM2D);
537
            } catch (CreateEnvelopeException e) {
538
                logger.error("Error creating the envelope", e);
539
            }
540
        }
541
        Envelope r = null;
542
        try {
543
            r = geomManager.createEnvelope(p1.getX(),p1.getY(),p2.getX(),p2.getY(), SUBTYPES.GEOM2D);
544
        } catch (CreateEnvelopeException e) {
545
            logger.error("Error creating the envelope", e);
546
        }
547
        return r;
548
    }
549

    
550
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
551
        PrintAttributes prroperties) {
552
        draw(g, at, null, null);
553
    }
554

    
555
    private void printX(Graphics2D g, AffineTransform at) {
556
        Rectangle2D.Double r = getBoundingBox(at);
557

    
558
        // Dibujamos en impresora
559
        Rectangle rclip = g.getClipBounds();
560
        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r.getWidth(),
561
            (int) r.getHeight());
562
        this.getMapContext().getViewPort().setOffset(new Point2D.Double(r.x, r.y));
563
        this.getMapContext().getViewPort().setImageSize(new Dimension((int) r.width,
564
            (int) r.height));
565

    
566
        try {
567
            this.getMapContext().print(g, getScale(), getLayoutContext().getAttributes().toPrintAttributes());
568
        } catch (ReadException e) {
569
            NotificationManager.addError(e.getMessage(), e);
570
        } catch (MapContextException e) {
571
            NotificationManager.addError(e.getMessage(), e);
572
        }
573

    
574
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
575
    }
576

    
577
    /**
578
     * Rellena la unidad de medida en la que est? la vista.
579
     *
580
     * @param i entero que representa la unidad de medida de la vista.
581
     */
582
    public void setMapUnits(int i) {
583
        m_mapUnits = i;
584
    }
585

    
586
    /**
587
     * Obtiene la unidad de medida en la que est? la vista.
588
     *
589
     * @return Unidad de medida.
590
     */
591
    public int getMapUnits() {
592
        return m_mapUnits;
593
    }
594

    
595
    /**
596
     * Devuelve la escala seg?n el tipo de escala que se haya seleccionado al
597
     * a?adida la vista.
598
     *
599
     * @return escala.
600
     */
601
    public long getScale() {
602
        /*if (m_bLinked){
603
           return getScaleView1(METROS);
604
               }
605
         */
606
        if (getMapContext()==null) {
607
            return 0;
608
        }
609
        if (getTypeScale() == AUTOMATICO) {
610
            return FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
611
                getBoundBox().width, getBoundingBox(null).width);
612
        } else if (getTypeScale() == CONSTANTE) {
613
            return (long) m_Scale;
614
        } else if (getTypeScale() == MANUAL) {
615
            return (long) m_Scale;
616
        }
617

    
618
        return (long) m_Scale;
619
    }
620

    
621
    /**
622
     * Seleccionar si la vista esta relacionada o no con la original.
623
     *
624
     * @param b true si est? ligada y false si no lo est?.
625
     */
626
    public void setLinked(boolean b) {
627
        m_bLinked = b;
628
    }
629

    
630
    /**
631
     * Devuelve si est? ligada o no el FFrameView con la vista.
632
     *
633
     * @return True si la vista est? ligada.
634
     */
635
    public boolean getLinked() {
636
        return m_bLinked;
637
    }
638

    
639
    /**
640
     * Devuelve la opci?n seleccionada:Rellenar marco de la vista o recorte a
641
     * la vista.
642
     *
643
     * @return entero que representa la opci?n elegida.
644
     */
645
    public int getExtension() {
646
        return m_extension;
647
    }
648

    
649
    /**
650
     * Devuelve el tipo de escala que est? seleccionada AUTOMATICO,CONSTANTE o
651
     * MANUAL.
652
     *
653
     * @return entero que representa el tipo seleccionado.
654
     */
655
    public int getTypeScale() {
656
        return m_typeScale;
657
    }
658

    
659
    /**
660
     * Rellenar si se quiere:Rellenar marco de la vista o recorte a la vista.
661
     *
662
     * @param i entero que representa la opci?n elegida.
663
     */
664
    public void setExtension(int i) {
665
        m_extension = i;
666
    }
667

    
668
    /**
669
     * Rellenar el tipo de escala que se desea.
670
     *
671
     * @param i entero que representa el tipo de escala.
672
     */
673
    public void setTypeScale(int i) {
674
        m_typeScale = i;
675
    }
676

    
677
    /**
678
     * Inserta la imagen para repintar el FFrameView.
679
     *
680
     * @param bi Imagen para repintar.
681
     */
682
    public void setBufferedImage(BufferedImage bi) {
683
        m_image = bi;
684
    }
685

    
686
    /**
687
     * Devuelve la imagen para repintar.
688
     *
689
     * @return Imagen para repintar.
690
     */
691
    public BufferedImage getBufferedImage() {
692
        return m_image;
693
    }
694

    
695
    /**
696
     * Devuelve la MAtriz de transformaci?n utilizada por la FFrameView.
697
     *
698
     * @return MAtriz de transformaci?n.
699
     */
700
    public AffineTransform getATMap() {
701
        return at;
702
    }
703

    
704
    /**
705
     * Inserta la matriz de transformaci?n.
706
     *
707
     * @param transform Matriz de transformaci?n.
708
     */
709
    public void setATMap(AffineTransform transform) {
710
        at = transform;
711
    }
712
    /**
713
     * Inserta el proyecto.
714
     *
715
     * @param p Proyecto.
716
     */
717
    public void setProject(Project p) {
718
        project = p;
719
    }
720

    
721

    
722
    /**
723
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
724
     */
725
    public String getNameFFrame() {
726
        return PluginServices.getText(this, "Vista")+ num;
727
    }
728
    
729
    public String getName() {
730
        return PERSISTENCE_DEFINITION_NAME;
731
    }
732

    
733
    /**
734
     * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(org.gvsig.fmap.mapcontext.events.ExtentEvent)
735
     */
736
    public void extentChanged(ExtentEvent e) {
737
        if (getTypeScale() == AUTOMATICO) {
738
            m_fmap.getViewPort().setEnvelope(e.getNewExtent());
739
            if (getLayoutControl() != null) {
740
                getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
741
            }
742
        }else if (getTypeScale() == MANUAL) {
743
            Envelope oldExtent = m_fmap.getViewPort().getEnvelope();
744
            Envelope newExtent=e.getNewExtent();
745
            double xDif=newExtent.getCenter(0)-oldExtent.getCenter(0);
746
            double yDif=newExtent.getCenter(1)-oldExtent.getCenter(1);
747
            try {
748
                m_fmap.getViewPort().setEnvelope(geomManager.createEnvelope(oldExtent.getMinimum(0)+xDif,oldExtent.getMinimum(1)+yDif,oldExtent.getMaximum(0)+xDif,oldExtent.getMaximum(1)+yDif, SUBTYPES.GEOM2D));
749
            } catch (CreateEnvelopeException e1) {
750
                e1.printStackTrace();
751
            }
752
            if (getLayoutControl() != null) {
753
                getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
754
            }
755
        }
756
        refresh=true;
757
    }
758

    
759
    /**
760
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
761
     */
762
    public void backColorChanged(ColorEvent e) {
763
        if (getLinked()) {
764
            m_fmap.getViewPort().setBackColor(e.getNewColor());
765
            getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
766

    
767
            //setBufferedImage(null);
768
        }
769
    }
770

    
771
    /**
772
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#projectionChanged(org.gvsig.fmap.mapcontext.events.ProjectionEvent)
773
     */
774
    public void projectionChanged(ProjectionEvent e) {
775
        if (getTypeScale() == AUTOMATICO) {
776
            m_fmap.getViewPort().setProjection(e.getNewProjection());
777

    
778
            if (getLayoutControl() != null) {
779
                getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
780
            }
781
        }
782
    }
783

    
784
    /**
785
     * DOCUMENT ME!
786
     *
787
     * @param e DOCUMENT ME!
788
     */
789
    public void legendChanged(final LegendChangedEvent e) {
790
        if( !SwingUtilities.isEventDispatchThread() ) {
791
            SwingUtilities.invokeLater(new Runnable() {
792
                public void run() {
793
                    legendChanged(e);
794
                }
795
            });
796
            return;
797
        }       
798
        if (getLinked()) {
799
            getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
800
            refresh=true;
801
        }
802
    }
803

    
804
    /**
805
     * DOCUMENT ME!
806
     *
807
     * @param arg0 DOCUMENT ME!
808
     *
809
     * @return DOCUMENT ME!
810
     */
811
    public boolean compare(Object arg0) {
812
        if (!(arg0 instanceof FFrameView)) {
813
            return false;
814
        }
815

    
816
        if (!this.getName().equals(((FFrameView) arg0).getName())) {
817
            return false;
818
        }
819

    
820
        if (Math.abs(this.getBoundBox().getWidth()-(((FFrameView) arg0).getBoundBox().getWidth()))>0.05) {
821
            return false;
822
        }
823
        if (Math.abs(this.getBoundBox().getHeight()-(((FFrameView) arg0).getBoundBox().getHeight()))>0.05) {
824
            return false;
825
        }
826

    
827
        if (!this.toString().equals(((FFrameView) arg0).toString())) {
828
            return false;
829
        }
830

    
831
        if (this.getMapContext()!=null && !this.getMapContext().equals(((FFrameView) arg0).getMapContext())) {
832
            return false;
833
        }
834

    
835
        if (this.getRotation()!=((FFrameView)arg0).getRotation()){
836
            return false;
837
        }
838
        return true;
839
    }
840

    
841
    /**
842
     * DOCUMENT ME!
843
     *
844
     * @param arg0 DOCUMENT ME!
845
     *
846
     * @return DOCUMENT ME!
847
     */
848
    public boolean equals(Object arg0) {
849
        return this.compare(arg0);
850
    }
851

    
852
    public void refresh() {
853
        if (view!=null && (getTypeScale() == MANUAL || getTypeScale() == CONSTANTE )) {
854
            getMapContext().getViewPort().setEnvelope(getNewEnvelope(getScale()));
855
        }
856
        refresh=true;
857
    }
858

    
859
    public void fullExtent() throws ReadException {
860
        setNewEnvelope(getMapContext().getFullEnvelope());
861
    }
862

    
863
    public void setPointsToZoom(Point2D px1, Point2D px2) {
864
        p1=px1;
865
        p2=px2;
866
    }
867

    
868
    public void movePoints(Point2D px1, Point2D px2) {
869
        double difX=-px2.getX()+px1.getX();
870
        double difY=-px2.getY()+px1.getY();
871
        if (p1!=null) {
872
            p1.setLocation(p1.getX()+difX,p1.getY()+difY);
873
            p2.setLocation(p2.getX()+difX,p2.getY()+difY);
874
        }
875
    }
876

    
877
    protected void cloneActions(IFFrame frame) {
878
        if (view ==null || view.getMapContext()==null) {
879
            return;
880
        }
881
        if (m_bLinked) {
882
            if (getTypeScale() == AUTOMATICO) {
883
                view.getMapContext().getViewPort().addViewPortListener(this);
884
                view.getMapContext().addLayerListener(this);
885
            } else if (getTypeScale() == CONSTANTE) {
886
                view.getMapContext().getViewPort().removeViewPortListener(this);
887
                view.getMapContext().addLayerListener(this);
888
            } else if (getTypeScale() == MANUAL) {
889
                view.getMapContext().getViewPort().removeViewPortListener(this);
890
                view.getMapContext().addLayerListener(this);
891
            }
892
        } else if (!m_bLinked) {
893
            if (getTypeScale() == AUTOMATICO) {
894
                view.getMapContext().getViewPort().addViewPortListener(this);
895
            } else if (getTypeScale() == CONSTANTE) {
896
                view.getMapContext().getViewPort().removeViewPortListener(this);
897
            } else if (getTypeScale() == MANUAL) {
898
                view.getMapContext().getViewPort().removeViewPortListener(this);
899
            }
900
        }
901
        ((FFrameView)frame).view.getMapContext().removeLayerListener((FFrameView)frame);
902
        ((FFrameView)frame).view.getMapContext().getViewPort().removeViewPortListener((FFrameView)frame);
903
    }
904
    
905
    public Object clone() throws CloneNotSupportedException {
906
        FFrameView frame = (FFrameView)super.clone();
907
 
908
        frame.setSelected(this.getSelected() != IFFrame.NOSELECT);
909
        
910
        if (grid != null){
911
            FFrameGrid newGrid = (FFrameGrid)this.grid.clone();
912
            newGrid.setFFrameDependence(frame);
913
            frame.setGrid(newGrid);
914
        }
915
        cloneActions(frame);
916
        return frame;
917
    }
918

    
919
    public void setGrid(IFFrame grid) {
920
        this.grid=grid;
921
        this.grid.setRotation(this.getRotation());
922
    }
923

    
924
    public IFFrame getGrid(){
925
        return this.grid;
926
    }
927

    
928
    public void setRotation(double rotation) {
929
        super.setRotation(rotation);
930
        if (grid!=null) {
931
            grid.setRotation(rotation);
932
        }
933
    }
934

    
935
    public void showGrid(boolean b){
936
        showGrid=b;
937
    }
938

    
939
    public boolean isShowGrid() {
940
        return showGrid;
941
    }
942

    
943
    public void refreshOriginalExtent(){
944
        if (getTypeScale() == AUTOMATICO) {
945
            view.getMapContext().getViewPort().setEnvelope(getMapContext().getViewPort().getEnvelope());
946
            view.getMapContext().getViewPort().refreshExtent();
947
        }else if (getTypeScale() == MANUAL){
948
            Envelope oldExtent = view.getMapContext().getViewPort().getEnvelope();
949
            Envelope newExtent=getMapContext().getViewPort().getEnvelope();
950
            double xDif=newExtent.getCenter(0)-oldExtent.getCenter(0);
951
            double yDif=newExtent.getCenter(1)-oldExtent.getCenter(1);
952
            try {
953
                view.getMapContext().getViewPort().setEnvelope(geomManager.createEnvelope(oldExtent.getMinimum(0)+xDif,oldExtent.getMinimum(1)+yDif,oldExtent.getMaximum(0)+xDif,oldExtent.getMaximum(1)+yDif, SUBTYPES.GEOM2D));
954
            } catch (CreateEnvelopeException e) {
955
                e.printStackTrace();
956
            }
957
            view.getMapContext().getViewPort().refreshExtent();
958
        }
959
    }
960

    
961
    public static void registerPersistent() {
962
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
963
        if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
964
            DynStruct definition = manager.addDefinition(
965
                FFrameView.class,
966
                PERSISTENCE_DEFINITION_NAME,
967
                "FFrameView persistence definition",
968
                null, 
969
                null
970
            ); 
971

    
972
            definition.extend(manager.getDefinition(FFrame.PERSISTENCE_DEFINITION_NAME));        
973

    
974

    
975
            definition.addDynFieldInt(MODE_FIELD).setMandatory(true);        
976
            definition.addDynFieldInt(TYPESCALE_FIELD).setMandatory(true);        
977
            definition.addDynFieldInt(EXTENSION_FIELD).setMandatory(true);
978
            definition.addDynFieldInt(QUALITY_FIELD).setMandatory(true);
979
            definition.addDynFieldInt(VIEWING_FIELD).setMandatory(true);
980
            definition.addDynFieldBoolean(BLINKED_FIELD).setMandatory(true);
981
            definition.addDynFieldInt(MAPUNITS_FIELD).setMandatory(true);
982
            definition.addDynFieldDouble(SCALE_FIELD).setMandatory(true);
983
            definition.addDynFieldObject(VIEW_FIELD).setClassOfValue(ViewDocument.class).setMandatory(false);
984
            definition.addDynFieldObject(ENVELOPE_FIELD).setClassOfValue(Envelope.class).setMandatory(false);
985
            definition.addDynFieldBoolean(SHOWGRID_FIELD).setMandatory(true);
986
            definition.addDynFieldObject(GRID_FIELD).setClassOfValue(IFFrame.class).setMandatory(false);
987
            definition.addDynFieldObject(MAPCONTEXT_FIELD).setClassOfValue(MapContext.class).setMandatory(false);
988
        }
989
    }
990

    
991
    @Override
992
    public void loadFromState(PersistentState state)
993
    throws PersistenceException {
994
        super.loadFromState(state);
995
        m_Mode = state.getInt(MODE_FIELD);
996
        m_typeScale = state.getInt(TYPESCALE_FIELD);  
997
        m_extension = state.getInt(EXTENSION_FIELD);
998
        m_quality = state.getInt(QUALITY_FIELD);  
999
        m_viewing = state.getInt(VIEWING_FIELD);
1000
        m_bLinked = state.getBoolean(BLINKED_FIELD);  
1001
        m_mapUnits = state.getInt(MAPUNITS_FIELD);
1002
        m_Scale = state.getDouble(SCALE_FIELD);  
1003
        view = (ViewDocument)state.get(VIEW_FIELD);
1004
        
1005
        if (state.hasValue(MAPCONTEXT_FIELD)){
1006
            m_fmap = (MapContext)state.get(MAPCONTEXT_FIELD);
1007
            m_fmap.getViewPort().setEnvelope((Envelope)state.get(ENVELOPE_FIELD));         
1008
        }
1009

    
1010
        showGrid = state.getBoolean(SHOWGRID_FIELD);  
1011
        grid = (IFFrame)state.get(GRID_FIELD);
1012
    }
1013

    
1014
    @Override
1015
    public void saveToState(PersistentState state) throws PersistenceException {
1016
        super.saveToState(state);
1017
        state.set(MODE_FIELD, m_Mode);
1018
        state.set(TYPESCALE_FIELD, m_typeScale);  
1019
        state.set(EXTENSION_FIELD, m_extension);
1020
        state.set(QUALITY_FIELD, m_quality);  
1021
        state.set(VIEWING_FIELD, m_viewing);
1022
        state.set(BLINKED_FIELD, m_bLinked);  
1023
        state.set(MAPUNITS_FIELD, m_mapUnits);
1024
        state.set(SCALE_FIELD, m_Scale);  
1025
        state.set(VIEW_FIELD, view);
1026

    
1027
        if (getMapContext() != null && getMapContext().getViewPort().getEnvelope() != null) {
1028
            state.set(ENVELOPE_FIELD, getMapContext().getViewPort().getEnvelope());
1029
            state.set(MAPCONTEXT_FIELD, m_fmap);
1030
        }
1031

    
1032
        state.set(SHOWGRID_FIELD, showGrid);  
1033
        state.set(GRID_FIELD, grid);
1034
    }
1035
}