Statistics
| Revision:

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

History | View | Annotate | Download (33.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.project.MapProperties;
74
import com.iver.cit.gvsig.project.castor.Project;
75
import com.iver.cit.gvsig.project.castor.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
                switch (status) {
427
                        case ZOOM_MAS:
428
                                logger.debug("zoom mas");
429
                                layoutPaint.drawRectangle((Graphics2D) g);
430

    
431
                                g.drawImage(img, 0, 0, this);
432
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
433
                                g.setColor(Color.black);
434
                                g.setXORMode(Color.white);
435

    
436
                                Rectangle r = new Rectangle();
437

    
438
                                r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
439
                                g.drawRect(r.x, r.y, r.width, r.height);
440

    
441
                                break;
442

    
443
                        case RECTANGLE:
444
                                logger.debug("rectangle");
445
                                layoutPaint.drawRectangle((Graphics2D) g);
446

    
447
                                g.drawImage(img, 0, 0, this);
448
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
449
                                g.setColor(Color.black);
450
                                g.setXORMode(Color.white);
451

    
452
                                Rectangle re = new Rectangle();
453
                                re.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
454

    
455
                                if (isCuadricula()) {
456
                                        FLayoutUtilities.setRectGrid(re,
457
                                                m_attributes.getUnitInPixelsX(),
458
                                                m_attributes.getUnitInPixelsY(), m_MatrizTransf);
459
                                }
460

    
461
                                g.drawRect(re.x, re.y, re.width, re.height);
462

    
463
                                break;
464

    
465
                        case PAN:
466
                                logger.debug("pan");
467

    
468
                                //g.setXORMode(Color.black);
469
                                rect.x = rectOrigin.x + (m_LastPoint.x - m_PointAnt.x);
470
                                rect.y = rectOrigin.y + (m_LastPoint.y - m_PointAnt.y);
471

    
472
                                //g.clearRect(0, 0, getWidth(), getHeight());
473
                                layoutPaint.drawRectangle((Graphics2D) g);
474

    
475
                                if (img != null) {
476
                                        g.drawImage(img, (getLastPoint().x - getPointAnt().x),
477
                                                (m_LastPoint.y - m_PointAnt.y), this);
478
                                }
479

    
480
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
481

    
482
                                break;
483

    
484
                        case VIEW_PAN:
485

    
486
                                // logger.debug("View pan");
487
                                layoutPaint.drawRectangle((Graphics2D) g);
488
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
489

    
490
                                for (int i = 0; i < getFFrames().size(); i++) {
491
                                        if (getFFrames().get(i) instanceof FFrameView) {
492
                                                FFrameView fframe = (FFrameView) getFFrames().get(i);
493

    
494
                                                if (fframe.getSelected() != FFrame.NOSELECT) {
495
                                                        Rectangle2D.Double rec = fframe.getBoundingBox(getAT());
496

    
497
                                                        if (img != null) {
498
                                                                //g.drawImage(img, 0, 0, this);
499
                                                                rec = (Rectangle2D.Double) rec.createIntersection(getVisibleRect());
500

    
501
                                                                //((Graphics2D)g).clearRect((int)rec.x,(int)rec.y,(int)rec.width,(int)rec.height);
502
                                                        }
503

    
504
                                                        if (fframe.getBufferedImage() != null) {
505
                                                                layoutPaint.drawHandlers((Graphics2D) g,
506
                                                                        Color.black);
507
                                                                g.clipRect((int) rec.x, (int) rec.y,
508
                                                                        (int) rec.width, (int) rec.height);
509

    
510
                                                                //g.setColor(Color.gray);
511
                                                                //g.fillRect(0,0,getWidth(),getHeight());
512
                                                                layoutPaint.drawRectangle((Graphics2D) g);
513
                                                                layoutPaint.drawRuler((Graphics2D) g,
514
                                                                        Color.black);
515

    
516
                                                                g.drawImage(fframe.getBufferedImage(),
517
                                                                        m_LastPoint.x - m_PointAnt.x,
518
                                                                        m_LastPoint.y - m_PointAnt.y, this);
519
                                                                System.out.println(
520
                                                                        "m_LastPoint.x - m_PointAnt.x" +
521
                                                                        (m_LastPoint.x - m_PointAnt.x));
522
                                                                System.out.println(
523
                                                                        "m_LastPoint.y - m_PointAnt.y" +
524
                                                                        (m_LastPoint.y - m_PointAnt.y));
525
                                                        }
526
                                                }
527
                                        }
528
                                }
529

    
530
                                //g.setXORMode(Color.black);
531
                                break;
532

    
533
                        case SELECT:
534
                                logger.debug("select");
535
                                layoutPaint.drawRectangle((Graphics2D) g);
536

    
537
                                g.drawImage(img, 0, 0, this);
538

    
539
                                if (isReSel) {
540
                                        reSel = new Rectangle();
541
                                        reSel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
542
                                        g.drawRect(reSel.x, reSel.y, reSel.width, reSel.height);
543
                                }
544

    
545
                                for (int i = 0; i < getFFrames().size(); i++) {
546
                                        IFFrame fframe = (IFFrame) getFFrames().get(i);
547

    
548
                                        if (fframe.getSelected() != FFrame.NOSELECT) {
549
                                                g.setColor(Color.black);
550
                                                fframe.drawHandlers((Graphics2D) g);
551

    
552
                                                int difx = (m_LastPoint.x - m_FirstPoint.x);
553
                                                int dify = (m_LastPoint.y - m_FirstPoint.y);
554

    
555
                                                if ((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) {
556
                                                        Rectangle2D rectangle = fframe.getMovieRect(difx,
557
                                                                        dify);
558

    
559
                                                        if (rectangle != null) {
560
                                                                if (isCuadricula) {
561
                                                                        FLayoutUtilities.setRectGrid(rectangle,
562
                                                                                m_attributes.getUnitInPixelsX(),
563
                                                                                m_attributes.getUnitInPixelsY(),
564
                                                                                m_MatrizTransf);
565
                                                                }
566

    
567
                                                                g.drawRect((int) rectangle.getMinX(),
568
                                                                        (int) rectangle.getMinY(),
569
                                                                        (int) rectangle.getWidth(),
570
                                                                        (int) rectangle.getHeight());
571
                                                        }
572
                                                }
573
                                        }
574
                                }
575

    
576
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
577

    
578
                                break;
579

    
580
                        case GRAPHICS:
581
                                layoutPaint.drawRectangle((Graphics2D) g);
582
                                g.drawImage(img, 0, 0, this);
583
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
584

    
585
                                break;
586

    
587
                        case DESACTUALIZADO:
588
                                logger.debug("desactualizado");
589
                                img = new BufferedImage(getWidth(), getHeight(),
590
                                                BufferedImage.TYPE_INT_ARGB);
591

    
592
                                Graphics gimag = img.getGraphics();
593
                                clipVisibleRect((Graphics2D) gimag);
594

    
595
                                if (initial) {
596
                                        fullRect();
597
                                        initial = false;
598
                                }
599

    
600
                                try {
601
                                        layoutPaint.drawLayout((Graphics2D) gimag, img);
602
                                } catch (DriverException e) {
603
                                        e.printStackTrace();
604
                                }
605

    
606
                                layoutPaint.drawRectangle((Graphics2D) g);
607

    
608
                                g.drawImage(img, 0, 0, this);
609
                                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
610
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
611

    
612
                                break;
613

    
614
                        case ACTUALIZADO:
615
                                layoutPaint.drawRectangle((Graphics2D) g);
616

    
617
                                g.drawImage(img, 0, 0, this);
618
                                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
619
                                layoutPaint.drawRuler((Graphics2D) g, Color.black);
620
                }
621
        }
622

    
623
        /**
624
         * A?ade un fframe al Arraylist m_fframes.
625
         *
626
         * @param frame fframe a a?adir.
627
         * @param clearSelection para que se quede seleccionado ?nicamente  el que
628
         *                   a?adimos y false si lo que se pretende es que no se
629
         *                   deseleccionen lo que ya est?n seleccionados.
630
         */
631
        public void addFFrame(IFFrame frame, boolean clearSelection) {
632
                if (clearSelection) {
633
                        for (int i = getFFrames().size() - 1; i >= 0; i--) {
634
                                IFFrame fframe1 = (IFFrame) getFFrames().get(i);
635
                                fframe1.setSelected(false);
636
                        }
637
                }
638

    
639
                if (frame instanceof FFrameView &&
640
                                (((FFrameView) frame).getFMap() != null)) {
641
                        ((FFrameView) frame).getFMap().getViewPort().addViewPortListener(this);
642
                }
643

    
644
                if (nums.containsKey(frame.getClass())) {
645
                        nums.put(frame.getClass(),
646
                                new Integer(Integer.parseInt(nums.get(frame.getClass())
647
                                                                                                 .toString()) + 1));
648
                } else {
649
                        nums.put(frame.getClass(), new Integer(0));
650
                }
651

    
652
                frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
653
                getFFrames().add(frame);
654
                frame.setSelected(true);
655
        }
656

    
657
        /**
658
         * Abre el di?logo de propiedades del folio, pasando como par?metro el
659
         * PrinterJob.
660
         *
661
         * @param job
662
         */
663
        public void showPagePropertiesWindow(PrinterJob job) {
664
                PageFormat pf1; //=new PageFormat();
665

    
666
                pf1 = m_attributes.getPageFormat();
667
                pf1 = job.pageDialog(pf1);
668
                m_attributes.setPageFormat(pf1);
669
                refresh();
670
        }
671

    
672
        /**
673
         * Refresca el Layout.
674
         */
675
        public void refresh() {
676
                rect = m_attributes.obtainRect(false, rect, getWidth(), getHeight());
677
                status = DESACTUALIZADO;
678
                repaint();
679
        }
680

    
681
        /**
682
         * Obtiene el rect?ngulo que representa el folio con las caracter?sticas
683
         * que contiene attributes y diferenciando si es para visualizar en
684
         * pantalla o para imprimir.
685
         *
686
         * @param isPrint si es para imprimir.
687
         */
688
        public void obtainRect(boolean isPrint) {
689
                m_attributes.obtainRect(isPrint, rect, getWidth(), getHeight());
690
        }
691

    
692
        /**
693
         * Muestra el di?logo de configuraci?n del Layout.
694
         */
695
        public void showFConfig() {
696
                FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
697
                PluginServices.getMDIManager().addView(m_configLayout);
698
        }
699

    
700
        /**
701
         * Mestra el di?logo de propiedades del Layout.
702
         */
703
        public void showFProperties() {
704
                if (map == null) {
705
                        map = new ProjectMap();
706
                        map.setModel(this);
707
                        map.setName(getName());
708
                }
709

    
710
                m_propertiesLayout = new MapProperties(map);
711
                PluginServices.getMDIManager().addView(m_propertiesLayout);
712
        }
713

    
714
        /**
715
         * Muestra el di?logo de imprimir pasandole el printerJob como par?metro.
716
         *
717
         * @param job
718
         */
719
        public void showPrintDialog(PrinterJob job) {
720
                if (job != null) {
721
                        job.printDialog();
722

    
723
                        try {
724
                                job.setPrintable((Printable) PluginServices.getExtension(
725
                                                com.iver.cit.gvsig.Print.class));
726
                                job.print();
727
                        } catch (PrinterException e) {
728
                                e.printStackTrace();
729
                        }
730
                } else {
731
                        boolean print = false;
732

    
733
                        //Actualizar attributes
734
                        att = m_attributes.toPrintAttributes();
735

    
736
                        //------------------ The Printing things --------------------- //
737
                        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
738

    
739
                        // returns the set of printers that support printing a specific document type (such as GIF)
740
                        // with a specific set of attributes (such as two sided).
741
                        // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
742
                        // interestingly, the printer dialog's default behavior has changed with the new API: by default the dialog is not shown.
743
                        // So we must use the ServiceUI class to create a print dialog
744
                        // returns the default print service.
745
                        if (m_cachePrintServices == null) {
746
                                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
747
                                                null);
748
                        }
749

    
750
                        PrintService defaultService = null;
751

    
752
                        if (m_cachePrintService == null) {
753
                                defaultService = PrintServiceLookup.lookupDefaultPrintService();
754
                        }
755

    
756
                        if (m_cachePrintService == null) {
757
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
758
                                                m_cachePrintServices, defaultService, flavor, att);
759

    
760
                                // m_cachePrintRequestAtributeSet = new HashPrintRequestAttributeSet();
761
                                // m_cachePrintRequestAtributeSet.addAll(pras);
762
                        } else {
763
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
764
                                                m_cachePrintServices, m_cachePrintService, flavor, att);
765
                        }
