Statistics
| Revision:

root / 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 @ 36648

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

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

    
49
import com.lowagie.text.Document;
50
import com.lowagie.text.DocumentException;
51
import com.lowagie.text.pdf.PdfContentByte;
52
import com.lowagie.text.pdf.PdfWriter;
53

    
54
import org.gvsig.andami.PluginServices;
55
import org.gvsig.app.extension.Print;
56
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
57
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
58
import org.gvsig.fmap.dal.exception.ReadException;
59

    
60
/**
61
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
62
 * 
63
 * @author Vicente Caballero Navarro
64
 */
65
public class FLayoutDraw {
66

    
67
    private LayoutPanel layout;
68

    
69
    /**
70
     * Crea un nuevo FLayoutDraw.
71
     * 
72
     * @param l
73
     *            Referencia al Layout.
74
     */
75
    public FLayoutDraw(LayoutPanel l) {
76
        layout = l;
77
    }
78

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

    
99
        layoutControl.setCancelDrawing(false);
100
        double scale = 0;
101
        scale = rLayout.getHeight() / attributes.m_sizePaper.getAlto() * 1;
102
        AffineTransform escalado = new AffineTransform();
103
        AffineTransform translacion = new AffineTransform();
104
        translacion.setToTranslation(rLayout.getMinX(), rLayout.getMinY());
105
        escalado.setToScale(scale, scale);
106
        at.setToIdentity();
107
        at.concatenate(translacion);
108
        at.concatenate(escalado);
109
        attributes.setDistanceUnitX(rLayout);
110
        attributes.setDistanceUnitY(rLayout);
111
        IFFrame[] fframes = layoutContext.getFFrames();
112
        for (int i = 0; i < fframes.length; i++) {
113
            IFFrame f = fframes[i];
114
            // original, lento (NO MAC)
115
            f.setLayoutControl(layoutControl);
116
            f.setLayoutContext(layoutContext);
117
            f.draw(g2, at, layoutControl.getComponent().getVisibleRect(),
118
                imgBase);
119
            // ESTILO MAC
120
            /*
121
             * if (f instanceof FFrameView)
122
             * {
123
             * FFrameView fframe = (FFrameView) f;
124
             * BufferedImage img = new BufferedImage((int)
125
             * layoutControl.getWidth(),
126
             * (int) layoutControl.getHeight(), BufferedImage.TYPE_INT_ARGB);
127
             * 
128
             * 
129
             * fframe.draw(img.createGraphics(), at,
130
             * layoutControl.getVisibleRect(), imgBase);
131
             * g2.drawImage(img, 0, 0, layoutControl);
132
             * fframe.setBufferedImage(img);
133
             * }
134
             * else
135
             * {
136
             * f.draw(g2, at, layoutControl.getVisibleRect(),imgBase);
137
             * }
138
             */
139

    
140
            // Dibuja el s?mbolo de que contiene un tag.
141
            if ((f.getTag() != null) && (layout.isShowIconTag())) {
142
                f.drawSymbolTag(g2);
143
            }
144
        }
145

    
146
        if (!(fframes.length == 0)) {
147
            layoutControl.setStatus(LayoutControl.ACTUALIZADO);
148
        } else {
149
            layoutControl.setStatus(LayoutControl.DESACTUALIZADO);
150
        }
151
    }
152

    
153
    /**
154
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
155
     * 
156
     * @param g
157
     *            Graphics2D
158
     */
159
    public void drawRectangle(Graphics2D g) {
160
        Attributes attributes = layout.getLayoutContext().getAttributes();
161
        Rectangle2D rLayout = layout.getLayoutControl().getRect();
162
        AffineTransform at = layout.getLayoutContext().getAT();
163

    
164
        double unidadesX = attributes.getNumUnitsX();
165
        double unidadesY = attributes.getNumUnitsY();
166

    
167
        if ((unidadesX == 0) && (unidadesY == 0)) {
168
            return;
169
        }
170

    
171
        g.setColor(Color.darkGray);
172

    
173
        Rectangle2D.Double rectBig =
174
            new Rectangle2D.Double(rLayout.getX(), rLayout.getY(),
175
                rLayout.getWidth(), rLayout.getHeight());
176

    
177
        g.fillRect((int) rectBig.x + 7, (int) rectBig.y + 7,
178
            (int) rectBig.width, (int) rectBig.height);
179

    
180
        Rectangle2D.Double r = new Rectangle2D.Double();
181

    
182
        if (attributes.isMargin()) {
183

    
184
            r =
185
                new Rectangle2D.Double(
186
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
187
                        attributes.m_area[2], at)),
188
                    (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
189
                        attributes.m_area[0], at)),
