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