766

    
767
                        if (m_cachePrintService != null) {
768
                                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
769
                                PrintJobListener pjlistener = new PrintJobAdapter() {
770
                                                public void printDataTransferCompleted(PrintJobEvent e) {
771
                                                        System.out.println("Fin de impresi?n");
772
                                                }
773
                                        };
774

    
775
                                jobNuevo.addPrintJobListener(pjlistener);
776

    
777
                                // DocAttributeSet das = new HashDocAttributeSet();
778
                                doc = new SimpleDoc((Printable) PluginServices.getExtension(
779
                                                        com.iver.cit.gvsig.Print.class), flavor, null);
780

    
781
                                try {
782
                                        jobNuevo.print(doc, att);
783

    
784
                                        // m_attributes.
785
                                } catch (PrintException pe) {
786
                                        pe.printStackTrace();
787
                                }
788
                        }
789
                }
790

    
791
                /* try {
792
                   print = job.printDialog(att);
793
                   } catch (Exception e) {
794
                       logger.error("Abriendo el Di?logo de imprimir"); //System.out.println("Excepci?n "+e);
795
                   }
796
                   if (print) {
797
                       job.setPrintable((Printable) App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
798
                       try {
799
                           job.print(att);
800
                       } catch (Exception ex) {
801
                           ex.printStackTrace();
802
                       }
803
                   } */
