Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Layout.java @ 1282

History | View | Annotate | Download (34.3 KB)

1
/*
2
 * Created on 20-feb-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.gui.layout;
46

    
47
import com.iver.andami.PluginServices;
48
import com.iver.andami.ui.mdiManager.SingletonView;
49
import com.iver.andami.ui.mdiManager.ViewInfo;
50
import com.iver.andami.ui.mdiManager.ViewListener;
51

    
52
import com.iver.cit.gvsig.Abrir;
53
import com.iver.cit.gvsig.fmap.ColorEvent;
54
import com.iver.cit.gvsig.fmap.DriverException;
55
import com.iver.cit.gvsig.fmap.ExtentEvent;
56
import com.iver.cit.gvsig.fmap.ViewPortListener;
57
import com.iver.cit.gvsig.gui.layout.dialogs.FConfigLayoutDialog;
58
import com.iver.cit.gvsig.gui.layout.fframes.FFrame;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphics;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
61
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
62
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
63
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
64
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
65
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
66
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameGraphicsDialog;
67
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameLegendDialog;
68
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFramePictureDialog;
69
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameScaleBarDialog;
70
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameTextDialog;
71
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameViewDialog;
72
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.IFFrameDialog;
73
import com.iver.cit.gvsig.gui.project.MapProperties;
74
import com.iver.cit.gvsig.project.Project;
75
import com.iver.cit.gvsig.project.ProjectMap;
76

    
77
import com.iver.utiles.XMLEntity;
78

    
79
import org.apache.log4j.Logger;
80

    
81
import java.awt.Color;
82
import java.awt.Cursor;
83
import java.awt.Graphics;
84
import java.awt.Graphics2D;
85
import java.awt.Image;
86
import java.awt.Point;
87
import java.awt.Rectangle;
88
import java.awt.Toolkit;
89
import java.awt.event.ActionEvent;
90
import java.awt.event.KeyEvent;
91
import java.awt.geom.AffineTransform;
92
import java.awt.geom.Rectangle2D;
93
import java.awt.image.BufferedImage;
94
import java.awt.print.PageFormat;
95
import java.awt.print.Printable;
96
import java.awt.print.PrinterException;
97
import java.awt.print.PrinterJob;
98

    
99
import java.util.ArrayList;
100
import java.util.Hashtable;
101

    
102
import javax.print.Doc;
103
import javax.print.DocFlavor;
104
import javax.print.DocPrintJob;
105
import javax.print.PrintException;
106
import javax.print.PrintService;
107
import javax.print.PrintServiceLookup;
108
import javax.print.ServiceUI;
109
import javax.print.SimpleDoc;
110
import javax.print.attribute.PrintRequestAttributeSet;
111
import javax.print.event.PrintJobAdapter;
112
import javax.print.event.PrintJobEvent;
113
import javax.print.event.PrintJobListener;
114

    
115
import javax.swing.AbstractAction;
116
import javax.swing.Action;
117
import javax.swing.ImageIcon;
118
import javax.swing.JPanel;
119
import javax.swing.KeyStroke;
120

    
121

    
122
/**
123
 * Mapa.
124
 *
125
 * @author Vicente Caballero Navarro
126
 */
