Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutDraw.java @ 28368

History | View | Annotate | Download (27.5 KB)

1 7304 caballero
/*
2
 * Created on 27-sep-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout;
46
47
import java.awt.Color;
48
import java.awt.Component;
49
import java.awt.Graphics2D;
50 28231 vcaballero
import java.awt.Image;
51 7304 caballero
import java.awt.geom.AffineTransform;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54
import java.awt.image.BufferedImage;
55
import java.awt.print.PageFormat;
56
import java.awt.print.PrinterException;
57
import java.io.BufferedOutputStream;
58
import java.io.File;
59
import java.io.FileOutputStream;
60
import java.io.IOException;
61
import java.io.OutputStream;
62
63
import javax.print.Doc;
64
import javax.print.DocFlavor;
65
import javax.print.DocPrintJob;
66
import javax.print.PrintException;
67
import javax.print.SimpleDoc;
68
import javax.print.StreamPrintService;
69
import javax.print.StreamPrintServiceFactory;
70
import javax.print.attribute.PrintRequestAttributeSet;
71
import javax.swing.JOptionPane;
72
73 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
74 7304 caballero
import com.iver.andami.PluginServices;
75
import com.iver.cit.gvsig.Print;
76
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
77 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
78 7304 caballero
import com.lowagie.text.Document;
79
import com.lowagie.text.DocumentException;
80
import com.lowagie.text.pdf.PdfContentByte;
81
import com.lowagie.text.pdf.PdfWriter;
82
83
84
/**
85
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
86
 *
87
 * @author Vicente Caballero Navarro
88
 */
89
public class FLayoutDraw {
90
    private Layout layout;
91
92 28231 vcaballero
93 7304 caballero
    /**
94
     * Crea un nuevo FLayoutDraw.
95
     *
96
     * @param l Referencia al Layout.
97
     */
98
    public FLayoutDraw(Layout l) {
99
        layout = l;
100
    }
101
102
    /**
103
     * M?todo para dibujar el Layout y modificar la matriz de transformaci?n  a
104
     * partir del tama?o en pixels que tenga rect y el formato de papel
105
     * seleccionado.
106
     *
107
     * @param g2
108
     * @param imgBase Si es null, est?s imprimiendo. Si no, la usas para el
109
     *        c?digo de  optimizaci?n.
110
     *
111 10661 caballero
     * @throws ReadDriverException
112 7304 caballero
     */
113
    public void drawLayout(Graphics2D g2, BufferedImage imgBase)
114 10626 caballero
        throws ReadDriverException {
115 28231 vcaballero
            LayoutControl layoutControl=layout.getLayoutControl();
116
            LayoutContext layoutContext= layout.getLayoutContext();
117
            Attributes attributes=layoutContext.getAtributes();
118
            Rectangle2D rLayout=layoutControl.getRect();
119
            AffineTransform at=layoutContext.getAT();
120
121
            layoutControl.setCancelDrawing(false);
122 7304 caballero
        double scale = 0;
123 28231 vcaballero
        scale = rLayout.getHeight() / attributes.m_sizePaper.getAlto() * 1;
124 7304 caballero
        AffineTransform escalado = new AffineTransform();
125
        AffineTransform translacion = new AffineTransform();
126 28231 vcaballero
        translacion.setToTranslation(rLayout.getMinX(),
127
            rLayout.getMinY());
128 7304 caballero
        escalado.setToScale(scale, scale);
129 28231 vcaballero
        at.setToIdentity();
130
        at.concatenate(translacion);
131
        at.concatenate(escalado);
132
        attributes.setDistanceUnitX(rLayout);
133
        attributes.setDistanceUnitY(rLayout);
134
        IFFrame[] fframes=layoutContext.getFFrames();
135 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
136
                IFFrame f = fframes[i];
137 9593 caballero
                //original, lento (NO MAC)
138 28231 vcaballero
                    f.draw(g2, at, layoutControl.getVisibleRect(),imgBase);
139 7304 caballero
                // ESTILO MAC
140
                /* if (f instanceof FFrameView)
141
                {
142
                        FFrameView fframe = (FFrameView) f;
143 28231 vcaballero
                        BufferedImage img = new BufferedImage((int) layoutControl.getWidth(),
144
                                    (int) layoutControl.getHeight(), BufferedImage.TYPE_INT_ARGB);
145 7304 caballero

146

147 28231 vcaballero
                        fframe.draw(img.createGraphics(), at,        layoutControl.getVisibleRect(), imgBase);
148
                        g2.drawImage(img, 0, 0, layoutControl);
149 7304 caballero
                        fframe.setBufferedImage(img);
150
                }
151
                else
152
                {
153 28231 vcaballero
                        f.draw(g2, at, layoutControl.getVisibleRect(),imgBase);
154 7304 caballero
                } */
155
156
            //          Dibuja el s?mbolo de que contiene un tag.
157 9303 caballero
            if ((f.getTag() != null) && (layout.isShowIconTag())) {
158 7304 caballero
                f.drawSymbolTag(g2);
159
            }
160
        }
161
162
        if (!(fframes.length==0)) {
163 28231 vcaballero
            layoutControl.setStatus(LayoutControl.ACTUALIZADO);
164 7304 caballero
        } else {
165 28231 vcaballero
            layoutControl.setStatus(LayoutControl.DESACTUALIZADO);
166 7304 caballero
        }
167
    }
