Statistics
| Revision:

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

History | View | Annotate | Download (36.9 KB)

1
/*
2
 * Created on 20-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.gui.layout;
8

    
9
import java.awt.Color;
10
import java.awt.Cursor;
11
import java.awt.Graphics;
12
import java.awt.Graphics2D;
13
import java.awt.Image;
14
import java.awt.Point;
15
import java.awt.Rectangle;
16
import java.awt.Toolkit;
17
import java.awt.event.ActionEvent;
18
import java.awt.event.KeyEvent;
19
import java.awt.geom.AffineTransform;
20
import java.awt.geom.Rectangle2D;
21
import java.awt.image.BufferedImage;
22
import java.awt.print.PageFormat;
23
import java.awt.print.Printable;
24
import java.awt.print.PrinterException;
25
import java.awt.print.PrinterJob;
26
import java.util.ArrayList;
27
import java.util.Hashtable;
28

    
29
import javax.print.Doc;
30
import javax.print.DocFlavor;
31
import javax.print.DocPrintJob;
32
import javax.print.PrintException;
33
import javax.print.PrintService;
34
import javax.print.PrintServiceLookup;
35
import javax.print.ServiceUI;
36
import javax.print.SimpleDoc;
37
import javax.print.attribute.PrintRequestAttributeSet;
38
import javax.print.event.PrintJobAdapter;
39
import javax.print.event.PrintJobEvent;
40
import javax.print.event.PrintJobListener;
41
import javax.swing.AbstractAction;
42
import javax.swing.Action;
43
import javax.swing.ImageIcon;
44
import javax.swing.JPanel;
45
import javax.swing.KeyStroke;
46

    
47
import org.apache.log4j.Logger;
48

    
49
import com.iver.cit.gvsig.Abrir;
50
import com.iver.cit.gvsig.fmap.ExtentEvent;
51
import com.iver.cit.gvsig.fmap.ExtentListener;
52
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
53
import com.iver.cit.gvsig.gui.layout.dialogs.FConfigLayoutDialog;
54
import com.iver.cit.gvsig.gui.layout.fframes.FFrame;
55
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphics;
56
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
57
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
58
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
61
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
62
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameGraphicsDialog;
63
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameLegendDialog;
64
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFramePictureDialog;
65
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameScaleBarDialog;
66
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameTextDialog;
67
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameViewDialog;
68
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.IFFrameDialog;
69
import com.iver.cit.gvsig.project.MapProperties;
70
import com.iver.cit.gvsig.project.castor.Project;
71
import com.iver.cit.gvsig.project.castor.ProjectMap;
72
import com.iver.mdiApp.App;
73
import com.iver.mdiApp.Utilities;
74
import com.iver.mdiApp.ui.MDIManager.SingletonView;
75
import com.iver.mdiApp.ui.MDIManager.ViewInfo;
76
import com.iver.utiles.XMLEntity;
77

    
78

    
79
/**
80
 * Mapa.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class Layout extends JPanel implements SingletonView,ExtentListener {
85
    private static Logger logger = Logger.getLogger(Layout.class.getName());
86

    
87
    /** DOCUMENT ME! */
88
    public static final int ZOOM_MAS = 1;
89

    
90
    /** DOCUMENT ME! */
91
    public static final int ZOOM_MENOS = 2;
92

    
93
    /** DOCUMENT ME! */
94
    public static final int PAN = 3;
95

    
96
    /** DOCUMENT ME! */
97
    public static final int DESACTUALIZADO = 4;
98

    
99
    /** DOCUMENT ME! */
100
    public static final int RECTANGLEVIEW = 5;
101

    
102
    /** DOCUMENT ME! */
103
    public static final int RECTANGLEPICTURE = 6;
104

    
105
    /** DOCUMENT ME! */
106
    public static final int RECTANGLESCALEBAR = 7;
107

    
108
    /** DOCUMENT ME! */
109
    public static final int RECTANGLELEGEND = 8;
110

    
111
    /** DOCUMENT ME! */
112
    public static final int RECTANGLETEXT = 9;
113

    
114
    /** DOCUMENT ME! */
115
    public static final int RECTANGLE = 10;
116

    
117
    /** DOCUMENT ME! */
118
    public static final int ACTUALIZADO = 11;
119

    
120
    /** DOCUMENT ME! */
121
    public static final int SELECT = 12;
122

    
123
    /** DOCUMENT ME! */
124
    public static final int POINT = 13;
125

    
126
    /** DOCUMENT ME! */
127
    public static final int LINE = 14;
128

    
129
    /** DOCUMENT ME! */
130
    public static final int POLYLINE = 15;
131

    
132
    /** DOCUMENT ME! */
133
    public static final int RECTANGLESIMPLE = 16;
134

    
135
    /** DOCUMENT ME! */
136
    public static final int CIRCLE = 17;