804
                fullRect();
805
        }
806

    
807
        /**
808
         * Sustituye el puntero del rat?n por la imagen que se le pasa como
809
         * par?metro.
810
         *
811
         * @param image
812
         */
813
        public void setMapCursor(Image image) {
814
                Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(image,
815
                                new Point(16, 16), "");
816
                setCursor(cur);
817
        }
818

    
819
        /**
820
         * Cambia el puntero del raton al cusor que se le introduce como par?metro.
821
         *
822
         * @param cursor cursor a mostrar en el puntero del rat?n.
823
         */
824
        public void setMapCursor(Cursor cursor) {
825
                setCursor(cursor);
826
        }
827

    
828
        /**
829
         * Pone como seleccionada a la herramienta a partir de su n?mero.
830
         *
831
         * @param i
832
         */
833
        public void setTool(int i) {
834
                tool = i;
835

    
836
                if (getTool() == PAN) {
837
                        setMapCursor(ipan);
838
                } else if (getTool() == ZOOM_MAS) {
839
                        setMapCursor(izoomin);
840
                } else if (getTool() == ZOOM_MENOS) {
841
                        setMapCursor(izoomout);
842
                } else if (getTool() == RECTANGLEVIEW) {
843
                        setMapCursor(icrux);
844
                } else if (getTool() == RECTANGLEPICTURE) {
845
                        setMapCursor(icrux);
846
                } else if (getTool() == RECTANGLESCALEBAR) {
847
                        setMapCursor(icrux);
848
                } else if (getTool() == RECTANGLELEGEND) {
849
                        setMapCursor(icrux);
850
                } else if (getTool() == RECTANGLETEXT) {
851
                        setMapCursor(icrux);
852
                } else if (getTool() == SELECT) {
853
                        setCursor(Cursor.getDefaultCursor());
854
                } else if (getTool() == SET_TAG) {
855
                        setMapCursor(itag);
856
                } else if (getTool() == POINT) {
857
                        setMapCursor(iPoint);
858
                } else if (getTool() == LINE) {
859
                        setMapCursor(iRect);
860
                } else if (getTool() == RECTANGLESIMPLE) {
861
                        setMapCursor(iRectangle);
862
                } else if (getTool() == POLYLINE) {
863
                        setMapCursor(iLine);
864
                } else if (getTool() == POLYGON) {
865
                        setMapCursor(iPoligon);
866
                } else if (getTool() == CIRCLE) {
867
                        setMapCursor(iCircle);
868
                }
869
        }