168
169
    /**
170
     * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
171
     *
172
     * @param g Graphics2D
173
     */
174
    public void drawRectangle(Graphics2D g) {
175 28231 vcaballero
            Attributes attributes=layout.getLayoutContext().getAtributes();
176
            Rectangle2D rLayout=layout.getLayoutControl().getRect();
177
            AffineTransform at=layout.getLayoutContext().getAT();
178 7304 caballero
179 28231 vcaballero
            double unidadesX = attributes.getNumUnitsX();
180
        double unidadesY = attributes.getNumUnitsY();
181
182 7304 caballero
        if ((unidadesX == 0) && (unidadesY == 0)) {
183
            return;
184
        }
185
186
        g.setColor(Color.darkGray);
187
188 28231 vcaballero
        Rectangle2D.Double rectBig = new Rectangle2D.Double(rLayout.getX(),
189
                rLayout.getY(), rLayout.getWidth(),
190
                rLayout.getHeight());
191 7304 caballero
192 28231 vcaballero
        g.fillRect((int) rectBig.x + 7, (int) rectBig.y + 7,
193
            (int) rectBig.width, (int) rectBig.height);
194 7304 caballero
195
        Rectangle2D.Double r = new Rectangle2D.Double();
196
197 28231 vcaballero
        if (attributes.isMargin()) {
198
199
            r = new Rectangle2D.Double((rLayout.getX() +
200 7304 caballero
                    FLayoutUtilities.fromSheetDistance(
201 28231 vcaballero
                        attributes.m_area[2], at)),
202
                    (rLayout.getY() +
203 7304 caballero
                    FLayoutUtilities.fromSheetDistance(
204 28231 vcaballero
                        attributes.m_area[0], at)),
205
                    (rLayout.getWidth() -
206
                    FLayoutUtilities.fromSheetDistance(
207
                            attributes.m_area[2] +
208
                        attributes.m_area[3], at)),
209
                    (rLayout.getHeight() -
210
                    FLayoutUtilities.fromSheetDistance(
211
                            attributes.m_area[0] +
212
                        attributes.m_area[1], at)));
213 7304 caballero
        } else {
214 28231 vcaballero
            r.setRect(rLayout);
215 7304 caballero
        }
216
217
        g.setColor(Color.white);
218 28231 vcaballero
        g.fill(rLayout);
219 7304 caballero
220
        g.setColor(Color.darkGray);
221 28231 vcaballero
        g.drawRect((int) rLayout.getMinX(),
222
            (int) rLayout.getMinY(),
223
            (int) rLayout.getWidth(),
224
            (int) rLayout.getHeight());
225 7304 caballero
226 28231 vcaballero
        if (attributes.isMargin()) {
227 7304 caballero
            g.setColor(Color.black);
228
229
            g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
230
        }
231
    }
