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 @ 1757

History | View | Annotate | Download (24.9 KB)

1 5 jldominguez
/* 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 1757 fdiaz
import com.lowagie.text.Document;
25
import com.lowagie.text.DocumentException;
26
import com.lowagie.text.Rectangle;
27
import com.lowagie.text.pdf.PdfContentByte;
28
import com.lowagie.text.pdf.PdfWriter;
29 5 jldominguez
import java.awt.Color;
30
import java.awt.Component;
31
import java.awt.Cursor;
32
import java.awt.Graphics;
33
import java.awt.Graphics2D;
34
import java.awt.geom.AffineTransform;
35
import java.awt.geom.Point2D;
36
import java.awt.geom.Rectangle2D;
37
import java.awt.image.BufferedImage;
38
import java.awt.print.PageFormat;
39
import java.awt.print.Printable;
40
import java.awt.print.PrinterException;
41
import java.io.BufferedOutputStream;
42
import java.io.File;
43
import java.io.FileOutputStream;
44
import java.io.IOException;
45
import java.io.OutputStream;
46
import javax.print.Doc;
47
import javax.print.DocFlavor;
48
import javax.print.DocPrintJob;
49
import javax.print.PrintException;
50
import javax.print.SimpleDoc;
51
import javax.print.StreamPrintService;
52
import javax.print.StreamPrintServiceFactory;
53
import javax.print.attribute.PrintRequestAttributeSet;
54
import javax.swing.JOptionPane;
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
57
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
58
import org.gvsig.tools.observer.Observable;
59
import org.gvsig.tools.observer.ObservableHelper;
60
import org.gvsig.tools.observer.Observer;
61 1757 fdiaz
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63 5 jldominguez
64
/**
65
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class FLayoutDraw implements Observable, Printable{
70
    private static final Logger LOG =
71
        LoggerFactory.getLogger(FLayoutDraw.class);
72
73
    private LayoutPanel layoutPanel;
74
    private LayoutControl layoutControl;
75
    private LayoutContext layoutContext;
76
    private ObservableHelper observers;
77
78
    /**
79
     * Crea un nuevo FLayoutDraw.
80
     *
81
     * @param l
82
     *            Referencia al Layout.
83
     */
84
    public FLayoutDraw(LayoutPanel layoutPanel) {
85
        this.layoutPanel = layoutPanel;
86
        observers = new ObservableHelper();
87
        observers.addObserver(layoutPanel.getLayoutControl());
88
    }
89
90
    public void initialize(){
91
        if ((layoutContext == null) || (layoutControl == null)){
92
            this.layoutControl = layoutPanel.getLayoutControl();
93
            this.layoutContext = layoutPanel.getLayoutContext();
94
        }
95
    }
96
97
    /**
98
     * M?todo para dibujar el Layout y modificar la matriz de transformaci?n a
99
     * partir del tama?o en pixels que tenga rect y el formato de papel
100
     * seleccionado.
101
     *
102
     * @param g2
103
     * @param imgBase
104
     *            Si es null, est?s imprimiendo. Si no, la usas para el
105
     *            c?digo de optimizaci?n.
106
     *
107
     */
108
    public void drawLayout(Graphics2D g2, BufferedImage imgBase)