127
public class Layout extends JPanel implements SingletonView, ViewPortListener,
128
        ViewListener {
129
        private static Logger logger = Logger.getLogger(Layout.class.getName());
130
        public static final int ZOOM_MAS = 1;
131
        public static final int ZOOM_MENOS = 2;
132
        public static final int PAN = 3;
133
        public static final int DESACTUALIZADO = 4;
134
        public static final int RECTANGLEVIEW = 5;
135
        public static final int RECTANGLEPICTURE = 6;
136
        public static final int RECTANGLESCALEBAR = 7;
137
        public static final int RECTANGLELEGEND = 8;
138
        public static final int RECTANGLETEXT = 9;
139
        public static final int RECTANGLE = 10;
140
        public static final int ACTUALIZADO = 11;
141
        public static final int SELECT = 12;
142
        public static final int POINT = 13;
143
        public static final int LINE = 14;
144
        public static final int POLYLINE = 15;
145
        public static final int RECTANGLESIMPLE = 16;
146
        public static final int CIRCLE = 17;
147
        public static final int POLYGON = 18;
148
        public static final int GRAPHICS = 19;
149
        public static final int RECTANGLESYMBOL = 20;
150
        public static final int RECTANGLEGROUP = 21;
151
        public static final int VIEW_ZOOMIN = 22;
152
        public static final int VIEW_ZOOMOUT = 23;
153
        public static final int VIEW_FULL = 24;
154
        public static final int VIEW_PAN = 25;
155
        public static final int SET_TAG = 26;
156
        public static final Image iLayoutpan = new ImageIcon(Abrir.class.getClassLoader()
157
                                                                                                                                        .getResource("images/LayoutHand.gif")).getImage();
158
        public static final Image ipan = new ImageIcon(Abrir.class.getClassLoader()
159
                                                                                                                          .getResource("images/Hand.gif")).getImage();
160
        public static final Image iLayoutzoomin = new ImageIcon(Abrir.class.getClassLoader()
161
                                                                                                                                           .getResource("images/LayoutZoomInCursor.gif")).getImage();
162
        public static final Image izoomin = new ImageIcon(Abrir.class.getClassLoader()
163
                                                                                                                                 .getResource("images/ZoomInCursor.gif")).getImage();
164
        public static final Image iLayoutzoomout = new ImageIcon(Abrir.class.getClassLoader()
165
                                                                                                                                                .getResource("images/LayoutZoomOutCursor.gif")).getImage();
166
        public static final Image izoomout = new ImageIcon(Abrir.class.getClassLoader()
167
                                                                                                                                  .getResource("images/ZoomOutCursor.gif")).getImage();
168
        public static final Image iinfo = new ImageIcon(Abrir.class.getClassLoader()
169
                                                                                                                           .getResource("images/InfoCursor.gif")).getImage();
170
        public static final Image icrux = new ImageIcon(Abrir.class.getClassLoader()
171
                                                                                                                           .getResource("images/CruxCursor.png")).getImage();
172
        public static final Image itag = new ImageIcon(Abrir.class.getClassLoader()
173
                                                                                                                          .getResource("images/tagCursor.gif")).getImage();
174
        public static final Image iPoint = new ImageIcon(Abrir.class.getClassLoader()
175
                                                                                                                                .getResource("images/PointCursor.png")).getImage();
176
        public static final Image iRect = new ImageIcon(Abrir.class.getClassLoader()
177
                                                                                                                           .getResource("images/RectCursor.png")).getImage();
178
        public static final Image iLine = new ImageIcon(Abrir.class.getClassLoader()
179
                                                                                                                           .getResource("images/LineCursor.png")).getImage();
180
        public static final Image iRectangle = new ImageIcon(Abrir.class.getClassLoader()
181
                                                                                                                                        .getResource("images/RectangleCursor.png")).getImage();
182
        public static final Image iCircle = new ImageIcon(Abrir.class.getClassLoader()
183
                                                                                                                                 .getResource("images/CircleCursor.png")).getImage();
184
        public static final Image iPoligon = new ImageIcon(Abrir.class.getClassLoader()
185
                                                                                                                                  .getResource("images/PoligonCursor.png")).getImage();
186
        public static Hashtable nums = new Hashtable();
187
        private Point origin = new Point(50, 50);
188
        private Point rectOrigin = new Point(origin);
189
        private Rectangle2D.Double rect = new Rectangle2D.Double(rectOrigin.x,
190
                        rectOrigin.y, 400, 300);
191
        private Point m_FirstPoint = new Point(0, 0);
192
        private Point m_PointAnt = new Point(0, 0);
193
        private Point m_LastPoint = new Point(0, 0);
194
        private EventsHandler events;
195
        private int tool = ZOOM_MAS;
196
        private int status = DESACTUALIZADO;
197
        private BufferedImage img = null;
198
        private AffineTransform m_MatrizTransf;
199
        private Rectangle2D rectVisible;
200
        private IFFrameDialog fframedialog = null;
201
        private ArrayList m_fframes = new ArrayList();
202
        private FConfigLayoutDialog m_configLayout = null;
203
        private MapProperties m_propertiesLayout = null;
204
        private Attributes m_attributes = null;
205
        private PrintService[] m_cachePrintServices = null;
206
        private PrintRequestAttributeSet m_cachePrintRequestAtributeSet = null;
207
        private PrintService m_cachePrintService = null;
208
        private boolean m_bCancelDrawing = false;
209
        private boolean isCuadricula = false;
210
        private boolean initial = true;
211
        private ProjectMap map = null;
212
        private Rectangle reSel = null;
213
        private boolean isReSel = true;
214

    
215
        // private String m_name = null;
216
        private boolean m_showRuler = true;
217
        private FLayoutDraw layoutPaint = null;
218
        private boolean isDrawCuadricula = true;
219
        private Doc doc = null;
220
        private PrintRequestAttributeSet att = null;
221

    
222
        /**
223
         * Creates a new Layout object.
224
         */
225
        public Layout() {
226
                this.setLayout(null);
227
                events = new EventsHandler(this);
228
                layoutPaint = new FLayoutDraw(this);
229
                addComponentListener(events);
230
                addMouseMotionListener(events);
231
                addMouseListener(events);
232
                m_MatrizTransf = new AffineTransform();
233
                m_MatrizTransf.setToIdentity();
234
                this.initComponents();
235
        }
236

    
237
        /**
238
         * Rellena el ProjectMap del Layout.
239
         *
240
         * @param m ProjectMap.
241
         */
242
        public void setProjectMap(ProjectMap m) {
243
                map = m;
244
        }
245

    
246
        /**
247
         * Devuelve el rect?ngulo de selecci?n por rect?ngulo.
248
         *
249
         * @return Rect?ngulo de selecci?n.
250
         */
251
        public Rectangle getReSel() {
252
                return reSel;
253
        }
254

    
255
        /**
256
         * Devuelve true si se debe dibujar el rect?ngulo de selecci?n y realizar
257
         * la sellecci?n.
258
         *
259
         * @return true si se realiza la selecci?n por rect?ngulo.
260
         */
261
        public boolean isReSel() {
262
                return isReSel;
263
        }
264

    
265
        /**
266
         * Rellena con true si se debe de dibujar y seleccionar por rect?ngulo de
267
         * selecci?n.
268
         *
269
         * @param b boolean.
270
         */
271
        public void setIsReSel(boolean b) {
272
                isReSel = b;
273
        }
274

    
275
        /**
276
         * Devuelve true si el dibujado ha sido cancelado.
277
         *
278
         * @return true si el dibujado ha sido cancelado.
279
         */
280
        public synchronized boolean isDrawingCancelled() {
281
                return m_bCancelDrawing;
282
        }
283

    
284
        /**
285
         * Pone el dibuja a cancelado o no seg?n se quiera.
286
         *
287
         * @param b true si se quiere cancelar el dibujado.
288
         */
289
        public synchronized void setCancelDrawing(boolean b) {
290
                m_bCancelDrawing = b;
291

    
292
                for (int i = 0; i < getFFrames().size(); i++) {
293
                        IFFrame fframe = (IFFrame) getFFrames().get(i);
294

    
295
                        if (fframe instanceof FFrameView &&
296
                                        (((FFrameView) fframe).getFMap() != null)) {
297
                                ////TODO((FFrameView) getFFrames().get(i)).getFMap().setCancelDrawing(b);
298
                        }
299
                }
300
        }
301

    
302
        /**
303
         * Obtiene el ArrayList con todos los FFrames que se han a?adido al Layout.
304
         *
305
         * @return Array con todos los fframes que contiene el Layout.
306
         */
307
        public ArrayList getFFrames() {
308
                return m_fframes;
309
        }
310

    
311
        /**
312
         * Obtiene el rect que se ajusta al tama?o de la ventana, para ver el folio
313
         * entero.
314
         */
315
        public void fullRect() {
316
                rect.setRect(origin.x, origin.y, getWidth() - (origin.x * 2),
317
                        getHeight() - (origin.x * 2));
318

    
319
                if (m_attributes.isLandSpace()) {
320
                        rect = m_attributes.getRectangleLandscape(rect, getWidth(),
321
                                        getHeight());
322
                } else {
323
                        rect = m_attributes.getRectanglePortrait(rect, getWidth(),
324
                                        getHeight());
325
                }
326

    
327
                status = DESACTUALIZADO;
328
                repaint();
329
        }
330

    
331
        /**
332
         * M?todo para imprimir el Layout sin modificar la matriz de
333
         * transformaci?n.
334
         *
335
         * @param g2
336
         */
337
        public void drawLayoutPrint(Graphics2D g2) {
338
                setCancelDrawing(false);
339

    
340
                setCursor(Cursor.getDefaultCursor());
341

    
342
                double scale = 0;
343

    
344
                ///if (rect.width > rect.height) {
345
                ///    scale = rect.width / m_attributes.m_sizePaper.getAlto() * 1;
346
                ///} else {
347
                scale = rect.height / m_attributes.m_sizePaper.getAlto() * 1;
348

    
349
                ///}
350
                AffineTransform escalado = new AffineTransform();
351
                AffineTransform translacion = new AffineTransform();
352
                translacion.setToTranslation(rect.getMinX(), rect.getMinY());
353
                escalado.setToScale(scale, scale);
354
                m_MatrizTransf.setToIdentity();
355
                m_MatrizTransf.concatenate(translacion);
356
                m_MatrizTransf.concatenate(escalado);
357
                m_attributes.setDistanceUnitX(rect);
358
                m_attributes.setDistanceUnitY(rect);
359

    
360
                for (int i = 0; i < getFFrames().size(); i++) {
361
                        try {
362
                                ((IFFrame) getFFrames().get(i)).print(g2, getAT());
363
                        } catch (DriverException e) {
364
                                e.printStackTrace();
365
                        }
366
                }
367

    
368
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
369
        }
370

    
371
        /**
372
         * Clip sobre el rect?ngulo visible.
373
         *
374
         * @param g2d Graphics sobre el que hacer el clip.
375
         */
376
        private void clipVisibleRect(Graphics2D g2d) {
377
                rectVisible = this.getVisibleRect();
378
                g2d.clipRect((int) rectVisible.getMinX(), (int) rectVisible.getMinY(),
379
                        (int) rectVisible.getWidth(), (int) rectVisible.getHeight());
380
        }
381

    
382
        /**
383
         * Inicializa los componentes.
384
         */
385
        private void initComponents() {
386
                m_attributes = new Attributes();
387
                m_attributes.setDistanceUnitX(rect);
388
                m_attributes.setDistanceUnitY(rect);
389
                actionDelFFrame();
390
        }
391

    
392
        /**
393
         * Crea un ActionEvent para controlar las teclas que se pulsen cuando este
394
         * el Layout Focusable a true.
395
         */
396
        private void actionDelFFrame() {
397
                Action doNothing = new AbstractAction() {
398
                                public void actionPerformed(ActionEvent e) {
399
                                        for (int i = getFFrames().size() - 1; i >= 0; i--) {
400
                                                IFFrame fframe = (IFFrame) getFFrames().get(i);
401

    
402
                                                if (fframe.getSelected() != FFrame.NOSELECT) {
403
                                                        getFFrames().remove(i);
404
                                                }
405
                                        }
406

    
407
                                        refresh();
408
                                }
409
                        };
410

    
411
                this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
412
                        "doNothing");
413
                this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0),
