Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutDraw.java @ 244

History | View | Annotate | Download (25.2 KB)

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

    
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Cursor;
27
import java.awt.Graphics;
28
import java.awt.Graphics2D;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Point2D;
31
import java.awt.geom.Rectangle2D;
32
import java.awt.image.BufferedImage;
33
import java.awt.print.PageFormat;
34
import java.awt.print.Printable;
35
import java.awt.print.PrinterException;
36
import java.io.BufferedOutputStream;
37
import java.io.File;
38
import java.io.FileOutputStream;
39
import java.io.IOException;
40
import java.io.OutputStream;
41

    
42
import javax.print.Doc;
43
import javax.print.DocFlavor;
44
import javax.print.DocPrintJob;
45
import javax.print.PrintException;
46
import javax.print.SimpleDoc;
47
import javax.print.StreamPrintService;
48
import javax.print.StreamPrintServiceFactory;
49
import javax.print.attribute.PrintRequestAttributeSet;
50
import javax.swing.JOptionPane;
51

    
52
import com.lowagie.text.Document;
53
import com.lowagie.text.DocumentException;
54
import com.lowagie.text.PageSize;
55
import com.lowagie.text.Rectangle;
56
import com.lowagie.text.pdf.PdfContentByte;
57
import com.lowagie.text.pdf.PdfWriter;
58

    
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61
import org.gvsig.andami.PluginServices;
62
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
63
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
64
import org.gvsig.fmap.dal.exception.ReadException;
65
import org.gvsig.tools.observer.Observable;
66
import org.gvsig.tools.observer.ObservableHelper;
67
import org.gvsig.tools.observer.Observer;
68

    
69
/**
70
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
71
 * 
72
 * @author Vicente Caballero Navarro
73
 */
74
public class FLayoutDraw implements Observable, Printable{
75
    private static final Logger LOG = 
76
        LoggerFactory.getLogger(FLayoutDraw.class);
77
    
78
    private LayoutPanel layoutPanel;
79
    private LayoutControl layoutControl;
80
    private LayoutContext layoutContext;
81
    private ObservableHelper observers;
82

    
83
    /**
84
     * Crea un nuevo FLayoutDraw.
85
     * 
86
     * @param l
87
     *            Referencia al Layout.
88
     */
89
    public FLayoutDraw(LayoutPanel layoutPanel) {
90
        this.layoutPanel = layoutPanel;
91
        observers = new ObservableHelper();
92
        observers.addObserver(layoutPanel.getLayoutControl());
93
    }
94

    
95
    public void initialize(){
96
        if ((layoutContext == null) || (layoutControl == null)){
97
            this.layoutControl = layoutPanel.getLayoutControl();
98
            this.layoutContext = layoutPanel.getLayoutContext();
99
        }
100
    }
101

    
102
    /**
103
     * M?todo para dibujar el Layout y modificar la matriz de transformaci?n a
104
     * partir del tama?o en pixels que tenga rect y el formato de papel
105
     * seleccionado.
106
     * 
107
     * @param g2
108
     * @param imgBase
109
     *            Si es null, est?s imprimiendo. Si no, la usas para el
110
     *            c?digo de optimizaci?n.
111
     * 
112
     * @throws ReadDriverException
113
     */
114
    public void drawLayout(Graphics2D g2, BufferedImage imgBase)
115
    throws ReadException {     
116
        if (!initializeAffineTransform()) {
117
                return;
118
        }
119

    
120
        IFFrame[] fframes = layoutContext.getFFrames();
121
        for (int i = 0; i < fframes.length; i++) {
122
            IFFrame f = fframes[i];
123
            f.draw(g2, layoutContext.getAT(), layoutControl.getComponent().getVisibleRect(),
124
                imgBase);          
125

    
126
            // Dibuja el s?mbolo de que contiene un tag.
127
            if ((f.getTag() != null) && (layoutPanel.isShowIconTag())) {
128
                f.drawSymbolTag(g2);
129
            }
130
        }
131

    
132
        if (!(fframes.length == 0)) {
133
            observers.notifyObservers(this, 
134
                new DefaultLayoutNotification(LayoutNotification.LAYOUT_VALIDATED));           
135
        } else {
136
            observers.notifyObservers(this, 
137
                new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));           
138
        }  
139
    }