137

    
138
    /** DOCUMENT ME! */
139
    public static final int POLYGON = 18;
140

    
141
    /** DOCUMENT ME! */
142
    public static final int GRAPHICS = 19;
143

    
144
    /** DOCUMENT ME! */
145
    public static final int RECTANGLESYMBOL = 20;
146

    
147
    /** DOCUMENT ME! */
148
    public static final int RECTANGLEGROUP = 21;
149

    
150
    /** DOCUMENT ME! */
151
    public static final int VIEW_ZOOMIN = 22;
152

    
153
    /** DOCUMENT ME! */
154
    public static final int VIEW_ZOOMOUT = 23;
155

    
156
    /** DOCUMENT ME! */
157
    public static final int VIEW_FULL = 24;
158

    
159
    /** DOCUMENT ME! */
160
    public static final int VIEW_PAN = 25;
161
    public static final int SET_TAG=26;
162
    /** DOCUMENT ME! */
163
    public static final Image iLayoutpan = new ImageIcon(Abrir.class.getClassLoader()
164
                                                                    .getResource("images/LayoutHand.gif")).getImage();
165

    
166
    /** DOCUMENT ME! */
167
    public static final Image ipan = new ImageIcon(Abrir.class.getClassLoader()
168
                                                              .getResource("images/Hand.gif")).getImage();
169

    
170
    /** DOCUMENT ME! */
171
    public static final Image iLayoutzoomin = new ImageIcon(Abrir.class.getClassLoader()
172
                                                                       .getResource("images/LayoutZoomInCursor.gif")).getImage();
173

    
174
    /** DOCUMENT ME! */
175
    public static final Image izoomin = new ImageIcon(Abrir.class.getClassLoader()
176
                                                                 .getResource("images/ZoomInCursor.gif")).getImage();
177

    
178
    /** DOCUMENT ME! */
179
    public static final Image iLayoutzoomout = new ImageIcon(Abrir.class.getClassLoader()
180
                                                                        .getResource("images/LayoutZoomOutCursor.gif")).getImage();
181

    
182
    /** DOCUMENT ME! */
183
    public static final Image izoomout = new ImageIcon(Abrir.class.getClassLoader()
184
                                                                  .getResource("images/ZoomOutCursor.gif")).getImage();
185

    
186
    /** DOCUMENT ME! */
187
    public static final Image iinfo = new ImageIcon(Abrir.class.getClassLoader()
188
                                                               .getResource("images/InfoCursor.gif")).getImage();
189

    
190
    /** DOCUMENT ME! */
191
    public static final Image icrux = new ImageIcon(Abrir.class.getClassLoader()
192
                                                               .getResource("images/CruxCursor.png")).getImage();
193
    
194
    public static final Image itag = new ImageIcon(Abrir.class.getClassLoader()
195
            .getResource("images/tagCursor.gif")).getImage();
196
    
197
    private Point origin = new Point(50, 50);
198
    private Point rectOrigin = new Point(origin);
199
    private Rectangle2D.Double rect = new Rectangle2D.Double(rectOrigin.x,
200
            rectOrigin.y, 400, 300);
201
    private Point m_FirstPoint = new Point(0, 0);
202
    private Point m_PointAnt = new Point(0, 0);
203
    private Point m_LastPoint = new Point(0, 0);
204
    private EventsHandler events;
205
    private int tool = ZOOM_MAS;
206
    private int status = DESACTUALIZADO;
207
    private BufferedImage img = null;
208
    private AffineTransform m_MatrizTransf;
209
    private Rectangle2D rectVisible;
210
    private IFFrameDialog fframedialog = null;
211
    private ArrayList m_fframes = new ArrayList();
212
    private FConfigLayoutDialog m_configLayout = null;
213
    private MapProperties m_propertiesLayout = null;
214
    private Attributes m_attributes = null;
215
    private PrintService[] m_cachePrintServices = null;
216
    private PrintRequestAttributeSet m_cachePrintRequestAtributeSet = null;
217
    private PrintService m_cachePrintService = null;
218
    private boolean m_bCancelDrawing = false;
219
    private boolean isCuadricula = false;
220
    private boolean initial = true;
221
    private ProjectMap map = null;
222
    private Rectangle reSel = null;
223
    private boolean isReSel = true;
224
   // private String m_name = null;
225
    private boolean m_showRuler = true;
226
    private FLayoutDraw layoutPaint = null;
227
    private ViewInfo m_viewinfo=null;
228
    private boolean isDrawCuadricula=true;
229
    public static Hashtable nums=new Hashtable();
230
    private Doc doc=null;
231
    private  PrintRequestAttributeSet att=null;
232
    /**
233
     * Creates a new Layout object.
234
     */