414
                        "doNothing");
415
                this.getActionMap().put("doNothing", doNothing);
416
        }
417

    
418
        /**
419
         * paintComponent del Layout.
420
         *
421
         * @param g Graphics del Layout.
422
         */
423
        protected void paintComponent(Graphics g) {
424
                super.paintComponent(g);
425
                
426
                clipVisibleRect((Graphics2D) g);
427
                Rectangle rClip = g.getClipBounds();
428
                if (rClip == null) System.err.println("clip = null");
429
                switch (status) {
430
                        case ZOOM_MAS:
431
                                logger.debug("zoom mas");
432
                                layoutPaint.drawRectangle((Graphics2D) g);
433

    
434
                                g.drawImage(img, 0, 0, this);
435
                                
436
                                g.setClip(rClip);
437
                                layoutPaint.drawGrid((Graphics2D) g);                                
438
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
439
                                g.setColor(Color.black);
440
                                g.setXORMode(Color.white);
441

    
442
                                Rectangle r = new Rectangle();
443

    
444
                                r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
445
                                g.drawRect(r.x, r.y, r.width, r.height);
446

    
447
                                break;
448

    
449
                        case RECTANGLE:
450
                                logger.debug("rectangle");
451
                                layoutPaint.drawRectangle((Graphics2D) g);
452

    
453
                                g.drawImage(img, 0, 0, this);
454

    
455
                                g.setClip(rClip);
456
                                layoutPaint.drawGrid((Graphics2D) g);                                
457
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
458
                                g.setColor(Color.black);
459
                                g.setXORMode(Color.white);
460

    
461
                                Rectangle re = new Rectangle();
462
                                re.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
463

    
464
                                if (isCuadricula()) {
465
                                        FLayoutUtilities.setRectGrid(re,
466
                                                m_attributes.getUnitInPixelsX(),
467
                                                m_attributes.getUnitInPixelsY(), m_MatrizTransf);
468
                                }
469

    
470
                                g.drawRect(re.x, re.y, re.width, re.height);
471

    
472
                                break;
473

    
474
                        case PAN:
475
                                logger.debug("pan");
476

    
477
                                //g.setXORMode(Color.black);
478
                                rect.x = rectOrigin.x + (m_LastPoint.x - m_PointAnt.x);
479
                                rect.y = rectOrigin.y + (m_LastPoint.y - m_PointAnt.y);
480

    
481
                                //g.clearRect(0, 0, getWidth(), getHeight());
482
                                layoutPaint.drawRectangle((Graphics2D) g);
483

    
484
                                if (img != null) {
485
                                        g.drawImage(img, (getLastPoint().x - getPointAnt().x),
486
                                                (m_LastPoint.y - m_PointAnt.y), this);
487
                                }
488

    
489
                                g.setClip(rClip);
490
                                layoutPaint.drawGrid((Graphics2D) g);
491
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
492

    
493
                                break;
494

    
495
                        case VIEW_PAN:
496

    
497
                                // logger.debug("View pan");
498
                                layoutPaint.drawRectangle((Graphics2D) g);
499

    
500
                                for (int i = 0; i < getFFrames().size(); i++) {
501
                                        if (getFFrames().get(i) instanceof FFrameView) {
502
                                                FFrameView fframe = (FFrameView) getFFrames().get(i);
503

    
504
                                                if (fframe.getSelected() != FFrame.NOSELECT) {
505
                                                        Rectangle2D.Double rec = fframe.getBoundingBox(getAT());
506

    
507
                                                        if (img != null) {
508
                                                                //g.drawImage(img, 0, 0, this);
509
                                                                rec = (Rectangle2D.Double) rec.createIntersection(getVisibleRect());
510

    
511
                                                                //((Graphics2D)g).clearRect((int)rec.x,(int)rec.y,(int)rec.width,(int)rec.height);
512
                                                        }
513

    
514
                                                        if (fframe.getBufferedImage() != null) {
515
                                                                layoutPaint.drawHandlers((Graphics2D) g,
516
                                                                        Color.black);
517
                                                                g.clipRect((int) rec.x, (int) rec.y,
518
                                                                        (int) rec.width, (int) rec.height);
519

    
520
                                                                //g.setColor(Color.gray);
521
                                                                //g.fillRect(0,0,getWidth(),getHeight());
522
                                                                layoutPaint.drawRectangle((Graphics2D) g);
523
                                                                layoutPaint.drawRuler((Graphics2D) g,
524
                                                                        Color.black);
525

    
526
                                                                g.drawImage(fframe.getBufferedImage(),
527
                                                                        m_LastPoint.x - m_PointAnt.x,
528
                                                                        m_LastPoint.y - m_PointAnt.y, this);
529
                                                                System.out.println(
530
                                                                        "m_LastPoint.x - m_PointAnt.x" +
531
                                                                        (m_LastPoint.x - m_PointAnt.x));
532
                                                                System.out.println(
533
                                                                        "m_LastPoint.y - m_PointAnt.y" +
534
                                                                        (m_LastPoint.y - m_PointAnt.y));
535
                                                        }
536
                                                }
537
                                        }
538
                                }
539
                                g.setClip(rClip);
540
                                layoutPaint.drawGrid((Graphics2D) g);
541
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
542
                                //g.setXORMode(Color.black);
543
                                break;
544

    
545
                        case SELECT:
546
                                logger.debug("select");
547
                                layoutPaint.drawRectangle((Graphics2D) g);
548

    
549
                                g.drawImage(img, 0, 0, this);
550

    
551
                                if (isReSel) {
552
                                        reSel = new Rectangle();
553
                                        reSel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
554
                                        g.drawRect(reSel.x, reSel.y, reSel.width, reSel.height);
555
                                }
556

    
557
                                for (int i = 0; i < getFFrames().size(); i++) {
558
                                        IFFrame fframe = (IFFrame) getFFrames().get(i);
559

    
560
                                        if (fframe.getSelected() != FFrame.NOSELECT) {
561
                                                g.setColor(Color.black);
562
                                                fframe.drawHandlers((Graphics2D) g);
563

    
564
                                                int difx = (m_LastPoint.x - m_FirstPoint.x);
565
                                                int dify = (m_LastPoint.y - m_FirstPoint.y);
566

    
567
                                                if ((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) {
568
                                                        Rectangle2D rectangle = fframe.getMovieRect(difx,
569
                                                                        dify);
570

    
571
                                                        if (rectangle != null) {
572
                                                                if (isCuadricula) {
573
                                                                        FLayoutUtilities.setRectGrid(rectangle,
574
                                                                                m_attributes.getUnitInPixelsX(),
575
                                                                                m_attributes.getUnitInPixelsY(),
576
                                                                                m_MatrizTransf);
577
                                                                }
578

    
579
                                                                g.drawRect((int) rectangle.getMinX(),
580
                                                                        (int) rectangle.getMinY(),
581
                                                                        (int) rectangle.getWidth(),
582
                                                                        (int) rectangle.getHeight());
583
                                                        }
584
                                                }
585
                                        }
586
                                }
587
                                g.setClip(rClip);
588
                                layoutPaint.drawGrid((Graphics2D) g);
589
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
590

    
591
                                break;
592

    
593
                        case GRAPHICS:
594
                                layoutPaint.drawRectangle((Graphics2D) g);
595
                                g.drawImage(img, 0, 0, this);
596
                                g.setClip(rClip);
597
                                layoutPaint.drawGrid((Graphics2D) g);                                
598
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
599

    
600
                                break;
601

    
602
                        case DESACTUALIZADO:
603
                                logger.debug("desactualizado");
604
                                img = new BufferedImage(getWidth(), getHeight(),
605
                                                BufferedImage.TYPE_INT_ARGB);
606

    
607
                                Graphics gimag = img.getGraphics();
608
                                clipVisibleRect((Graphics2D) gimag);
609

    
610
                                if (initial) {
611
                                        fullRect();
612
                                        initial = false;
613
                                }
614
                                try {
615
                                        layoutPaint.drawLayout((Graphics2D) gimag, img);
616
                                } catch (DriverException e) {
617
                                        e.printStackTrace();
618
                                }
619
                                
620
                                g.setClip(rClip);
621
                                layoutPaint.drawRectangle((Graphics2D) g);
622

    
623
                                g.drawImage(img, 0, 0, this);
624
                                g.setClip(rClip);
625
                                layoutPaint.drawGrid((Graphics2D) g);                                
626
                                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
627
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
628

    
629
                                break;
630

    
631
                        case ACTUALIZADO:
632
                                layoutPaint.drawRectangle((Graphics2D) g);
633

    
634
                                g.drawImage(img, 0, 0, this);
635

    
636
                                g.setClip(rClip);
637
                                layoutPaint.drawGrid((Graphics2D) g);
638
                                
639
                                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
640
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
641
                                
642
                }
643

    
644
        }
645

    
646
        /**
647
         * A?ade un fframe al Arraylist m_fframes.
648
         *
649
         * @param frame fframe a a?adir.
650
         * @param clearSelection para que se quede seleccionado ?nicamente  el que
651
         *                   a?adimos y false si lo que se pretende es que no se
652
         *                   deseleccionen lo que ya est?n seleccionados.
653
         */
654
        public void addFFrame(IFFrame frame, boolean clearSelection) {
655
                if (clearSelection) {
656
                        for (int i = getFFrames().size() - 1; i >= 0; i--) {
657
                                IFFrame fframe1 = (IFFrame) getFFrames().get(i);
658
                                fframe1.setSelected(false);
659
                        }
660
                }
661

    
662
                if (frame instanceof FFrameView &&
663
                                (((FFrameView) frame).getFMap() != null)) {
664
                        ((FFrameView) frame).getFMap().getViewPort().addViewPortListener(this);
665
                }
666

    
667
                if (nums.containsKey(frame.getClass())) {
668
                        nums.put(frame.getClass(),
669
                                new Integer(Integer.parseInt(nums.get(frame.getClass())
670
                                                                                                 .toString()) + 1));
671
                } else {
672
                        nums.put(frame.getClass(), new Integer(0));
673
                }
674

    
675
                frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
676
                getFFrames().add(frame);
677
                frame.setSelected(true);
678
        }
679

    
680
        /**
681
         * Abre el di?logo de propiedades del folio, pasando como par?metro el
682
         * PrinterJob.
683
         *
684
         * @param job
685
         */
686
        public void showPagePropertiesWindow(PrinterJob job) {
687
                PageFormat pf1; //=new PageFormat();
688

    
689
                pf1 = m_attributes.getPageFormat();
690
                pf1 = job.pageDialog(pf1);
691
                m_attributes.setPageFormat(pf1);
692
                refresh();
693
        }
694

    
695
        /**
696
         * Refresca el Layout.
697
         */
698
        public void refresh() {
699
                rect = m_attributes.obtainRect(false, rect, getWidth(), getHeight());
700
                status = DESACTUALIZADO;
701
                repaint();
702
        }
703

    
704
        /**
705
         * Obtiene el rect?ngulo que representa el folio con las caracter?sticas
706
         * que contiene attributes y diferenciando si es para visualizar en
707
         * pantalla o para imprimir.
708
         *
709
         * @param isPrint si es para imprimir.
710
         */
711
        public void obtainRect(boolean isPrint) {
712
                m_attributes.obtainRect(isPrint, rect, getWidth(), getHeight());
713
        }
714

    
715
        /**
716
         * Muestra el di?logo de configuraci?n del Layout.
717
         */
718
        public void showFConfig() {
719
                FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
720
                PluginServices.getMDIManager().addView(m_configLayout);
721
        }
722

    
723
        /**
724
         * Mestra el di?logo de propiedades del Layout.
725
         */
726
        public void showFProperties() {
727
                if (map == null) {
728
                        map = new ProjectMap();
729
                        map.setModel(this);
730
                        map.setName(getName());
731
                }
732

    
733
                m_propertiesLayout = new MapProperties(map);
734
                PluginServices.getMDIManager().addView(m_propertiesLayout);
735
        }
736

    
737
        /**
738
         * Muestra el di?logo de imprimir pasandole el printerJob como par?metro.
739
         *
740
         * @param job
741
         */
742
        public void showPrintDialog(PrinterJob job) {
743
                if (job != null) {
744
                        job.printDialog();
745

    
746
                        try {
747
                                job.setPrintable((Printable) PluginServices.getExtension(
748
                                                com.iver.cit.gvsig.Print.class));
749
                                job.print();
750
                        } catch (PrinterException e) {
751
                                e.printStackTrace();
752
                        }
753
                } else {
754
                        boolean print = false;
755

    
756
                        //Actualizar attributes
757
                        att = m_attributes.toPrintAttributes();
758

    
759
                        //------------------ The Printing things --------------------- //
760
                        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
761

    
762
                        // returns the set of printers that support printing a specific document type (such as GIF)
763
                        // with a specific set of attributes (such as two sided).
764
                        // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
765
                        // interestingly, the printer dialog's default behavior has changed with the new API: by default the dialog is not shown.
766
                        // So we must use the ServiceUI class to create a print dialog
767
                        // returns the default print service.
768
                        if (m_cachePrintServices == null) {
769
                                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
770
                                                null);
771
                        }
772

    
773
                        PrintService defaultService = null;
774

    
775
                        if (m_cachePrintService == null) {
776
                                defaultService = PrintServiceLookup.lookupDefaultPrintService();
777
                        }
778

    
779
                        if (m_cachePrintService == null) {
780
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
781
                                                m_cachePrintServices, defaultService, flavor, att);
782

    
783
                                // m_cachePrintRequestAtributeSet = new HashPrintRequestAttributeSet();
784
                                // m_cachePrintRequestAtributeSet.addAll(pras);
785
                        } else {
786
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
787
                                                m_cachePrintServices, m_cachePrintService, flavor, att);
788
                        }