140

    
141
    private boolean initializeAffineTransform(){
142
        initialize();
143
        AffineTransform at = layoutContext.getAT();
144
        Rectangle2D rLayout = layoutControl.getRect();
145
        if (rLayout.getHeight()<0) {
146
                // still not ready to be painted
147
                return false;
148
        }
149
        Attributes attributes = layoutContext.getAttributes();
150

    
151
        layoutControl.setCancelDrawing(false);
152

    
153
        double scale = 0;
154
        scale = rLayout.getHeight() / attributes.m_sizePaper.getHeight() * 1; // paper (paper units) to screen (pixels) scale
155
        AffineTransform escalado = new AffineTransform();
156
        AffineTransform translacion = new AffineTransform();
157
        translacion.setToTranslation(rLayout.getMinX(), rLayout.getMinY());
158
        escalado.setToScale(scale, scale);
159
        at.setToIdentity();
160
        at.concatenate(translacion);
161
        at.concatenate(escalado);
162
        attributes.calculateGridGapX(rLayout);
163
        attributes.calculateGridGapY(rLayout);
164
        return true;
165
    }
166

    
167
    /**
168
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
169
     * 
170
     * @param g
171
     *            Graphics2D
172
     */
173
    public void drawRectangle(Graphics2D g) {
174
        initialize();
175
        Attributes attributes = layoutContext.getAttributes();
176
        Rectangle2D rLayout = layoutControl.getRect();
177
        AffineTransform at = layoutContext.getAT();
178

    
179
        double unidadesX = attributes.getHGridGapCm();
180
        double unidadesY = attributes.getVGridGapCm();
181

    
182
        if ((unidadesX == 0) && (unidadesY == 0)) {
183
            return;
184
        }
185

    
186
        g.setColor(Color.darkGray);
187

    
188
        Rectangle2D.Double rectBig =
189
            new Rectangle2D.Double(rLayout.getX(), rLayout.getY(),
190
                rLayout.getWidth(), rLayout.getHeight());
191

    
192
        g.fillRect((int) rectBig.x + 7, (int) rectBig.y + 7,
193
            (int) rectBig.width, (int) rectBig.height);
194

    
195
        Rectangle2D.Double r = new Rectangle2D.Double();
196

    
197
        if (attributes.isMargin()) {
198

    
199
            r =
200
                new Rectangle2D.Double(
201
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
202
                        attributes.getAreaInsets()[2], at)),
203
                        (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
204
                            attributes.getAreaInsets()[0], at)),
205
                            (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
206
                                attributes.getAreaInsets()[2] + attributes.getAreaInsets()[3], at)),
207
                                (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
208
                                    attributes.getAreaInsets()[0] + attributes.getAreaInsets()[1], at)));
209
        } else {
210
            r.setRect(rLayout);
211
        }
212

    
213
        g.setColor(Color.white);
214
        g.fill(rLayout);
215

    
216
        g.setColor(Color.darkGray);
217
        g.drawRect((int) rLayout.getMinX(), (int) rLayout.getMinY(),
218
            (int) rLayout.getWidth(), (int) rLayout.getHeight());
219

    
220
        if (attributes.isMargin()) {
221
            g.setColor(Color.black);
222

    
223
            g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
224
        }
225
    }
226

    
227
    /**
228
     * DOCUMENT ME!
229
     * 
230
     * @param g
231
     *            DOCUMENT ME!
232
     */
233
    public void drawGrid(Graphics2D g) {
234
        int unidadesMin = 6;       
235
        Attributes attributes = layoutContext.getAttributes();
236
        Rectangle2D rLayout = layoutControl.getRect();
237
        AffineTransform at = layoutContext.getAT();
238

    
239
        double unidadesX = attributes.getHGridGapPx();
240
        double unidadesY = attributes.getVGridGapPx();
241

    
242
        Rectangle2D.Double r = new Rectangle2D.Double();
243

    
244
        if (attributes.isMargin()) {
245
            r =
246
                new Rectangle2D.Double(
247
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
248
                        attributes.getAreaInsets()[2], at)),
249
                        (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
250
                            attributes.getAreaInsets()[0], at)),
251
                            (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
252
                                attributes.getAreaInsets()[2] + attributes.getAreaInsets()[3], at)),
253
                                (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
254
                                    attributes.getAreaInsets()[0] + attributes.getAreaInsets()[1], at)));
