Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 9273

History | View | Annotate | Download (8.61 KB)

1 312 fernando
/*
2
 * Created on 17-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7 1103 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47 312 fernando
package com.iver.cit.gvsig;
48
49
import java.awt.Graphics;
50
import java.awt.Graphics2D;
51
import java.awt.geom.AffineTransform;
52
import java.awt.geom.Rectangle2D;
53
import java.awt.print.PageFormat;
54
import java.awt.print.Printable;
55
import java.awt.print.PrinterException;
56
import java.awt.print.PrinterJob;
57
58 724 vcaballero
import javax.print.Doc;
59
import javax.print.DocFlavor;
60
import javax.print.DocPrintJob;
61
import javax.print.PrintException;
62
import javax.print.PrintService;
63
import javax.print.PrintServiceLookup;
64
import javax.print.ServiceUI;
65
import javax.print.SimpleDoc;
66
import javax.print.attribute.PrintRequestAttributeSet;
67
import javax.print.event.PrintJobAdapter;
68
import javax.print.event.PrintJobEvent;
69
import javax.print.event.PrintJobListener;
70
71 5005 jorpiell
import com.iver.andami.PluginServices;
72
import com.iver.andami.plugins.Extension;
73 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
74 5005 jorpiell
import com.iver.cit.gvsig.gui.layout.Attributes;
75
import com.iver.cit.gvsig.gui.layout.Layout;
76 8765 jjdelcerro
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
77 312 fernando
78 5005 jorpiell
79 312 fernando
/**
80
 * Extensi?n desde la que se imprime.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84 5005 jorpiell
public class Print extends Extension implements Printable {
85 1219 vcaballero
        public static PrinterJob printerJob = PrinterJob.getPrinterJob();
86
        private static Layout l = null;
87 2429 caballero
        //private Paper paper;
88 1219 vcaballero
        Rectangle2D.Double aux = null;
89
        private int veces = 0;
90
        private PrintService[] m_cachePrintServices = null;
91
        private PrintService m_cachePrintService = null;
92 312 fernando
93 1219 vcaballero
        /**
94 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
95 1219 vcaballero
         */
96
        public void execute(String s) {
97 6880 cesar
                l = (Layout) PluginServices.getMDIManager().getActiveWindow();
98 312 fernando
99 2429 caballero
                //PageFormat format = printerJob.defaultPage(); //new PageFormat();
100
                //paper = format.getPaper();
101 312 fernando
102 1219 vcaballero
                //double margin = 72.0 / 25.4 * 5.0;
103
                try {
104
                        PluginServices.backgroundExecution(new Runnable() {
105
                                        public void run() {
106
                                                if (l.getAtributes().getType() == Attributes.CUSTOM) {
107
                                                        l.showPrintDialog(printerJob);
108
                                                } else {
109 5446 fjp
                                                        l.showPrintDialog(null);
110 1219 vcaballero
                                                }
111
                                        }
112
                                });
113 312 fernando
114 1219 vcaballero
                        //Thread.sleep(10000);
115
                } catch (Exception e) {
116
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
117
                                e);
118
                }
119
        }
120 2342 vcaballero
        public void setLayout(Layout l){
121
                this.l=l;
122
        }
123 1219 vcaballero
        /**
124
         * Se dibuja sobre el graphics el Layout.
125
         *
126
         * @param g2 graphics sobre el que se dibuja.
127
         */
128
        public void drawShapes(Graphics2D g2) {
129
                ///double w = paper.getWidth();
130
                //System.out.println("w " + w);
131
                ///double h = paper.getHeight();
132
                //System.out.println("h " + h);
133
                //g2.translate(-l.getRect().x, -l.getRect().y);
134
                l.drawLayoutPrint(g2);
135 312 fernando
136 1219 vcaballero
                // System.out.println("drawShapes = "+g2.getTransform().getScaleX());
137
                //g2.translate(l.getRect().x, l.getRect().y);
138
        }
139 312 fernando
140 1219 vcaballero
        /**
141 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isVisible()
142 1219 vcaballero
         */
143
        public boolean isVisible() {
144 6880 cesar
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
145 312 fernando
146 1219 vcaballero
                if (f == null) {
147
                        return false;
148
                }
149 312 fernando
150 5900 jorpiell
                return (f instanceof Layout);
151 1219 vcaballero
        }
152 312 fernando
153 1219 vcaballero
        /**
154 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isEnabled()
155 1219 vcaballero
         */
156
        public boolean isEnabled() {
157 6880 cesar
                Layout f = (Layout) PluginServices.getMDIManager().getActiveWindow();
158 312 fernando
159 1219 vcaballero
                if (f == null) {
160
                        return false;
161
                }
162 312 fernando
163 1219 vcaballero
                return true;
164
        }
165 312 fernando
166 1219 vcaballero
        /* (non-Javadoc)
167
         * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
168
         */
169
        public int print(Graphics g, PageFormat format, int pi)