789

    
790
                        if (m_cachePrintService != null) {
791
                                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
792
                                PrintJobListener pjlistener = new PrintJobAdapter() {
793
                                                public void printDataTransferCompleted(PrintJobEvent e) {
794
                                                        System.out.println("Fin de impresi?n");
795
                                                }
796
                                        };
797

    
798
                                jobNuevo.addPrintJobListener(pjlistener);
799

    
800
                                // DocAttributeSet das = new HashDocAttributeSet();
801
                                doc = new SimpleDoc((Printable) PluginServices.getExtension(
802
                                                        com.iver.cit.gvsig.Print.class), flavor, null);
803

    
804
                                try {
805
                                        jobNuevo.print(doc, att);
806

    
807
                                        // m_attributes.
808
                                } catch (PrintException pe) {
809
                                        pe.printStackTrace();
810
                                }
811
                        }
812
                }
813

    
814
                /* try {
815
                   print = job.printDialog(att);
816
                   } catch (Exception e) {
817
                       logger.error("Abriendo el Di?logo de imprimir"); //System.out.println("Excepci?n "+e);
818
                   }
819
                   if (print) {
820
                       job.setPrintable((Printable) App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
821
                       try {
822
                           job.print(att);
823
                       } catch (Exception ex) {
824
                           ex.printStackTrace();
825
                       }
826
                   } */