255
        } else {
256
            r.setRect(rLayout);
257
        }
258
        if ((unidadesX == 0) && (unidadesY == 0)) {
259
            return;
260
        }
261
        g.setColor(Color.darkGray);
262

    
263
        if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin))
264
            && layoutContext.isGridVisible()) {
265
            double ax = r.getMinX();
266
            double ay;
267

    
268
            while (ax < (r.getMaxX())) {
269
                ay = (r.getMinY());
270

    
271
                while (ay < (r.getMaxY())) {
272
                    g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
273
                    ay = ay + unidadesY;
274
                }
275

    
276
                ax = ax + unidadesX;
277
            }
278
        }
279
    }
280

    
281
    /**
282
     * Dibuja sobre el graphics2d las reglas.
283
     * 
284
     * @param g
285
     *            graphics2d sobre el que se dibuja.
286
     * @param color
287
     *            Color de la regla.
288
     */
289
    public void drawRuler(Graphics2D g, Color color) {     
290
        Attributes attributes = layoutContext.getAttributes();
291
        Rectangle2D rLayout = layoutControl.getRect();
292
        AffineTransform at = layoutContext.getAT();
293

    
294
        if (layoutContext.getRuler()) {
295
            int ini = 10;
296
            int w = 30;
297
            int wi = 16;
298

    
299
            g.setColor(new Color(250, 255, 250, 180));
300
            g.fillRect(ini, w, wi, layoutControl.getComponent().getHeight() - w);
301
            g.fillRect(w, ini, layoutControl.getWidth() - w, wi);
302

    
303
            g.setColor(new Color(100, 155, 150, 180));
304
            g.fillRect(w, ini, (int) rLayout.getX() - w, wi);
305
            g.fillRect((int) rLayout.getMaxX(), ini, layoutControl.getWidth()
306
                - (int) rLayout.getMaxX(), wi);
307

    
308
            g.fillRect(ini, w, wi, (int) rLayout.getY() - w);
309
            g.fillRect(ini, (int) rLayout.getMaxY(), wi,
310
                layoutControl.getHeight() - (int) rLayout.getMaxY());
311

    
312
            if (attributes.isMargin()) {
313
                g.setColor(new Color(50, 55, 50, 180));
314
                g.fillRect((int) rLayout.getX(), ini, (int) FLayoutUtilities
315
                    .fromSheetDistance(attributes.getAreaInsets()[2], at), wi);
316
                g.fillRect(
317
                    (int) rLayout.getMaxX()
318
                    - (int) FLayoutUtilities.fromSheetDistance(
319
                        attributes.getAreaInsets()[3], at), ini,
320
                        (int) FLayoutUtilities.fromSheetDistance(
321
                            attributes.getAreaInsets()[3], at), wi);
322

    
323
                g.fillRect(ini, (int) rLayout.getY(), wi,
324
                    (int) FLayoutUtilities.fromSheetDistance(
325
                        attributes.getAreaInsets()[0], at));
326
                g.fillRect(
327
                    ini,
328
                    (int) rLayout.getMaxY()
329
                    - (int) FLayoutUtilities.fromSheetDistance(
330
                        attributes.getAreaInsets()[1], at), wi,
331
                        (int) FLayoutUtilities.fromSheetDistance(
332
                            attributes.getAreaInsets()[1], at));
333
            }
334

    
335
            g.setColor(color);
336
            g.drawLine(w, wi + ini, layoutControl.getWidth(), wi + ini);
337
            g.drawLine(w, ini, layoutControl.getWidth(), ini);
338
            g.drawLine(ini, w, ini, layoutControl.getHeight());
339
            g.drawLine(wi + ini, w, wi + ini, layoutControl.getHeight());
340

    
341
            drawLineY(g, 5, 12, 22, rLayout.getY(), 0);
342
            drawLineX(g, 5, 12, 22, rLayout.getX(), 0);
343

    
344
            if (FLayoutUtilities.fromSheetDistance(1, at) > 15) {
345
                drawLineY(g, 1, 12, 22, rLayout.getY(), 0);
346
                drawLineX(g, 1, 12, 22, rLayout.getX(), 0);
347

    
348
                if (FLayoutUtilities.fromSheetDistance(1, at) > 25) {
349
                    drawLineY(g, 1, 18, 22, rLayout.getY(), 0.5);
350
                    drawLineY(g, 0.1, 21, 22, rLayout.getY(), 0);
351
                    drawLineX(g, 1, 18, 22, rLayout.getX(), 0.5);
352
                    drawLineX(g, 0.1, 21, 22, rLayout.getX(), 0);
353
                }
354
            }
355
        }
356
    }