235
    public Layout() {
236
        this.setLayout(null);
237
        events = new EventsHandler(this);
238
        layoutPaint = new FLayoutDraw(this);
239
        addComponentListener(events);
240
        addMouseMotionListener(events);
241
        addMouseListener(events);
242
        m_MatrizTransf = new AffineTransform();
243
        m_MatrizTransf.setToIdentity();
244
        this.initComponents();
245
    }
246

    
247
    /**
248
     * Rellena el ProjectMap del Layout.
249
     *
250
     * @param m ProjectMap.
251
     */
252
    public void setProjectMap(ProjectMap m) {
253
        map = m;
254
    }
255

    
256
    /**
257
     * Devuelve el rect?ngulo de selecci?n por rect?ngulo.
258
     *
259
     * @return Rect?ngulo de selecci?n.
260
     */
261
    public Rectangle getReSel() {
262
        return reSel;
263
    }
264

    
265
    /**
266
     * Devuelve true si se debe dibujar el rect?ngulo de selecci?n y realizar
267
     * la sellecci?n.
268
     *
269
     * @return true si se realiza la selecci?n por rect?ngulo.
270
     */
271
    public boolean isReSel() {
272
        return isReSel;
273
    }
274

    
275
    /**
276
     * Rellena con true si se debe de dibujar y seleccionar por rect?ngulo de
277
     * selecci?n.
278
     *
279
     * @param b boolean.
280
     */
281
    public void setIsReSel(boolean b) {
282
        isReSel = b;
283
    }
284

    
285
    /**
286
     * Devuelve true si el dibujado ha sido cancelado.
287
     *
288
     * @return true si el dibujado ha sido cancelado.
289
     */
290
    public synchronized boolean isDrawingCancelled() {
291
        return m_bCancelDrawing;
292
    }
293

    
294
    /**
295
     * Pone el dibuja a cancelado o no seg?n se quiera.
296
     *
297
     * @param b true si se quiere cancelar el dibujado.
298
     */
299
    public synchronized void setCancelDrawing(boolean b) {
300
        m_bCancelDrawing = b;
301

    
302
        for (int i = 0; i < getFFrames().size(); i++) {
303
            IFFrame fframe = (IFFrame) getFFrames().get(i);
304

    
305
            if (fframe instanceof FFrameView &&
306
                    (((FFrameView) fframe).getFMap() != null)) {
307
                ////TODO((FFrameView) getFFrames().get(i)).getFMap().setCancelDrawing(b);
308
            }
309
        }
310
    }
311

    
312
    /**
313
     * Obtiene el ArrayList con todos los FFrames que se han a?adido al Layout.
314
     *
315
     * @return Array con todos los fframes que contiene el Layout.
316
     */
317
    public ArrayList getFFrames() {
318
        return m_fframes;
319
    }
320

    
321
    /**
322
     * DOCUMENT ME!
323
     *
324
     * @param frames DOCUMENT ME!
325
     */
326
    private void setFFrames(ArrayList frames) {
327
        m_fframes = frames;
328
    }
329

    
330
    /**
331
     * Obtiene el rect que se ajusta al tama?o de la ventana, para ver el folio
332
     * entero.
333
     */
334
    public void fullRect() {
335
        rect.setRect(origin.x, origin.y, getWidth() - (origin.x * 2),
336
            getHeight() - (origin.x * 2));
337

    
338
        if (m_attributes.isLandSpace()) {
339
            rect = m_attributes.getRectangleLandscape(rect, getWidth(),
340
                    getHeight());
341
        } else {
342
            rect = m_attributes.getRectanglePortrait(rect, getWidth(),
343
                    getHeight());
344
        }
345

    
346
        status = DESACTUALIZADO;
347
        repaint();
348
    }
349

    
350
    /**
351
     * M?todo para imprimir el Layout sin modificar la matriz de
352
     * transformaci?n.
353
     *
354
     * @param g2
355
     */
356
    public void drawLayoutPrint(Graphics2D g2) {
357
        setCancelDrawing(false);
358
        
359
        setCursor(Cursor.getDefaultCursor());
360

    
361
        double scale = 0;
362

    
363
        ///if (rect.width > rect.height) {
364
        ///    scale = rect.width / m_attributes.m_sizePaper.getAlto() * 1;
365
        ///} else {
366
        scale = rect.height / m_attributes.m_sizePaper.getAlto() * 1;
367

    
368
        ///}
369
        AffineTransform escalado = new AffineTransform();
370
        AffineTransform translacion = new AffineTransform();
371
        translacion.setToTranslation(rect.getMinX(), rect.getMinY());
372
        escalado.setToScale(scale, scale);
373
        m_MatrizTransf.setToIdentity();
374
        m_MatrizTransf.concatenate(translacion);
375
        m_MatrizTransf.concatenate(escalado);
376
        m_attributes.setDistanceUnitX(rect);
377
        m_attributes.setDistanceUnitY(rect);
378

    
379
        for (int i = 0; i < getFFrames().size(); i++) {
380
            try {
381
                                ((IFFrame) getFFrames().get(i)).draw(g2, getAT(), null, null);
382
                        } catch (DriverIOException e) {
383
                                e.printStackTrace();
384
                        }
385
        }
386
        
387
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
388
    }
