root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 9212
History | View | Annotate | Download (9.13 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 | 7318 | caballero | import com.iver.cit.gvsig.gui.layout.Attributes; |
75 | 5005 | jorpiell | import com.iver.cit.gvsig.gui.layout.Layout; |
76 | 7304 | caballero | 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 | 9212 | fdiaz | doPrint((Layout) PluginServices.getMDIManager().getActiveWindow()); |
98 | /*
|
||
99 | 6880 | cesar | l = (Layout) PluginServices.getMDIManager().getActiveWindow();
|
100 | 312 | fernando | |
101 | 2429 | caballero | //PageFormat format = printerJob.defaultPage(); //new PageFormat();
|
102 | //paper = format.getPaper();
|
||
103 | 312 | fernando | |
104 | 1219 | vcaballero | //double margin = 72.0 / 25.4 * 5.0;
|
105 | try {
|
||
106 | PluginServices.backgroundExecution(new Runnable() {
|
||
107 | public void run() {
|
||
108 | if (l.getAtributes().getType() == Attributes.CUSTOM) {
|
||
109 | l.showPrintDialog(printerJob);
|
||
110 | } else {
|
||
111 | 5446 | fjp | l.showPrintDialog(null);
|
112 | 1219 | vcaballero | }
|
113 | }
|
||
114 | });
|
||
115 | 312 | fernando | |
116 | 1219 | vcaballero | //Thread.sleep(10000);
|
117 | } catch (Exception e) {
|
||
118 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
|
||
119 | e);
|
||
120 | }
|
||
121 | 9212 | fdiaz | */
|
122 | 1219 | vcaballero | } |
123 | 9212 | fdiaz | |
124 | public void doPrint(Layout layout) { |
||
125 | l = layout; |
||
126 | try {
|
||
127 | PluginServices.backgroundExecution(new Runnable() { |
||
128 | public void run() { |
||
129 | if (l.getAtributes().getType() == Attributes.CUSTOM) { |
||
130 | l.showPrintDialog(printerJob); |
||
131 | } else {
|
||
132 | l.showPrintDialog(null);
|
||
133 | } |
||
134 | } |
||
135 | }); |
||
136 | |||
137 | //Thread.sleep(10000);
|
||
138 | } catch (Exception e) { |
||
139 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
||
140 | e); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | 2342 | vcaballero | public void setLayout(Layout l){ |
145 | this.l=l;
|
||
146 | } |
||
147 | 1219 | vcaballero | /**
|
148 | * Se dibuja sobre el graphics el Layout.
|
||
149 | *
|
||
150 | * @param g2 graphics sobre el que se dibuja.
|
||
151 | */
|
||
152 | public void drawShapes(Graphics2D g2) { |
||
153 | ///double w = paper.getWidth();
|
||
154 | //System.out.println("w " + w);
|
||
155 | ///double h = paper.getHeight();
|
||
156 | //System.out.println("h " + h);
|
||
157 | //g2.translate(-l.getRect().x, -l.getRect().y);
|
||
158 | l.drawLayoutPrint(g2); |
||
159 | 312 | fernando | |
160 | 1219 | vcaballero | // System.out.println("drawShapes = "+g2.getTransform().getScaleX());
|
161 | //g2.translate(l.getRect().x, l.getRect().y);
|
||
162 | } |
||
163 | 312 | fernando | |
164 | 1219 | vcaballero | /**
|
165 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
166 | 1219 | vcaballero | */
|
167 | public boolean isVisible() { |
||
168 | 6880 | cesar | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
169 | 312 | fernando | |
170 | 1219 | vcaballero | if (f == null) { |
171 | return false; |
||
172 | } |
||
173 | 312 | fernando | |
174 | 5900 | jorpiell | return (f instanceof Layout); |
175 | 1219 | vcaballero | } |
176 | 312 | fernando | |
177 | 1219 | vcaballero | /**
|
178 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#isEnabled()
|
179 | 1219 | vcaballero | */
|
180 | public boolean isEnabled() { |
||
181 | 6880 | cesar | Layout f = (Layout) PluginServices.getMDIManager().getActiveWindow(); |
182 | 312 | fernando | |
183 | 1219 | vcaballero | if (f == null) { |
184 | return false; |
||
185 | } |
||
186 | 312 | fernando | |
187 | 1219 | vcaballero | return true; |
188 | } |
||
189 | 312 | fernando | |
190 | 1219 | vcaballero | /* (non-Javadoc)
|
191 | * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
|
||
192 | */
|
||
193 | public int print(Graphics g, PageFormat format, int pi) |
||
194 | throws PrinterException { |
||
195 | if (pi >= 1) { |
||
196 | return Printable.NO_SUCH_PAGE; |
||
197 | } |
||
198 | 312 | fernando | |
199 | 1219 | vcaballero | System.err.println("Clip 0 = " + g.getClip()); |
200 | 312 | fernando | |
201 | 1219 | vcaballero | Graphics2D g2d = (Graphics2D) g; |
202 | 312 | fernando | |
203 | 1219 | vcaballero | double x = format.getImageableX();
|
204 | double y = format.getImageableY();
|
||
205 | double w = format.getImageableWidth();
|
||
206 | double h = format.getImageableHeight();
|
||
207 | 312 | fernando | |
208 | 1219 | vcaballero | //System.err.println("Orientaci?n en Print: " + format.getOrientation());
|
209 | System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")"); |
||
210 | System.err.println("Clip 1 = " + g2d.getClip()); |
||
211 | 312 | fernando | |
212 | 1219 | vcaballero | AffineTransform at = g2d.getTransform();
|
213 | g2d.translate(0, 0); |
||
214 | l.obtainRect(true);
|
||
215 | 312 | fernando | |
216 | 1219 | vcaballero | //LWSAffineTransform at = g2d.getTransform();
|
217 | g2d.scale((double) 72 / (double) (Attributes.DPI), |
||
218 | (double) 72 / (double) (Attributes.DPI)); |
||
219 | System.err.println("Clip 2 =" + g2d.getClip()); |
||
220 | 312 | fernando | |
221 | 1219 | vcaballero | if (l.getAtributes().isMargin()) {
|
222 | g2d.setClip((int) (l.getRect().getMinX() +
|
||
223 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2],
|
||
224 | l.getAT())), |
||
225 | (int) (l.getRect().getMinY() +
|
||
226 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0],
|
||
227 | l.getAT())), |
||
228 | (int) (l.getRect().getWidth() -
|
||
229 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2] +
|
||
230 | l.getAtributes().m_area[3], l.getAT())),
|
||
231 | (int) (l.getRect().getHeight() -
|
||
232 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0] +
|
||
233 | l.getAtributes().m_area[1], l.getAT())));
|
||
234 | } else {
|
||
235 | /*
|
||
236 | Rectangle rec=(Rectangle)g2d.getClipBounds();
|
||
237 | Rectangle2D.Double rec2d=new Rectangle2D.Double();
|
||
238 | rec2d.setRect(rec.x,rec.y,l.getRect().width,rec.getHeight());
|
||
239 | g2d.setClip((int)rec2d.x,(int)rec2d.y,(int)rec2d.width,(int)rec2d.height);
|
||
240 | */
|
||
241 | /* g2d.setClip((int) l.getRect().getMinX(),
|
||
242 | (int) l.getRect().getMinY(), (int) l.getRect().getWidth(),
|
||
243 | (int) l.getRect().getHeight()); */
|
||
244 | } |
||
245 | 312 | fernando | |
246 | 1219 | vcaballero | veces++; |
247 | System.out.println("veces = " + veces); |
||
248 | 312 | fernando | |
249 | 1219 | vcaballero | drawShapes(g2d); |
250 | g2d.setTransform(at); |
||
251 | 312 | fernando | |
252 | 1219 | vcaballero | return Printable.PAGE_EXISTS; |
253 | } |
||
254 | 312 | fernando | |
255 | 596 | fernando | /**
|
256 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
257 | 596 | fernando | */
|
258 | 5005 | jorpiell | public void initialize() { |
259 | 596 | fernando | } |
260 | 1219 | vcaballero | |
261 | /**
|
||
262 | * Abre un di?logo para imprimir.
|
||
263 | *
|
||
264 | * @param layout Layout a imprimir.
|
||
265 | */
|
||
266 | 724 | vcaballero | public void OpenDialogToPrint(Layout layout) { |
267 | 1219 | vcaballero | l = layout; |
268 | 724 | vcaballero | |
269 | 2429 | caballero | //PageFormat format = printerJob.defaultPage(); //new PageFormat();
|
270 | //paper = format.getPaper();
|
||
271 | 1219 | vcaballero | |
272 | try {
|
||
273 | if (layout.getAtributes().getType() == Attributes.CUSTOM) { |
||
274 | layout.showPrintDialog(printerJob); |
||
275 | } else {
|
||
276 | layout.showPrintDialog(null);
|
||
277 | } |
||
278 | } catch (Exception e) { |
||
279 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
||
280 | e); |
||
281 | e.printStackTrace(); |
||
282 | } |
||
283 | 724 | vcaballero | } |
284 | 1219 | vcaballero | |
285 | /**
|
||
286 | * Imprime el Layout que se pasa como par?metro.
|
||
287 | *
|
||
288 | * @param layout Layout a imprimir.
|
||
289 | */
|
||
290 | public void printLayout(Layout layout) { |
||
291 | l = layout; |
||
292 | |||
293 | try {
|
||
294 | printerJob.setPrintable((Printable) PluginServices.getExtension(
|
||
295 | com.iver.cit.gvsig.Print.class)); |
||
296 | |||
297 | //Actualizar attributes
|
||
298 | PrintRequestAttributeSet att = layout.getAtributes()
|
||
299 | .toPrintAttributes(); |
||
300 | DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
||
301 | |||
302 | if (m_cachePrintServices == null) { |
||
303 | m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
|
||
304 | null);
|
||
305 | } |
||
306 | |||
307 | PrintService defaultService = null; |
||
308 | |||
309 | if (m_cachePrintService == null) { |
||
310 | defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
||
311 | } |
||
312 | |||
313 | if (m_cachePrintService == null) { |
||
314 | m_cachePrintService = ServiceUI.printDialog(null, 200, 200, |
||
315 | m_cachePrintServices, defaultService, flavor, att); |
||
316 | } |
||
317 | |||
318 | if (m_cachePrintService != null) { |
||
319 | DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
||
320 | PrintJobListener pjlistener = new PrintJobAdapter() { |
||
321 | public void printDataTransferCompleted(PrintJobEvent e) { |
||
322 | System.out.println("Fin de impresi?n"); |
||
323 | } |
||
324 | }; |
||
325 | |||
326 | jobNuevo.addPrintJobListener(pjlistener); |
||
327 | |||
328 | Doc doc = new SimpleDoc((Printable) PluginServices.getExtension( |
||
329 | com.iver.cit.gvsig.Print.class), flavor, null);
|
||
330 | jobNuevo.print(doc, att); |
||
331 | } |
||
332 | } catch (PrintException pe) { |
||
333 | pe.printStackTrace(); |
||
334 | } |
||
335 | } |
||
336 | 312 | fernando | } |