357

    
358
    /**
359
     * Dibuja sobre el graphics2d las l?neas verticales que representan a las
360
     * unidades de medida.
361
     * 
362
     * @param g
363
     *            Graphics2d sobre el que se dibuja.
364
     * @param dist
365
     *            distancia en cent?metros de una l?nea a otra.
366
     * @param init
367
     *            inicio de la l?nea.
368
     * @param end
369
     *            fin de la l?nea.
370
     * @param y
371
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
372
     * @param desp
373
     *            Desplazamiento.
374
     */
375
    private void drawLineY(Graphics2D g, double dist, int init, int end,
376
        double y, double desp) {
377
        initialize();
378
        AffineTransform at = layoutContext.getAT();
379

    
380
        double distY = FLayoutUtilities.fromSheetDistance(dist, at);
381
        double rota = Math.toRadians(90);
382

    
383
        if (distY > 4) {
384
            double despY = FLayoutUtilities.fromSheetDistance(desp, at);
385

    
386
            double posUnitY = y + despY;
387
            double posUnitYNeg = posUnitY;
388
            int num = 0;
389
            double iniY = 40;
390
            Point2D.Double pfin =
391
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
392
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
393

    
394
            while (posUnitY < (pfin.y - 5)) {
395
                posUnitYNeg -= distY;
396

    
397
                if (distY > 16) {
398
                    if (init == 12) {
399
                        if (posUnitY > iniY) {
400
                            g.rotate(-rota, 20, posUnitY - 12);
401
                            g.drawString(String.valueOf(num), 10,
402
                                (int) posUnitY - 12);
403
                            g.rotate(rota, 20, posUnitY - 12);
404
                        }
405

    
406
                        if (dist == 5) {
407
                            num = num + 5;
408
                        } else {
409
                            num++;
410
                        }
411

    
412
                        if (posUnitYNeg > iniY) {
413
                            g.rotate(-rota, 20, posUnitYNeg - 12);
414
                            g.drawString(String.valueOf(-num), 10,
415
                                (int) posUnitYNeg - 12);
416
                            g.rotate(rota, 20, posUnitYNeg - 12);
417
                        }
418
                    }
419
                }
420

    
421
                if (posUnitY > iniY) {
422
                    g.drawLine(2 + init, (int) posUnitY, 2 + end,
423
                        (int) posUnitY);
424
                }
425

    
426
                if (posUnitYNeg > iniY) {
427
                    g.drawLine(2 + init, (int) posUnitYNeg, 2 + end,
428
                        (int) posUnitYNeg);
429
                }
430

    
431
                posUnitY += distY;
432
            }
433
        }
434
    }
435

    
436
    /**
437
     * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
438
     * unidades de medida.
439
     * 
440
     * @param g
441
     *            Graphics2d sobre el que se dibuja.
442
     * @param dist
443
     *            distancia en cent?metros de una l?nea a otra.
444
     * @param init
445
     *            inicio de la l?nea.
446
     * @param end
447
     *            fin de la l?nea.
448
     * @param x
449
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
450
     * @param desp
451
     *            Desplazamiento.
452
     */