389

    
390
    /**
391
     * Clip sobre el rect?ngulo visible.
392
     *
393
     * @param g2d Graphics sobre el que hacer el clip.
394
     */
395
    private void clipVisibleRect(Graphics2D g2d) {
396
        rectVisible = this.getVisibleRect();
397
        g2d.clipRect((int) rectVisible.getMinX(), (int) rectVisible.getMinY(),
398
            (int) rectVisible.getWidth(), (int) rectVisible.getHeight());
399
    }
400

    
401
    /**
402
     * Inicializa los componentes.
403
     */
404
    private void initComponents() {
405
        m_attributes = new Attributes();
406
        m_attributes.setDistanceUnitX(rect);
407
        m_attributes.setDistanceUnitY(rect);
408
        actionDelFFrame();
409
    }
410

    
411
    /**
412
     * Crea un ActionEvent para controlar las teclas que se pulsen cuando este
413
     * el Layout Focusable a true.
414
     */
415
    private void actionDelFFrame() {
416
        Action doNothing = new AbstractAction() {
417
                public void actionPerformed(ActionEvent e) {
418
                    for (int i = getFFrames().size() - 1; i >= 0; i--) {
419
                        IFFrame fframe = (IFFrame) getFFrames().get(i);
420

    
421
                        if (fframe.getSelected() != FFrame.NOSELECT) {
422
                            getFFrames().remove(i);
423
                        }
424
                    }
425

    
426
                    refresh();
427
                }
428
            };
429

    
430
        this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
431
            "doNothing");
432
        this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0),
433
            "doNothing");
434
        this.getActionMap().put("doNothing", doNothing);
435
    }
436

    
437
    /**
438
     * paintComponent del Layout.
439
     *
440
     * @param g Graphics del Layout.
441
     */