232
233
    /**
234
     * DOCUMENT ME!
235
     *
236
     * @param g DOCUMENT ME!
237
     */
238
    public void drawGrid(Graphics2D g) {
239
        int unidadesMin = 6;
240 28231 vcaballero
        LayoutContext layoutContext= layout.getLayoutContext();
241
        Attributes attributes=layoutContext.getAtributes();
242
            Rectangle2D rLayout=layout.getLayoutControl().getRect();
243
            AffineTransform at=layoutContext.getAT();
244 7304 caballero
245 28231 vcaballero
246
            double unidadesX = attributes.getUnitInPixelsX();
247
        double unidadesY = attributes.getUnitInPixelsY();
248
249 7304 caballero
        Rectangle2D.Double r = new Rectangle2D.Double();
250
251 28231 vcaballero
        if (attributes.isMargin()) {
252
            r = new Rectangle2D.Double((rLayout.getX() +
253 7304 caballero
                    FLayoutUtilities.fromSheetDistance(
254 28231 vcaballero
                        attributes.m_area[2], at)),
255
                    (rLayout.getY() +
256 7304 caballero
                    FLayoutUtilities.fromSheetDistance(
257 28231 vcaballero
                        attributes.m_area[0], at)),
258
                    (rLayout.getWidth() -
259
                    FLayoutUtilities.fromSheetDistance(attributes.m_area[2] +
260
                        attributes.m_area[3], at)),
261
                    (rLayout.getHeight() -
262
                    FLayoutUtilities.fromSheetDistance(attributes.m_area[0] +
263
                        attributes.m_area[1], at)));
264 7304 caballero
        } else {
265 28231 vcaballero
            r.setRect(rLayout);
266 7304 caballero
        }
267 10851 caballero
        if ((unidadesX == 0) && (unidadesY == 0)) {
268
            return;
269
        }
270 7304 caballero
        g.setColor(Color.darkGray);
271
272
        if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin)) &&
273 28231 vcaballero
                layoutContext.isGridVisible()) {
274 7304 caballero
            double ax = r.getMinX();
275
            double ay;
276
277
            while (ax < (r.getMaxX())) {
278
                ay = (r.getMinY());
279
280
                while (ay < (r.getMaxY())) {
281
                    g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
282
                    ay = ay + unidadesY;
283
                }
284
285
                ax = ax + unidadesX;
286
            }
287
        }
288
    }
289
290
    /**
291
     * Dibuja sobre el graphics2d las reglas.
292
     *
293
     * @param g graphics2d sobre el que se dibuja.
294
     * @param color Color de la regla.
295
     */
296
    public void drawRuler(Graphics2D g, Color color) {
297 28231 vcaballero
            LayoutControl layoutControl=layout.getLayoutControl();
298
            LayoutContext layoutContext= layout.getLayoutContext();
299
            Attributes attributes=layoutContext.getAtributes();
300
            Rectangle2D rLayout=layoutControl.getRect();
301
            AffineTransform at=layoutContext.getAT();
302
303
        if (layoutContext.getRuler()) {
304 7304 caballero
            int ini = 10;
305
            int w = 30;
306
            int wi = 16;
307
308
            g.setColor(new Color(250, 255, 250, 180));
309 28231 vcaballero
            g.fillRect(ini, w, wi, layoutControl.getHeight() - w);
310
            g.fillRect(w, ini, layoutControl.getWidth() - w, wi);
311 7304 caballero
312
            g.setColor(new Color(100, 155, 150, 180));
313 28231 vcaballero
            g.fillRect(w, ini, (int) rLayout.getX() - w, wi);
314
            g.fillRect((int) rLayout.getMaxX(), ini,
315
                layoutControl.getWidth() - (int) rLayout.getMaxX(), wi);
316 7304 caballero
317 28231 vcaballero
            g.fillRect(ini, w, wi, (int) rLayout.getY() - w);
318
            g.fillRect(ini, (int) rLayout.getMaxY(), wi,
319
                layoutControl.getHeight() - (int) rLayout.getMaxY());
320 7304 caballero
321 28231 vcaballero
            if (attributes.isMargin()) {
322 7304 caballero
                g.setColor(new Color(50, 55, 50, 180));
323 28231 vcaballero
                g.fillRect((int) rLayout.getX(), ini,
324 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
325 28231 vcaballero
                        attributes.m_area[2], at), wi);
326
                g.fillRect((int) rLayout.getMaxX() -
327 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
328 28231 vcaballero
                        attributes.m_area[3], at), ini,
329 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
330 28231 vcaballero
                        attributes.m_area[3], at), wi);
