svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 4928
History | View | Annotate | Download (8.59 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 | 1219 | vcaballero | import com.iver.andami.PluginServices; |
50 | import com.iver.andami.plugins.Extension; |
||
51 | import com.iver.andami.ui.mdiManager.View; |
||
52 | |||
53 | import com.iver.cit.gvsig.gui.layout.Attributes; |
||
54 | import com.iver.cit.gvsig.gui.layout.FLayoutUtilities; |
||
55 | import com.iver.cit.gvsig.gui.layout.Layout; |
||
56 | |||
57 | 312 | fernando | import java.awt.Graphics; |
58 | import java.awt.Graphics2D; |
||
59 | import java.awt.geom.AffineTransform; |
||
60 | import java.awt.geom.Rectangle2D; |
||
61 | import java.awt.print.PageFormat; |
||
62 | import java.awt.print.Printable; |
||
63 | import java.awt.print.PrinterException; |
||
64 | import java.awt.print.PrinterJob; |
||
65 | |||
66 | 724 | vcaballero | import javax.print.Doc; |
67 | import javax.print.DocFlavor; |
||
68 | import javax.print.DocPrintJob; |
||
69 | import javax.print.PrintException; |
||
70 | import javax.print.PrintService; |
||
71 | import javax.print.PrintServiceLookup; |
||
72 | import javax.print.ServiceUI; |
||
73 | import javax.print.SimpleDoc; |
||
74 | import javax.print.attribute.PrintRequestAttributeSet; |
||
75 | import javax.print.event.PrintJobAdapter; |
||
76 | import javax.print.event.PrintJobEvent; |
||
77 | import javax.print.event.PrintJobListener; |
||
78 | |||
79 | 312 | fernando | |
80 | /**
|
||
81 | * Extensi?n desde la que se imprime.
|
||
82 | *
|
||
83 | * @author Vicente Caballero Navarro
|
||
84 | */
|
||
85 | 596 | fernando | public class Print implements Extension, Printable { |
86 | 1219 | vcaballero | public static PrinterJob printerJob = PrinterJob.getPrinterJob(); |
87 | private static Layout l = null; |
||
88 | 2429 | caballero | //private Paper paper;
|
89 | 1219 | vcaballero | Rectangle2D.Double aux = null; |
90 | private int veces = 0; |
||
91 | private PrintService[] m_cachePrintServices = null; |
||
92 | private PrintService m_cachePrintService = null; |
||
93 | 312 | fernando | |
94 | 1219 | vcaballero | /**
|
95 | * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
||
96 | */
|
||
97 | public void execute(String s) { |
||
98 | l = (Layout) PluginServices.getMDIManager().getActiveView(); |
||
99 | 312 | fernando | |
100 | 2429 | caballero | //PageFormat format = printerJob.defaultPage(); //new PageFormat();
|
101 | //paper = format.getPaper();
|
||
102 | 312 | fernando | |
103 | 1219 | vcaballero | //double margin = 72.0 / 25.4 * 5.0;
|
104 | try {
|
||
105 | PluginServices.backgroundExecution(new Runnable() { |
||
106 | public void run() { |
||
107 | if (l.getAtributes().getType() == Attributes.CUSTOM) { |
||
108 | l.showPrintDialog(printerJob); |
||
109 | } else {
|
||
110 | l.showPrintDialog(null);
|
||
111 | } |
||
112 | } |
||
113 | }); |
||
114 | 312 | fernando | |
115 | 1219 | vcaballero | //Thread.sleep(10000);
|
116 | } catch (Exception e) { |
||
117 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
||
118 | e); |
||
119 | } |
||
120 | } |
||
121 | 2342 | vcaballero | public void setLayout(Layout l){ |
122 | this.l=l;
|
||
123 | } |
||
124 | 1219 | vcaballero | /**
|
125 | * Se dibuja sobre el graphics el Layout.
|
||
126 | *
|
||
127 | * @param g2 graphics sobre el que se dibuja.
|
||
128 | */
|
||
129 | public void drawShapes(Graphics2D g2) { |
||
130 | ///double w = paper.getWidth();
|
||
131 | //System.out.println("w " + w);
|
||
132 | ///double h = paper.getHeight();
|
||
133 | //System.out.println("h " + h);
|
||
134 | //g2.translate(-l.getRect().x, -l.getRect().y);
|
||
135 | l.drawLayoutPrint(g2); |
||
136 | 312 | fernando | |
137 | 1219 | vcaballero | // System.out.println("drawShapes = "+g2.getTransform().getScaleX());
|
138 | //g2.translate(l.getRect().x, l.getRect().y);
|
||
139 | } |
||
140 | 312 | fernando | |
141 | 1219 | vcaballero | /**
|
142 | * @see com.iver.andami.plugins.Extension#isVisible()
|
||
143 | */
|
||
144 | public boolean isVisible() { |
||
145 | View f = PluginServices.getMDIManager().getActiveView();
|
||
146 | 312 | fernando | |
147 | 1219 | vcaballero | if (f == null) { |
148 | return false; |
||
149 | } |
||
150 | 312 | fernando | |
151 | 1219 | vcaballero | return (f.getClass() == Layout.class);
|
152 | } |
||
153 | 312 | fernando | |
154 | 1219 | vcaballero | /**
|
155 | * @see com.iver.mdiApp.plugins.Extension#isEnabled()
|
||
156 | */
|
||
157 | public boolean isEnabled() { |
||
158 | Layout f = (Layout) PluginServices.getMDIManager().getActiveView(); |
||
159 | 312 | fernando | |
160 | 1219 | vcaballero | if (f == null) { |
161 | return false; |
||
162 | } |
||
163 | 312 | fernando | |
164 | 1219 | vcaballero | return true; |
165 | } |
||
166 | 312 | fernando | |
167 | 1219 | vcaballero | /* (non-Javadoc)
|
168 | * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
|
||
169 | */
|
||
170 | public int print(Graphics g, PageFormat format, int pi) |
||
171 | throws PrinterException { |
||
172 | if (pi >= 1) { |
||
173 | return Printable.NO_SUCH_PAGE; |
||
174 | } |
||
175 | 312 | fernando | |
176 | 1219 | vcaballero | System.err.println("Clip 0 = " + g.getClip()); |
177 | 312 | fernando | |
178 | 1219 | vcaballero | Graphics2D g2d = (Graphics2D) g; |
179 | 312 | fernando | |
180 | 1219 | vcaballero | double x = format.getImageableX();
|
181 | double y = format.getImageableY();
|
||
182 | double w = format.getImageableWidth();
|
||
183 | double h = format.getImageableHeight();
|
||
184 | 312 | fernando | |
185 | 1219 | vcaballero | //System.err.println("Orientaci?n en Print: " + format.getOrientation());
|
186 | System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")"); |
||
187 | System.err.println("Clip 1 = " + g2d.getClip()); |
||
188 | 312 | fernando | |
189 | 1219 | vcaballero | AffineTransform at = g2d.getTransform();
|
190 | g2d.translate(0, 0); |
||
191 | l.obtainRect(true);
|
||
192 | 312 | fernando | |
193 | 1219 | vcaballero | //LWSAffineTransform at = g2d.getTransform();
|
194 | g2d.scale((double) 72 / (double) (Attributes.DPI), |
||
195 | (double) 72 / (double) (Attributes.DPI)); |
||
196 | System.err.println("Clip 2 =" + g2d.getClip()); |
||
197 | 312 | fernando | |
198 | 1219 | vcaballero | if (l.getAtributes().isMargin()) {
|
199 | g2d.setClip((int) (l.getRect().getMinX() +
|
||
200 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2],
|
||
201 | l.getAT())), |
||
202 | (int) (l.getRect().getMinY() +
|
||
203 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0],
|
||
204 | l.getAT())), |
||
205 | (int) (l.getRect().getWidth() -
|
||
206 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2] +
|
||
207 | l.getAtributes().m_area[3], l.getAT())),
|
||
208 | (int) (l.getRect().getHeight() -
|
||
209 | FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0] +
|
||
210 | l.getAtributes().m_area[1], l.getAT())));
|
||
211 | } else {
|
||
212 | /*
|
||
213 | Rectangle rec=(Rectangle)g2d.getClipBounds();
|
||
214 | Rectangle2D.Double rec2d=new Rectangle2D.Double();
|
||
215 | rec2d.setRect(rec.x,rec.y,l.getRect().width,rec.getHeight());
|
||
216 | g2d.setClip((int)rec2d.x,(int)rec2d.y,(int)rec2d.width,(int)rec2d.height);
|
||
217 | */
|
||
218 | /* g2d.setClip((int) l.getRect().getMinX(),
|
||
219 | (int) l.getRect().getMinY(), (int) l.getRect().getWidth(),
|
||
220 | (int) l.getRect().getHeight()); */
|
||
221 | } |
||
222 | 312 | fernando | |
223 | 1219 | vcaballero | veces++; |
224 | System.out.println("veces = " + veces); |
||
225 | 312 | fernando | |
226 | 1219 | vcaballero | drawShapes(g2d); |
227 | g2d.setTransform(at); |
||
228 | 312 | fernando | |
229 | 1219 | vcaballero | return Printable.PAGE_EXISTS; |
230 | } |
||
231 | 312 | fernando | |
232 | 596 | fernando | /**
|
233 | * @see com.iver.andami.plugins.Extension#inicializar()
|
||
234 | */
|
||
235 | public void inicializar() { |
||
236 | } |
||
237 | 1219 | vcaballero | |
238 | /**
|
||
239 | * Abre un di?logo para imprimir.
|
||
240 | *
|
||
241 | * @param layout Layout a imprimir.
|
||
242 | */
|
||
243 | 724 | vcaballero | public void OpenDialogToPrint(Layout layout) { |
244 | 1219 | vcaballero | l = layout; |
245 | 724 | vcaballero | |
246 | 2429 | caballero | //PageFormat format = printerJob.defaultPage(); //new PageFormat();
|
247 | //paper = format.getPaper();
|
||
248 | 1219 | vcaballero | |
249 | try {
|
||
250 | if (layout.getAtributes().getType() == Attributes.CUSTOM) { |
||
251 | layout.showPrintDialog(printerJob); |
||
252 | } else {
|
||
253 | layout.showPrintDialog(null);
|
||
254 | } |
||
255 | } catch (Exception e) { |
||
256 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
||
257 | e); |
||
258 | e.printStackTrace(); |
||
259 | } |
||
260 | 724 | vcaballero | } |
261 | 1219 | vcaballero | |
262 | /**
|
||
263 | * Imprime el Layout que se pasa como par?metro.
|
||
264 | *
|
||
265 | * @param layout Layout a imprimir.
|
||
266 | */
|
||
267 | public void printLayout(Layout layout) { |
||
268 | l = layout; |
||
269 | |||
270 | try {
|
||
271 | printerJob.setPrintable((Printable) PluginServices.getExtension(
|
||
272 | com.iver.cit.gvsig.Print.class)); |
||
273 | |||
274 | //Actualizar attributes
|
||
275 | PrintRequestAttributeSet att = layout.getAtributes()
|
||
276 | .toPrintAttributes(); |
||
277 | DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
||
278 | |||
279 | if (m_cachePrintServices == null) { |
||
280 | m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
|
||
281 | null);
|
||
282 | } |
||
283 | |||
284 | PrintService defaultService = null; |
||
285 | |||
286 | if (m_cachePrintService == null) { |
||
287 | defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
||
288 | } |
||
289 | |||
290 | if (m_cachePrintService == null) { |
||
291 | m_cachePrintService = ServiceUI.printDialog(null, 200, 200, |
||
292 | m_cachePrintServices, defaultService, flavor, att); |
||
293 | } |
||
294 | |||
295 | if (m_cachePrintService != null) { |
||
296 | DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
||
297 | PrintJobListener pjlistener = new PrintJobAdapter() { |
||
298 | public void printDataTransferCompleted(PrintJobEvent e) { |
||
299 | System.out.println("Fin de impresi?n"); |
||
300 | } |
||
301 | }; |
||
302 | |||
303 | jobNuevo.addPrintJobListener(pjlistener); |
||
304 | |||
305 | Doc doc = new SimpleDoc((Printable) PluginServices.getExtension( |
||
306 | com.iver.cit.gvsig.Print.class), flavor, null);
|
||
307 | jobNuevo.print(doc, att); |
||
308 | } |
||
309 | } catch (PrintException pe) { |
||
310 | pe.printStackTrace(); |
||
311 | } |
||
312 | } |
||
313 | 312 | fernando | } |