svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 2147
History | View | Annotate | Download (8.56 KB)
1 |
/*
|
---|---|
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 |
/* 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 |
package com.iver.cit.gvsig; |
48 |
|
49 |
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 |
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.Paper; |
63 |
import java.awt.print.Printable; |
64 |
import java.awt.print.PrinterException; |
65 |
import java.awt.print.PrinterJob; |
66 |
|
67 |
import javax.print.Doc; |
68 |
import javax.print.DocFlavor; |
69 |
import javax.print.DocPrintJob; |
70 |
import javax.print.PrintException; |
71 |
import javax.print.PrintService; |
72 |
import javax.print.PrintServiceLookup; |
73 |
import javax.print.ServiceUI; |
74 |
import javax.print.SimpleDoc; |
75 |
import javax.print.attribute.PrintRequestAttributeSet; |
76 |
import javax.print.event.PrintJobAdapter; |
77 |
import javax.print.event.PrintJobEvent; |
78 |
import javax.print.event.PrintJobListener; |
79 |
|
80 |
|
81 |
/**
|
82 |
* Extensi?n desde la que se imprime.
|
83 |
*
|
84 |
* @author Vicente Caballero Navarro
|
85 |
*/
|
86 |
public class Print implements Extension, Printable { |
87 |
public static PrinterJob printerJob = PrinterJob.getPrinterJob(); |
88 |
private static Layout l = null; |
89 |
private Paper paper; |
90 |
Rectangle2D.Double aux = null; |
91 |
private int veces = 0; |
92 |
private PrintService[] m_cachePrintServices = null; |
93 |
private PrintService m_cachePrintService = null; |
94 |
|
95 |
/**
|
96 |
* @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
97 |
*/
|
98 |
public void execute(String s) { |
99 |
l = (Layout) PluginServices.getMDIManager().getActiveView(); |
100 |
|
101 |
PageFormat format = printerJob.defaultPage(); //new PageFormat(); |
102 |
paper = format.getPaper(); |
103 |
|
104 |
//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 |
l.showPrintDialog(null);
|
112 |
} |
113 |
} |
114 |
}); |
115 |
|
116 |
//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 |
} |
122 |
|
123 |
/**
|
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 |
|
136 |
// System.out.println("drawShapes = "+g2.getTransform().getScaleX());
|
137 |
//g2.translate(l.getRect().x, l.getRect().y);
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.iver.andami.plugins.Extension#isVisible()
|
142 |
*/
|
143 |
public boolean isVisible() { |
144 |
View f = PluginServices.getMDIManager().getActiveView();
|
145 |
|
146 |
if (f == null) { |
147 |
return false; |
148 |
} |
149 |
|
150 |
return (f.getClass() == Layout.class);
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* @see com.iver.mdiApp.plugins.Extension#isEnabled()
|
155 |
*/
|
156 |
public boolean isEnabled() { |
157 |
Layout f = (Layout) PluginServices.getMDIManager().getActiveView(); |
158 |
|
159 |
if (f == null) { |
160 |
return false; |
161 |
} |
162 |
|
163 |
return true; |
164 |
} |
165 |
|
166 |
/* (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 |
|
175 |
System.err.println("Clip 0 = " + g.getClip()); |
176 |
|
177 |
Graphics2D g2d = (Graphics2D) g; |
178 |
|
179 |
double x = format.getImageableX();
|
180 |
double y = format.getImageableY();
|
181 |
double w = format.getImageableWidth();
|
182 |
double h = format.getImageableHeight();
|
183 |
|
184 |
//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 |
|
188 |
AffineTransform at = g2d.getTransform();
|
189 |
g2d.translate(0, 0); |
190 |
l.obtainRect(true);
|
191 |
|
192 |
//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 |
|
197 |
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 |
|
222 |
veces++; |
223 |
System.out.println("veces = " + veces); |
224 |
|
225 |
drawShapes(g2d); |
226 |
g2d.setTransform(at); |
227 |
|
228 |
return Printable.PAGE_EXISTS; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @see com.iver.andami.plugins.Extension#inicializar()
|
233 |
*/
|
234 |
public void inicializar() { |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Abre un di?logo para imprimir.
|
239 |
*
|
240 |
* @param layout Layout a imprimir.
|
241 |
*/
|
242 |
public void OpenDialogToPrint(Layout layout) { |
243 |
l = layout; |
244 |
|
245 |
PageFormat format = printerJob.defaultPage(); //new PageFormat(); |
246 |
paper = format.getPaper(); |
247 |
|
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 |
} |
260 |
|
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 |
} |