331 7304 caballero
332 28231 vcaballero
                g.fillRect(ini, (int) rLayout.getY(), wi,
333 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
334 28231 vcaballero
                        attributes.m_area[0], at));
335 7304 caballero
                g.fillRect(ini,
336 28231 vcaballero
                    (int) rLayout.getMaxY() -
337 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
338 28231 vcaballero
                        attributes.m_area[1], at), wi,
339 7304 caballero
                    (int) FLayoutUtilities.fromSheetDistance(
340 28231 vcaballero
                        attributes.m_area[1], at));
341 7304 caballero
            }
342
343
            g.setColor(color);
344 28231 vcaballero
            g.drawLine(w, wi + ini, layoutControl.getWidth(), wi + ini);
345
            g.drawLine(w, ini, layoutControl.getWidth(), ini);
346
            g.drawLine(ini, w, ini, layoutControl.getHeight());
347
            g.drawLine(wi + ini, w, wi + ini, layoutControl.getHeight());
348 7304 caballero
349 28231 vcaballero
            drawLineY(g, 5, 12, 22, rLayout.getY(), 0);
350
            drawLineX(g, 5, 12, 22, rLayout.getX(), 0);
351 7304 caballero
352 28231 vcaballero
            if (FLayoutUtilities.fromSheetDistance(1, at) > 15) {
353
                drawLineY(g, 1, 12, 22, rLayout.getY(), 0);
354
                drawLineX(g, 1, 12, 22, rLayout.getX(), 0);
355 7304 caballero
356 28231 vcaballero
                if (FLayoutUtilities.fromSheetDistance(1, at) > 25) {
357
                    drawLineY(g, 1, 18, 22, rLayout.getY(), 0.5);
358
                    drawLineY(g, 0.1, 21, 22, rLayout.getY(), 0);
359
                    drawLineX(g, 1, 18, 22, rLayout.getX(), 0.5);
360
                    drawLineX(g, 0.1, 21, 22, rLayout.getX(), 0);
361 7304 caballero
                }
362
            }
363
        }
364
    }
365
366
    /**
367
     * Dibuja sobre el graphics2d las l?neas verticales que representan a las
368
     * unidades de medida.
369
     *
370
     * @param g Graphics2d sobre el que se dibuja.
371
     * @param dist distancia en cent?metros de una l?nea a otra.
372
     * @param init inicio de la l?nea.
373
     * @param end fin de la l?nea.
374
     * @param y rect?ngulo, dentro del cual se dibujan las l?neas.
375
     * @param desp Desplazamiento.
376
     */
377
    private void drawLineY(Graphics2D g, double dist, int init, int end,
378
        double y, double desp) {
379 28231 vcaballero
            AffineTransform at=layout.getLayoutContext().getAT();
380
            LayoutControl layoutControl=layout.getLayoutControl();
381
382
            double distY = FLayoutUtilities.fromSheetDistance(dist, at);
383 7304 caballero
        double rota = Math.toRadians(90);
384
385
        if (distY > 4) {
386
            double despY = FLayoutUtilities.fromSheetDistance(desp,
387 28231 vcaballero
                    at);
388 7304 caballero
389
            double posUnitY = y + despY;
390
            double posUnitYNeg = posUnitY;
391
            int num = 0;
392
            double iniY = 40;
393
            Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
394 28231 vcaballero
                        layoutControl.getWidth(), layoutControl.getHeight()), at);