442
    protected void paintComponent(Graphics g) {
443
        super.paintComponent(g);
444

    
445
        switch (status) {
446
            case ZOOM_MAS:
447
                logger.debug("zoom mas");
448
                layoutPaint.drawRectangle((Graphics2D) g);
449

    
450
                g.drawImage(img, 0, 0, this);
451
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
452
                g.setColor(Color.black);
453
                g.setXORMode(Color.white);
454

    
455
                Rectangle r = new Rectangle();
456

    
457
                r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
458
                g.drawRect(r.x, r.y, r.width, r.height);
459

    
460
                break;
461

    
462
            case RECTANGLE:
463
                logger.debug("rectangle");
464
                layoutPaint.drawRectangle((Graphics2D) g);
465

    
466
                g.drawImage(img, 0, 0, this);
467
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
468
                g.setColor(Color.black);
469
                g.setXORMode(Color.white);
470

    
471
                Rectangle re = new Rectangle();
472
                re.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
473

    
474
                if (isCuadricula()) {
475
                    FLayoutUtilities.setRectGrid(re,
476
                        m_attributes.getUnitInPixelsX(),
477
                        m_attributes.getUnitInPixelsY(), m_MatrizTransf);
478
                }
479

    
480
                g.drawRect(re.x, re.y, re.width, re.height);
481

    
482
                break;
483

    
484
            case PAN:
485
                logger.debug("pan");
486

    
487
                //g.setXORMode(Color.black);
488
                rect.x = rectOrigin.x + (m_LastPoint.x - m_PointAnt.x);
489
                rect.y = rectOrigin.y + (m_LastPoint.y - m_PointAnt.y);
490

    
491
                //g.clearRect(0, 0, getWidth(), getHeight());
492
                layoutPaint.drawRectangle((Graphics2D) g);
493

    
494
                if (img != null) {
495
                    g.drawImage(img, (getLastPoint().x - getPointAnt().x),
496
                        (m_LastPoint.y - m_PointAnt.y), this);
497
                }
498

    
499
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
500

    
501
                break;
502

    
503
            case VIEW_PAN:
504

    
505
                // logger.debug("View pan");
506
                layoutPaint.drawRectangle((Graphics2D) g);
507
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
508

    
509
                for (int i = 0; i < getFFrames().size(); i++) {
510
                    if (getFFrames().get(i) instanceof FFrameView) {
511
                        FFrameView fframe = (FFrameView) getFFrames().get(i);
512

    
513
                        if (fframe.getSelected() != FFrame.NOSELECT) {
514
                            Rectangle2D.Double rec = fframe.getBoundingBox(getAT());
515

    
516
                            if (img != null) {
517
                                    
518
                                //g.drawImage(img, 0, 0, this);
519
                               
520
                                rec = (Rectangle2D.Double) rec.createIntersection(getVisibleRect());
521
                                //((Graphics2D)g).clearRect((int)rec.x,(int)rec.y,(int)rec.width,(int)rec.height);
522
                            }
523

    
524
                            if (fframe.getBufferedImage() != null) {
525
                                layoutPaint.drawHandlers((Graphics2D) g,
526
                                    Color.black);
527
                                g.clipRect((int) rec.x, (int) rec.y,
528
                                    (int) rec.width, (int) rec.height);
529

    
530
                                //g.setColor(Color.gray);
531
                                //g.fillRect(0,0,getWidth(),getHeight());
532
                                layoutPaint.drawRectangle((Graphics2D) g);
533
                                layoutPaint.drawRuler((Graphics2D) g,
534
                                    Color.black);
535

    
536
                                g.drawImage(fframe.getBufferedImage(),
537
                                    m_LastPoint.x - m_PointAnt.x,
538
                                    m_LastPoint.y - m_PointAnt.y, this);
539
                                System.out.println(
540
                                    "m_LastPoint.x - m_PointAnt.x" +
541
                                    (m_LastPoint.x - m_PointAnt.x));
542
                                System.out.println(
543
                                    "m_LastPoint.y - m_PointAnt.y" +
544
                                    (m_LastPoint.y - m_PointAnt.y));
545
                            }
546
                        }
547
                    }
548
                }
549

    
550
                //g.setXORMode(Color.black);
551
                break;
552

    
553
            case SELECT:
554
                logger.debug("select");
555
                layoutPaint.drawRectangle((Graphics2D) g);
556

    
557
                g.drawImage(img, 0, 0, this);
558

    
559
                if (isReSel) {
560
                    reSel = new Rectangle();
561
                    reSel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
562
                    g.drawRect(reSel.x, reSel.y, reSel.width, reSel.height);
563
                }
564

    
565
                for (int i = 0; i < getFFrames().size(); i++) {
566
                    IFFrame fframe = (IFFrame) getFFrames().get(i);
567

    
568
                    if (fframe.getSelected() != FFrame.NOSELECT) {
569
                        g.setColor(Color.black);
570
                        fframe.drawHandlers((Graphics2D) g);
571

    
572
                        int difx = (m_LastPoint.x - m_FirstPoint.x);
573
                        int dify = (m_LastPoint.y - m_FirstPoint.y);
574

    
575
                        if ((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) {
576
                            Rectangle2D rectangle = fframe.getMovieRect(difx,
577
                                    dify);
578

    
579
                            if (rectangle != null) {
580
                                if (isCuadricula) {
581
                                    FLayoutUtilities.setRectGrid(rectangle,
582
                                        m_attributes.getUnitInPixelsX(),
583
                                        m_attributes.getUnitInPixelsY(),
584
                                        m_MatrizTransf);
585
                                }
586

    
587
                                g.drawRect((int) rectangle.getMinX(),
588
                                    (int) rectangle.getMinY(),
589
                                    (int) rectangle.getWidth(),
590
                                    (int) rectangle.getHeight());
591
                            }
592
                        }
593
                    }
594
                }
595

    
596
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
597

    
598
                break;
599

    
600
            case GRAPHICS:
601
                layoutPaint.drawRectangle((Graphics2D) g);
602
                g.drawImage(img, 0, 0, this);
603
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
604

    
605
                break;
606

    
607
            case DESACTUALIZADO:
608
                logger.debug("desactualizado");
609
                img = new BufferedImage(getWidth(), getHeight(),
610
                        BufferedImage.TYPE_INT_ARGB);
611

    
612
                Graphics gimag = img.getGraphics();
613
                clipVisibleRect((Graphics2D) gimag);
614

    
615
                if (initial) {
616
                    fullRect();
617
                    initial = false;
618
                }
619

    
620
                try {
621
                                        layoutPaint.drawLayout((Graphics2D) gimag, img);
622
                                } catch (DriverIOException e) {
623
                                        e.printStackTrace();
624
                                }
625
                layoutPaint.drawRectangle((Graphics2D) g);
626

    
627
                g.drawImage(img, 0, 0, this);
628
                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
629
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
630

    
631
                break;
632

    
633
            case ACTUALIZADO:
634
                layoutPaint.drawRectangle((Graphics2D) g);
635

    
636
                g.drawImage(img, 0, 0, this);
637
                layoutPaint.drawHandlers((Graphics2D) g, Color.black);
638
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
639
        }
640
    }
641

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

    
658
        if (frame instanceof FFrameView &&
659
                (((FFrameView) frame).getFMap() != null)) {
660
            ((FFrameView) frame).getFMap().getViewPort().addExtentListener(this);
661
        }
662
        if (nums.containsKey(frame.getClass())){
663
                nums.put(frame.getClass(),new Integer(Integer.parseInt(nums.get(frame.getClass()).toString())+1));
664
        }else{
665
                nums.put(frame.getClass(),new Integer(0));
666
        }
667
        
668
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
669
        getFFrames().add(frame);
670
        frame.setSelected(true);
671
        
672
    }