827
                fullRect();
828
        }
829

    
830
        /**
831
         * Sustituye el puntero del rat?n por la imagen que se le pasa como
832
         * par?metro.
833
         *
834
         * @param image
835
         */
836
        public void setMapCursor(Image image) {
837
                Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(image,
838
                                new Point(16, 16), "");
839
                setCursor(cur);
840
        }
841

    
842
        /**
843
         * Cambia el puntero del raton al cusor que se le introduce como par?metro.
844
         *
845
         * @param cursor cursor a mostrar en el puntero del rat?n.
846
         */
847
        public void setMapCursor(Cursor cursor) {
848
                setCursor(cursor);
849
        }
850

    
851
        /**
852
         * Pone como seleccionada a la herramienta a partir de su n?mero.
853
         *
854
         * @param i
855
         */
856
        public void setTool(int i) {
857
                tool = i;
858

    
859
                if (getTool() == PAN) {
860
                        setMapCursor(ipan);
861
                } else if (getTool() == ZOOM_MAS) {
862
                        setMapCursor(izoomin);
863
                } else if (getTool() == ZOOM_MENOS) {
864
                        setMapCursor(izoomout);
865
                } else if (getTool() == RECTANGLEVIEW) {
866
                        setMapCursor(icrux);
867
                } else if (getTool() == RECTANGLEPICTURE) {
868
                        setMapCursor(icrux);
869
                } else if (getTool() == RECTANGLESCALEBAR) {
870
                        setMapCursor(icrux);
871
                } else if (getTool() == RECTANGLELEGEND) {
872
                        setMapCursor(icrux);
873
                } else if (getTool() == RECTANGLETEXT) {
874
                        setMapCursor(icrux);
875
                } else if (getTool() == SELECT) {
876
                        setCursor(Cursor.getDefaultCursor());
877
                } else if (getTool() == SET_TAG) {
878
                        setMapCursor(itag);
879
                } else if (getTool() == POINT) {
880
                        setMapCursor(iPoint);
881
                } else if (getTool() == LINE) {
882
                        setMapCursor(iRect);
883
                } else if (getTool() == RECTANGLESIMPLE) {
884
                        setMapCursor(iRectangle);
885
                } else if (getTool() == POLYLINE) {
886
                        setMapCursor(iLine);
887
                } else if (getTool() == POLYGON) {
888
                        setMapCursor(iPoligon);
889
                } else if (getTool() == CIRCLE) {
890
                        setMapCursor(iCircle);
891
                }
892
        }