190
                    (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
191
                        attributes.m_area[2] + attributes.m_area[3], at)),
192
                    (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
193
                        attributes.m_area[0] + attributes.m_area[1], at)));
194
        } else {
195
            r.setRect(rLayout);
196
        }
197

    
198
        g.setColor(Color.white);
199
        g.fill(rLayout);
200

    
201
        g.setColor(Color.darkGray);
202
        g.drawRect((int) rLayout.getMinX(), (int) rLayout.getMinY(),
203
            (int) rLayout.getWidth(), (int) rLayout.getHeight());
204

    
205
        if (attributes.isMargin()) {
206
            g.setColor(Color.black);
207

    
208
            g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
209
        }
210
    }
211

    
212
    /**
213
     * DOCUMENT ME!
214
     * 
215
     * @param g
216
     *            DOCUMENT ME!
217
     */
218
    public void drawGrid(Graphics2D g) {
219
        int unidadesMin = 6;
220
        LayoutContext layoutContext = layout.getLayoutContext();
221
        Attributes attributes = layoutContext.getAttributes();
222
        Rectangle2D rLayout = layout.getLayoutControl().getRect();
223
        AffineTransform at = layoutContext.getAT();
224

    
225
        double unidadesX = attributes.getUnitInPixelsX();
226
        double unidadesY = attributes.getUnitInPixelsY();
227

    
228
        Rectangle2D.Double r = new Rectangle2D.Double();
229

    
230
        if (attributes.isMargin()) {
231
            r =
232
                new Rectangle2D.Double(
233
                    (rLayout.getX() + FLayoutUtilities.fromSheetDistance(
234
                        attributes.m_area[2], at)),
235
                    (rLayout.getY() + FLayoutUtilities.fromSheetDistance(
236
                        attributes.m_area[0], at)),
237
                    (rLayout.getWidth() - FLayoutUtilities.fromSheetDistance(
238
                        attributes.m_area[2] + attributes.m_area[3], at)),
239
                    (rLayout.getHeight() - FLayoutUtilities.fromSheetDistance(
240
                        attributes.m_area[0] + attributes.m_area[1], at)));
241
        } else {
242
            r.setRect(rLayout);
243
        }
244
        if ((unidadesX == 0) && (unidadesY == 0)) {
245
            return;
246
        }
247
        g.setColor(Color.darkGray);
248

    
249
        if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin))
250
            && layoutContext.isGridVisible()) {
251
            double ax = r.getMinX();
252
            double ay;
253

    
254
            while (ax < (r.getMaxX())) {
255
                ay = (r.getMinY());
256

    
257
                while (ay < (r.getMaxY())) {
258
                    g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
259
                    ay = ay + unidadesY;
260
                }
261

    
262
                ax = ax + unidadesX;
263
            }
264
        }
265
    }
266

    
267
    /**
268
     * Dibuja sobre el graphics2d las reglas.
269
     * 
270
     * @param g
271
     *            graphics2d sobre el que se dibuja.
272
     * @param color
273
     *            Color de la regla.
274
     */