673

    
674
    /**
675
     * Abre el di?logo de propiedades del folio, pasando como par?metro el
676
     * PrinterJob.
677
     *
678
     * @param job
679
     */
680
    public void showPagePropertiesWindow(PrinterJob job) {
681
        PageFormat pf1; //=new PageFormat();
682

    
683
        pf1 = m_attributes.getPageFormat();
684
        pf1 = job.pageDialog(pf1);
685
        m_attributes.setPageFormat(pf1);
686
        refresh();
687
    }
688

    
689
    /**
690
     * Refresca el Layout.
691
     */
692
    public void refresh() {
693
        rect = m_attributes.obtainRect(false, rect, getWidth(), getHeight());
694
        status = DESACTUALIZADO;
695
        repaint();
696
    }
697

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

    
709
    /**
710
     * Muestra el di?logo de configuraci?n del Layout.
711
     */
712
    public void showFConfig() {
713
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
714
        App.instance.getMDIContext().addView(m_configLayout);
715
    }
716

    
717
    /**
718
     * Mestra el di?logo de propiedades del Layout.
719
     */
720
    public void showFProperties() {
721
        if (map == null) {
722
            map = new ProjectMap();
723
            map.setModel(this);
724
            map.setName(getName());
725
        }
726

    
727
        m_propertiesLayout = new MapProperties(map);
728
        App.instance.getMDIContext().addView(m_propertiesLayout);
729
    }
730

    
731
    /**
732
     * Muestra el di?logo de imprimir pasandole el printerJob como par?metro.
733
     *
734
     * @param job
735
     */
736
    public void showPrintDialog(PrinterJob job) {
737
            if (job!=null){
738
                job.printDialog();
739
                        try {
740
                                job.setPrintable((Printable) App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
741
                                job.print();
742
                                } catch (PrinterException e) {
743
                                        e.printStackTrace();
744
                                }
745
           }else{
746
            boolean print = false;
747

    
748
        //Actualizar attributes
749
        
750
        att = m_attributes.toPrintAttributes();
751
        
752
        //------------------ The Printing things --------------------- //
753
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
754

    
755
        // returns the set of printers that support printing a specific document type (such as GIF)
756
        // with a specific set of attributes (such as two sided).
757
        // PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
758
        
759

    
760
        // interestingly, the printer dialog's default behavior has changed with the new API: by default the dialog is not shown.
761
        // So we must use the ServiceUI class to create a print dialog
762
        // returns the default print service.
763
        if (m_cachePrintServices == null)
764
                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor, null);
765
        
766
        PrintService defaultService = null;
767
        if (m_cachePrintService == null)
768
                defaultService = PrintServiceLookup.lookupDefaultPrintService();
769
                
770
        if (m_cachePrintService == null)
771
        {
772
                
773
                m_cachePrintService = ServiceUI.printDialog(null, 200, 200, 
774
                        m_cachePrintServices, defaultService, flavor, att);
775
                
776
                // m_cachePrintRequestAtributeSet = new HashPrintRequestAttributeSet();
777
                // m_cachePrintRequestAtributeSet.addAll(pras);
778
        }
779
        else
780
                m_cachePrintService = ServiceUI.printDialog(null, 200, 200, 
781
                            m_cachePrintServices, m_cachePrintService, flavor, att);
782
                
783
        if (m_cachePrintService != null)
784
        {
785
                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
786
                PrintJobListener pjlistener = new PrintJobAdapter() {
787
                        public void printDataTransferCompleted(PrintJobEvent e) {
788
                            System.out.println("Fin de impresi?n");
789
                        }
790
                    };
791
                jobNuevo.addPrintJobListener(pjlistener);
792
        
793
                // DocAttributeSet das = new HashDocAttributeSet();
794
                doc =new SimpleDoc((Printable) App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class), flavor, null);
795
                
796
                try {
797
                        
798
                        jobNuevo.print(doc, att);
799

    
800
                    // m_attributes.
801
                } catch (PrintException pe) {
802
                    pe.printStackTrace();
803
                }
804
                }
805
        }        
806

    
807
        /* try {
808
            print = job.printDialog(att);
809
        } catch (Exception e) {
810
            logger.error("Abriendo el Di?logo de imprimir"); //System.out.println("Excepci?n "+e);
811
        }
812

813
        if (print) {
814
            job.setPrintable((Printable) App.instance.getPc().getExtension(com.iver.cit.gvsig.Print.class));
815
            try {
816
                job.print(att);
817
            } catch (Exception ex) {
818
                ex.printStackTrace();
819
            }
820
        } */
821

    
822
        fullRect(); 
823
    }
