Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameView.java @ 36648

History | View | Annotate | Download (39.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout.fframes;
23

    
24
import java.awt.Color;
25
import java.awt.Dimension;
26
import java.awt.Graphics2D;
27
import java.awt.Point;
28
import java.awt.Rectangle;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Point2D;
31
import java.awt.geom.Rectangle2D;
32
import java.awt.image.BufferedImage;
33

    
34
import javax.swing.SwingUtilities;
35

    
36
import org.cresques.cts.IProjection;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.messages.NotificationManager;
42
import org.gvsig.app.project.Project;
43
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
44
import org.gvsig.app.project.documents.layout.LayoutControl;
45
import org.gvsig.app.project.documents.view.DefaultViewDocument;
46
import org.gvsig.app.project.documents.view.ViewDocument;
47
import org.gvsig.compat.print.PrintAttributes;
48
import org.gvsig.fmap.dal.exception.ReadException;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.GeometryManager;
53
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
54
import org.gvsig.fmap.geom.primitive.Envelope;
55
import org.gvsig.fmap.mapcontext.MapContext;
56
import org.gvsig.fmap.mapcontext.MapContextException;
57
import org.gvsig.fmap.mapcontext.ViewPort;
58
import org.gvsig.fmap.mapcontext.events.ColorEvent;
59
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
60
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
61
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
62
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
63
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
64
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.dynobject.DynStruct;
66
import org.gvsig.tools.persistence.PersistenceManager;
67
import org.gvsig.tools.persistence.PersistentState;
68
import org.gvsig.tools.persistence.exception.PersistenceException;
69

    
70
/**
71
 * FFrame para introducir una vista en el Layout.
72
 * 
73
 * @author Vicente Caballero Navarro
74
 */
75
public class FFrameView extends FFrame implements ViewPortListener,
76
    LegendListener, IFFrameUseProject, IFFrameUseFMap {
77

    
78
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameView";
79

    
80
    private static final String MODE_FIELD = "mode";
81
    private static final String TYPESCALE_FIELD = "typeScale";
82
    private static final String EXTENSION_FIELD = "extension";
83
    private static final String QUALITY_FIELD = "quality";
84
    private static final String VIEWING_FIELD = "viewing";
85
    private static final String BLINKED_FIELD = "bLinked";
86
    private static final String MAPUNITS_FIELD = "mapUnits";
87
    private static final String SCALE_FIELD = "scale";
88
    private static final String VIEW_FIELD = "view";
89
    private static final String ENVELOPE_FIELD = "envelope";
90
    private static final String SHOWGRID_FIELD = "showGrid";
91
    private static final String GRID_FIELD = "gridview";
92
    private static final String MAPCONTEXT_FIELD = "mapContext";
93

    
94
    private static final GeometryManager geomManager = GeometryLocator
95
        .getGeometryManager();
96
    private static final Logger logger = LoggerFactory
97
        .getLogger(FFrameView.class);
98
    public static final int PRESENTACION = 0;
99
    public static final int BORRADOR = 1;
100
    protected int m_Mode;
101
    protected int m_typeScale = AUTOMATICO;
102
    protected int m_extension = 0;
103
    protected int m_quality = PRESENTACION;
104
    protected int m_viewing = 0;
105
    protected boolean m_bLinked = true;
106
    protected ViewDocument view = null;
107
    protected MapContext m_fmap = null;
108
    protected double m_Scale = 0;
109
    protected int m_mapUnits = 1; // Metros.
110

    
111
    private BufferedImage m_image = null;
112
    private AffineTransform at = null;
113
    protected Project project = null;
114
    private double scaleAnt;
115
    private boolean refresh = false;
116
    private Point origin;
117
    private Point2D p1;
118
    private Point2D p2;
119
    private IFFrame grid;
120
    private boolean showGrid = false;
121

    
122
    /**
123
     * Creates a new FFrameView object.
124
     */
125
    public FFrameView() {
126
        num++;
127
    }
128

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

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

    
142
    /**
143
     * Rellena la escala de la vista que contiene el fframe.
144
     * 
145
     * @param d
146
     *            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
156
     *            Rect?ngulo a ocupar por el FFrameView.
157
     */
158
    public void setNewEnvelope(Envelope r) {
159
        getMapContext().getViewPort().setEnvelope(r);
160
        refresh = true;
161
        m_Scale =
162
            FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
163
                getBoundBox().width, getBoundingBox(null).width
164
                    / getBoundBox().width);
165
    }
166

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

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

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

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

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

    
216
    /**
217
     * Inserta el ProjectView de donde obtener las propiedades de la vista a
218
     * mostrar.
219
     * 
220
     * @param v
221
     *            Modelo de la vista.
222
     */
223
    public void setView(DefaultViewDocument v) {
224
        view = v;
225
        ViewPort vp = null;
226
        if (getMapContext() != null) {
227
            vp = getMapContext().getViewPort();
228
        } else {
229
            try {
230
                vp = (ViewPort) v.getMapContext().getViewPort().clone();
231
            } catch (CloneNotSupportedException e1) {
232
                NotificationManager.addError("Excepci?n :", e1);
233
            }
234

    
235
        }
236
        vp.setImageSize(new Dimension((int) getBoundingBox(null).width,
237
            (int) getBoundingBox(null).height));
238

    
239
        if (m_bLinked) {
240
            if (getTypeScale() == AUTOMATICO) {
241
                try {
242
                    m_fmap =
243
                        v.getMapContext().createNewFMap(
244
                            (ViewPort) v.getMapContext().getViewPort().clone());
245
                } catch (CloneNotSupportedException e1) {
246
                    NotificationManager.addError("Excepci?n :", e1);
247
                }
248

    
249
                m_fmap.getViewPort().setImageSize(
250
                    new Dimension((int) getBoundingBox(null).width,
251
                        (int) getBoundingBox(null).height));
252
                v.getMapContext().getViewPort().addViewPortListener(this);
253
                v.getMapContext().addLayerListener(this);
254
            } else
255
                if (getTypeScale() == CONSTANTE) {
256
                    m_fmap = v.getMapContext().createNewFMap(vp);
257
                    vp.setEnvelope(getNewEnvelope(getScale()));
258
                    v.getMapContext().getViewPort().addViewPortListener(this);
259
                    v.getMapContext().addLayerListener(this);
260
                } else
261
                    if (getTypeScale() == MANUAL) {
262
                        m_fmap = v.getMapContext().createNewFMap(vp);
263
                        vp.setEnvelope(getNewEnvelope(getScale()));
264
                        v.getMapContext().getViewPort()
265
                            .addViewPortListener(this);
266
                        v.getMapContext().addLayerListener(this);
267
                    }
268
        } else
269
            if (!m_bLinked) {
270

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

    
305
    /**
306
     * Devuelve el modelo de la vista.
307
     * 
308
     * @return Modelo de la vista.
309
     */
310
    public ViewDocument getView() {
311
        return view;
312
    }
313

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

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

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

    
403
                    if (getQuality() == PRESENTACION) {
404
                        if (rv.intersects(r)) {
405
                            ViewPort viewPort =
406
                                this.getMapContext().getViewPort();
407
                            Color theBackColor = viewPort.getBackColor();
408

    
409
                            if (origin != null
410
                                && origin.equals(getLayoutControl()
411
                                    .getRectOrigin())
412
                                && getLayoutControl().getAT().getScaleX() == scaleAnt
413
                                && m_image != null
414
                                && !refresh
415
                                && !(r.getWidth() > getLayoutControl()
416
                                    .getWidth() || r.getHeight() > getLayoutControl()
417
                                    .getHeight())) {
418
                                if (theBackColor != null) {
419
                                    g.setColor(theBackColor);
420
                                    g.fillRect((int) r.x, (int) r.y,
421
                                        viewPort.getImageWidth(),
422
                                        viewPort.getImageHeight());
423
                                }
424
                                g.translate(r.getX(), r.getY());
425
                                g.drawImage(m_image, 0, 0, getLayoutControl()
426
                                    .getComponent());
427
                                g.translate(-r.getX(), -r.getY());
428
                                scaleAnt =
429
                                    getLayoutControl().getAT().getScaleX();
430
                                origin = getLayoutControl().getRectOrigin();// .clone();
431
                            } else {
432

    
433
                                // System.err.println("r : " + r);
434
                                if (r.getWidth() > getLayoutControl()
435
                                    .getWidth()
436
                                    || r.getHeight() > getLayoutControl()
437
                                        .getHeight()) {
438
                                    viewPort.setOffset(new Point2D.Double(r
439
                                        .getX(), r.getY()));
440
                                    viewPort.setImageSize(new Dimension((int) r
441
                                        .getWidth(), (int) r.getHeight()));
442
                                    MapContext fmap =
443
                                        getMapContext().cloneToDraw();
444

    
445
                                    ViewPort viewp;
446
                                    try {
447
                                        viewp = (ViewPort) viewPort.clone();
448
                                        viewp.setImageSize(new Dimension(
449
                                            getLayoutControl().getWidth(),
450
                                            getLayoutControl().getHeight()));
451
                                        Envelope r1 = calculateEnvelope();
452
                                        double width =
453
                                            getLayoutContext().getAttributes()
454
                                                .getSizeInUnits().getAncho();
455
                                        double scale =
456
                                            FLayoutUtilities.getScaleView(
457
                                                viewp, width, r1.getLength(0)
458
                                                    / width);
459
                                        viewp.setEnvelope(r1);
460

    
461
                                        fmap.setViewPort(viewp);
462
                                        g.translate(-r.getX(), -r.getY());
463
                                        if (theBackColor != null) {
464
                                            g.setColor(theBackColor);
465
                                            g.fillRect((int) r.x, (int) r.y,
466
                                                viewp.getImageWidth(),
467
                                                viewp.getImageHeight());
468
                                        }
469
                                        fmap.draw(imgBase, g, scale);
470
                                    } catch (ReadException e) {
471
                                        e.printStackTrace();
472
                                    } catch (MapContextException mpex) {
473
                                        mpex.printStackTrace();
474
                                    } catch (CloneNotSupportedException e1) {
475
                                        NotificationManager.addError(
476
                                            "Excepci?n :", e1);
477
                                    }
478

    
479
                                    g.translate(r.getX(), r.getY());
480

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

    
537
        if (getMapContext() != null) {
538
            setATMap(getMapContext().getViewPort().getAffineTransform());
539

    
540
        }
541
        if (showGrid && grid != null) {
542
            ((FFrameGrid) grid).setFFrameDependence(this);
543
            grid.draw(g, at, rv, imgBase);
544
        }
545
    }
546

    
547
    private Envelope calculateEnvelope() {
548
        if (p1 == null || p2 == null) {
549
            try {
550
                return geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D);
551
            } catch (CreateEnvelopeException e) {
552
                logger.error("Error creating the envelope", e);
553
            }
554
        }
555
        Envelope r = null;
556
        try {
557
            r =
558
                geomManager.createEnvelope(p1.getX(), p1.getY(), p2.getX(),
559
                    p2.getY(), SUBTYPES.GEOM2D);
560
        } catch (CreateEnvelopeException e) {
561
            logger.error("Error creating the envelope", e);
562
        }
563
        return r;
564
    }
565

    
566
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
567
        PrintAttributes prroperties) {
568
        draw(g, at, null, null);
569
    }
570

    
571
    private void printX(Graphics2D g, AffineTransform at) {
572
        Rectangle2D.Double r = getBoundingBox(at);
573

    
574
        // Dibujamos en impresora
575
        Rectangle rclip = g.getClipBounds();
576
        g.clipRect((int) r.getMinX(), (int) r.getMinY(), (int) r.getWidth(),
577
            (int) r.getHeight());
578
        this.getMapContext().getViewPort()
579
            .setOffset(new Point2D.Double(r.x, r.y));
580
        this.getMapContext().getViewPort()
581
            .setImageSize(new Dimension((int) r.width, (int) r.height));
582

    
583
        try {
584
            this.getMapContext().print(g, getScale(),
585
                getLayoutContext().getAttributes().toPrintAttributes());
586
        } catch (ReadException e) {
587
            NotificationManager.addError(e.getMessage(), e);
588
        } catch (MapContextException e) {
589
            NotificationManager.addError(e.getMessage(), e);
590
        }
591

    
592
        g.setClip(rclip.x, rclip.y, rclip.width, rclip.height);
593
    }
594

    
595
    /**
596
     * Rellena la unidad de medida en la que est? la vista.
597
     * 
598
     * @param i
599
     *            entero que representa la unidad de medida de la vista.
600
     */
601
    public void setMapUnits(int i) {
602
        m_mapUnits = i;
603
    }
604

    
605
    /**
606
     * Obtiene la unidad de medida en la que est? la vista.
607
     * 
608
     * @return Unidad de medida.
609
     */
610
    public int getMapUnits() {
611
        return m_mapUnits;
612
    }
613

    
614
    /**
615
     * Devuelve la escala seg?n el tipo de escala que se haya seleccionado al
616
     * a?adida la vista.
617
     * 
618
     * @return escala.
619
     */
620
    public long getScale() {
621
        /*
622
         * if (m_bLinked){
623
         * return getScaleView1(METROS);
624
         * }
625
         */
626
        if (getMapContext() == null) {
627
            return 0;
628
        }
629
        if (getTypeScale() == AUTOMATICO) {
630
            return FLayoutUtilities.getScaleView(getMapContext().getViewPort(),
631
                getBoundBox().width, getBoundingBox(null).width);
632
        } else
633
            if (getTypeScale() == CONSTANTE) {
634
                return (long) m_Scale;
635
            } else
636
                if (getTypeScale() == MANUAL) {
637
                    return (long) m_Scale;
638
                }
639

    
640
        return (long) m_Scale;
641
    }
642

    
643
    /**
644
     * Seleccionar si la vista esta relacionada o no con la original.
645
     * 
646
     * @param b
647
     *            true si est? ligada y false si no lo est?.
648
     */
649
    public void setLinked(boolean b) {
650
        m_bLinked = b;
651
    }
652

    
653
    /**
654
     * Devuelve si est? ligada o no el FFrameView con la vista.
655
     * 
656
     * @return True si la vista est? ligada.
657
     */
658
    public boolean getLinked() {
659
        return m_bLinked;
660
    }
661

    
662
    /**
663
     * Devuelve la opci?n seleccionada:Rellenar marco de la vista o recorte a
664
     * la vista.
665
     * 
666
     * @return entero que representa la opci?n elegida.
667
     */
668
    public int getExtension() {
669
        return m_extension;
670
    }
671

    
672
    /**
673
     * Devuelve el tipo de escala que est? seleccionada AUTOMATICO,CONSTANTE o
674
     * MANUAL.
675
     * 
676
     * @return entero que representa el tipo seleccionado.
677
     */
678
    public int getTypeScale() {
679
        return m_typeScale;
680
    }
681

    
682
    /**
683
     * Rellenar si se quiere:Rellenar marco de la vista o recorte a la vista.
684
     * 
685
     * @param i
686
     *            entero que representa la opci?n elegida.
687
     */
688
    public void setExtension(int i) {
689
        m_extension = i;
690
    }
691

    
692
    /**
693
     * Rellenar el tipo de escala que se desea.
694
     * 
695
     * @param i
696
     *            entero que representa el tipo de escala.
697
     */
698
    public void setTypeScale(int i) {
699
        m_typeScale = i;
700
    }
701

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

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

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

    
730
    /**
731
     * Inserta la matriz de transformaci?n.
732
     * 
733
     * @param transform
734
     *            Matriz de transformaci?n.
735
     */
736
    public void setATMap(AffineTransform transform) {
737
        at = transform;
738
    }
739

    
740
    /**
741
     * Inserta el proyecto.
742
     * 
743
     * @param p
744
     *            Proyecto.
745
     */
746
    public void setProject(Project p) {
747
        project = p;
748
    }
749

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

    
757
    public String getName() {
758
        return PERSISTENCE_DEFINITION_NAME;
759
    }
760

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

    
792
    /**
793
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#backColorChanged(org.gvsig.fmap.mapcontext.events.ColorEvent)
794
     */
795
    public void backColorChanged(ColorEvent e) {
796
        if (getLinked()) {
797
            m_fmap.getViewPort().setBackColor(e.getNewColor());
798
            getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
799

    
800
            // setBufferedImage(null);
801
        }
802
    }
803

    
804
    /**
805
     * @see org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener#projectionChanged(org.gvsig.fmap.mapcontext.events.ProjectionEvent)
806
     */
807
    public void projectionChanged(ProjectionEvent e) {
808
        if (getTypeScale() == AUTOMATICO) {
809
            m_fmap.getViewPort().setProjection(e.getNewProjection());
810

    
811
            if (getLayoutControl() != null) {
812
                getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
813
            }
814
        }
815
    }
816

    
817
    /**
818
     * DOCUMENT ME!
819
     * 
820
     * @param e
821
     *            DOCUMENT ME!
822
     */
823
    public void legendChanged(final LegendChangedEvent e) {
824
        if (!SwingUtilities.isEventDispatchThread()) {
825
            SwingUtilities.invokeLater(new Runnable() {
826

    
827
                public void run() {
828
                    legendChanged(e);
829
                }
830
            });
831
            return;
832
        }
833
        if (getLinked()) {
834
            getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
835
            refresh = true;
836
        }
837
    }
838

    
839
    /**
840
     * DOCUMENT ME!
841
     * 
842
     * @param arg0
843
     *            DOCUMENT ME!
844
     * 
845
     * @return DOCUMENT ME!
846
     */
847
    public boolean compare(Object arg0) {
848
        if (!(arg0 instanceof FFrameView)) {
849
            return false;
850
        }
851

    
852
        if (!this.getName().equals(((FFrameView) arg0).getName())) {
853
            return false;
854
        }
855

    
856
        if (Math.abs(this.getBoundBox().getWidth()
857
            - (((FFrameView) arg0).getBoundBox().getWidth())) > 0.05) {
858
            return false;
859
        }
860
        if (Math.abs(this.getBoundBox().getHeight()
861
            - (((FFrameView) arg0).getBoundBox().getHeight())) > 0.05) {
862
            return false;
863
        }
864

    
865
        if (!this.toString().equals(((FFrameView) arg0).toString())) {
866
            return false;
867
        }
868

    
869
        if (this.getMapContext() != null
870
            && !this.getMapContext()
871
                .equals(((FFrameView) arg0).getMapContext())) {
872
            return false;
873
        }
874

    
875
        if (this.getRotation() != ((FFrameView) arg0).getRotation()) {
876
            return false;
877
        }
878
        return true;
879
    }
880

    
881
    /**
882
     * DOCUMENT ME!
883
     * 
884
     * @param arg0
885
     *            DOCUMENT ME!
886
     * 
887
     * @return DOCUMENT ME!
888
     */
889
    public boolean equals(Object arg0) {
890
        return this.compare(arg0);
891
    }
892

    
893
    public void refresh() {
894
        if (view != null
895
            && (getTypeScale() == MANUAL || getTypeScale() == CONSTANTE)) {
896
            getMapContext().getViewPort().setEnvelope(
897
                getNewEnvelope(getScale()));
898
        }
899
        refresh = true;
900
    }
901

    
902
    public void fullExtent() throws ReadException {
903
        setNewEnvelope(getMapContext().getFullEnvelope());
904
    }
905

    
906
    public void setPointsToZoom(Point2D px1, Point2D px2) {
907
        p1 = px1;
908
        p2 = px2;
909
    }
910

    
911
    public void movePoints(Point2D px1, Point2D px2) {
912
        double difX = -px2.getX() + px1.getX();
913
        double difY = -px2.getY() + px1.getY();
914
        if (p1 != null) {
915
            p1.setLocation(p1.getX() + difX, p1.getY() + difY);
916
            p2.setLocation(p2.getX() + difX, p2.getY() + difY);
917
        }
918
    }
919

    
920
    protected void cloneActions(IFFrame frame) {
921
        if (view == null || view.getMapContext() == null) {
922
            return;
923
        }
924
        if (m_bLinked) {
925
            if (getTypeScale() == AUTOMATICO) {
926
                view.getMapContext().getViewPort().addViewPortListener(this);
927
                view.getMapContext().addLayerListener(this);
928
            } else
929
                if (getTypeScale() == CONSTANTE) {
930
                    view.getMapContext().getViewPort()
931
                        .removeViewPortListener(this);
932
                    view.getMapContext().addLayerListener(this);
933
                } else
934
                    if (getTypeScale() == MANUAL) {
935
                        view.getMapContext().getViewPort()
936
                            .removeViewPortListener(this);
937
                        view.getMapContext().addLayerListener(this);
938
                    }
939
        } else
940
            if (!m_bLinked) {
941
                if (getTypeScale() == AUTOMATICO) {
942
                    view.getMapContext().getViewPort()
943
                        .addViewPortListener(this);
944
                } else
945
                    if (getTypeScale() == CONSTANTE) {
946
                        view.getMapContext().getViewPort()
947
                            .removeViewPortListener(this);
948
                    } else
949
                        if (getTypeScale() == MANUAL) {
950
                            view.getMapContext().getViewPort()
951
                                .removeViewPortListener(this);
952
                        }
953
            }
954
        ((FFrameView) frame).view.getMapContext().removeLayerListener(
955
            (FFrameView) frame);
956
        ((FFrameView) frame).view.getMapContext().getViewPort()
957
            .removeViewPortListener((FFrameView) frame);
958
    }
959

    
960
    public Object clone() throws CloneNotSupportedException {
961
        FFrameView frame = (FFrameView) super.clone();
962

    
963
        frame.setSelected(this.getSelected() != IFFrame.NOSELECT);
964

    
965
        if (grid != null) {
966
            FFrameGrid newGrid = (FFrameGrid) this.grid.clone();
967
            newGrid.setFFrameDependence(frame);
968
            frame.setGrid(newGrid);
969
        }
970
        cloneActions(frame);
971
        return frame;
972
    }
973

    
974
    public void setGrid(IFFrame grid) {
975
        this.grid = grid;
976
        this.grid.setRotation(this.getRotation());
977
    }
978

    
979
    public IFFrame getGrid() {
980
        return this.grid;
981
    }
982

    
983
    public void setRotation(double rotation) {
984
        super.setRotation(rotation);
985
        if (grid != null) {
986
            grid.setRotation(rotation);
987
        }
988
    }
989

    
990
    public void showGrid(boolean b) {
991
        showGrid = b;
992
    }
993

    
994
    public boolean isShowGrid() {
995
        return showGrid;
996
    }
997

    
998
    public void refreshOriginalExtent() {
999
        if (getTypeScale() == AUTOMATICO) {
1000
            view.getMapContext().getViewPort()
1001
                .setEnvelope(getMapContext().getViewPort().getEnvelope());
1002
            view.getMapContext().getViewPort().refreshExtent();
1003
        } else
1004
            if (getTypeScale() == MANUAL) {
1005
                Envelope oldExtent =
1006
                    view.getMapContext().getViewPort().getEnvelope();
1007
                Envelope newExtent =
1008
                    getMapContext().getViewPort().getEnvelope();
1009
                double xDif = newExtent.getCenter(0) - oldExtent.getCenter(0);
1010
                double yDif = newExtent.getCenter(1) - oldExtent.getCenter(1);
1011
                try {
1012
                    view.getMapContext()
1013
                        .getViewPort()
1014
                        .setEnvelope(
1015
                            geomManager.createEnvelope(oldExtent.getMinimum(0)
1016
                                + xDif, oldExtent.getMinimum(1) + yDif,
1017
                                oldExtent.getMaximum(0) + xDif,
1018
                                oldExtent.getMaximum(1) + yDif, SUBTYPES.GEOM2D));
1019
                } catch (CreateEnvelopeException e) {
1020
                    e.printStackTrace();
1021
                }
1022
                view.getMapContext().getViewPort().refreshExtent();
1023
            }
1024
    }
1025

    
1026
    public static void registerPersistent() {
1027
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1028
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
1029
            DynStruct definition =
1030
                manager.addDefinition(FFrameView.class,
1031
                    PERSISTENCE_DEFINITION_NAME,
1032
                    "FFrameView persistence definition", null, null);
1033

    
1034
            definition.extend(manager
1035
                .getDefinition(FFrame.PERSISTENCE_DEFINITION_NAME));
1036

    
1037
            definition.addDynFieldInt(MODE_FIELD).setMandatory(true);
1038
            definition.addDynFieldInt(TYPESCALE_FIELD).setMandatory(true);
1039
            definition.addDynFieldInt(EXTENSION_FIELD).setMandatory(true);
1040
            definition.addDynFieldInt(QUALITY_FIELD).setMandatory(true);
1041
            definition.addDynFieldInt(VIEWING_FIELD).setMandatory(true);
1042
            definition.addDynFieldBoolean(BLINKED_FIELD).setMandatory(true);
1043
            definition.addDynFieldInt(MAPUNITS_FIELD).setMandatory(true);
1044
            definition.addDynFieldDouble(SCALE_FIELD).setMandatory(true);
1045
            definition.addDynFieldObject(VIEW_FIELD)
1046
                .setClassOfValue(ViewDocument.class).setMandatory(false);
1047
            definition.addDynFieldObject(ENVELOPE_FIELD)
1048
                .setClassOfValue(Envelope.class).setMandatory(false);
1049
            definition.addDynFieldBoolean(SHOWGRID_FIELD).setMandatory(true);
1050
            definition.addDynFieldObject(GRID_FIELD)
1051
                .setClassOfValue(IFFrame.class).setMandatory(false);
1052
            definition.addDynFieldObject(MAPCONTEXT_FIELD)
1053
                .setClassOfValue(MapContext.class).setMandatory(false);
1054
        }
1055
    }
1056

    
1057
    @Override
1058
    public void loadFromState(PersistentState state)
1059
        throws PersistenceException {
1060
        super.loadFromState(state);
1061
        m_Mode = state.getInt(MODE_FIELD);
1062
        m_typeScale = state.getInt(TYPESCALE_FIELD);
1063
        m_extension = state.getInt(EXTENSION_FIELD);
1064
        m_quality = state.getInt(QUALITY_FIELD);
1065
        m_viewing = state.getInt(VIEWING_FIELD);
1066
        m_bLinked = state.getBoolean(BLINKED_FIELD);
1067
        m_mapUnits = state.getInt(MAPUNITS_FIELD);
1068
        m_Scale = state.getDouble(SCALE_FIELD);
1069
        view = (ViewDocument) state.get(VIEW_FIELD);
1070

    
1071
        if (state.hasValue(MAPCONTEXT_FIELD)) {
1072
            m_fmap = (MapContext) state.get(MAPCONTEXT_FIELD);
1073
            m_fmap.getViewPort().setEnvelope(
1074
                (Envelope) state.get(ENVELOPE_FIELD));
1075
        }
1076

    
1077
        showGrid = state.getBoolean(SHOWGRID_FIELD);
1078
        grid = (IFFrame) state.get(GRID_FIELD);
1079
    }
1080

    
1081
    @Override
1082
    public void saveToState(PersistentState state) throws PersistenceException {
1083
        super.saveToState(state);
1084
        state.set(MODE_FIELD, m_Mode);
1085
        state.set(TYPESCALE_FIELD, m_typeScale);
1086
        state.set(EXTENSION_FIELD, m_extension);
1087
        state.set(QUALITY_FIELD, m_quality);
1088
        state.set(VIEWING_FIELD, m_viewing);
1089
        state.set(BLINKED_FIELD, m_bLinked);
1090
        state.set(MAPUNITS_FIELD, m_mapUnits);
1091
        state.set(SCALE_FIELD, m_Scale);
1092
        state.set(VIEW_FIELD, view);
1093

    
1094
        if (getMapContext() != null
1095
            && getMapContext().getViewPort().getEnvelope() != null) {
1096
            state.set(ENVELOPE_FIELD, getMapContext().getViewPort()
1097
                .getEnvelope());
1098
            state.set(MAPCONTEXT_FIELD, m_fmap);
1099
        }
1100

    
1101
        state.set(SHOWGRID_FIELD, showGrid);
1102
        state.set(GRID_FIELD, grid);
1103
    }
1104
}