275
    public void drawRuler(Graphics2D g, Color color) {
276
        LayoutControl layoutControl = layout.getLayoutControl();
277
        LayoutContext layoutContext = layout.getLayoutContext();
278
        Attributes attributes = layoutContext.getAttributes();
279
        Rectangle2D rLayout = layoutControl.getRect();
280
        AffineTransform at = layoutContext.getAT();
281

    
282
        if (layoutContext.getRuler()) {
283
            int ini = 10;
284
            int w = 30;
285
            int wi = 16;
286

    
287
            g.setColor(new Color(250, 255, 250, 180));
288
            g.fillRect(ini, w, wi, layoutControl.getComponent().getHeight() - w);
289
            g.fillRect(w, ini, layoutControl.getWidth() - w, wi);
290

    
291
            g.setColor(new Color(100, 155, 150, 180));
292
            g.fillRect(w, ini, (int) rLayout.getX() - w, wi);
293
            g.fillRect((int) rLayout.getMaxX(), ini, layoutControl.getWidth()
294
                - (int) rLayout.getMaxX(), wi);
295

    
296
            g.fillRect(ini, w, wi, (int) rLayout.getY() - w);
297
            g.fillRect(ini, (int) rLayout.getMaxY(), wi,
298
                layoutControl.getHeight() - (int) rLayout.getMaxY());
299

    
300
            if (attributes.isMargin()) {
301
                g.setColor(new Color(50, 55, 50, 180));
302
                g.fillRect((int) rLayout.getX(), ini, (int) FLayoutUtilities
303
                    .fromSheetDistance(attributes.m_area[2], at), wi);
304
                g.fillRect(
305
                    (int) rLayout.getMaxX()
306
                        - (int) FLayoutUtilities.fromSheetDistance(
307
                            attributes.m_area[3], at), ini,
308
                    (int) FLayoutUtilities.fromSheetDistance(
309
                        attributes.m_area[3], at), wi);
310

    
311
                g.fillRect(ini, (int) rLayout.getY(), wi,
312
                    (int) FLayoutUtilities.fromSheetDistance(
313
                        attributes.m_area[0], at));
314
                g.fillRect(
315
                    ini,
316
                    (int) rLayout.getMaxY()
317
                        - (int) FLayoutUtilities.fromSheetDistance(
318
                            attributes.m_area[1], at), wi,
319
                    (int) FLayoutUtilities.fromSheetDistance(
320
                        attributes.m_area[1], at));
321
            }
322

    
323
            g.setColor(color);
324
            g.drawLine(w, wi + ini, layoutControl.getWidth(), wi + ini);
325
            g.drawLine(w, ini, layoutControl.getWidth(), ini);
326
            g.drawLine(ini, w, ini, layoutControl.getHeight());
327
            g.drawLine(wi + ini, w, wi + ini, layoutControl.getHeight());
328

    
329
            drawLineY(g, 5, 12, 22, rLayout.getY(), 0);
330
            drawLineX(g, 5, 12, 22, rLayout.getX(), 0);
331

    
332
            if (FLayoutUtilities.fromSheetDistance(1, at) > 15) {
333
                drawLineY(g, 1, 12, 22, rLayout.getY(), 0);
334
                drawLineX(g, 1, 12, 22, rLayout.getX(), 0);
335

    
336
                if (FLayoutUtilities.fromSheetDistance(1, at) > 25) {
337
                    drawLineY(g, 1, 18, 22, rLayout.getY(), 0.5);
338
                    drawLineY(g, 0.1, 21, 22, rLayout.getY(), 0);
339
                    drawLineX(g, 1, 18, 22, rLayout.getX(), 0.5);
340
                    drawLineX(g, 0.1, 21, 22, rLayout.getX(), 0);
341
                }
342
            }
343
        }
344
    }
345

    
346
    /**
347
     * Dibuja sobre el graphics2d las l?neas verticales que representan a las
348
     * unidades de medida.
349
     * 
350
     * @param g
351
     *            Graphics2d sobre el que se dibuja.
352
     * @param dist
353
     *            distancia en cent?metros de una l?nea a otra.
354
     * @param init
355
     *            inicio de la l?nea.
356
     * @param end
357
     *            fin de la l?nea.
358
     * @param y
359
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
360
     * @param desp
361
     *            Desplazamiento.
362
     */
