Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutDraw.java @ 37415

History | View | Annotate | Download (26 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.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.Point2D;
30
import java.awt.geom.Rectangle2D;
31
import java.awt.image.BufferedImage;
32
import java.awt.print.PageFormat;
33
import java.awt.print.Printable;
34
import java.awt.print.PrinterException;
35
import java.io.BufferedOutputStream;
36
import java.io.File;
37
import java.io.FileOutputStream;
38
import java.io.IOException;
39
import java.io.OutputStream;
40

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

    
51
import com.lowagie.text.Document;
52
import com.lowagie.text.DocumentException;
53
import com.lowagie.text.pdf.PdfContentByte;
54
import com.lowagie.text.pdf.PdfWriter;
55

    
56
import org.gvsig.andami.PluginServices;
57
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
58
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.tools.observer.Observable;
61
import org.gvsig.tools.observer.ObservableHelper;
62
import org.gvsig.tools.observer.Observer;
63

    
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

    
71
    private LayoutPanel layoutPanel;
72
    private ObservableHelper observers;
73

    
74
    /**
75
     * Crea un nuevo FLayoutDraw.
76
     * 
77
     * @param l
78
     *            Referencia al Layout.
79
     */
80
    public FLayoutDraw(LayoutPanel layoutPanel) {
81
        this.layoutPanel = layoutPanel;
82
        observers = new ObservableHelper();
83
        observers.addObserver(layoutPanel.getLayoutControl());
84
    }
85

    
86
    /**
87
     * M?todo para dibujar el Layout y modificar la matriz de transformaci?n a
88
     * partir del tama?o en pixels que tenga rect y el formato de papel
89
     * seleccionado.
90
     * 
91
     * @param g2
92
     * @param imgBase
93
     *            Si es null, est?s imprimiendo. Si no, la usas para el
94
     *            c?digo de optimizaci?n.
95
     * 
96
     * @throws ReadDriverException
97
     */
98
    public void drawLayout(Graphics2D g2, BufferedImage imgBase)
99
        throws ReadException {
100
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
101
        LayoutContext layoutContext = layoutPanel.getLayoutContext();
102
        Attributes attributes = layoutContext.getAttributes();
103
        Rectangle2D rLayout = layoutControl.getRect();
104
        AffineTransform at = layoutContext.getAT();
105

    
106
        layoutControl.setCancelDrawing(false);
107
        double scale = 0;
108
        scale = rLayout.getHeight() / attributes.m_sizePaper.getAlto() * 1;
109
        AffineTransform escalado = new AffineTransform();
110
        AffineTransform translacion = new AffineTransform();
111
        translacion.setToTranslation(rLayout.getMinX(), rLayout.getMinY());
112
        escalado.setToScale(scale, scale);
113
        at.setToIdentity();
114
        at.concatenate(translacion);
115
        at.concatenate(escalado);
116
        attributes.setDistanceUnitX(rLayout);
117
        attributes.setDistanceUnitY(rLayout);
118
        IFFrame[] fframes = layoutContext.getFFrames();
119
        for (int i = 0; i < fframes.length; i++) {
120
            IFFrame f = fframes[i];
121
            f.draw(g2, at, layoutControl.getComponent().getVisibleRect(),
122
                imgBase);          
123

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

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

    
139
    /**
140
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
141
     * 
142
     * @param g
143
     *            Graphics2D
144
     */
145
    public void drawRectangle(Graphics2D g) {
146
        Attributes attributes = layoutPanel.getLayoutContext().getAttributes();
147
        Rectangle2D rLayout = layoutPanel.getLayoutControl().getRect();
148
        AffineTransform at = layoutPanel.getLayoutContext().getAT();
149

    
150
        double unidadesX = attributes.getNumUnitsX();
151
        double unidadesY = attributes.getNumUnitsY();
152

    
153
        if ((unidadesX == 0) && (unidadesY == 0)) {
154
            return;
155
        }
156

    
157
        g.setColor(Color.darkGray);
158

    
159
        Rectangle2D.Double rectBig =
160
            new Rectangle2D.Double(rLayout.getX(), rLayout.getY(),
161
                rLayout.getWidth(), rLayout.getHeight());
162

    
163
        g.fillRect((int) rectBig.x + 7, (int) rectBig.y + 7,
164
            (int) rectBig.width, (int) rectBig.height);
165

    
166
        Rectangle2D.Double r = new Rectangle2D.Double();
167

    
168
        if (attributes.isMargin()) {
169

    
170
            r =
171
                new Rectangle2D.Double(
172
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
173
                        attributes.m_area[2], at)),
174
                    (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
175
                        attributes.m_area[0], at)),
176
                    (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
177
                        attributes.m_area[2] + attributes.m_area[3], at)),
178
                    (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
179
                        attributes.m_area[0] + attributes.m_area[1], at)));