824

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

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

    
846
    /**
847
     * Pone como seleccionada a la herramienta a partir de su n?mero.
848
     *
849
     * @param i
850
     */
851
    public void setTool(int i) {
852
        tool = i;
853

    
854
        if (getTool() == PAN) {
855
            setMapCursor(ipan);
856
        } else if (getTool() == ZOOM_MAS) {
857
            setMapCursor(izoomin);
858
        } else if (getTool() == ZOOM_MENOS) {
859
            setMapCursor(izoomout);
860
        } else if (getTool() == RECTANGLEVIEW) {
861
            setMapCursor(icrux);
862
        } else if (getTool() == RECTANGLEPICTURE) {
863
            setMapCursor(icrux);
864
        } else if (getTool() == RECTANGLESCALEBAR) {
865
            setMapCursor(icrux);
866
        } else if (getTool() == RECTANGLELEGEND) {
867
            setMapCursor(icrux);
868
        } else if (getTool() == RECTANGLETEXT) {
869
            setMapCursor(icrux);
870
        } else if (getTool() == SELECT) {
871
            setCursor(Cursor.getDefaultCursor());
872
        } else if (getTool() == SET_TAG){
873
                setMapCursor(itag);
874
        }
875
    }
876

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

    
890
                break;
891

    
892
            case RECTANGLEPICTURE:
893
                fframedialog = new FFramePictureDialog(this,
894
                        (FFramePicture) fframe);
895

    
896
                break;
897

    
898
            case RECTANGLESCALEBAR:
899
                fframedialog = new FFrameScaleBarDialog(this,
900
                        (FFrameScaleBar) fframe);
901

    
902
              //  fframedialog = new FFrameScaleBarDialog(this,
903
              //          (FFrameScaleBar) fframe);
904

    
905
                break;
906

    
907
            case RECTANGLELEGEND:
908
                fframedialog = new FFrameLegendDialog(this,
909
                        (FFrameLegend) fframe);
910

    
911
                break;
912

    
913
            case RECTANGLETEXT:
914
                fframedialog = new FFrameTextDialog(this, (FFrameText) fframe);
915

    
916
                break;
917

    
918
            case GRAPHICS:
919
                fframedialog = new FFrameGraphicsDialog(this,
920
                        (FFrameGraphics) fframe);
921

    
922
                break;
923

    
924
            default:
925
                return true;
926
        }
927

    
928
        if (fframedialog != null) {
929
            fframedialog.setRectangle(fframe.getBoundingBox(m_MatrizTransf));
930
            App.instance.getMDIContext().addView(fframedialog);
931
        }
932

    
933
        return fframedialog.getIsAcepted();
934
    }
935

    
936
    /**
937
     * Devuelve el Rectangle2D que representa en pixels al folio.
938
     *
939
     * @return Rectangle2D.Double
940
     */
941
    public Rectangle2D.Double getRect() {
942
        return rect;
943
    }
944

    
945
    /**
946
     * Obtiene un entero que representa la herramienta seleccionada.
947
     *
948
     * @return entero.
949
     */
950
    public int getTool() {
951
        return tool;
952
    }
953

    
954
    /**
955
     * Devuelve la Matriz transformada que se esta plicando al Layout.
956
     *
957
     * @return AffineTransform
958
     */
959
    public AffineTransform getAT() {
960
        return m_MatrizTransf;
961
    }
962

    
963
    /**
964
     * Devuelve los atributos del Mapa.
965
     *
966
     * @return Attributes.
967
     */
968
    public Attributes getAtributes() {
969
        return m_attributes;
970
    }
971

    
972
    /**
973
     * Pone el estado que se quiera.
974
     *
975
     * @param s estado.
976
     */
977
    public void setStatus(int s) {
978
        status = s;
979
    }
980

    
981
    /**
982
     * Devuelve el punto que representa la esquina noroeste del mapa.
983
     *
984
     * @return un punto.
985
     */
986
    public Point getRectOrigin() {
987
        return rectOrigin;
988
    }
989

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

    
999
    /**
1000
     * Devuelve el punto de click de rat?n anterior.
1001
     *
1002
     * @return un punto.
1003
     */
1004
    public Point getPointAnt() {
1005
        return m_PointAnt;
1006
    }
1007

    
1008
    /**
1009
     * Devuelve el punto donde se solt? el bot?n del rat?n.
1010
     *
1011
     * @return un punto.
1012
     */
1013
    public Point getLastPoint() {
1014
        return m_LastPoint;
1015
    }
1016

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

    
1026
    /**
1027
     * Rellena el punto de click de rat?n anterior.
1028
     *
1029
     * @param p punto.
1030
     */
1031
    public void setPointAnt(Point p) {
1032
        m_PointAnt = p;
1033
    }
1034

    
1035
    /**
1036
     * Rellena el punto donde se solto el bot?n del rat?n.
1037
     *
1038
     * @param p punto.
1039
     */