363
    private void drawLineY(Graphics2D g, double dist, int init, int end,
364
        double y, double desp) {
365
        AffineTransform at = layout.getLayoutContext().getAT();
366
        LayoutControl layoutControl = layout.getLayoutControl();
367

    
368
        double distY = FLayoutUtilities.fromSheetDistance(dist, at);
369
        double rota = Math.toRadians(90);
370

    
371
        if (distY > 4) {
372
            double despY = FLayoutUtilities.fromSheetDistance(desp, at);
373

    
374
            double posUnitY = y + despY;
375
            double posUnitYNeg = posUnitY;
376
            int num = 0;
377
            double iniY = 40;
378
            Point2D.Double pfin =
379
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
380
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
381

    
382
            while (posUnitY < (pfin.y - 5)) {
383
                posUnitYNeg -= distY;
384

    
385
                if (distY > 16) {
386
                    if (init == 12) {
387
                        if (posUnitY > iniY) {
388
                            g.rotate(-rota, 20, posUnitY - 12);
389
                            g.drawString(String.valueOf(num), 10,
390
                                (int) posUnitY - 12);
391
                            g.rotate(rota, 20, posUnitY - 12);
392
                        }
393

    
394
                        if (dist == 5) {
395
                            num = num + 5;
396
                        } else {
397
                            num++;
398
                        }
399

    
400
                        if (posUnitYNeg > iniY) {
401
                            g.rotate(-rota, 20, posUnitYNeg - 12);
402
                            g.drawString(String.valueOf(-num), 10,
403
                                (int) posUnitYNeg - 12);
404
                            g.rotate(rota, 20, posUnitYNeg - 12);
405
                        }
406
                    }
407
                }
408

    
409
                if (posUnitY > iniY) {
410
                    g.drawLine(2 + init, (int) posUnitY, 2 + end,
411
                        (int) posUnitY);
412
                }
413

    
414
                if (posUnitYNeg > iniY) {
415
                    g.drawLine(2 + init, (int) posUnitYNeg, 2 + end,
416
                        (int) posUnitYNeg);
417
                }
418

    
419
                posUnitY += distY;
420
            }
421
        }
422
    }
423

    
424
    /**
425
     * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
426
     * unidades de medida.
427
     * 
428
     * @param g
429
     *            Graphics2d sobre el que se dibuja.
430
     * @param dist
431
     *            distancia en cent?metros de una l?nea a otra.
432
     * @param init
433
     *            inicio de la l?nea.
434
     * @param end
435
     *            fin de la l?nea.
436
     * @param x
437
     *            rect?ngulo, dentro del cual se dibujan las l?neas.
438
     * @param desp
439
     *            Desplazamiento.
440
     */
441
    private void drawLineX(Graphics2D g, double dist, int init, int end,
442
        double x, double desp) {
443
        AffineTransform at = layout.getLayoutContext().getAT();
444
        LayoutControl layoutControl = layout.getLayoutControl();
445

    
446
        double distX = FLayoutUtilities.fromSheetDistance(dist, at);
447

    
448
        if (distX > 4) {
449
            double despX = FLayoutUtilities.fromSheetDistance(desp, at);
450
            double posUnitX = x + despX;
451
            double posUnitXNeg = posUnitX;
452
            int num = 0;
453
            double iniX = 40;
454
            Point2D.Double pfin =
455
                FLayoutUtilities.fromSheetPoint(new Point2D.Double(
456
                    layoutControl.getWidth(), layoutControl.getHeight()), at);
457

    
458
            while (posUnitX < (pfin.x - 5)) {
459
                posUnitXNeg -= distX;
460

    
461
                if (init == 12) {
462
                    if (distX > 16) {
463
                        if (posUnitX > iniX) {
464
                            g.drawString(String.valueOf(num),
465
                                (int) posUnitX + 3, 20);
466
                        }
467

    
468
                        if (dist == 5) {
469
                            num = num + 5;
470
                        } else {
471
                            num++;
472
                        }
473

    
474
                        if (posUnitXNeg > iniX) {
475
                            g.drawString(String.valueOf(-num),
476
                                (int) posUnitXNeg + 3, 20);
477
                        }
478
                    }
479
                }
480

    
481
                if (posUnitX > iniX) {
482
                    g.drawLine((int) posUnitX, 2 + init, (int) posUnitX,
483
                        2 + end);
484
                }
485

    
486
                if (posUnitXNeg > iniX) {
487
                    g.drawLine((int) posUnitXNeg, 2 + init, (int) posUnitXNeg,
488
                        2 + end);
489
                }
490

    
491
                posUnitX += distX;
492
            }
493
        }
494
    }
495

    
496
    /**
497
     * Dibuja los handlers sobre los fframes que esten seleccionados.
498
     * 
499
     * @param g
500
     *            Graphics sobre el que se dibuja.
501
     * @param color
502
     *            Color de los Handlers.
503
     */
504
    public void drawHandlers(Graphics2D g, Color color) {
505
        LayoutContext layoutContext = layout.getLayoutContext();
506

    
507
        g.setColor(color);
508
        IFFrame[] fframes = layoutContext.getFFrames();
509
        for (int i = 0; i < fframes.length; i++) {
510
            IFFrame fframe = fframes[i];
511

    
512
            if (fframe.getSelected() != IFFrame.NOSELECT) {
513
                fframe.drawHandlers(g);
514
            }
515
        }
516
    }