870

    
871
        /**
872
         * Se crean aqu? los dialogos cada vez que se necesitan.
873
         *
874
         * @param fframe Rectangle que representa el lugar que ocupara el elemento
875
         *                   a?adido.
876
         *
877
         * @return true si se debe de a?adir el fframe.
878
         */
879
        public boolean openFFrameDialog(IFFrame fframe) {
880
                switch (tool) {
881
                        case RECTANGLEVIEW:
882
                                fframedialog = new FFrameViewDialog(this, (FFrameView) fframe);
883

    
884
                                break;
885

    
886
                        case RECTANGLEPICTURE:
887
                                fframedialog = new FFramePictureDialog(this,
888
                                                (FFramePicture) fframe);
889

    
890
                                break;
891

    
892
                        case RECTANGLESCALEBAR:
893
                                fframedialog = new FFrameScaleBarDialog(this,
894
                                                (FFrameScaleBar) fframe);
895

    
896
                                break;
897

    
898
                        case RECTANGLELEGEND:
899
                                fframedialog = new FFrameLegendDialog(this,
900
                                                (FFrameLegend) fframe);
901

    
902
                                break;
903

    
904
                        case RECTANGLETEXT:
905
                                fframedialog = new FFrameTextDialog(this, (FFrameText) fframe);
906

    
907
                                break;
908

    
909
                        case GRAPHICS:
910
                                fframedialog = new FFrameGraphicsDialog(this,
911
                                                (FFrameGraphics) fframe);
912

    
913
                                break;
914

    
915
                        default:
916
                                return true;
917
                }
918

    
919
                if (fframedialog != null) {
920
                        fframedialog.setRectangle(fframe.getBoundingBox(m_MatrizTransf));
921
                        PluginServices.getMDIManager().addView(fframedialog);
922
                }
923

    
924
                return fframedialog.getIsAcepted();
925
        }
