svn-gvsig-desktop / tags / v1_0_1_RELEASE / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 9531
History | View | Annotate | Download (9.13 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 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 |
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 |
import com.iver.andami.PluginServices; |
72 |
import com.iver.andami.plugins.Extension; |
73 |
import com.iver.andami.ui.mdiManager.IWindow; |
74 |
import com.iver.cit.gvsig.gui.layout.Attributes; |
75 |
import com.iver.cit.gvsig.gui.layout.Layout; |
76 |
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities; |
77 |
|
78 |
|
79 |
/**
|
80 |
* Extensi?n desde la que se imprime.
|
81 |
*
|
82 |
* @author Vicente Caballero Navarro
|
83 |
*/
|
84 |
public class Print extends Extension implements Printable { |
85 |
public static PrinterJob printerJob = PrinterJob.getPrinterJob(); |
86 |
private static Layout l = null; |
87 |
//private Paper paper;
|
88 |
Rectangle2D.Double aux = null; |
89 |
private int veces = 0; |
90 |
private PrintService[] m_cachePrintServices = null; |
91 |
private PrintService m_cachePrintService = null; |
92 |
|
93 |
/**
|
94 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
95 |
*/
|
96 |
public void execute(String s) { |
97 |
doPrint((Layout) PluginServices.getMDIManager().getActiveWindow()); |
98 |
/*
|
99 |
l = (Layout) PluginServices.getMDIManager().getActiveWindow();
|
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 |
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 |
public void setLayout(Layout l){ |
145 |
this.l=l;
|
146 |
} |
147 |
/**
|
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 |
|
160 |
// System.out.println("drawShapes = "+g2.getTransform().getScaleX());
|
161 |
//g2.translate(l.getRect().x, l.getRect().y);
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
166 |
*/
|
167 |
public boolean isVisible() { |
168 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
169 |
|
170 |
if (f == null) { |
171 |
return false; |
172 |
} |
173 |
|
174 |
return (f instanceof Layout); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* @see com.iver.mdiApp.plugins.IExtension#isEnabled()
|
179 |
*/
|
180 |
public boolean isEnabled() { |
181 |
Layout f = (Layout) PluginServices.getMDIManager().getActiveWindow(); |
182 |
|
183 |
if (f == null) { |
184 |
return false; |
185 |
} |
186 |
|
187 |
return true; |
188 |
} |
189 |
|
190 |
/* (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 |
|
199 |
System.err.println("Clip 0 = " + g.getClip()); |
200 |
|
201 |
Graphics2D g2d = (Graphics2D) g; |
202 |
|
203 |
double x = format.getImageableX();
|
204 |
double y = format.getImageableY();
|
205 |
double w = format.getImageableWidth();
|
206 |
double h = format.getImageableHeight();
|
207 |
|
208 |
//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 |
|
212 |
AffineTransform at = g2d.getTransform();
|
213 |
g2d.translate(0, 0); |
214 |
l.obtainRect(true);
|
215 |
|
216 |
//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 |
|
221 |
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 |
|
246 |
veces++; |
247 |
System.out.println("veces = " + veces); |
248 |
|
249 |
drawShapes(g2d); |
250 |
g2d.setTransform(at); |
251 |
|
252 |
return Printable.PAGE_EXISTS; |
253 |
} |
254 |
|
255 |
/**
|
256 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
257 |
*/
|
258 |
public void initialize() { |
259 |
} |
260 |
|
261 |
/**
|
262 |
* Abre un di?logo para imprimir.
|
263 |
*
|
264 |
* @param layout Layout a imprimir.
|
265 |
*/
|
266 |
public void OpenDialogToPrint(Layout layout) { |
267 |
l = layout; |
268 |
|
269 |
//PageFormat format = printerJob.defaultPage(); //new PageFormat();
|
270 |
//paper = format.getPaper();
|
271 |
|
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 |
} |
284 |
|
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 |
} |