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

History | View | Annotate | Download (25.7 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.pdf.PdfContentByte;
55
import com.lowagie.text.pdf.PdfWriter;
56

    
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

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

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

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

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

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

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

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

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

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

    
150
        layoutControl.setCancelDrawing(false);
151

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

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

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

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

    
185
        g.setColor(Color.darkGray);
186

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

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

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

    
196
        if (attributes.isMargin()) {
197

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
617
        double w = 0;
618
        double h = 0;
619
        Document document = new Document();
620

    
621
        if (attributes.isLandscape()) {
622
            w = ((attributes.m_sizePaper.getHeight() * Attributes.DPISCREEN) / Attributes.PULGADA);
623
            h = ((attributes.m_sizePaper.getWidth() * Attributes.DPISCREEN) / Attributes.PULGADA);
624
        } else {
625
            w = ((attributes.m_sizePaper.getWidth() * Attributes.DPISCREEN) / Attributes.PULGADA);
626
            h = ((attributes.m_sizePaper.getHeight() * Attributes.DPISCREEN) / Attributes.PULGADA);
627
        }
628

    
629
        document.setPageSize(new com.lowagie.text.Rectangle((float) w,
630
            (float) h));
631

    
632
        try {
633
            FileOutputStream fos = new FileOutputStream(pdf);
634
            PdfWriter writer = PdfWriter.getInstance(document, fos);
635
            document.open();
636

    
637
            PdfContentByte cb = writer.getDirectContent();
638
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
639

    
640
            try {
641
                if (attributes.isLandscape()) {
642
                    g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)), 0 + (h / 2));
643
                    print(g2, new PageFormat(), 0);
644
                    g2.rotate(Math.toRadians(90), 0 + (w / (h / w)), 0 + (h / 2));
645
                } else {
646
                    print(g2, new PageFormat(), 0);
647
                }
648
            } catch (PrinterException e) {
649
                LOG.error("Error printing the map", e);
650
            }
651

    
652
            g2.dispose();
653

    
654
        } catch (DocumentException de) {
655
            LOG.error("Error printing the map", de);
656
        } catch (IOException ioe) {
657
            JOptionPane.showMessageDialog(
658
                (Component) PluginServices.getMainFrame(), ioe.getMessage());
659
        }
660

    
661
        document.close();
662

    
663
        layoutControl.fullRect();
664
    }
665

    
666
    public int print(Graphics g, PageFormat format, int pi)
667
    throws PrinterException {
668
        initialize();
669
        if (pi >= 1) {
670
            return Printable.NO_SUCH_PAGE;
671
        }
672

    
673
        Graphics2D g2d = (Graphics2D) g;
674

    
675
        AffineTransform at = g2d.getTransform();
676
        g2d.translate(0, 0);
677
        layoutPanel.obtainRect(true);
678

    
679
        g2d.scale((double) 72 / (double) (Attributes.DPI), (double) 72
680
            / (double) (Attributes.DPI));
681

    
682
        if (layoutContext.getAttributes().isMargin()) {
683
            g2d.setClip(
684
                (int) (layoutControl.getRect().getMinX() + FLayoutUtilities.fromSheetDistance(
685
                    layoutContext.getAttributes().getAreaInsets()[2], layoutControl.getAT())),
686
                    (int) (layoutPanel.getLayoutControl().getRect().getMinY() + FLayoutUtilities.fromSheetDistance(
687
                        layoutContext.getAttributes().getAreaInsets()[0], layoutControl.getAT())),
688
                        (int) (layoutControl.getRect().getWidth() - FLayoutUtilities.fromSheetDistance(
689
                            layoutContext.getAttributes().getAreaInsets()[2] + layoutContext.getAttributes().getAreaInsets()[3], layoutControl.getAT())),
690
                            (int) (layoutPanel.getLayoutControl().getRect().getHeight() - FLayoutUtilities.fromSheetDistance(
691
                                layoutContext.getAttributes().getAreaInsets()[0] + layoutContext.getAttributes().getAreaInsets()[1], layoutControl.getAT())));
692
        }
693
        drawShapes(g2d);
694
        g2d.setTransform(at);
695
        return Printable.PAGE_EXISTS;
696
    }
697

    
698
    /**
699
     * Se dibuja sobre el graphics el Layout.
700
     * 
701
     * @param g2
702
     *            graphics sobre el que se dibuja.
703
     */
704
    public void drawShapes(Graphics2D g2) {
705
        initialize();
706
        layoutPanel.setCursor(Cursor.getDefaultCursor());
707
        if (!initializeAffineTransform()) {
708
                return;
709
        }
710

    
711
        IFFrame[] fframes = layoutContext.getFFrames();
712
        for (int i = 0; i < fframes.length; i++) {
713
            fframes[i].print(g2, layoutControl.getAT(), null, layoutContext
714
                .getAttributes().toPrintAttributes());
715
        }
716
    }
717

    
718
    public void addObserver(Observer o) {
719
        observers.addObserver(o);        
720
    }
721

    
722
    public void deleteObserver(Observer o) {
723
        observers.deleteObserver(o);        
724
    }
725

    
726
    public void deleteObservers() {
727
        observers.deleteObservers();        
728
    }
729
}