926

    
927
        /**
928
         * Devuelve el Rectangle2D que representa en pixels al folio.
929
         *
930
         * @return Rectangle2D.Double
931
         */
932
        public Rectangle2D.Double getRect() {
933
                return rect;
934
        }
935

    
936
        /**
937
         * Obtiene un entero que representa la herramienta seleccionada.
938
         *
939
         * @return entero.
940
         */
941
        public int getTool() {
942
                return tool;
943
        }
944

    
945
        /**
946
         * Devuelve la Matriz transformada que se esta plicando al Layout.
947
         *
948
         * @return AffineTransform
949
         */
950
        public AffineTransform getAT() {
951
                return m_MatrizTransf;
952
        }
953

    
954
        /**
955
         * Devuelve los atributos del Mapa.
956
         *
957
         * @return Attributes.
958
         */
959
        public Attributes getAtributes() {
960
                return m_attributes;
961
        }
962

    
963
        /**
964
         * Pone el estado que se quiera.
965
         *
966
         * @param s estado.
967
         */
968
        public void setStatus(int s) {
969
                status = s;
970
        }
971

    
972
        /**
973
         * Devuelve el punto que representa la esquina noroeste del mapa.
974
         *
975
         * @return un punto.
976
         */
977
        public Point getRectOrigin() {
978
                return rectOrigin;
979
        }
980

    
981
        /**
982
         * Devuelve el punto del primer click de rat?n.
983
         *
984
         * @return un punto.
985
         */