395 7304 caballero
396
            while (posUnitY < (pfin.y - 5)) {
397
                posUnitYNeg -= distY;
398
399
                if (distY > 16) {
400
                    if (init == 12) {
401
                        if (posUnitY > iniY) {
402
                            g.rotate(-rota, 20, posUnitY - 12);
403
                            g.drawString(String.valueOf(num), 10,
404
                                (int) posUnitY - 12);
405
                            g.rotate(rota, 20, posUnitY - 12);
406
                        }
407
408
                        if (dist == 5) {
409
                            num = num + 5;
410
                        } else {
411
                            num++;
412
                        }
413
414
                        if (posUnitYNeg > iniY) {
415
                            g.rotate(-rota, 20, posUnitYNeg - 12);
416
                            g.drawString(String.valueOf(-num), 10,
417
                                (int) posUnitYNeg - 12);
418
                            g.rotate(rota, 20, posUnitYNeg - 12);
419
                        }
420
                    }
421
                }
422
423
                if (posUnitY > iniY) {
424
                    g.drawLine( 2 + init, (int) posUnitY, 2 + end,
425
                        (int) posUnitY);
426
                }
427
428
                if (posUnitYNeg > iniY) {
429
                    g.drawLine(2 + init, (int) posUnitYNeg,
430
                        2 + end, (int) posUnitYNeg);
431
                }
432
433
                posUnitY += distY;
434
            }
435
        }
436
    }
437
438
    /**
439
     * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
440
     * unidades de medida.
441
     *
442
     * @param g Graphics2d sobre el que se dibuja.
443
     * @param dist distancia en cent?metros de una l?nea a otra.
444
     * @param init inicio de la l?nea.
445
     * @param end fin de la l?nea.
446
     * @param x rect?ngulo, dentro del cual se dibujan las l?neas.
447
     * @param desp Desplazamiento.
448
     */
449
    private void drawLineX(Graphics2D g, double dist, int init, int end,
450
        double x, double desp) {
451 28231 vcaballero
            AffineTransform at=layout.getLayoutContext().getAT();
452
            LayoutControl layoutControl=layout.getLayoutControl();
453 7304 caballero
454 28231 vcaballero
            double distX = FLayoutUtilities.fromSheetDistance(dist, at);
455
456 7304 caballero
        if (distX > 4) {
457
            double despX = FLayoutUtilities.fromSheetDistance(desp,
458 28231 vcaballero
                    at);
459 7304 caballero
            double posUnitX = x + despX;
460
            double posUnitXNeg = posUnitX;
461
            int num = 0;
462
            double iniX = 40;
463
            Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
464 28231 vcaballero
                        layoutControl.getWidth(), layoutControl.getHeight()), at);
465 7304 caballero
466
            while (posUnitX < (pfin.x - 5)) {
467
                posUnitXNeg -= distX;
468
469
                if (init == 12) {
470
                    if (distX > 16) {
471
                        if (posUnitX > iniX) {
472
                            g.drawString(String.valueOf(num),
473
                                (int) posUnitX + 3, 20);
474
                        }
475
476
                        if (dist == 5) {
477
                            num = num + 5;
478
                        } else {
479
                            num++;
480
                        }
481
482
                        if (posUnitXNeg > iniX) {
483
                            g.drawString(String.valueOf(-num),
484
                                (int) posUnitXNeg + 3, 20);
485
                        }
486
                    }
487
                }
488
489
                if (posUnitX > iniX) {
490
                    g.drawLine((int) posUnitX, 2 + init, (int) posUnitX,
491
                        2 + end);
492
                }
493
494
                if (posUnitXNeg > iniX) {
495
                    g.drawLine((int) posUnitXNeg, 2 + init,
496
                        (int) posUnitXNeg, 2 + end);
497
                }
498
499
                posUnitX += distX;
500
            }
501
        }
502
    }
503
504
    /**
505
     * Dibuja los handlers sobre los fframes que esten seleccionados.
506
     *
507
     * @param g Graphics sobre el que se dibuja.
508
     * @param color Color de los Handlers.
509
     */
510
    public void drawHandlers(Graphics2D g, Color color) {
511 28231 vcaballero
            LayoutContext layoutContext= layout.getLayoutContext();
512
513 7304 caballero
        g.setColor(color);
514 28231 vcaballero
        IFFrame[] fframes=layoutContext.getFFrames();
515 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
516
            IFFrame fframe = fframes[i];
517
518
            if (fframe.getSelected() != IFFrame.NOSELECT) {
519
                fframe.drawHandlers(g);
520
            }
521
        }