109 1757 fdiaz
    {
110 175 cmartinez
        if (!initializeAffineTransform()) {
111
                return;
112
        }
113 5 jldominguez
114
        IFFrame[] fframes = layoutContext.getFFrames();
115 1757 fdiaz
        for (IFFrame f : fframes) {
116 5 jldominguez
            f.draw(g2, layoutContext.getAT(), layoutControl.getComponent().getVisibleRect(),
117 1757 fdiaz
                imgBase);
118
119 5 jldominguez
            // Dibuja el s?mbolo de que contiene un tag.
120
            if ((f.getTag() != null) && (layoutPanel.isShowIconTag())) {
121
                f.drawSymbolTag(g2);
122
            }
123
        }
124
125
        if (!(fframes.length == 0)) {
126
            observers.notifyObservers(this,
127
                new DefaultLayoutNotification(LayoutNotification.LAYOUT_VALIDATED));
128
        } else {
129
            observers.notifyObservers(this,
130
                new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
131
        }
132
    }
133
134 357 cmartinez
    public boolean initializeAffineTransform(){
135 5 jldominguez
        initialize();
136
        AffineTransform at = layoutContext.getAT();
137
        Rectangle2D rLayout = layoutControl.getRect();
138 175 cmartinez
        if (rLayout.getHeight()<0) {
139
                // still not ready to be painted
140
                return false;
141
        }
142 5 jldominguez
        Attributes attributes = layoutContext.getAttributes();
143
144
        layoutControl.setCancelDrawing(false);
145
146 1757 fdiaz
        double scale = rLayout.getHeight() / attributes.getPaperSize().getHeight() * 1; // paper (paper units) to screen (pixels) scale
147 5 jldominguez
        AffineTransform escalado = new AffineTransform();
148
        AffineTransform translacion = new AffineTransform();
149
        translacion.setToTranslation(rLayout.getMinX(), rLayout.getMinY());
150
        escalado.setToScale(scale, scale);
151
        at.setToIdentity();
152
        at.concatenate(translacion);
153
        at.concatenate(escalado);
154 228 cmartinez
        attributes.calculateGridGapX(rLayout);
155
        attributes.calculateGridGapY(rLayout);
156 175 cmartinez
        return true;
157 5 jldominguez
    }
158
159
    /**
160
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
161
     *
162
     * @param g
163
     *            Graphics2D
164
     */
165
    public void drawRectangle(Graphics2D g) {
166
        initialize();
167
        Attributes attributes = layoutContext.getAttributes();
168
        Rectangle2D rLayout = layoutControl.getRect();
169
        AffineTransform at = layoutContext.getAT();
170
171 228 cmartinez
        double unidadesX = attributes.getHGridGapCm();
172
        double unidadesY = attributes.getVGridGapCm();
173 5 jldominguez
174
        if ((unidadesX == 0) && (unidadesY == 0)) {
175
            return;
176
        }
177
178
        g.setColor(Color.darkGray);
179
180
        Rectangle2D.Double rectBig =
181
            new Rectangle2D.Double(rLayout.getX(), rLayout.getY(),
182
                rLayout.getWidth(), rLayout.getHeight());
183
184
        g.fillRect((int) rectBig.x + 7, (int) rectBig.y + 7,
185
            (int) rectBig.width, (int) rectBig.height);
186
187
        Rectangle2D.Double r = new Rectangle2D.Double();
188
189
        if (attributes.isMargin()) {
190
191
            r =
192
                new Rectangle2D.Double(
193
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
194 58 jldominguez
                        attributes.getAreaInsets()[2], at)),
195 5 jldominguez
                        (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
196 58 jldominguez
                            attributes.getAreaInsets()[0], at)),
197 5 jldominguez
                            (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
198 58 jldominguez
                                attributes.getAreaInsets()[2] + attributes.getAreaInsets()[3], at)),
199 5 jldominguez
                                (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
200 58 jldominguez
                                    attributes.getAreaInsets()[0] + attributes.getAreaInsets()[1], at)));
201 5 jldominguez
        } else {
202
            r.setRect(rLayout);
203
        }
204
205
        g.setColor(Color.white);
206
        g.fill(rLayout);
207
208
        g.setColor(Color.darkGray);
209
        g.drawRect((int) rLayout.getMinX(), (int) rLayout.getMinY(),
210
            (int) rLayout.getWidth(), (int) rLayout.getHeight());
211
212
        if (attributes.isMargin()) {
213
            g.setColor(Color.black);
214
215
            g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
216
        }
217
    }
218
219
    /**
220
     * DOCUMENT ME!
221
     *
222
     * @param g
223
     *            DOCUMENT ME!
224
     */
225
    public void drawGrid(Graphics2D g) {
226
        int unidadesMin = 6;
227
        Attributes attributes = layoutContext.getAttributes();
228
        Rectangle2D rLayout = layoutControl.getRect();
229
        AffineTransform at = layoutContext.getAT();
230
231 228 cmartinez
        double unidadesX = attributes.getHGridGapPx();
232
        double unidadesY = attributes.getVGridGapPx();
233 5 jldominguez
234
        Rectangle2D.Double r = new Rectangle2D.Double();
235
236
        if (attributes.isMargin()) {
237
            r =
238
                new Rectangle2D.Double(
239
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
240 58 jldominguez
                        attributes.getAreaInsets()[2], at)),
241 5 jldominguez
                        (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
242 58 jldominguez
                            attributes.getAreaInsets()[0], at)),
243 5 jldominguez
                            (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
244 58 jldominguez
                                attributes.getAreaInsets()[2] + attributes.getAreaInsets()[3], at)),