893

    
894
        /**
895
         * Se crean aqu? los dialogos cada vez que se necesitan.
896
         *
897
         * @param fframe Rectangle que representa el lugar que ocupara el elemento
898
         *                   a?adido.
899
         *
900
         * @return true si se debe de a?adir el fframe.
901
         */
902
        public boolean openFFrameDialog(IFFrame fframe) {
903
                switch (tool) {
904
                        case RECTANGLEVIEW:
905
                                fframedialog = new FFrameViewDialog(this, (FFrameView) fframe);
906

    
907
                                break;
908

    
909
                        case RECTANGLEPICTURE:
910
                                fframedialog = new FFramePictureDialog(this,
911
                                                (FFramePicture) fframe);
912

    
913
                                break;
914

    
915
                        case RECTANGLESCALEBAR:
916
                                fframedialog = new FFrameScaleBarDialog(this,
917
                                                (FFrameScaleBar) fframe);
918

    
919
                                break;
920

    
921
                        case RECTANGLELEGEND:
922
                                fframedialog = new FFrameLegendDialog(this,
923
                                                (FFrameLegend) fframe);
924

    
925
                                break;
926

    
927
                        case RECTANGLETEXT:
928
                                fframedialog = new FFrameTextDialog(this, (FFrameText) fframe);
929

    
930
                                break;
931

    
932
                        case GRAPHICS:
933
                                fframedialog = new FFrameGraphicsDialog(this,
934
                                                (FFrameGraphics) fframe);
935

    
936
                                break;
937

    
938
                        default:
939
                                return true;
940
                }
941

    
942
                if (fframedialog != null) {
943
                        fframedialog.setRectangle(fframe.getBoundingBox(m_MatrizTransf));
944
                        PluginServices.getMDIManager().addView(fframedialog);
945
                }
946

    
947
                return fframedialog.getIsAcepted();
948
        }