180
        } else {
181
            r.setRect(rLayout);
182
        }
183

    
184
        g.setColor(Color.white);
185
        g.fill(rLayout);
186

    
187
        g.setColor(Color.darkGray);
188
        g.drawRect((int) rLayout.getMinX(), (int) rLayout.getMinY(),
189
            (int) rLayout.getWidth(), (int) rLayout.getHeight());
190

    
191
        if (attributes.isMargin()) {
192
            g.setColor(Color.black);
193

    
194
            g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
195
        }
196
    }
197

    
198
    /**
199
     * DOCUMENT ME!
200
     * 
201
     * @param g
202
     *            DOCUMENT ME!
203
     */
204
    public void drawGrid(Graphics2D g) {
205
        int unidadesMin = 6;
206
        LayoutContext layoutContext = layoutPanel.getLayoutContext();
207
        Attributes attributes = layoutContext.getAttributes();
208
        Rectangle2D rLayout = layoutPanel.getLayoutControl().getRect();
209
        AffineTransform at = layoutContext.getAT();
210

    
211
        double unidadesX = attributes.getUnitInPixelsX();
212
        double unidadesY = attributes.getUnitInPixelsY();
213

    
214
        Rectangle2D.Double r = new Rectangle2D.Double();
215

    
216
        if (attributes.isMargin()) {
217
            r =
218
                new Rectangle2D.Double(
219
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
220
                        attributes.m_area[2], at)),
221
                    (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
222
                        attributes.m_area[0], at)),
223
                    (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
224
                        attributes.m_area[2] + attributes.m_area[3], at)),
225
                    (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
226
                        attributes.m_area[0] + attributes.m_area[1], at)));
227
        } else {
228
            r.setRect(rLayout);
229
        }
230
        if ((unidadesX == 0) && (unidadesY == 0)) {
231
            return;
232
        }
233
        g.setColor(Color.darkGray);
234

    
235
        if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin))
236
            && layoutContext.isGridVisible()) {
237
            double ax = r.getMinX();
238
            double ay;
239

    
240
            while (ax < (r.getMaxX())) {
241
                ay = (r.getMinY());
242

    
243
                while (ay < (r.getMaxY())) {
244
                    g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
245
                    ay = ay + unidadesY;
246
                }
247

    
248
                ax = ax + unidadesX;
249
            }
250
        }
251
    }
252

    
253
    /**
254
     * Dibuja sobre el graphics2d las reglas.
255
     * 
256
     * @param g
257
     *            graphics2d sobre el que se dibuja.
258
     * @param color
259
     *            Color de la regla.
260
     */