245 5 jldominguez
                                (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
246 58 jldominguez
                                    attributes.getAreaInsets()[0] + attributes.getAreaInsets()[1], at)));
247 5 jldominguez
        } else {
248
            r.setRect(rLayout);
249
        }
250
        if ((unidadesX == 0) && (unidadesY == 0)) {
251
            return;
252
        }
253
        g.setColor(Color.darkGray);
254
255
        if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin))
256
            && layoutContext.isGridVisible()) {
257
            double ax = r.getMinX();
258
            double ay;
259
260
            while (ax < (r.getMaxX())) {
261
                ay = (r.getMinY());
262
263
                while (ay < (r.getMaxY())) {
264
                    g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
265
                    ay = ay + unidadesY;
266
                }
267
268
                ax = ax + unidadesX;
269
            }
270
        }
271
    }
272
273
    /**
274
     * Dibuja sobre el graphics2d las reglas.
275
     *
276
     * @param g
277
     *            graphics2d sobre el que se dibuja.
278
     * @param color
279
     *            Color de la regla.
280
     */
281
    public void drawRuler(Graphics2D g, Color color) {
282
        Attributes attributes = layoutContext.getAttributes();
283
        Rectangle2D rLayout = layoutControl.getRect();
284
        AffineTransform at = layoutContext.getAT();
285
286
        if (layoutContext.getRuler()) {
287
            int ini = 10;
288
            int w = 30;
289
            int wi = 16;
290
291
            g.setColor(new Color(250, 255, 250, 180));
292
            g.fillRect(ini, w, wi, layoutControl.getComponent().getHeight() - w);
293
            g.fillRect(w, ini, layoutControl.getWidth() - w, wi);
294
295
            g.setColor(new Color(100, 155, 150, 180));
296
            g.fillRect(w, ini, (int) rLayout.getX() - w, wi);
297
            g.fillRect((int) rLayout.getMaxX(), ini, layoutControl.getWidth()
298
                - (int) rLayout.getMaxX(), wi);
299
300
            g.fillRect(ini, w, wi, (int) rLayout.getY() - w);
301
            g.fillRect(ini, (int) rLayout.getMaxY(), wi,
302
                layoutControl.getHeight() - (int) rLayout.getMaxY());
303
304
            if (attributes.isMargin()) {
305
                g.setColor(new Color(50, 55, 50, 180));
306
                g.fillRect((int) rLayout.getX(), ini, (int) FLayoutUtilities
307 58 jldominguez
                    .fromSheetDistance(attributes.getAreaInsets()[2], at), wi);
308 5 jldominguez
                g.fillRect(
309
                    (int) rLayout.getMaxX()
310
                    - (int) FLayoutUtilities.fromSheetDistance(
311 58 jldominguez
                        attributes.getAreaInsets()[3], at), ini,
312 5 jldominguez
                        (int) FLayoutUtilities.fromSheetDistance(
313 58 jldominguez
                            attributes.getAreaInsets()[3], at), wi);
314 5 jldominguez
315
                g.fillRect(ini, (int) rLayout.getY(), wi,
316
                    (int) FLayoutUtilities.fromSheetDistance(
317 58 jldominguez
                        attributes.getAreaInsets()[0], at));
318 5 jldominguez
                g.fillRect(
319
                    ini,
320
                    (int) rLayout.getMaxY()
321
                    - (int) FLayoutUtilities.fromSheetDistance(
322 58 jldominguez
                        attributes.getAreaInsets()[1], at), wi,
323 5 jldominguez
                        (int) FLayoutUtilities.fromSheetDistance(
324 58 jldominguez
                            attributes.getAreaInsets()[1], at));
325 5 jldominguez
            }
326
327
            g.setColor(color);
328
            g.drawLine(w, wi + ini, layoutControl.getWidth(), wi + ini);
329
            g.drawLine(w, ini, layoutControl.getWidth(), ini);
330
            g.drawLine(ini, w, ini, layoutControl.getHeight());
331
            g.drawLine(wi + ini, w, wi + ini, layoutControl.getHeight());
332
333
            drawLineY(g, 5, 12, 22, rLayout.getY(), 0);
334
            drawLineX(g, 5, 12, 22, rLayout.getX(), 0);
335
336
            if (FLayoutUtilities.fromSheetDistance(1, at) > 15) {
337
                drawLineY(g, 1, 12, 22, rLayout.getY(), 0);
338
                drawLineX(g, 1, 12, 22, rLayout.getX(), 0);
339
340
                if (FLayoutUtilities.fromSheetDistance(1, at) > 25) {
341
                    drawLineY(g, 1, 18, 22, rLayout.getY(), 0.5);
342
                    drawLineY(g, 0.1, 21, 22, rLayout.getY(), 0);
343
                    drawLineX(g, 1, 18, 22, rLayout.getX(), 0.5);
344
                    drawLineX(g, 0.1, 21, 22, rLayout.getX(), 0);
345
                }
346
            }
347
        }
348
    }