517

    
518
    /**
519
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
520
     * contenido del Layout.
521
     * 
522
     * @param pdf
523
     */
524
    /*
525
     * public void toPS(File ps) {
526
     * 
527
     * try {
528
     * // Open the image file
529
     * // InputStream is = new BufferedInputStream(
530
     * // new FileInputStream("filename.gif"));
531
     * 
532
     * // Prepare the output file to receive the postscript
533
     * OutputStream fos = new BufferedOutputStream(
534
     * new FileOutputStream("filename.ps"));
535
     * 
536
     * // Find a factory that can do the conversion
537
     * //DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
538
     * DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
539
     * StreamPrintServiceFactory[] factories =
540
     * StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
541
     * flavor,
542
     * DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
543
     * 
544
     * if (factories.length > 0) {
545
     * StreamPrintService service = factories[0].getPrintService(fos);
546
     * 
547
     * // Create the print job
548
     * DocPrintJob job = service.createPrintJob();
549
     * Print print= (Print)PluginServices.getExtension(
550
     * com.iver.cit.gvsig.Print.class);
551
     * print.setLayout(layout);
552
     * Doc doc = new SimpleDoc((Printable)print, flavor, null);
553
     * //Doc doc = new SimpleDoc(is, flavor, null);
554
     * // Monitor print job events; for the implementation of PrintJobWatcher,
555
     * // see e702 Determining When a Print Job Has Finished
556
     * //PrintJobWatcher pjDone = new PrintJobWatcher(job);
557
     * // Actualizar attributes
558
     * PrintRequestAttributeSet att = layout.getAtributes()
559
     * .toPrintAttributes();
560
     * // Print it
561
     * job.print(doc, att);
562
     * 
563
     * // Wait for the print job to be done
564
     * //pjDone.waitForDone();
565
     * // It is now safe to close the streams
566
     * }
567
     * 
568
     * // is.close();
569
     * fos.close();
570
     * } catch (PrintException e) {
571
     * } catch (IOException e) {
572
     * }
573
     * }
574
     */
575

    
576
    /*
577
     * public void printLayout(Layout layout) {
578
     * l = layout;
579
     * 
580
     * try {
581
     * printerJob.setPrintable((Printable) PluginServices.getExtension(
582
     * com.iver.cit.gvsig.Print.class));
583
     * 
584
     * //Actualizar attributes
585
     * PrintRequestAttributeSet att = layout.getAtributes()
586
     * .toPrintAttributes();
587
     * DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
588
     * 
589
     * if (m_cachePrintServices == null) {
590
     * m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
591
     * null);
592
     * }
593
     * 
594
     * PrintService defaultService = null;
595
     * 
596
     * if (m_cachePrintService == null) {
597
     * defaultService = PrintServiceLookup.lookupDefaultPrintService();
598
     * }
599
     * 
600
     * if (m_cachePrintService == null) {
601
     * m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
602
     * m_cachePrintServices, defaultService, flavor, att);
603
     * }
604
     * 
605
     * if (m_cachePrintService != null) {
606
     * DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
607
     * PrintJobListener pjlistener = new PrintJobAdapter() {
608
     * public void printDataTransferCompleted(PrintJobEvent e) {
609
     * System.out.println("Fin de impresi?n");
610
     * }
611
     * };
612
     * 
613
     * jobNuevo.addPrintJobListener(pjlistener);
614
     * 
615
     * Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
616
     * com.iver.cit.gvsig.Print.class), flavor, null);
617
     * jobNuevo.print(doc, att);
618
     * }
619
     * } catch (PrintException pe) {
620
     * pe.printStackTrace();
621
     * }
622
     * }
623
     * 
624
     * document.close();
625
     * 
626
     * layout.fullRect();
627
     * }
628
     */
629

    
630
    /**
631
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
632
     * contenido del Layout.
633
     * 
634
     * @param ps
635
     */