522
    }
523
524
    /**
525
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
526
     * contenido del Layout.
527
     *
528
     * @param pdf
529
     */
530
 /*   public void toPS(File ps) {
531

532
             try {
533
                 // Open the image file
534
                // InputStream is = new BufferedInputStream(
535
                //     new FileInputStream("filename.gif"));
536

537
                 // Prepare the output file to receive the postscript
538
                 OutputStream fos = new BufferedOutputStream(
539
                     new FileOutputStream("filename.ps"));
540

541
                 // Find a factory that can do the conversion
542
                 //DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
543
                 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
544
                 StreamPrintServiceFactory[] factories =
545
                     StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
546
                         flavor,
547
                         DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
548

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

552
                     // Create the print job
553
                     DocPrintJob job = service.createPrintJob();
554
                     Print print= (Print)PluginServices.getExtension(
555
                               com.iver.cit.gvsig.Print.class);
556
                     print.setLayout(layout);
557
                     Doc doc = new SimpleDoc((Printable)print, flavor, null);
558
                     //Doc doc = new SimpleDoc(is, flavor, null);
559
                     // Monitor print job events; for the implementation of PrintJobWatcher,
560
                     // see e702 Determining When a Print Job Has Finished
561
                     //PrintJobWatcher pjDone = new PrintJobWatcher(job);
562
//                   Actualizar attributes
563
                      PrintRequestAttributeSet att = layout.getAtributes()
564
                                                           .toPrintAttributes();
565
                     // Print it
566
                     job.print(doc, att);
567

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

573
                // is.close();
574
                 fos.close();
575
             } catch (PrintException e) {
576
             } catch (IOException e) {
577
             }
578
             }
579
             */
580
581
/*
582
             public void printLayout(Layout layout) {
583
                 l = layout;
584

585
                 try {
586
                     printerJob.setPrintable((Printable) PluginServices.getExtension(
587
                             com.iver.cit.gvsig.Print.class));
588

589
                     //Actualizar attributes
590
                     PrintRequestAttributeSet att = layout.getAtributes()
591
                                                          .toPrintAttributes();
592
                     DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
593

594
                     if (m_cachePrintServices == null) {
595
                         m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
596
                                 null);
597
                     }
598

599
                     PrintService defaultService = null;
600

601
                     if (m_cachePrintService == null) {
602
                         defaultService = PrintServiceLookup.lookupDefaultPrintService();
603
                     }
604

605
                     if (m_cachePrintService == null) {
606
                         m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
607
                                 m_cachePrintServices, defaultService, flavor, att);
608
                     }
609

610
                     if (m_cachePrintService != null) {
611
                         DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
612
                         PrintJobListener pjlistener = new PrintJobAdapter() {
613
                                 public void printDataTransferCompleted(PrintJobEvent e) {
614
                                     System.out.println("Fin de impresi?n");
615
                                 }
616
                             };
617

618
                         jobNuevo.addPrintJobListener(pjlistener);
619

620
                         Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
621
                                     com.iver.cit.gvsig.Print.class), flavor, null);
622
                         jobNuevo.print(doc, att);
623
                     }
624
                 } catch (PrintException pe) {
625
                     pe.printStackTrace();
626
                 }
627
             }
628

629
        document.close();
630

631
        layout.fullRect();
632
    }
633
*/
634
635
636
637
    /**
638
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
639
     * contenido del Layout.
640
     *
641
     * @param ps
642
     */