453
    private void drawLineX(Graphics2D g, double dist, int init, int end,
454
        double x, double desp) {
455
        initialize();
456
        AffineTransform at = layoutContext.getAT();
457

    
458
        double distX = FLayoutUtilities.fromSheetDistance(dist, at);
459

    
460
        if (distX > 4) {
461
            double despX = FLayoutUtilities.fromSheetDistance(desp, at);
462
            double posUnitX = x + despX;
463
            double posUnitXNeg = posUnitX;
464
            int num = 0;
465
            double iniX = 40;
466
            Point2D.Double pfin =
467
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
468
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
469

    
470
            while (posUnitX < (pfin.x - 5)) {
471
                posUnitXNeg -= distX;
472

    
473
                if (init == 12) {
474
                    if (distX > 16) {
475
                        if (posUnitX > iniX) {
476
                            g.drawString(String.valueOf(num),
477
                                (int) posUnitX + 3, 20);
478
                        }
479

    
480
                        if (dist == 5) {
481
                            num = num + 5;
482
                        } else {
483
                            num++;
484
                        }
485

    
486
                        if (posUnitXNeg > iniX) {
487
                            g.drawString(String.valueOf(-num),
488
                                (int) posUnitXNeg + 3, 20);
489
                        }
490
                    }
491
                }
492

    
493
                if (posUnitX > iniX) {
494
                    g.drawLine((int) posUnitX, 2 + init, (int) posUnitX,
495
                        2 + end);
496
                }
497

    
498
                if (posUnitXNeg > iniX) {
499
                    g.drawLine((int) posUnitXNeg, 2 + init, (int) posUnitXNeg,
500
                        2 + end);
501
                }
502

    
503
                posUnitX += distX;
504
            }
505
        }
506
    }
507

    
508
    /**
509
     * Dibuja los handlers sobre los fframes que esten seleccionados.
510
     * 
511
     * @param g
512
     *            Graphics sobre el que se dibuja.
513
     * @param color
514
     *            Color de los Handlers.
515
     */
516
    public void drawHandlers(Graphics2D g, Color color) {
517
        initialize();
518

    
519
        g.setColor(color);
520
        IFFrame[] fframes = layoutContext.getFFrames();
521
        for (int i = 0; i < fframes.length; i++) {
522
            IFFrame fframe = fframes[i];
523

    
524
            if (fframe.getSelected() != IFFrame.NOSELECT) {
525
                fframe.drawHandlers(g);
526
            }
527
        }
528
    }
529

    
530
    /**
531
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
532
     * contenido del Layout.
533
     * 
534
     * @param ps
535
     */
536
    public void toPS(File ps) {
537
        initialize();
538
        Attributes attributes = layoutContext.getAttributes();
539

    
540
        try {
541
            // Prepare the output file to receive the postscript
542
            OutputStream fos =
543
                new BufferedOutputStream(new FileOutputStream(ps));
544

    
545
            // Find a factory that can do the conversion
546
            // DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
547
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
548
            StreamPrintServiceFactory[] factories =
549
                StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
550
                    flavor, "application/postscript");
551

    
552
            if (factories.length > 0) {
553
                StreamPrintService service = factories[0].getPrintService(fos);
554

    
555
                // Create the print job
556
                DocPrintJob job = service.createPrintJob();
557

    
558
                Doc doc = new SimpleDoc(this, flavor, null);
559
                // Doc doc = new SimpleDoc(is, flavor, null);
560
                // Monitor print job events; for the implementation of
561
                // PrintJobWatcher,
562
                // see e702 Determining When a Print Job Has Finished
563
                // PrintJobWatcher pjDone = new PrintJobWatcher(job);
564
                // Actualizar attributes
565
                PrintRequestAttributeSet att =
566
                    attributes.toPrintRequestAttributeSet();
567
                // Print it
568
                job.print(doc, att);
569

    
570
                // Wait for the print job to be done
571
                // pjDone.waitForDone();
572
                // It is now safe to close the streams
573
            }
574

    
575
            // is.close();
576
            fos.close();
577
        } catch (PrintException e) {
578
            LOG.error("Error printing the map", e);
579
        } catch (IOException e) {
580
            LOG.error("Error printing the map", e);
581
        }
582
        /*
583
         * PrintService defaultPrintService =
584
         * PrintServiceLookup.lookupDefaultPrintService();
585
         * DocPrintJob printerJob = defaultPrintService.createPrintJob();
586
         * 
587
         * try {
588
         * DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
589
         * if (!defaultPrintService.isDocFlavorSupported(flavor)) {
590
         * System.err.println(
591
         * "The printer does not support the appropriate DocFlavor");
592
         * }else {
593
         * 
594
         * SimpleDoc simpleDoc;
595
         * simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
596
         * printerJob.print(simpleDoc, null);
597
         * }
598
         * } catch (MalformedURLException e) {
599
         * // TODO Auto-generated catch block
600
         * e.printStackTrace();
601
         * } catch (PrintException e) {
602
         * // TODO Auto-generated catch block
603
         * e.printStackTrace();
604
         * }
605
         */
606
        layoutControl.fullRect();
607
    }