1040
    public void setLastPoint(Point p) {
1041
        m_LastPoint = p;
1042
    }
1043

    
1044
    /**
1045
     * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
1046
     */
1047
    public ViewInfo getViewInfo() {
1048
       if (m_viewinfo==null){
1049
               m_viewinfo=new ViewInfo();
1050
            m_viewinfo.setFeatures(ViewInfo.ICONIFIABLE | ViewInfo.MAXIMIZABLE |
1051
            ViewInfo.RESIZABLE);
1052
        
1053
        m_viewinfo.setWidth(500);
1054
        m_viewinfo.setHeight(400);
1055
       }
1056
       m_viewinfo.setTitle(Utilities.getMessage(this, "Mapa") + " : " + getName());
1057
        return m_viewinfo;
1058
    }
1059

    
1060
    /**
1061
     * DOCUMENT ME!
1062
     *
1063
     * @param b DOCUMENT ME!
1064
     */
1065
    public void setRuler(boolean b) {
1066
        m_showRuler = b;
1067
    }
1068

    
1069
    /**
1070
     * DOCUMENT ME!
1071
     *
1072
     * @return DOCUMENT ME!
1073
     */
1074
    public boolean getRuler() {
1075
        return m_showRuler;
1076
    }
1077

    
1078
    /**
1079
     * Devuelve si se esta aplicando en los fframes que se a?den al Layout la
1080
     * cuadr?cula, o no.
1081
     *
1082
     * @return true si se esta aplicando la cuadr?cula.
1083
     */
1084
    public boolean isCuadricula() {
1085
        return isCuadricula;
1086
    }
1087

    
1088
    /**
1089
     * Se actualiza el estado de la cuadr?cula, si se aplica o no.
1090
     *
1091
     * @param b true si se aplica la cuadr?cula.
1092
     */
1093
    public void setIsCuadricula(boolean b) {
1094
        isCuadricula = b;
1095
    }
1096

    
1097
    /**
1098
     * DOCUMENT ME!
1099
     * @throws DriverIOException
1100
     */
1101
    public void viewFull() throws DriverIOException {
1102
        for (int i = 0; i < getFFrames().size(); i++) {
1103
            if (getFFrames().get(i) instanceof FFrameView) {
1104
                FFrameView fframe = (FFrameView) getFFrames().get(i);
1105

    
1106
                if (fframe.getSelected() != FFrame.NOSELECT) {
1107
                    fframe.getFMap().getViewPort().setExtent(fframe.getFMap().getFullExtent());
1108
                }
1109
            }
1110
        }
1111

    
1112
        setStatus(DESACTUALIZADO);
1113
        repaint();
1114
    }
1115

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

    
1129
        for (int i = 0; i < getFFrames().size(); i++) {
1130
            xml.addChild(((IFFrame) getFFrames().get(i)).getXMLEntity());
1131
        }
1132

    
1133
        return xml;
1134
    }
1135

    
1136
    /**
1137
     * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1138
     *
1139
     * @param xml XMLEntity
1140
     * @param p DOCUMENT ME!
1141
     *
1142
     * @return Objeto de esta clase.
1143
     */
1144
    public static Layout createLayout(XMLEntity xml, Project p) {
1145
        Layout layout = new Layout();
1146
        layout.isCuadricula = xml.getBooleanProperty("isCuadricula");
1147
        layout.setName(xml.getStringProperty("m_name"));
1148
        layout.m_attributes = Attributes.createAtributes(xml.getChild(0));
1149

    
1150
        for (int i = 1; i < xml.getNumChild(); i++) {
1151
            layout.addFFrame(FFrame.createFFrame(xml.getChild(i), p), true);
1152
        }
1153

    
1154
        return layout;
1155
    }
1156

    
1157
    /**
1158
     * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
1159
     */
1160
    public void viewActivated() {
1161
        App.instance.getMDIContext().setMessage("5", "");
1162
        status = DESACTUALIZADO;
1163
        repaint();
1164

    
1165
        //refresh();
1166
    }
1167

    
1168
    /**
1169
     * @see com.iver.cit.gvsig.fmap.IMapExtentListener#extentChanged(java.awt.geom.Rectangle2D)
1170
     */
1171
    public void extentChanged(Rectangle2D newExtent) {
1172
        //refresh();
1173
    }
1174

    
1175
    /**
1176
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
1177
     */
1178
    public Object getViewModel() {
1179
        return this;
1180
    }
1181
    public boolean getIsDrawGrid(){
1182
            return isDrawCuadricula;
1183
    }
1184
    public void setIsDrawGrid(boolean b){
1185
            isDrawCuadricula=b;
1186
    }
1187

    
1188
        /**
1189
         * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
1190
         */
1191
        public void extentChanged(ExtentEvent e) {
1192
        }
1193
   
1194
}