Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutDraw.java @ 24962

History | View | Annotate | Download (29.2 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
import java.awt.geom.AffineTransform;
51
import java.awt.geom.Point2D;
52
import java.awt.geom.Rectangle2D;
53
import java.awt.image.BufferedImage;
54
import java.awt.print.PageFormat;
55
import java.awt.print.PrinterException;
56
import java.io.BufferedOutputStream;
57
import java.io.File;
58
import java.io.FileOutputStream;
59
import java.io.IOException;
60
import java.io.OutputStream;
61
62
import javax.print.Doc;
63
import javax.print.DocFlavor;
64
import javax.print.DocPrintJob;
65
import javax.print.PrintException;
66
import javax.print.SimpleDoc;
67
import javax.print.StreamPrintService;
68
import javax.print.StreamPrintServiceFactory;
69
import javax.print.attribute.PrintRequestAttributeSet;
70
import javax.swing.JOptionPane;
71
72 24962 vcaballero
import org.gvsig.fmap.dal.exception.ReadException;
73 21299 vcaballero
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
    /**
93
     * Crea un nuevo FLayoutDraw.
94
     *
95
     * @param l Referencia al Layout.
96
     */
97
    public FLayoutDraw(Layout l) {
98
        layout = l;
99
    }
100
101
    /**
102
     * M?todo para dibujar el Layout y modificar la matriz de transformaci?n  a
103
     * partir del tama?o en pixels que tenga rect y el formato de papel
104
     * seleccionado.
105
     *
106
     * @param g2
107
     * @param imgBase Si es null, est?s imprimiendo. Si no, la usas para el
108
     *        c?digo de  optimizaci?n.
109
     *
110 10661 caballero
     * @throws ReadDriverException
111 7304 caballero
     */
112
    public void drawLayout(Graphics2D g2, BufferedImage imgBase)
113 21299 vcaballero
        throws ReadException {
114 9392 caballero
        layout.getLayoutControl().setCancelDrawing(false);
115 7304 caballero
        double scale = 0;
116 23069 vcaballero
        scale = layout.getLayoutControl().getRect().height / layout.getLayoutContext().getAttributes().m_sizePaper.getAlto() * 1;
117 7304 caballero
        AffineTransform escalado = new AffineTransform();
118
        AffineTransform translacion = new AffineTransform();
119 9392 caballero
        translacion.setToTranslation(layout.getLayoutControl().getRect().getMinX(),
120
            layout.getLayoutControl().getRect().getMinY());
121 7304 caballero
        escalado.setToScale(scale, scale);
122 9392 caballero
        layout.getLayoutControl().getAT().setToIdentity();
123
        layout.getLayoutControl().getAT().concatenate(translacion);
124
        layout.getLayoutControl().getAT().concatenate(escalado);
125 23069 vcaballero
        layout.getLayoutContext().getAttributes().setDistanceUnitX(layout.getLayoutControl().getRect());
126
        layout.getLayoutContext().getAttributes().setDistanceUnitY(layout.getLayoutControl().getRect());
127 9392 caballero
        IFFrame[] fframes=layout.getLayoutContext().getFFrames();
128 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
129
                IFFrame f = fframes[i];
130 9593 caballero
                //original, lento (NO MAC)
131 9392 caballero
                    f.draw(g2, layout.getLayoutControl().getAT(), layout.getLayoutControl().getVisibleRect(),imgBase);
132 7304 caballero
                // ESTILO MAC
133
                /* if (f instanceof FFrameView)
134
                {
135
                        FFrameView fframe = (FFrameView) f;
136 9392 caballero
                        BufferedImage img = new BufferedImage((int) layout.getLayoutControl().getWidth(),
137
                                    (int) layout.getLayoutControl().getHeight(), BufferedImage.TYPE_INT_ARGB);
138 7304 caballero

139

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

498
             try {
499
                 // Open the image file
500
                // InputStream is = new BufferedInputStream(
501
                //     new FileInputStream("filename.gif"));
502

503
                 // Prepare the output file to receive the postscript
504
                 OutputStream fos = new BufferedOutputStream(
505
                     new FileOutputStream("filename.ps"));
506

507
                 // Find a factory that can do the conversion
508
                 //DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
509
                 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
510
                 StreamPrintServiceFactory[] factories =
511
                     StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
512
                         flavor,
513
                         DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
514

515
                 if (factories.length > 0) {
516
                     StreamPrintService service = factories[0].getPrintService(fos);
517

518
                     // Create the print job
519
                     DocPrintJob job = service.createPrintJob();
520
                     Print print= (Print)PluginServices.getExtension(
521
                               com.iver.cit.gvsig.Print.class);
522
                     print.setLayout(layout);
523
                     Doc doc = new SimpleDoc((Printable)print, flavor, null);
524
                     //Doc doc = new SimpleDoc(is, flavor, null);
525
                     // Monitor print job events; for the implementation of PrintJobWatcher,
526
                     // see e702 Determining When a Print Job Has Finished
527
                     //PrintJobWatcher pjDone = new PrintJobWatcher(job);
528
//                   Actualizar attributes
529
                      PrintRequestAttributeSet att = layout.getAtributes()
530
                                                           .toPrintAttributes();
531
                     // Print it
532
                     job.print(doc, att);
533

534
                     // Wait for the print job to be done
535
                     //pjDone.waitForDone();
536
                     // It is now safe to close the streams
537
                 }
538

539
                // is.close();
540
                 fos.close();
541
             } catch (PrintException e) {
542
             } catch (IOException e) {
543
             }
544
             }
545
             */
546
547
/*
548
             public void printLayout(Layout layout) {
549
                 l = layout;
550

551
                 try {
552
                     printerJob.setPrintable((Printable) PluginServices.getExtension(
553
                             com.iver.cit.gvsig.Print.class));
554

555
                     //Actualizar attributes
556
                     PrintRequestAttributeSet att = layout.getAtributes()
557
                                                          .toPrintAttributes();
558
                     DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
559

560
                     if (m_cachePrintServices == null) {
561
                         m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
562
                                 null);
563
                     }
564

565
                     PrintService defaultService = null;
566

567
                     if (m_cachePrintService == null) {
568
                         defaultService = PrintServiceLookup.lookupDefaultPrintService();
569
                     }
570

571
                     if (m_cachePrintService == null) {
572
                         m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
573
                                 m_cachePrintServices, defaultService, flavor, att);
574
                     }
575

576
                     if (m_cachePrintService != null) {
577
                         DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
578
                         PrintJobListener pjlistener = new PrintJobAdapter() {
579
                                 public void printDataTransferCompleted(PrintJobEvent e) {
580
                                     System.out.println("Fin de impresi?n");
581
                                 }
582
                             };
583

584
                         jobNuevo.addPrintJobListener(pjlistener);
585

586
                         Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
587
                                     com.iver.cit.gvsig.Print.class), flavor, null);
588
                         jobNuevo.print(doc, att);
589
                     }
590
                 } catch (PrintException pe) {
591
                     pe.printStackTrace();
592
                 }
593
             }
594

595
        document.close();
596

597
        layout.fullRect();
598
    }
599
*/
600
601
602
603
    /**
604
     * A partir de un fichero que se pasa como par?metro se crea un ps con el
605
     * contenido del Layout.
606
     *
607
     * @param ps
608
     */
609
    public void toPS(File ps) {
610
            try {
611
            // Prepare the output file to receive the postscript
612
            OutputStream fos = new BufferedOutputStream(
613
                new FileOutputStream(ps));
614
615
            // Find a factory that can do the conversion
616
            //DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
617
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
618
            StreamPrintServiceFactory [] factories =
619
                 StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
620
                       "application/postscript");
621
622
            if (factories.length > 0) {
623
                StreamPrintService service = factories[0].getPrintService(fos);
624
625
                // Create the print job
626
                DocPrintJob job = service.createPrintJob();
627 14379 vcaballero
                Print print= new Print();//(Print)PluginServices.getExtension(
628
//                            com.iver.cit.gvsig.Print.class);
629 7304 caballero
                print.setLayout(layout);
630
                Doc doc = new SimpleDoc(print, flavor, null);
631
                //Doc doc = new SimpleDoc(is, flavor, null);
632
                // Monitor print job events; for the implementation of PrintJobWatcher,
633
                // see e702 Determining When a Print Job Has Finished
634
                //PrintJobWatcher pjDone = new PrintJobWatcher(job);
635
//              Actualizar attributes
636 23069 vcaballero
                 PrintRequestAttributeSet att = layout.getLayoutContext().getAttributes()
637 7304 caballero
                                                      .toPrintAttributes();
638
                // Print it
639
                job.print(doc, att);
640
641
                // Wait for the print job to be done
642
                //pjDone.waitForDone();
643
                // It is now safe to close the streams
644
            }
645
646
           // is.close();
647
            fos.close();
648
        } catch (PrintException e) {
649
        } catch (IOException e) {
650
        }
651
    /*    PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
652
        DocPrintJob printerJob = defaultPrintService.createPrintJob();
653

654
        try {
655
                DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
656
                if (!defaultPrintService.isDocFlavorSupported(flavor)) {
657
                           System.err.println("The printer does not support the appropriate DocFlavor");
658
                }else {
659

660
                SimpleDoc simpleDoc;
661
            simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
662
            printerJob.print(simpleDoc, null);
663
                }
664
        } catch (MalformedURLException e) {
665
            // TODO Auto-generated catch block
666
            e.printStackTrace();
667
        } catch (PrintException e) {
668
            // TODO Auto-generated catch block
669
            e.printStackTrace();
670
        }
671
        */
672 9392 caballero
         layout.getLayoutControl().fullRect();
673 7304 caballero
    }
674
675
676
    /**
677
     * A partir de un fichero que se pasa como par?metro se crea un pdf con el
678
     * contenido del Layout.
679
     *
680
     * @param pdf
681
     */
682
    public void toPDF(File pdf) {
683
        double w = 0;
684
        double h = 0;
685
        Document document = new Document();
686
687 23069 vcaballero
        if (layout.getLayoutContext().getAttributes().isLandSpace()) {
688
            w = ((layout.getLayoutContext().getAttributes().m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
689
            h = ((layout.getLayoutContext().getAttributes().m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
690 7304 caballero
        } else {
691 23069 vcaballero
            w = ((layout.getLayoutContext().getAttributes().m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA);
692
            h = ((layout.getLayoutContext().getAttributes().m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA);
693 7304 caballero
        }
694
695
        document.setPageSize(new com.lowagie.text.Rectangle((float) w, (float) h));
696
697
        try {
698
                FileOutputStream fos=new FileOutputStream(pdf);
699
            PdfWriter writer = PdfWriter.getInstance(document,fos);
700
            document.open();
701
702
            Print print = new Print();
703
            print.setLayout(layout);
704
705
            PdfContentByte cb = writer.getDirectContent();
706
            Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h);
707
708
            try {
709 23069 vcaballero
                if (layout.getLayoutContext().getAttributes().isLandSpace()) {
710 7304 caballero
                    g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)),
711
                        0 + (h / 2));
712
                    print.print(g2, new PageFormat(), 0);
713
                    g2.rotate(Math.toRadians(90), 0 + (w / (h / w)), 0 +
714
                        (h / 2));
715
                } else {
716
                    print.print(g2, new PageFormat(), 0);
717
                }
718
            } catch (PrinterException e) {
719
                e.printStackTrace();
720
            }
721
722
            g2.dispose();
723
724
        } catch (DocumentException de) {
725
            System.err.println(de.getMessage());
726
        } catch (IOException ioe) {
727
            JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
728
                ioe.getMessage());
729
        }
730
731
        document.close();
732
733 9392 caballero
        layout.getLayoutControl().fullRect();
734 7304 caballero
      }
735
}