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

History | View | Annotate | Download (27.2 KB)

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

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

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

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

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

    
65
/**
66
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
67
 * 
68
 * @author Vicente Caballero Navarro
69
 */
70
public class FLayoutDraw implements Observable, Printable{
71

    
72
    private LayoutPanel layoutPanel;
73
    private ObservableHelper observers;
74

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

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

    
107
        layoutControl.setCancelDrawing(false);
108
        double scale = 0;
109
        scale = rLayout.getHeight() / attributes.m_sizePaper.getAlto() * 1;
110
        AffineTransform escalado = new AffineTransform();
111
        AffineTransform translacion = new AffineTransform();
112
        translacion.setToTranslation(rLayout.getMinX(), rLayout.getMinY());
113
        escalado.setToScale(scale, scale);
114
        at.setToIdentity();
115
        at.concatenate(translacion);
116
        at.concatenate(escalado);
117
        attributes.setDistanceUnitX(rLayout);
118
        attributes.setDistanceUnitY(rLayout);
119
        IFFrame[] fframes = layoutContext.getFFrames();
120
        for (int i = 0; i < fframes.length; i++) {
121
            IFFrame f = fframes[i];
122
            f.draw(g2, at, 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
    /**
141
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
142
     * 
143
     * @param g
144
     *            Graphics2D
145
     */
146
    public void drawRectangle(Graphics2D g) {
147
        Attributes attributes = layoutPanel.getLayoutContext().getAttributes();
148
        Rectangle2D rLayout = layoutPanel.getLayoutControl().getRect();
149
        AffineTransform at = layoutPanel.getLayoutContext().getAT();
150

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

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

    
158
        g.setColor(Color.darkGray);
159

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

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

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

    
169
        if (attributes.isMargin()) {
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
611
        try {
612
            FileOutputStream fos = new FileOutputStream(pdf);
613
            PdfWriter writer = PdfWriter.getInstance(document, fos);
614
            document.open();
615
          
616
            PdfContentByte cb = writer.getDirectContent();
617
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
618

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

    
633
            g2.dispose();
634

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

    
642
        document.close();
643

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

    
653
        Graphics2D g2d = (Graphics2D) g;
654

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

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

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

    
685

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

    
689
        return Printable.PAGE_EXISTS;
690
    }
691
    
692
    /**
693
     * Se dibuja sobre el graphics el Layout.
694
     * 
695
     * @param g2
696
     *            graphics sobre el que se dibuja.
697
     */
698
    public void drawShapes(Graphics2D g2) {
699
        LayoutControl layoutControl = layoutPanel.getLayoutControl();
700
        LayoutContext layoutContext = layoutPanel.getLayoutContext();
701
        
702
        layoutControl.setCancelDrawing(false);
703

    
704
        layoutPanel.setCursor(Cursor.getDefaultCursor());
705

    
706
        AffineTransform at = layoutContext.getAT();        
707
        double scale = 0;
708
        scale =
709
            layoutControl.getRect().height
710
                / layoutContext.getAttributes().m_sizePaper.getAlto() * 1;
711
        AffineTransform escalado = new AffineTransform();
712
        AffineTransform translacion = new AffineTransform();
713
        translacion.setToTranslation(layoutControl.getRect().getMinX(),
714
            layoutControl.getRect().getMinY());
715
        escalado.setToScale(scale, scale);
716
        at.setToIdentity();
717
        at.concatenate(translacion);
718
        at.concatenate(escalado);
719
        layoutContext.getAttributes().setDistanceUnitX(layoutControl.getRect());
720
        layoutContext.getAttributes().setDistanceUnitY(layoutControl.getRect());
721
        IFFrame[] fframes = layoutContext.getFFrames();
722
        for (int i = 0; i < fframes.length; i++) {
723
            fframes[i].print(g2, at, null, layoutContext
724
                .getAttributes().toPrintAttributes());
725
        }
726
    }
727

    
728
    public void addObserver(Observer o) {
729
        observers.addObserver(o);        
730
    }
731

    
732
    public void deleteObserver(Observer o) {
733
      observers.deleteObserver(o);        
734
    }
735

    
736
    public void deleteObservers() {
737
       observers.deleteObservers();        
738
    }
739
}