261
    public void drawRuler(Graphics2D g, Color color) {
262
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
263
        LayoutContext layoutContext = layoutPanel.getLayoutContext();
264
        Attributes attributes = layoutContext.getAttributes();
265
        Rectangle2D rLayout = layoutControl.getRect();
266
        AffineTransform at = layoutContext.getAT();
267

    
268
        if (layoutContext.getRuler()) {
269
            int ini = 10;
270
            int w = 30;
271
            int wi = 16;
272

    
273
            g.setColor(new Color(250, 255, 250, 180));
274
            g.fillRect(ini, w, wi, layoutControl.getComponent().getHeight() - w);
275
            g.fillRect(w, ini, layoutControl.getWidth() - w, wi);
276

    
277
            g.setColor(new Color(100, 155, 150, 180));
278
            g.fillRect(w, ini, (int) rLayout.getX() - w, wi);
279
            g.fillRect((int) rLayout.getMaxX(), ini, layoutControl.getWidth()
280
                - (int) rLayout.getMaxX(), wi);
281

    
282
            g.fillRect(ini, w, wi, (int) rLayout.getY() - w);
283
            g.fillRect(ini, (int) rLayout.getMaxY(), wi,
284
                layoutControl.getHeight() - (int) rLayout.getMaxY());
285

    
286
            if (attributes.isMargin()) {
287
                g.setColor(new Color(50, 55, 50, 180));
288
                g.fillRect((int) rLayout.getX(), ini, (int) FLayoutUtilities
289
                    .fromSheetDistance(attributes.m_area[2], at), wi);
290
                g.fillRect(
291
                    (int) rLayout.getMaxX()
292
                        - (int) FLayoutUtilities.fromSheetDistance(
293
                            attributes.m_area[3], at), ini,
294
                    (int) FLayoutUtilities.fromSheetDistance(
295
                        attributes.m_area[3], at), wi);
296

    
297
                g.fillRect(ini, (int) rLayout.getY(), wi,
298
                    (int) FLayoutUtilities.fromSheetDistance(
299
                        attributes.m_area[0], at));
300
                g.fillRect(
301
                    ini,
302
                    (int) rLayout.getMaxY()
303
                        - (int) FLayoutUtilities.fromSheetDistance(
304
                            attributes.m_area[1], at), wi,
305
                    (int) FLayoutUtilities.fromSheetDistance(
306
                        attributes.m_area[1], at));
307
            }
308

    
309
            g.setColor(color);
310
            g.drawLine(w, wi + ini, layoutControl.getWidth(), wi + ini);
311
            g.drawLine(w, ini, layoutControl.getWidth(), ini);
312
            g.drawLine(ini, w, ini, layoutControl.getHeight());
313
            g.drawLine(wi + ini, w, wi + ini, layoutControl.getHeight());
314

    
315
            drawLineY(g, 5, 12, 22, rLayout.getY(), 0);
316
            drawLineX(g, 5, 12, 22, rLayout.getX(), 0);
317

    
318
            if (FLayoutUtilities.fromSheetDistance(1, at) > 15) {
319
                drawLineY(g, 1, 12, 22, rLayout.getY(), 0);
320
                drawLineX(g, 1, 12, 22, rLayout.getX(), 0);
321

    
322
                if (FLayoutUtilities.fromSheetDistance(1, at) > 25) {
323
                    drawLineY(g, 1, 18, 22, rLayout.getY(), 0.5);
324
                    drawLineY(g, 0.1, 21, 22, rLayout.getY(), 0);
325
                    drawLineX(g, 1, 18, 22, rLayout.getX(), 0.5);
326
                    drawLineX(g, 0.1, 21, 22, rLayout.getX(), 0);
327
                }
328
            }
329
        }
330
    }
331

    
332
    /**
333
     * Dibuja sobre el graphics2d las l?neas verticales que representan a las
334
     * unidades de medida.
335
     * 
336
     * @param g
337
     *            Graphics2d sobre el que se dibuja.
338
     * @param dist
339
     *            distancia en cent?metros de una l?nea a otra.
340
     * @param init
341
     *            inicio de la l?nea.
342
     * @param end
343
     *            fin de la l?nea.
344
     * @param y
345
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
346
     * @param desp
347
     *            Desplazamiento.
348
     */