349
350
    /**
351
     * Dibuja sobre el graphics2d las l?neas verticales que representan a las
352
     * unidades de medida.
353
     *
354
     * @param g
355
     *            Graphics2d sobre el que se dibuja.
356
     * @param dist
357
     *            distancia en cent?metros de una l?nea a otra.
358
     * @param init
359
     *            inicio de la l?nea.
360
     * @param end
361
     *            fin de la l?nea.
362
     * @param y
363
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
364
     * @param desp
365
     *            Desplazamiento.
366
     */
367
    private void drawLineY(Graphics2D g, double dist, int init, int end,
368
        double y, double desp) {
369
        initialize();
370
        AffineTransform at = layoutContext.getAT();
371
372
        double distY = FLayoutUtilities.fromSheetDistance(dist, at);
373
        double rota = Math.toRadians(90);
374
375
        if (distY > 4) {
376
            double despY = FLayoutUtilities.fromSheetDistance(desp, at);
377
378
            double posUnitY = y + despY;
379
            double posUnitYNeg = posUnitY;
380
            int num = 0;
381
            double iniY = 40;
382
            Point2D.Double pfin =
383
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
384
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
385
386
            while (posUnitY < (pfin.y - 5)) {
387
                posUnitYNeg -= distY;
388
389
                if (distY > 16) {
390
                    if (init == 12) {
391
                        if (posUnitY > iniY) {
392
                            g.rotate(-rota, 20, posUnitY - 12);
393
                            g.drawString(String.valueOf(num), 10,
394
                                (int) posUnitY - 12);
395
                            g.rotate(rota, 20, posUnitY - 12);
396
                        }
397
398
                        if (dist == 5) {
399
                            num = num + 5;
400
                        } else {
401
                            num++;
402
                        }
403
404
                        if (posUnitYNeg > iniY) {
405
                            g.rotate(-rota, 20, posUnitYNeg - 12);
406
                            g.drawString(String.valueOf(-num), 10,
407
                                (int) posUnitYNeg - 12);
408
                            g.rotate(rota, 20, posUnitYNeg - 12);
409
                        }
410
                    }
411
                }
412
413
                if (posUnitY > iniY) {
414
                    g.drawLine(2 + init, (int) posUnitY, 2 + end,
415
                        (int) posUnitY);
416
                }
417
418
                if (posUnitYNeg > iniY) {
419
                    g.drawLine(2 + init, (int) posUnitYNeg, 2 + end,
420
                        (int) posUnitYNeg);
421
                }
422
423
                posUnitY += distY;
424
            }
425
        }
426
    }
427
428
    /**
429
     * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
430
     * unidades de medida.
431
     *
432
     * @param g
433
     *            Graphics2d sobre el que se dibuja.
434
     * @param dist
435
     *            distancia en cent?metros de una l?nea a otra.
436
     * @param init
437
     *            inicio de la l?nea.
438
     * @param end
439
     *            fin de la l?nea.
440
     * @param x
441
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
442
     * @param desp
443
     *            Desplazamiento.
444
     */
445
    private void drawLineX(Graphics2D g, double dist, int init, int end,
446
        double x, double desp) {
447
        initialize();
448
        AffineTransform at = layoutContext.getAT();
449
450
        double distX = FLayoutUtilities.fromSheetDistance(dist, at);
451
452
        if (distX > 4) {
453
            double despX = FLayoutUtilities.fromSheetDistance(desp, at);
454
            double posUnitX = x + despX;
455
            double posUnitXNeg = posUnitX;
456
            int num = 0;
457
            double iniX = 40;
458
            Point2D.Double pfin =
459
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
460
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
461
462
            while (posUnitX < (pfin.x - 5)) {
463
                posUnitXNeg -= distX;
464
465
                if (init == 12) {
466
                    if (distX > 16) {
467
                        if (posUnitX > iniX) {
468
                            g.drawString(String.valueOf(num),
469
                                (int) posUnitX + 3, 20);
470
                        }
471
472
                        if (dist == 5) {
473
                            num = num + 5;
474
                        } else {
475
                            num++;
476
                        }
477
478
                        if (posUnitXNeg > iniX) {
479
                            g.drawString(String.valueOf(-num),
480
                                (int) posUnitXNeg + 3, 20);
481
                        }
482
                    }
483
                }
484
485
                if (posUnitX > iniX) {
486
                    g.drawLine((int) posUnitX, 2 + init, (int) posUnitX,
487
                        2 + end);
488
                }
489
490
                if (posUnitXNeg > iniX) {
491
                    g.drawLine((int) posUnitXNeg, 2 + init, (int) posUnitXNeg,
492
                        2 + end);
493
                }
494
495
                posUnitX += distX;
496
            }
497
        }
498
    }