986
        public Point getFirstPoint() {
987
                return m_FirstPoint;
988
        }
989

    
990
        /**
991
         * Devuelve el punto de click de rat?n anterior.
992
         *
993
         * @return un punto.
994
         */
995
        public Point getPointAnt() {
996
                return m_PointAnt;
997
        }
998

    
999
        /**
1000
         * Devuelve el punto donde se solt? el bot?n del rat?n.
1001
         *
1002
         * @return un punto.
1003
         */
1004
        public Point getLastPoint() {
1005
                return m_LastPoint;
1006
        }
1007

    
1008
        /**
1009
         * Rellena el primer punto de click de rat?n.
1010
         *
1011
         * @param p punto.
1012
         */
1013
        public void setFirstPoint(Point p) {
1014
                m_FirstPoint = p;
1015
        }
1016

    
1017
        /**
1018
         * Rellena el punto de click de rat?n anterior.
1019
         *
1020
         * @param p punto.
1021
         */
1022
        public void setPointAnt(Point p) {
1023
                m_PointAnt = p;
1024
        }
1025

    
1026
        /**
1027
         * Rellena el punto donde se solto el bot?n del rat?n.
1028
         *
1029
         * @param p punto.
1030
         */
1031
        public void setLastPoint(Point p) {
1032
                m_LastPoint = p;
1033
        }
1034

    
1035
        /**
1036
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
1037
         */
1038
        public ViewInfo getViewInfo() {
1039
                ViewInfo m_viewinfo = new ViewInfo(ViewInfo.ICONIFIABLE |
1040
                                ViewInfo.MAXIMIZABLE | ViewInfo.RESIZABLE);
1041

    
1042
                m_viewinfo.setWidth(500);
1043
                m_viewinfo.setHeight(400);
1044

    
1045
                m_viewinfo.setTitle(PluginServices.getText(this, "Mapa") + " : " +
1046
                        getName());
1047

    
1048
                return m_viewinfo;
1049
        }
1050

    
1051
        /**
1052
         * Inserta si se muestra o no la regla del Layout.
1053
         *
1054
         * @param b True si se muestra la regla.
1055
         */
1056
        public void setRuler(boolean b) {
1057
                m_showRuler = b;
1058
        }
1059

    
1060
        /**
1061
         * Devuelve si se muestra la regla del Layout.
1062
         *
1063
         * @return True si se muestra la regla.
1064
         */
1065
        public boolean getRuler() {
1066
                return m_showRuler;
1067
        }
1068

    
1069
        /**
1070
         * Devuelve si se esta aplicando en los fframes que se a?den al Layout la
1071
         * cuadr?cula, o no.
1072
         *
1073
         * @return true si se esta aplicando la cuadr?cula.
1074
         */
1075
        public boolean isCuadricula() {
1076
                return isCuadricula;
1077
        }
1078

    
1079
        /**
1080
         * Se actualiza el estado de la cuadr?cula, si se aplica o no.
1081
         *
1082
         * @param b true si se aplica la cuadr?cula.
1083
         */
1084
        public void setIsCuadricula(boolean b) {
1085
                isCuadricula = b;
1086
        }
1087

    
1088
        /**
1089
         * Realiza una exteni?n completa de las capas que contiene la vista del
1090
         * FFrameView.
1091
         *
1092
         * @throws DriverException
1093
         */
1094
        public void viewFull() throws DriverException {
1095
                for (int i = 0; i < getFFrames().size(); i++) {
1096
                        if (getFFrames().get(i) instanceof FFrameView) {
1097
                                FFrameView fframe = (FFrameView) getFFrames().get(i);
1098

    
1099
                                if (fframe.getSelected() != FFrame.NOSELECT) {
1100
                                        fframe.getFMap().getViewPort().setExtent(fframe.getFMap()
1101
                                                                                                                                   .getFullExtent());
1102
                                }
1103
                        }
1104
                }
1105

    
1106
                setStatus(DESACTUALIZADO);
1107
                repaint();
1108
        }