349
    private void drawLineY(Graphics2D g, double dist, int init, int end,
350
        double y, double desp) {
351
        AffineTransform at = layoutPanel.getLayoutContext().getAT();
352
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
353

    
354
        double distY = FLayoutUtilities.fromSheetDistance(dist, at);
355
        double rota = Math.toRadians(90);
356

    
357
        if (distY > 4) {
358
            double despY = FLayoutUtilities.fromSheetDistance(desp, at);
359

    
360
            double posUnitY = y + despY;
361
            double posUnitYNeg = posUnitY;
362
            int num = 0;
363
            double iniY = 40;
364
            Point2D.Double pfin =
365
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
366
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
367

    
368
            while (posUnitY < (pfin.y - 5)) {
369
                posUnitYNeg -= distY;
370

    
371
                if (distY > 16) {
372
                    if (init == 12) {
373
                        if (posUnitY > iniY) {
374
                            g.rotate(-rota, 20, posUnitY - 12);
375
                            g.drawString(String.valueOf(num), 10,
376
                                (int) posUnitY - 12);
377
                            g.rotate(rota, 20, posUnitY - 12);
378
                        }
379

    
380
                        if (dist == 5) {
381
                            num = num + 5;
382
                        } else {
383
                            num++;
384
                        }
385

    
386
                        if (posUnitYNeg > iniY) {
387
                            g.rotate(-rota, 20, posUnitYNeg - 12);
388
                            g.drawString(String.valueOf(-num), 10,
389
                                (int) posUnitYNeg - 12);
390
                            g.rotate(rota, 20, posUnitYNeg - 12);
391
                        }
392
                    }
393
                }
394

    
395
                if (posUnitY > iniY) {
396
                    g.drawLine(2 + init, (int) posUnitY, 2 + end,
397
                        (int) posUnitY);
398
                }
399

    
400
                if (posUnitYNeg > iniY) {
401
                    g.drawLine(2 + init, (int) posUnitYNeg, 2 + end,
402
                        (int) posUnitYNeg);
403
                }
404

    
405
                posUnitY += distY;
406
            }
407
        }
408
    }
409

    
410
    /**
411
     * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
412
     * unidades de medida.
413
     * 
414
     * @param g
415
     *            Graphics2d sobre el que se dibuja.
416
     * @param dist
417
     *            distancia en cent?metros de una l?nea a otra.
418
     * @param init
419
     *            inicio de la l?nea.
420
     * @param end
421
     *            fin de la l?nea.
422
     * @param x
423
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
424
     * @param desp
425
     *            Desplazamiento.
426
     */
427
    private void drawLineX(Graphics2D g, double dist, int init, int end,
428
        double x, double desp) {
429
        AffineTransform at = layoutPanel.getLayoutContext().getAT();
430
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
431

    
432
        double distX = FLayoutUtilities.fromSheetDistance(dist, at);
433

    
434
        if (distX > 4) {
435
            double despX = FLayoutUtilities.fromSheetDistance(desp, at);
436
            double posUnitX = x + despX;
437
            double posUnitXNeg = posUnitX;
438
            int num = 0;
439
            double iniX = 40;
440
            Point2D.Double pfin =
441
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
442
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
443

    
444
            while (posUnitX < (pfin.x - 5)) {
445
                posUnitXNeg -= distX;
446

    
447
                if (init == 12) {
448
                    if (distX > 16) {
449
                        if (posUnitX > iniX) {
450
                            g.drawString(String.valueOf(num),
451
                                (int) posUnitX + 3, 20);
452
                        }
453

    
454
                        if (dist == 5) {
455
                            num = num + 5;
456
                        } else {
457
                            num++;
458
                        }
459

    
460
                        if (posUnitXNeg > iniX) {
461
                            g.drawString(String.valueOf(-num),
462
                                (int) posUnitXNeg + 3, 20);
463
                        }
464
                    }
465
                }
466

    
467
                if (posUnitX > iniX) {
468
                    g.drawLine((int) posUnitX, 2 + init, (int) posUnitX,
469
                        2 + end);
470
                }
471

    
472
                if (posUnitXNeg > iniX) {
473
                    g.drawLine((int) posUnitXNeg, 2 + init, (int) posUnitXNeg,
474
                        2 + end);
475
                }
476

    
477
                posUnitX += distX;
478
            }
479
        }
480
    }