499
500
    /**
501
     * Dibuja los handlers sobre los fframes que esten seleccionados.
502
     *
503
     * @param g
504
     *            Graphics sobre el que se dibuja.
505
     * @param color
506
     *            Color de los Handlers.
507
     */
508
    public void drawHandlers(Graphics2D g, Color color) {
509
        initialize();
510
511
        g.setColor(color);
512
        IFFrame[] fframes = layoutContext.getFFrames();
513 1757 fdiaz
        for (IFFrame fframe : fframes) {
514 5 jldominguez
            if (fframe.getSelected() != IFFrame.NOSELECT) {
515
                fframe.drawHandlers(g);
516
            }
517
        }
518
    }
519
520
    /**
521
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
522
     * contenido del Layout.
523
     *
524
     * @param ps
525
     */
526
    public void toPS(File ps) {
527
        initialize();
528
        Attributes attributes = layoutContext.getAttributes();
529
530
        try {
531
            // Prepare the output file to receive the postscript
532
            OutputStream fos =
533
                new BufferedOutputStream(new FileOutputStream(ps));
534
535
            // Find a factory that can do the conversion
536
            // DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
537
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
538
            StreamPrintServiceFactory[] factories =
539
                StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
540
                    flavor, "application/postscript");
541
542
            if (factories.length > 0) {
543
                StreamPrintService service = factories[0].getPrintService(fos);
544
545
                // Create the print job
546
                DocPrintJob job = service.createPrintJob();
547
                Doc doc = new SimpleDoc(this, flavor, null);
548
                // Doc doc = new SimpleDoc(is, flavor, null);
549
                // Monitor print job events; for the implementation of
550
                // PrintJobWatcher,
551
                // see e702 Determining When a Print Job Has Finished
552
                // PrintJobWatcher pjDone = new PrintJobWatcher(job);
553
                // Actualizar attributes
554
                PrintRequestAttributeSet att =
555
                    attributes.toPrintRequestAttributeSet();
556
                // Print it
557
                job.print(doc, att);
558
559
                // Wait for the print job to be done
560
                // pjDone.waitForDone();
561
                // It is now safe to close the streams
562
            }
563
564
            // is.close();
565
            fos.close();
566 1757 fdiaz
        } catch (PrintException | IOException e) {
567 5 jldominguez
            LOG.error("Error printing the map", e);
568
        }
569
        /*
570
         * PrintService defaultPrintService =
571
         * PrintServiceLookup.lookupDefaultPrintService();
572
         * DocPrintJob printerJob = defaultPrintService.createPrintJob();
573
         *
574
         * try {
575
         * DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
576
         * if (!defaultPrintService.isDocFlavorSupported(flavor)) {
577
         * System.err.println(
578
         * "The printer does not support the appropriate DocFlavor");
579
         * }else {
580
         *
581
         * SimpleDoc simpleDoc;
582
         * simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
583
         * printerJob.print(simpleDoc, null);
584
         * }
585
         * } catch (MalformedURLException e) {
586
         * // TODO Auto-generated catch block
587
         * e.printStackTrace();
588
         * } catch (PrintException e) {
589
         * // TODO Auto-generated catch block
590
         * e.printStackTrace();
591
         * }
592
         */
593
        layoutControl.fullRect();
594
    }
595
596
    /**
597
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
598
     * contenido del Layout.
599
     *
600
     * @param pdf
601
     */
