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

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