170
                throws PrinterException {
171
                if (pi >= 1) {
172
                        return Printable.NO_SUCH_PAGE;
173
                }
174 312 fernando
175 1219 vcaballero
                System.err.println("Clip 0 = " + g.getClip());
176 312 fernando
177 1219 vcaballero
                Graphics2D g2d = (Graphics2D) g;
178 312 fernando
179 1219 vcaballero
                double x = format.getImageableX();
180
                double y = format.getImageableY();
181
                double w = format.getImageableWidth();
182
                double h = format.getImageableHeight();
183 312 fernando
184 1219 vcaballero
                //System.err.println("Orientaci?n en Print: " + format.getOrientation());
185
                System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")");
186
                System.err.println("Clip 1 = " + g2d.getClip());
187 312 fernando
188 1219 vcaballero
                AffineTransform at = g2d.getTransform();
189
                g2d.translate(0, 0);
190
                l.obtainRect(true);
191 312 fernando
192 1219 vcaballero
                //LWSAffineTransform at = g2d.getTransform();
193
                g2d.scale((double) 72 / (double) (Attributes.DPI),
194
                        (double) 72 / (double) (Attributes.DPI));
195
                System.err.println("Clip 2 =" + g2d.getClip());
196 312 fernando
197 1219 vcaballero
                if (l.getAtributes().isMargin()) {
198
                        g2d.setClip((int) (l.getRect().getMinX() +
199
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2],
200
                                        l.getAT())),
201
                                (int) (l.getRect().getMinY() +
202
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0],
203
                                        l.getAT())),
204
                                (int) (l.getRect().getWidth() -
205
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2] +
206
                                        l.getAtributes().m_area[3], l.getAT())),
207
                                (int) (l.getRect().getHeight() -
208
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0] +
209
                                        l.getAtributes().m_area[1], l.getAT())));
210
                } else {
211
                        /*
212
                           Rectangle rec=(Rectangle)g2d.getClipBounds();
213
                           Rectangle2D.Double rec2d=new Rectangle2D.Double();
214
                           rec2d.setRect(rec.x,rec.y,l.getRect().width,rec.getHeight());
215
                           g2d.setClip((int)rec2d.x,(int)rec2d.y,(int)rec2d.width,(int)rec2d.height);
216
                         */
217
                        /* g2d.setClip((int) l.getRect().getMinX(),
218
                           (int) l.getRect().getMinY(), (int) l.getRect().getWidth(),
219
                           (int) l.getRect().getHeight()); */
220
                }
221 312 fernando
222 1219 vcaballero
                veces++;
223
                System.out.println("veces = " + veces);
224 312 fernando
225 1219 vcaballero
                drawShapes(g2d);
226
                g2d.setTransform(at);
227 312 fernando
228 1219 vcaballero
                return Printable.PAGE_EXISTS;
229
        }
230 312 fernando
231 596 fernando
        /**
232 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
233 596 fernando
         */
234 5005 jorpiell
        public void initialize() {
235 596 fernando
        }
236 1219 vcaballero
237
        /**
238
         * Abre un di?logo para imprimir.
239
         *
240
         * @param layout Layout a imprimir.
241
         */
242 724 vcaballero
        public void OpenDialogToPrint(Layout layout) {
243 1219 vcaballero
                l = layout;
244 724 vcaballero
245 2429 caballero
                //PageFormat format = printerJob.defaultPage(); //new PageFormat();
246
                //paper = format.getPaper();
247 1219 vcaballero
248
                try {
249
                        if (layout.getAtributes().getType() == Attributes.CUSTOM) {
250
                                layout.showPrintDialog(printerJob);
251
                        } else {
252
                                layout.showPrintDialog(null);
253
                        }
254
                } catch (Exception e) {
255
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
256
                                e);
257
                        e.printStackTrace();
258
                }
259 724 vcaballero
        }
260 1219 vcaballero
261
        /**
262
         * Imprime el Layout que se pasa como par?metro.
263
         *
264
         * @param layout Layout a imprimir.
265
         */
266
        public void printLayout(Layout layout) {
267
                l = layout;
268
269
                try {
270
                        printerJob.setPrintable((Printable) PluginServices.getExtension(
271
                                        com.iver.cit.gvsig.Print.class));
272
273
                        //Actualizar attributes
274
                        PrintRequestAttributeSet att = layout.getAtributes()
275
                                                                                                 .toPrintAttributes();
276
                        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
277
278
                        if (m_cachePrintServices == null) {
279
                                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
280
                                                null);
281
                        }
282
283
                        PrintService defaultService = null;
284
285
                        if (m_cachePrintService == null) {
286
                                defaultService = PrintServiceLookup.lookupDefaultPrintService();
287
                        }
288
289
                        if (m_cachePrintService == null) {
290
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
291
                                                m_cachePrintServices, defaultService, flavor, att);
292
                        }
293
294
                        if (m_cachePrintService != null) {
295
                                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
296
                                PrintJobListener pjlistener = new PrintJobAdapter() {
297
                                                public void printDataTransferCompleted(PrintJobEvent e) {
298
                                                        System.out.println("Fin de impresi?n");
299
                                                }
300
                                        };
301
302
                                jobNuevo.addPrintJobListener(pjlistener);
303
304
                                Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
305
                                                        com.iver.cit.gvsig.Print.class), flavor, null);
306
                                jobNuevo.print(doc, att);
307
                        }
308
                } catch (PrintException pe) {
309
                        pe.printStackTrace();
310
                }
311
        }
312 312 fernando
}