481

    
482
    /**
483
     * Dibuja los handlers sobre los fframes que esten seleccionados.
484
     * 
485
     * @param g
486
     *            Graphics sobre el que se dibuja.
487
     * @param color
488
     *            Color de los Handlers.
489
     */
490
    public void drawHandlers(Graphics2D g, Color color) {
491
        LayoutContext layoutContext = layoutPanel.getLayoutContext();
492

    
493
        g.setColor(color);
494
        IFFrame[] fframes = layoutContext.getFFrames();
495
        for (int i = 0; i < fframes.length; i++) {
496
            IFFrame fframe = fframes[i];
497

    
498
            if (fframe.getSelected() != IFFrame.NOSELECT) {
499
                fframe.drawHandlers(g);
500
            }
501
        }
502
    }
503

    
504
    /**
505
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
506
     * contenido del Layout.
507
     * 
508
     * @param ps
509
     */
510
    public void toPS(File ps) {
511
        Attributes attributes = layoutPanel.getLayoutContext().getAttributes();
512
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
513

    
514
        try {
515
            // Prepare the output file to receive the postscript
516
            OutputStream fos =
517
                new BufferedOutputStream(new FileOutputStream(ps));
518

    
519
            // Find a factory that can do the conversion
520
            // DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
521
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
522
            StreamPrintServiceFactory[] factories =
523
                StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
524
                    flavor, "application/postscript");
525

    
526
            if (factories.length > 0) {
527
                StreamPrintService service = factories[0].getPrintService(fos);
528

    
529
                // Create the print job
530
                DocPrintJob job = service.createPrintJob();
531
               
532
                Doc doc = new SimpleDoc(this, flavor, null);
533
                // Doc doc = new SimpleDoc(is, flavor, null);
534
                // Monitor print job events; for the implementation of
535
                // PrintJobWatcher,
536
                // see e702 Determining When a Print Job Has Finished
537
                // PrintJobWatcher pjDone = new PrintJobWatcher(job);
538
                // Actualizar attributes
539
                PrintRequestAttributeSet att =
540
                    attributes.toPrintRequestAttributeSet();
541
                // Print it
542
                job.print(doc, att);
543

    
544
                // Wait for the print job to be done
545
                // pjDone.waitForDone();
546
                // It is now safe to close the streams
547
            }
548

    
549
            // is.close();
550
            fos.close();
551
        } catch (PrintException e) {
552
        } catch (IOException e) {
553
        }
554
        /*
555
         * PrintService defaultPrintService =
556
         * PrintServiceLookup.lookupDefaultPrintService();
557
         * DocPrintJob printerJob = defaultPrintService.createPrintJob();
558
         * 
559
         * try {
560
         * DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
561
         * if (!defaultPrintService.isDocFlavorSupported(flavor)) {
562
         * System.err.println(
563
         * "The printer does not support the appropriate DocFlavor");
564
         * }else {
565
         * 
566
         * SimpleDoc simpleDoc;
567
         * simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
568
         * printerJob.print(simpleDoc, null);
569
         * }
570
         * } catch (MalformedURLException e) {
571
         * // TODO Auto-generated catch block
572
         * e.printStackTrace();
573
         * } catch (PrintException e) {
574
         * // TODO Auto-generated catch block
575
         * e.printStackTrace();
576
         * }
577
         */
578
        layoutControl.fullRect();
579
    }
580

    
581
    /**
582
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
583
     * contenido del Layout.
584
     * 
585
     * @param pdf
586
     */
