Statistics
| Revision:

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

History | View | Annotate | Download (36.8 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.andami.PluginServices;
50
import com.iver.andami.ui.mdiManager.SingletonView;
51
import com.iver.andami.ui.mdiManager.ViewInfo;
52
import com.iver.cit.gvsig.Abrir;
53
import com.iver.cit.gvsig.fmap.ExtentEvent;
54
import com.iver.cit.gvsig.fmap.ExtentListener;
55
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
56
import com.iver.cit.gvsig.gui.layout.dialogs.FConfigLayoutDialog;
57
import com.iver.cit.gvsig.gui.layout.fframes.FFrame;
58
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphics;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
61
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
62
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
63
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
64
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
65
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameGraphicsDialog;
66
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameLegendDialog;
67
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFramePictureDialog;
68
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameScaleBarDialog;
69
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameTextDialog;
70
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.FFrameViewDialog;
71
import com.iver.cit.gvsig.gui.layout.fframes.dialogs.IFFrameDialog;
72
import com.iver.cit.gvsig.project.MapProperties;
73
import com.iver.cit.gvsig.project.castor.Project;
74
import com.iver.cit.gvsig.project.castor.ProjectMap;
75
import com.iver.utiles.XMLEntity;
76

    
77

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
344
        status = DESACTUALIZADO;
345
        repaint();
346
    }
347

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

    
359
        double scale = 0;
360

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

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

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

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

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

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

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

    
424
                    refresh();
425
                }
426
            };
427

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

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

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

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

    
453
                Rectangle r = new Rectangle();
454

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

    
458
                break;
459

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

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

    
469
                Rectangle re = new Rectangle();
470
                re.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
471

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

    
478
                g.drawRect(re.x, re.y, re.width, re.height);
479

    
480
                break;
481

    
482
            case PAN:
483
                logger.debug("pan");
484

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

    
489
                //g.clearRect(0, 0, getWidth(), getHeight());
490
                layoutPaint.drawRectangle((Graphics2D) g);
491

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

    
497
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
498

    
499
                break;
500

    
501
            case VIEW_PAN:
502

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

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

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

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

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

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

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

    
548
                //g.setXORMode(Color.black);
549
                break;
550

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

    
555
                g.drawImage(img, 0, 0, this);
556

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

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

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

    
570
                        int difx = (m_LastPoint.x - m_FirstPoint.x);
571
                        int dify = (m_LastPoint.y - m_FirstPoint.y);
572

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

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

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

    
594
                layoutPaint.drawRuler((Graphics2D) g, Color.black);
595

    
596
                break;
597

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

    
603
                break;
604

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

    
610
                Graphics gimag = img.getGraphics();
611
                clipVisibleRect((Graphics2D) gimag);
612

    
613
                if (initial) {
614
                    fullRect();
615
                    initial = false;
616
                }
617

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

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

    
629
                break;
630

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

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

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

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

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

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

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

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

    
707
    /**
708
     * Muestra el di?logo de configuraci?n del Layout.
709
     */
710
    public void showFConfig() {
711
        FConfigLayoutDialog m_configLayout = new FConfigLayoutDialog(this);
712
        PluginServices.getMDIManager().addView(m_configLayout);
713
    }
714

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

    
725
        m_propertiesLayout = new MapProperties(map);
726
        PluginServices.getMDIManager().addView(m_propertiesLayout);
727
    }
728

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

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

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

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

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

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

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

    
820
        fullRect(); 
821
    }
822

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

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

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

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

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

    
888
                break;
889

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

    
894
                break;
895

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

    
900
              //  fframedialog = new FFrameScaleBarDialog(this,
901
              //          (FFrameScaleBar) fframe);
902

    
903
                break;
904

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

    
909
                break;
910

    
911
            case RECTANGLETEXT:
912
                fframedialog = new FFrameTextDialog(this, (FFrameText) fframe);
913

    
914
                break;
915

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

    
920
                break;
921

    
922
            default:
923
                return true;
924
        }
925

    
926
        if (fframedialog != null) {
927
            fframedialog.setRectangle(fframe.getBoundingBox(m_MatrizTransf));
928
            PluginServices.getMDIManager().addView(fframedialog);
929
        }
930

    
931
        return fframedialog.getIsAcepted();
932
    }
933

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1056
    /**
1057
     * DOCUMENT ME!
1058
     *
1059
     * @param b DOCUMENT ME!
1060
     */
1061
    public void setRuler(boolean b) {
1062
        m_showRuler = b;
1063
    }
1064

    
1065
    /**
1066
     * DOCUMENT ME!
1067
     *
1068
     * @return DOCUMENT ME!
1069
     */
1070
    public boolean getRuler() {
1071
        return m_showRuler;
1072
    }
1073

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

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

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

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

    
1108
        setStatus(DESACTUALIZADO);
1109
        repaint();
1110
    }
1111

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

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

    
1129
        return xml;
1130
    }
1131

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

    
1146
        for (int i = 1; i < xml.getNumChild(); i++) {
1147
            layout.addFFrame(FFrame.createFFrame(xml.getChild(i), p), true);
1148
        }
1149

    
1150
        return layout;
1151
    }
1152

    
1153
    /**
1154
     * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
1155
     */
1156
    public void viewActivated() {
1157
            PluginServices.getMainFrame().getStatusBar().setMessage("5", "");
1158
        status = DESACTUALIZADO;
1159
        repaint();
1160

    
1161
        //refresh();
1162
    }
1163

    
1164
    /**
1165
     * @see com.iver.cit.gvsig.fmap.IMapExtentListener#extentChanged(java.awt.geom.Rectangle2D)
1166
     */
1167
    public void extentChanged(Rectangle2D newExtent) {
1168
        //refresh();
1169
    }
1170

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

    
1184
        /**
1185
         * @see com.iver.cit.gvsig.fmap.ExtentListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
1186
         */
1187
        public void extentChanged(ExtentEvent e) {
1188
        }
1189
   
1190
}