643
    public void toPS(File ps) {
644 28231 vcaballero
            Attributes attributes=layout.getLayoutContext().getAtributes();
645
            LayoutControl layoutControl=layout.getLayoutControl();
646
647 7304 caballero
            try {
648
            // Prepare the output file to receive the postscript
649
            OutputStream fos = new BufferedOutputStream(
650
                new FileOutputStream(ps));
651
652
            // Find a factory that can do the conversion
653
            //DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
654
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
655
            StreamPrintServiceFactory [] factories =
656
                 StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
657
                       "application/postscript");
658
659
            if (factories.length > 0) {
660
                StreamPrintService service = factories[0].getPrintService(fos);
661
662
                // Create the print job
663
                DocPrintJob job = service.createPrintJob();
664 14379 vcaballero
                Print print= new Print();//(Print)PluginServices.getExtension(
665
//                            com.iver.cit.gvsig.Print.class);
666 7304 caballero
                print.setLayout(layout);
667
                Doc doc = new SimpleDoc(print, flavor, null);
668
                //Doc doc = new SimpleDoc(is, flavor, null);
669
                // Monitor print job events; for the implementation of PrintJobWatcher,
670
                // see e702 Determining When a Print Job Has Finished
671
                //PrintJobWatcher pjDone = new PrintJobWatcher(job);
672
//              Actualizar attributes
673 28231 vcaballero
                 PrintRequestAttributeSet att = attributes
674 7304 caballero
                                                      .toPrintAttributes();
675
                // Print it
676
                job.print(doc, att);
677
678
                // Wait for the print job to be done
679
                //pjDone.waitForDone();
680
                // It is now safe to close the streams
681
            }
682
683
           // is.close();
684
            fos.close();
685
        } catch (PrintException e) {
686
        } catch (IOException e) {
687
        }
688
    /*    PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
689
        DocPrintJob printerJob = defaultPrintService.createPrintJob();
690

691
        try {
692
                DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
693
                if (!defaultPrintService.isDocFlavorSupported(flavor)) {
694
                           System.err.println("The printer does not support the appropriate DocFlavor");
695
                }else {
696

697
                SimpleDoc simpleDoc;
698
            simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
699
            printerJob.print(simpleDoc, null);
700
                }
701
        } catch (MalformedURLException e) {
702
            // TODO Auto-generated catch block
703
            e.printStackTrace();
704
        } catch (PrintException e) {
705
            // TODO Auto-generated catch block
706
            e.printStackTrace();
707
        }
708
        */
709 28231 vcaballero
         layoutControl.fullRect();
710 7304 caballero
    }
711
712
713
    /**
714
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
715
     * contenido del Layout.
716
     *
717
     * @param pdf
718
     */
719
    public void toPDF(File pdf) {
720 28231 vcaballero
            Attributes attributes=layout.getLayoutContext().getAtributes();
721
            LayoutControl layoutControl=layout.getLayoutControl();
722
723
            double w = 0;
724 7304 caballero
        double h = 0;
725
        Document document = new Document();
726
727 28231 vcaballero
        if (attributes.isLandSpace()) {
728
            w = ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
729
            h = ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
730 7304 caballero
        } else {
731 28231 vcaballero
            w = ((attributes.m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
732
            h = ((attributes.m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
733 7304 caballero
        }
734
735
        document.setPageSize(new com.lowagie.text.Rectangle((float) w, (float) h));
736
737
        try {
738
                FileOutputStream fos=new FileOutputStream(pdf);
739
            PdfWriter writer = PdfWriter.getInstance(document,fos);
740
            document.open();
741
742
            Print print = new Print();
743
            print.setLayout(layout);
744
745
            PdfContentByte cb = writer.getDirectContent();
746
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
747
748
            try {
749 28231 vcaballero
                if (attributes.isLandSpace()) {
750 7304 caballero
                    g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)),
751
                        0 + (h / 2));
752
                    print.print(g2, new PageFormat(), 0);
753
                    g2.rotate(Math.toRadians(90), 0 + (w / (h / w)), 0 +
754
                        (h / 2));
755
                } else {
756
                    print.print(g2, new PageFormat(), 0);
757
                }
758
            } catch (PrinterException e) {
759
                e.printStackTrace();
760
            }
761
762
            g2.dispose();
763
764
        } catch (DocumentException de) {
765
            System.err.println(de.getMessage());
766
        } catch (IOException ioe) {
767
            JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
768
                ioe.getMessage());
769
        }
770
771
        document.close();
772
773 28231 vcaballero
        layoutControl.fullRect();
774 7304 caballero
      }
775
}