1109

    
1110
        /**
1111
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
1112
         * para poder despu?s volver a crear el objeto original.
1113
         *
1114
         * @return XMLEntity.
1115
         */
1116
        public XMLEntity getXMLEntity() {
1117
                XMLEntity xml = new XMLEntity();
1118
                xml.putProperty("className",this.getClass().getName());
1119
                xml.setName("layout");
1120
                xml.putProperty("isCuadricula", isCuadricula());
1121
                xml.putProperty("m_name", this.getName());
1122
                xml.addChild(getAtributes().getXMLEntity());
1123

    
1124
                for (int i = 0; i < getFFrames().size(); i++) {
1125
                        xml.addChild(((IFFrame) getFFrames().get(i)).getXMLEntity());
1126
                }
1127

    
1128
                return xml;
1129
        }
1130

    
1131
        /**
1132
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1133
         *
1134
         * @param xml XMLEntity
1135
         * @param p Proyecto.
1136
         *
1137
         * @return Objeto de esta clase.
1138
         */
1139
        public static Layout createLayout(XMLEntity xml, Project p) {
1140
                Layout layout = new Layout();
1141
                layout.isCuadricula = xml.getBooleanProperty("isCuadricula");
1142
                layout.setName(xml.getStringProperty("m_name"));
1143
                layout.m_attributes = Attributes.createAtributes(xml.getChild(0));
1144

    
1145
                for (int i = 1; i < xml.getNumChild(); i++) {
1146
                        if (xml.getChild(i).getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.fframe.FFrameView")) {
1147
                                layout.addFFrame(FFrame.createFFrame(xml.getChild(i), layout, p),
1148
                                        true);
1149
                        }
1150
                }
1151

    
1152
                for (int i = 1; i < xml.getNumChild(); i++) {
1153
                        if (!xml.getChild(i).getStringProperty("className").equals("com.iver.cit.gvsig.gui.layout.fframe.FFrameView")) {
1154
                                layout.addFFrame(FFrame.createFFrame(xml.getChild(i), layout, p),
1155
                                        true);
1156
                        }
1157
                }
1158

    
1159
                return layout;
1160
        }
1161

    
1162
        /**
1163
         * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
1164
         */
1165
        public void viewActivated() {
1166
                PluginServices.getMainFrame().getStatusBar().setMessage("1",
1167
                        getAtributes().getNameUnit());
1168

    
1169
                ///System.out.println("---------- 1 "+ getAtributes().getNameUnit());
1170
        }
1171

    
1172
        /**
1173
         * @see com.iver.cit.gvsig.fmap.IMapExtentListener#extentChanged(java.awt.geom.Rectangle2D)
1174
         */
1175
        public void extentChanged(Rectangle2D newExtent) {
1176
        }
1177

    
1178
        /**
1179
         * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
1180
         */
1181
        public Object getViewModel() {
1182
                return this;
1183
        }
1184

    
1185
        /**
1186
         * Devuelve si se dibuja el Grid en el Layout o no.
1187
         *
1188
         * @return True si se dibuja el Grid en el Layout.
1189
         */
1190
        public boolean getIsDrawGrid() {
1191
                return isDrawCuadricula;
1192
        }
1193

    
1194
        /**
1195
         * Inserta si se muestra el Grid del Layout.
1196
         *
1197
         * @param b True si se muestra el Grid del Layout.
1198
         */
1199
        public void setIsDrawGrid(boolean b) {
1200
                isDrawCuadricula = b;
1201
        }
1202

    
1203
        /**
1204
         * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
1205
         */
1206
        public void extentChanged(ExtentEvent e) {
1207
        }
1208

    
1209
        /**
1210
         * @see com.iver.andami.ui.mdiManager.ViewListener#viewClosed()
1211
         */
1212
        public void viewClosed() {
1213
                ///PluginServices.getMainFrame().getStatusBar().setMessage("1","");
1214
        }
1215

    
1216
        /**
1217
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
1218
         */
1219
        public void backColorChanged(ColorEvent e) {
1220
        }
1221
}