949

    
950
        /**
951
         * Devuelve el Rectangle2D que representa en pixels al folio.
952
         *
953
         * @return Rectangle2D.Double
954
         */
955
        public Rectangle2D.Double getRect() {
956
                return rect;
957
        }
958

    
959
        /**
960
         * Obtiene un entero que representa la herramienta seleccionada.
961
         *
962
         * @return entero.
963
         */
964
        public int getTool() {
965
                return tool;
966
        }
967

    
968
        /**
969
         * Devuelve la Matriz transformada que se esta plicando al Layout.
970
         *
971
         * @return AffineTransform
972
         */
973
        public AffineTransform getAT() {
974
                return m_MatrizTransf;
975
        }
976

    
977
        /**
978
         * Devuelve los atributos del Mapa.
979
         *
980
         * @return Attributes.
981
         */
982
        public Attributes getAtributes() {
983
                return m_attributes;
984
        }
985

    
986
        /**
987
         * Pone el estado que se quiera.
988
         *
989
         * @param s estado.
990
         */
991
        public void setStatus(int s) {
992
                status = s;
993
        }
994

    
995
        /**
996
         * Devuelve el punto que representa la esquina noroeste del mapa.
997
         *
998
         * @return un punto.
999
         */
1000
        public Point getRectOrigin() {
1001
                return rectOrigin;
1002
        }
1003

    
1004
        /**
1005
         * Devuelve el punto del primer click de rat?n.
1006
         *
1007
         * @return un punto.
1008
         */
1009
        public Point getFirstPoint() {
1010
                return m_FirstPoint;
1011
        }
1012

    
1013
        /**
1014
         * Devuelve el punto de click de rat?n anterior.
1015
         *
1016
         * @return un punto.
1017
         */
1018
        public Point getPointAnt() {
1019
                return m_PointAnt;
1020
        }
1021

    
1022
        /**
1023
         * Devuelve el punto donde se solt? el bot?n del rat?n.
1024
         *
1025
         * @return un punto.
1026
         */
1027
        public Point getLastPoint() {
1028
                return m_LastPoint;
1029
        }
1030

    
1031
        /**
1032
         * Rellena el primer punto de click de rat?n.
1033
         *
1034
         * @param p punto.
1035
         */
1036
        public void setFirstPoint(Point p) {
1037
                m_FirstPoint = p;
1038
        }
1039

    
1040
        /**
1041
         * Rellena el punto de click de rat?n anterior.
1042
         *
1043
         * @param p punto.
1044
         */
1045
        public void setPointAnt(Point p) {
1046
                m_PointAnt = p;
1047
        }
1048

    
1049
        /**
1050
         * Rellena el punto donde se solto el bot?n del rat?n.
1051
         *
1052
         * @param p punto.
1053
         */
1054
        public void setLastPoint(Point p) {
1055
                m_LastPoint = p;
1056
        }
1057

    
1058
        /**
1059
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
1060
         */
1061
        public ViewInfo getViewInfo() {
1062
                ViewInfo m_viewinfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1063
                                ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
1064

    
1065
                m_viewinfo.setWidth(500);
1066
                m_viewinfo.setHeight(400);
1067

    
1068
                m_viewinfo.setTitle(PluginServices.getText(this, "Mapa") + " : " +
1069
                        getName());
1070

    
1071
                return m_viewinfo;
1072
        }
1073

    
1074
        /**
1075
         * Inserta si se muestra o no la regla del Layout.
1076
         *
1077
         * @param b True si se muestra la regla.
1078
         */
1079
        public void setRuler(boolean b) {
1080
                m_showRuler = b;
1081
        }
1082

    
1083
        /**
1084
         * Devuelve si se muestra la regla del Layout.
1085
         *
1086
         * @return True si se muestra la regla.
1087
         */
1088
        public boolean getRuler() {
1089
                return m_showRuler;
1090
        }
1091

    
1092
        /**
1093
         * Devuelve si se esta aplicando en los fframes que se a?den al Layout la
1094
         * cuadr?cula, o no.
1095
         *
1096
         * @return true si se esta aplicando la cuadr?cula.
1097
         */
1098
        public boolean isCuadricula() {
1099
                return isCuadricula;
1100
        }