608

    
609
    /**
610
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
611
     * contenido del Layout.
612
     * 
613
     * @param pdf
614
     */
615
    public void toPDF(File pdf) {
616
        Attributes attributes = layoutContext.getAttributes();
617

    
618
        double w = ((attributes.m_sizePaper.getWidth() * Attributes.DPISCREEN) / Attributes.PULGADA);
619
        double h = ((attributes.m_sizePaper.getHeight() * Attributes.DPISCREEN) / Attributes.PULGADA);
620
        Rectangle pageSize = new com.lowagie.text.Rectangle((float) w, (float) h);
621
        Document document = new Document(pageSize);
622

    
623
        try {
624
            FileOutputStream fos = new FileOutputStream(pdf);
625
            PdfWriter writer = PdfWriter.getInstance(document, fos);
626
            document.open();
627

    
628
            PdfContentByte cb = writer.getDirectContent();
629
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
630

    
631
            try {
632
                    print(g2, new PageFormat(), 0);
633
            } catch (PrinterException e) {
634
                LOG.error("Error printing the map", e);
635
            }
636
            g2.dispose();
637

    
638
        } catch (DocumentException de) {
639
            LOG.error("Error printing the map", de);
640
        } catch (IOException ioe) {
641
            JOptionPane.showMessageDialog(
642
                (Component) PluginServices.getMainFrame(), ioe.getMessage());
643
        }
644

    
645
        document.close();
646

    
647
        layoutControl.fullRect();
648
    }
649

    
650
    public int print(Graphics g, PageFormat format, int pi)
651
    throws PrinterException {
652
        initialize();
653
        if (pi >= 1) {
654
            return Printable.NO_SUCH_PAGE;
655
        }
656

    
657
        Graphics2D g2d = (Graphics2D) g;
658

    
659
        AffineTransform at = g2d.getTransform();
660
        g2d.translate(0, 0);
661
        layoutPanel.obtainRect(true);
662

    
663
        g2d.scale((double) 72 / (double) (Attributes.DPI), (double) 72
664
            / (double) (Attributes.DPI));
665

    
666
        if (layoutContext.getAttributes().isMargin()) {
667
            g2d.setClip(
668
                (int) (layoutControl.getRect().getMinX() + FLayoutUtilities.fromSheetDistance(
669
                    layoutContext.getAttributes().getAreaInsets()[2], layoutControl.getAT())),
670
                    (int) (layoutPanel.getLayoutControl().getRect().getMinY() + FLayoutUtilities.fromSheetDistance(
671
                        layoutContext.getAttributes().getAreaInsets()[0], layoutControl.getAT())),
672
                        (int) (layoutControl.getRect().getWidth() - FLayoutUtilities.fromSheetDistance(
673
                            layoutContext.getAttributes().getAreaInsets()[2] + layoutContext.getAttributes().getAreaInsets()[3], layoutControl.getAT())),
674
                            (int) (layoutPanel.getLayoutControl().getRect().getHeight() - FLayoutUtilities.fromSheetDistance(
675
                                layoutContext.getAttributes().getAreaInsets()[0] + layoutContext.getAttributes().getAreaInsets()[1], layoutControl.getAT())));
676
        }
677
        drawShapes(g2d);
678
        g2d.setTransform(at);
679
        return Printable.PAGE_EXISTS;
680
    }
681

    
682
    /**
683
     * Se dibuja sobre el graphics el Layout.
684
     * 
685
     * @param g2
686
     *            graphics sobre el que se dibuja.
687
     */
688
    public void drawShapes(Graphics2D g2) {
689
        initialize();
690
        layoutPanel.setCursor(Cursor.getDefaultCursor());
691
        if (!initializeAffineTransform()) {
692
                return;
693
        }
694

    
695
        IFFrame[] fframes = layoutContext.getFFrames();
696
        for (int i = 0; i < fframes.length; i++) {
697
            fframes[i].print(g2, layoutControl.getAT(), null, layoutContext
698
                .getAttributes().toPrintAttributes());
699
        }
700
    }
701

    
702
    public void addObserver(Observer o) {
703
        observers.addObserver(o);        
704
    }
705

    
706
    public void deleteObserver(Observer o) {
707
        observers.deleteObserver(o);        
708
    }
709

    
710
    public void deleteObservers() {
711
        observers.deleteObservers();        
712
    }
713
}