587
    public void toPDF(File pdf) {
588
        Attributes attributes = layoutPanel.getLayoutContext().getAttributes();
589
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
590

    
591
        double w = 0;
592
        double h = 0;
593
        Document document = new Document();
594

    
595
        if (attributes.isLandSpace()) {
596
            w =
597
                ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
598
            h =
599
                ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
600
        } else {
601
            w =
602
                ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
603
            h =
604
                ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
605
        }
606

    
607
        document.setPageSize(new com.lowagie.text.Rectangle((float) w,
608
            (float) h));
609

    
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
                if (attributes.isLandSpace()) {
620
                    g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)),
621
                        0 + (h / 2));
622
                    print(g2, new PageFormat(), 0);
623
                    g2.rotate(Math.toRadians(90), 0 + (w / (h / w)),
624
                        0 + (h / 2));
625
                } else {
626
                    print(g2, new PageFormat(), 0);
627
                }
628
            } catch (PrinterException e) {
629
                e.printStackTrace();
630
            }
631

    
632
            g2.dispose();
633

    
634
        } catch (DocumentException de) {
635
            System.err.println(de.getMessage());
636
        } catch (IOException ioe) {
637
            JOptionPane.showMessageDialog(
638
                (Component) PluginServices.getMainFrame(), ioe.getMessage());
639
        }
640

    
641
        document.close();
642

    
643
        layoutControl.fullRect();
644
    }
645
    
646
    public int print(Graphics g, PageFormat format, int pi)
647
        throws PrinterException {
648
        if (pi >= 1) {
649
            return Printable.NO_SUCH_PAGE;
650
        }
651

    
652
        Graphics2D g2d = (Graphics2D) g;
653

    
654
        AffineTransform at = g2d.getTransform();
655
        g2d.translate(0, 0);
656
        layoutPanel.obtainRect(true);
657

    
658
        g2d.scale((double) 72 / (double) (Attributes.DPI), (double) 72
659
            / (double) (Attributes.DPI));
660
        System.err.println("Clip 2 =" + g2d.getClip());
661

    
662
        if (layoutPanel.getLayoutContext().getAttributes().isMargin()) {
663
            g2d.setClip(
664
                (int) (layoutPanel.getLayoutControl().getRect().getMinX() + FLayoutUtilities
665
                    .fromSheetDistance(
666
                        layoutPanel.getLayoutContext().getAttributes().m_area[2], layoutPanel
667
                        .getLayoutControl().getAT())),
668
                        (int) (layoutPanel.getLayoutControl().getRect().getMinY() + FLayoutUtilities
669
                            .fromSheetDistance(
670
                                layoutPanel.getLayoutContext().getAttributes().m_area[0], layoutPanel
671
                                .getLayoutControl().getAT())),
672
                                (int) (layoutPanel.getLayoutControl().getRect().getWidth() - FLayoutUtilities
673
                                    .fromSheetDistance(
674
                                        layoutPanel.getLayoutContext().getAttributes().m_area[2]
675
                                                                                    + layoutPanel.getLayoutContext().getAttributes().m_area[3], layoutPanel
676
                                                                                    .getLayoutControl().getAT())),
677
                                                                                    (int) (layoutPanel.getLayoutControl().getRect().getHeight() - FLayoutUtilities
678
                                                                                        .fromSheetDistance(
679
                                                                                            layoutPanel.getLayoutContext().getAttributes().m_area[0]
680
                                                                                                                                        + layoutPanel.getLayoutContext().getAttributes().m_area[1], layoutPanel
681
                                                                                                                                        .getLayoutControl().getAT())));
682
        }
683

    
684

    
685
        drawShapes(g2d);
686
        g2d.setTransform(at);
687

    
688
        return Printable.PAGE_EXISTS;
689
    }
690
    
691
    /**
692
     * Se dibuja sobre el graphics el Layout.
693
     * 
694
     * @param g2
695
     *            graphics sobre el que se dibuja.
696
     */
697
    public void drawShapes(Graphics2D g2) {
698
        layoutPanel.drawLayoutPrint(g2);
699
    }
700

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

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

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