svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 28460
History | View | Annotate | Download (7.86 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.project.documents.layout.Attributes; |
75 |
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities; |
76 |
import com.iver.cit.gvsig.project.documents.layout.gui.Layout; |
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 |
public void execute(String s) { |
94 |
doPrint((Layout) PluginServices.getMDIManager().getActiveWindow()); |
95 |
} |
96 |
|
97 |
public void doPrint(Layout layout) { |
98 |
l = layout; |
99 |
try {
|
100 |
PluginServices.backgroundExecution(new Runnable() { |
101 |
public void run() { |
102 |
if (l.getLayoutContext().getAtributes().getType() == Attributes.CUSTOM) { |
103 |
l.showPrintDialog(printerJob); |
104 |
} else {
|
105 |
l.showPrintDialog(null);
|
106 |
} |
107 |
} |
108 |
}); |
109 |
|
110 |
} catch (Exception e) { |
111 |
System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
112 |
e); |
113 |
} |
114 |
} |
115 |
|
116 |
public void setLayout(Layout l){ |
117 |
this.l=l;
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* Se dibuja sobre el graphics el Layout.
|
122 |
*
|
123 |
* @param g2 graphics sobre el que se dibuja.
|
124 |
*/
|
125 |
public void drawShapes(Graphics2D g2) { |
126 |
l.drawLayoutPrint(g2); |
127 |
} |
128 |
|
129 |
public boolean isVisible() { |
130 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
131 |
|
132 |
if (f == null) { |
133 |
return false; |
134 |
} |
135 |
|
136 |
return (f instanceof Layout); |
137 |
} |
138 |
|
139 |
public boolean isEnabled() { |
140 |
Layout f = (Layout) PluginServices.getMDIManager().getActiveWindow(); |
141 |
|
142 |
if (f == null) { |
143 |
return false; |
144 |
} |
145 |
|
146 |
return true; |
147 |
} |
148 |
|
149 |
public int print(Graphics g, PageFormat format, int pi) |
150 |
throws PrinterException { |
151 |
if (pi >= 1) { |
152 |
return Printable.NO_SUCH_PAGE; |
153 |
} |
154 |
|
155 |
System.err.println("Clip 0 = " + g.getClip()); |
156 |
|
157 |
Graphics2D g2d = (Graphics2D) g; |
158 |
|
159 |
double x = format.getImageableX();
|
160 |
double y = format.getImageableY();
|
161 |
double w = format.getImageableWidth();
|
162 |
double h = format.getImageableHeight();
|
163 |
|
164 |
//System.err.println("Orientaci?n en Print: " + format.getOrientation());
|
165 |
System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")"); |
166 |
System.err.println("Clip 1 = " + g2d.getClip()); |
167 |
|
168 |
AffineTransform at = g2d.getTransform();
|
169 |
g2d.translate(0, 0); |
170 |
l.obtainRect(true);
|
171 |
|
172 |
//LWSAffineTransform at = g2d.getTransform();
|
173 |
g2d.scale((double) 72 / (double) (Attributes.DPI), |
174 |
(double) 72 / (double) (Attributes.DPI)); |
175 |
System.err.println("Clip 2 =" + g2d.getClip()); |
176 |
|
177 |
if (l.getLayoutContext().getAtributes().isMargin()) {
|
178 |
g2d.setClip((int) (l.getLayoutControl().getRect().getMinX() +
|
179 |
FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[2],
|
180 |
l.getLayoutControl().getAT())), |
181 |
(int) (l.getLayoutControl().getRect().getMinY() +
|
182 |
FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[0],
|
183 |
l.getLayoutControl().getAT())), |
184 |
(int) (l.getLayoutControl().getRect().getWidth() -
|
185 |
FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[2] +
|
186 |
l.getLayoutContext().getAtributes().m_area[3], l.getLayoutControl().getAT())),
|
187 |
(int) (l.getLayoutControl().getRect().getHeight() -
|
188 |
FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[0] +
|
189 |
l.getLayoutContext().getAtributes().m_area[1], l.getLayoutControl().getAT())));
|
190 |
} |
191 |
|
192 |
veces++; |
193 |
System.out.println("veces = " + veces); |
194 |
|
195 |
drawShapes(g2d); |
196 |
g2d.setTransform(at); |
197 |
|
198 |
return Printable.PAGE_EXISTS; |
199 |
} |
200 |
|
201 |
public void initialize() { |
202 |
registerIcons(); |
203 |
} |
204 |
|
205 |
private void registerIcons(){ |
206 |
PluginServices.getIconTheme().registerDefault( |
207 |
"document-print",
|
208 |
this.getClass().getClassLoader().getResource("images/print.png") |
209 |
); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Abre un di?logo para imprimir.
|
214 |
*
|
215 |
* @param layout Layout a imprimir.
|
216 |
*/
|
217 |
public void OpenDialogToPrint(Layout layout) { |
218 |
l = layout; |
219 |
|
220 |
try {
|
221 |
if (layout.getLayoutContext().getAtributes().getType() == Attributes.CUSTOM) { |
222 |
layout.showPrintDialog(printerJob); |
223 |
} else {
|
224 |
layout.showPrintDialog(null);
|
225 |
} |
226 |
} catch (Exception e) { |
227 |
System.out.println("Excepci?n al abrir el di?logo de impresi?n: " + |
228 |
e); |
229 |
e.printStackTrace(); |
230 |
} |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Imprime el Layout que se pasa como par?metro.
|
235 |
*
|
236 |
* @param layout Layout a imprimir.
|
237 |
*/
|
238 |
public void printLayout(Layout layout) { |
239 |
l = layout; |
240 |
|
241 |
try {
|
242 |
printerJob.setPrintable((Printable) PluginServices.getExtension(
|
243 |
com.iver.cit.gvsig.Print.class)); |
244 |
|
245 |
//Actualizar attributes
|
246 |
PrintRequestAttributeSet att = layout.getLayoutContext().getAtributes()
|
247 |
.toPrintAttributes(); |
248 |
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
249 |
|
250 |
if (m_cachePrintServices == null) { |
251 |
m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
|
252 |
null);
|
253 |
} |
254 |
|
255 |
PrintService defaultService = null; |
256 |
|
257 |
if (m_cachePrintService == null) { |
258 |
defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
259 |
} |
260 |
|
261 |
if (m_cachePrintService == null) { |
262 |
m_cachePrintService = ServiceUI.printDialog(null, 200, 200, |
263 |
m_cachePrintServices, defaultService, flavor, att); |
264 |
} |
265 |
|
266 |
if (m_cachePrintService != null) { |
267 |
DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
268 |
PrintJobListener pjlistener = new PrintJobAdapter() { |
269 |
public void printDataTransferCompleted(PrintJobEvent e) { |
270 |
System.out.println("Fin de impresi?n"); |
271 |
} |
272 |
}; |
273 |
|
274 |
jobNuevo.addPrintJobListener(pjlistener); |
275 |
|
276 |
Doc doc = new SimpleDoc((Printable) PluginServices.getExtension( |
277 |
com.iver.cit.gvsig.Print.class), flavor, null);
|
278 |
jobNuevo.print(doc, att); |
279 |
} |
280 |
} catch (PrintException pe) { |
281 |
pe.printStackTrace(); |
282 |
} |
283 |
} |
284 |
} |