602
    public void toPDF(File pdf) {
603
        Attributes attributes = layoutContext.getAttributes();
604
605 371 cmartinez
        double w = ((attributes.getPaperSize().getWidth() * Attributes.DPISCREEN) / Attributes.PULGADA);
606
        double h = ((attributes.getPaperSize().getHeight() * Attributes.DPISCREEN) / Attributes.PULGADA);
607 244 cmartinez
        Rectangle pageSize = new com.lowagie.text.Rectangle((float) w, (float) h);
608
        Document document = new Document(pageSize);
609 5 jldominguez
610
        try {
611
            FileOutputStream fos = new FileOutputStream(pdf);
612
            PdfWriter writer = PdfWriter.getInstance(document, fos);
613
            document.open();
614
615
            PdfContentByte cb = writer.getDirectContent();
616
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
617
618
            try {
619 244 cmartinez
                    print(g2, new PageFormat(), 0);
620 5 jldominguez
            } catch (PrinterException e) {
621
                LOG.error("Error printing the map", e);
622
            }
623
            g2.dispose();
624
625
        } catch (DocumentException de) {
626
            LOG.error("Error printing the map", de);
627
        } catch (IOException ioe) {
628
            JOptionPane.showMessageDialog(
629
                (Component) PluginServices.getMainFrame(), ioe.getMessage());
630
        }
631
632
        document.close();
633
634
        layoutControl.fullRect();
635
    }
636
637 1757 fdiaz
    @Override
638 5 jldominguez
    public int print(Graphics g, PageFormat format, int pi)
639
    throws PrinterException {
640
        initialize();
641
        if (pi >= 1) {
642
            return Printable.NO_SUCH_PAGE;
643
        }
644
645
        Graphics2D g2d = (Graphics2D) g;
646
647
        AffineTransform at = g2d.getTransform();
648
        g2d.translate(0, 0);
649
        layoutPanel.obtainRect(true);
650
651
        g2d.scale((double) 72 / (double) (Attributes.DPI), (double) 72
652
            / (double) (Attributes.DPI));
653
654
        if (layoutContext.getAttributes().isMargin()) {
655
            g2d.setClip(
656
                (int) (layoutControl.getRect().getMinX() + FLayoutUtilities.fromSheetDistance(
657 58 jldominguez
                    layoutContext.getAttributes().getAreaInsets()[2], layoutControl.getAT())),
658 5 jldominguez
                    (int) (layoutPanel.getLayoutControl().getRect().getMinY() + FLayoutUtilities.fromSheetDistance(
659 58 jldominguez
                        layoutContext.getAttributes().getAreaInsets()[0], layoutControl.getAT())),
660 5 jldominguez
                        (int) (layoutControl.getRect().getWidth() - FLayoutUtilities.fromSheetDistance(
661 58 jldominguez
                            layoutContext.getAttributes().getAreaInsets()[2] + layoutContext.getAttributes().getAreaInsets()[3], layoutControl.getAT())),
662 5 jldominguez
                            (int) (layoutPanel.getLayoutControl().getRect().getHeight() - FLayoutUtilities.fromSheetDistance(
663 58 jldominguez
                                layoutContext.getAttributes().getAreaInsets()[0] + layoutContext.getAttributes().getAreaInsets()[1], layoutControl.getAT())));
664 5 jldominguez
        }
665
        drawShapes(g2d);
666
        g2d.setTransform(at);
667
        return Printable.PAGE_EXISTS;
668
    }
669
670
    /**
671
     * Se dibuja sobre el graphics el Layout.
672
     *
673
     * @param g2
674
     *            graphics sobre el que se dibuja.
675
     */
676
    public void drawShapes(Graphics2D g2) {
677
        initialize();
678
        layoutPanel.setCursor(Cursor.getDefaultCursor());
679 175 cmartinez
        if (!initializeAffineTransform()) {
680
                return;
681
        }
682 5 jldominguez
683
        IFFrame[] fframes = layoutContext.getFFrames();
684 1757 fdiaz
        for (IFFrame fframe : fframes) {
685
            fframe.print(g2, layoutControl.getAT(), null, layoutContext
686 5 jldominguez
                .getAttributes().toPrintAttributes());
687
        }
688
    }
689
690 1757 fdiaz
    @Override
691 5 jldominguez
    public void addObserver(Observer o) {
692
        observers.addObserver(o);
693
    }
694
695 1757 fdiaz
    @Override
696 5 jldominguez
    public void deleteObserver(Observer o) {
697
        observers.deleteObserver(o);
698
    }
699
700 1757 fdiaz
    @Override
701 5 jldominguez
    public void deleteObservers() {
702
        observers.deleteObservers();
703
    }
704
}