636
    public void toPS(File ps) {
637
        Attributes attributes = layout.getLayoutContext().getAttributes();
638
        LayoutControl layoutControl = layout.getLayoutControl();
639

    
640
        try {
641
            // Prepare the output file to receive the postscript
642
            OutputStream fos =
643
                new BufferedOutputStream(new FileOutputStream(ps));
644

    
645
            // Find a factory that can do the conversion
646
            // DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
647
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
648
            StreamPrintServiceFactory[] factories =
649
                StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
650
                    flavor, "application/postscript");
651

    
652
            if (factories.length > 0) {
653
                StreamPrintService service = factories[0].getPrintService(fos);
654

    
655
                // Create the print job
656
                DocPrintJob job = service.createPrintJob();
657
                Print print = new Print();// (Print)PluginServices.getExtension(
658
                // com.iver.cit.gvsig.Print.class);
659
                print.setLayout(layout);
660
                Doc doc = new SimpleDoc(print, flavor, null);
661
                // Doc doc = new SimpleDoc(is, flavor, null);
662
                // Monitor print job events; for the implementation of
663
                // PrintJobWatcher,
664
                // see e702 Determining When a Print Job Has Finished
665
                // PrintJobWatcher pjDone = new PrintJobWatcher(job);
666
                // Actualizar attributes
667
                PrintRequestAttributeSet att =
668
                    attributes.toPrintRequestAttributeSet();
669
                // Print it
670
                job.print(doc, att);
671

    
672
                // Wait for the print job to be done
673
                // pjDone.waitForDone();
674
                // It is now safe to close the streams
675
            }
676

    
677
            // is.close();
678
            fos.close();
679
        } catch (PrintException e) {
680
        } catch (IOException e) {
681
        }
682
        /*
683
         * PrintService defaultPrintService =
684
         * PrintServiceLookup.lookupDefaultPrintService();
685
         * DocPrintJob printerJob = defaultPrintService.createPrintJob();
686
         * 
687
         * try {
688
         * DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
689
         * if (!defaultPrintService.isDocFlavorSupported(flavor)) {
690
         * System.err.println(
691
         * "The printer does not support the appropriate DocFlavor");
692
         * }else {
693
         * 
694
         * SimpleDoc simpleDoc;
695
         * simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
696
         * printerJob.print(simpleDoc, null);
697
         * }
698
         * } catch (MalformedURLException e) {
699
         * // TODO Auto-generated catch block
700
         * e.printStackTrace();
701
         * } catch (PrintException e) {
702
         * // TODO Auto-generated catch block
703
         * e.printStackTrace();
704
         * }
705
         */
706
        layoutControl.fullRect();
707
    }
708

    
709
    /**
710
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
711
     * contenido del Layout.
712
     * 
713
     * @param pdf
714
     */
715
    public void toPDF(File pdf) {
716
        Attributes attributes = layout.getLayoutContext().getAttributes();
717
        LayoutControl layoutControl = layout.getLayoutControl();
718

    
719
        double w = 0;
720
        double h = 0;
721
        Document document = new Document();
722

    
723
        if (attributes.isLandSpace()) {
724
            w =
725
                ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
726
            h =
727
                ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
728
        } else {
729
            w =
730
                ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
731
            h =
732
                ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
733
        }
734

    
735
        document.setPageSize(new com.lowagie.text.Rectangle((float) w,
736
            (float) h));
737

    
738
        try {
739
            FileOutputStream fos = new FileOutputStream(pdf);
740
            PdfWriter writer = PdfWriter.getInstance(document, fos);
741
            document.open();
742

    
743
            Print print = new Print();
744
            print.setLayout(layout);
745

    
746
            PdfContentByte cb = writer.getDirectContent();
747
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
748

    
749
            try {
750
                if (attributes.isLandSpace()) {
751
                    g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)),
752
                        0 + (h / 2));
753
                    print.print(g2, new PageFormat(), 0);
754
                    g2.rotate(Math.toRadians(90), 0 + (w / (h / w)),
755
                        0 + (h / 2));
756
                } else {
757
                    print.print(g2, new PageFormat(), 0);
758
                }
759
            } catch (PrinterException e) {
760
                e.printStackTrace();
761
            }
762

    
763
            g2.dispose();
764

    
765
        } catch (DocumentException de) {
766
            System.err.println(de.getMessage());
767
        } catch (IOException ioe) {
768
            JOptionPane.showMessageDialog(
769
                (Component) PluginServices.getMainFrame(), ioe.getMessage());
770
        }
771

    
772
        document.close();
773

    
774
        layoutControl.fullRect();
775
    }
776
}