1101

    
1102
        /**
1103
         * Se actualiza el estado de la cuadr?cula, si se aplica o no.
1104
         *
1105
         * @param b true si se aplica la cuadr?cula.
1106
         */
1107
        public void setIsCuadricula(boolean b) {
1108
                isCuadricula = b;
1109
        }
1110

    
1111
        /**
1112
         * Realiza una exteni?n completa de las capas que contiene la vista del
1113
         * FFrameView.
1114
         *
1115
         * @throws DriverException
1116
         */
1117
        public void viewFull() throws DriverException {
1118
                for (int i = 0; i < getFFrames().size(); i++) {
1119
                        if (getFFrames().get(i) instanceof FFrameView) {
1120
                                FFrameView fframe = (FFrameView) getFFrames().get(i);
1121

    
1122
                                if (fframe.getSelected() != FFrame.NOSELECT) {
1123
                                        fframe.getFMap().getViewPort().setExtent(fframe.getFMap()
1124
                                                                                                                                   .getFullExtent());
1125
                                }
1126
                        }
1127
                }
1128

    
1129
                setStatus(DESACTUALIZADO);
1130
                repaint();
1131
        }
1132

    
1133
        /**
1134
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
1135
         * para poder despu?s volver a crear el objeto original.
1136
         *
1137
         * @return XMLEntity.
1138
         */
1139
        public XMLEntity getXMLEntity() {
1140
                XMLEntity xml = new XMLEntity();
1141
                xml.putProperty("className",this.getClass().getName());
1142
                xml.setName("layout");
1143
                xml.putProperty("isCuadricula", isCuadricula());
1144
                xml.putProperty("m_name", this.getName());
1145
                xml.addChild(getAtributes().getXMLEntity());
1146

    
1147
                for (int i = 0; i < getFFrames().size(); i++) {
1148
                        xml.addChild(((IFFrame) getFFrames().get(i)).getXMLEntity());
1149
                }
1150

    
1151
                return xml;
1152
        }
1153

    
1154
        /**
1155
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1156
         *
1157
         * @param xml XMLEntity
1158
         * @param p Proyecto.
1159
         *
1160
         * @return Objeto de esta clase.
1161
         */
1162
        public static Layout createLayout(XMLEntity xml, Project p) {
1163
                Layout layout = new Layout();
1164
                layout.isCuadricula = xml.getBooleanProperty("isCuadricula");
1165
                layout.setName(xml.getStringProperty("m_name"));
1166
                layout.m_attributes = Attributes.createAtributes(xml.getChild(0));
1167

    
1168
                for (int i = 1; i < xml.getNumChild(); i++) {
1169
                        if (xml.getChild(i).getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.fframe.FFrameView")) {
1170
                                layout.addFFrame(FFrame.createFFrame(xml.getChild(i), layout, p),
1171
                                        true);
1172
                        }
1173
                }
1174

    
1175
                for (int i = 1; i < xml.getNumChild(); i++) {
1176
                        if (!xml.getChild(i).getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.fframe.FFrameView")) {
1177
                                layout.addFFrame(FFrame.createFFrame(xml.getChild(i), layout, p),
1178
                                        true);
1179
                        }
1180
                }
1181

    
1182
                return layout;
1183
        }
1184

    
1185
        /**
1186
         * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
1187
         */
1188
        public void viewActivated() {
1189
                PluginServices.getMainFrame().getStatusBar().setMessage("1",
1190
                        getAtributes().getNameUnit());
1191

    
1192
                ///System.out.println("---------- 1 "+ getAtributes().getNameUnit());
1193
        }
1194

    
1195
        /**
1196
         * @see com.iver.cit.gvsig.fmap.IMapExtentListener#extentChanged(java.awt.geom.Rectangle2D)
1197
         */
1198
        public void extentChanged(Rectangle2D newExtent) {
1199
        }
1200

    
1201
        /**
1202
         * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
1203
         */
1204
        public Object getViewModel() {
1205
                return map;
1206
        }
1207

    
1208
        /**
1209
         * Devuelve si se dibuja el Grid en el Layout o no.
1210
         *
1211
         * @return True si se dibuja el Grid en el Layout.
1212
         */
1213
        public boolean getIsDrawGrid() {
1214
                return isDrawCuadricula;
1215
        }
1216

    
1217
        /**
1218
         * Inserta si se muestra el Grid del Layout.
1219
         *
1220
         * @param b True si se muestra el Grid del Layout.
1221
         */
1222
        public void setIsDrawGrid(boolean b) {
1223
                isDrawCuadricula = b;
1224
        }
1225

    
1226
        /**
1227
         * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
1228
         */
1229
        public void extentChanged(ExtentEvent e) {
1230
        }
1231

    
1232
        /**
1233
         * @see com.iver.andami.ui.mdiManager.ViewListener#viewClosed()
1234
         */
1235
        public void viewClosed() {
1236
                ///PluginServices.getMainFrame().getStatusBar().setMessage("1","");
1237
        }
1238

    
1239
        /**
1240
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
1241
         */
1242
        public void backColorChanged(ColorEvent e) {
1243
        }
1244
        public IFFrame[] getFFrameSelected(){
1245
                ArrayList selecList=new ArrayList();
1246
                for (int i = getFFrames().size() - 1; i >= 0; i--) {
1247
                        IFFrame fframe = (IFFrame) getFFrames().get(i);
1248

    
1249
                        if (fframe.getSelected() != FFrame.NOSELECT) {
1250
                                selecList.add(fframe);
1251
                        }
1252
                }
1253
                return (IFFrame[])selecList.toArray(new IFFrame[0]);
1254
        }
1255
}