root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutDraw.java @ 7304
History | View | Annotate | Download (26.7 KB)
1 |
/*
|
---|---|
2 |
* Created on 27-sep-2004
|
3 |
*
|
4 |
*/
|
5 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
6 |
*
|
7 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
8 |
*
|
9 |
* This program is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU General Public License
|
11 |
* as published by the Free Software Foundation; either version 2
|
12 |
* of the License, or (at your option) any later version.
|
13 |
*
|
14 |
* This program is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
* GNU General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU General Public License
|
20 |
* along with this program; if not, write to the Free Software
|
21 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
22 |
*
|
23 |
* For more information, contact:
|
24 |
*
|
25 |
* Generalitat Valenciana
|
26 |
* Conselleria d'Infraestructures i Transport
|
27 |
* Av. Blasco Ib??ez, 50
|
28 |
* 46010 VALENCIA
|
29 |
* SPAIN
|
30 |
*
|
31 |
* +34 963862235
|
32 |
* gvsig@gva.es
|
33 |
* www.gvsig.gva.es
|
34 |
*
|
35 |
* or
|
36 |
*
|
37 |
* IVER T.I. S.A
|
38 |
* Salamanca 50
|
39 |
* 46005 Valencia
|
40 |
* Spain
|
41 |
*
|
42 |
* +34 963163400
|
43 |
* dac@iver.es
|
44 |
*/
|
45 |
package com.iver.cit.gvsig.project.documents.layout; |
46 |
|
47 |
import java.awt.Color; |
48 |
import java.awt.Component; |
49 |
import java.awt.Graphics2D; |
50 |
import java.awt.geom.AffineTransform; |
51 |
import java.awt.geom.Point2D; |
52 |
import java.awt.geom.Rectangle2D; |
53 |
import java.awt.image.BufferedImage; |
54 |
import java.awt.print.PageFormat; |
55 |
import java.awt.print.PrinterException; |
56 |
import java.io.BufferedOutputStream; |
57 |
import java.io.File; |
58 |
import java.io.FileOutputStream; |
59 |
import java.io.IOException; |
60 |
import java.io.OutputStream; |
61 |
|
62 |
import javax.print.Doc; |
63 |
import javax.print.DocFlavor; |
64 |
import javax.print.DocPrintJob; |
65 |
import javax.print.PrintException; |
66 |
import javax.print.SimpleDoc; |
67 |
import javax.print.StreamPrintService; |
68 |
import javax.print.StreamPrintServiceFactory; |
69 |
import javax.print.attribute.PrintRequestAttributeSet; |
70 |
import javax.swing.JOptionPane; |
71 |
|
72 |
import com.iver.andami.PluginServices; |
73 |
import com.iver.cit.gvsig.Print; |
74 |
import com.iver.cit.gvsig.fmap.DriverException; |
75 |
import com.iver.cit.gvsig.gui.layout.Layout; |
76 |
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame; |
77 |
import com.lowagie.text.Document; |
78 |
import com.lowagie.text.DocumentException; |
79 |
import com.lowagie.text.pdf.PdfContentByte; |
80 |
import com.lowagie.text.pdf.PdfWriter; |
81 |
|
82 |
|
83 |
/**
|
84 |
* Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
|
85 |
*
|
86 |
* @author Vicente Caballero Navarro
|
87 |
*/
|
88 |
public class FLayoutDraw { |
89 |
private Layout layout;
|
90 |
|
91 |
/**
|
92 |
* Crea un nuevo FLayoutDraw.
|
93 |
*
|
94 |
* @param l Referencia al Layout.
|
95 |
*/
|
96 |
public FLayoutDraw(Layout l) {
|
97 |
layout = l; |
98 |
} |
99 |
|
100 |
/**
|
101 |
* M?todo para dibujar el Layout y modificar la matriz de transformaci?n a
|
102 |
* partir del tama?o en pixels que tenga rect y el formato de papel
|
103 |
* seleccionado.
|
104 |
*
|
105 |
* @param g2
|
106 |
* @param imgBase Si es null, est?s imprimiendo. Si no, la usas para el
|
107 |
* c?digo de optimizaci?n.
|
108 |
*
|
109 |
* @throws DriverException
|
110 |
*/
|
111 |
public void drawLayout(Graphics2D g2, BufferedImage imgBase) |
112 |
throws DriverException {
|
113 |
layout.setCancelDrawing(false);
|
114 |
double scale = 0; |
115 |
scale = layout.getRect().height / layout.getAtributes().m_sizePaper.getAlto() * 1;
|
116 |
AffineTransform escalado = new AffineTransform(); |
117 |
AffineTransform translacion = new AffineTransform(); |
118 |
translacion.setToTranslation(layout.getRect().getMinX(), |
119 |
layout.getRect().getMinY()); |
120 |
escalado.setToScale(scale, scale); |
121 |
layout.getAT().setToIdentity(); |
122 |
layout.getAT().concatenate(translacion); |
123 |
layout.getAT().concatenate(escalado); |
124 |
layout.getAtributes().setDistanceUnitX(layout.getRect()); |
125 |
layout.getAtributes().setDistanceUnitY(layout.getRect()); |
126 |
IFFrame[] fframes=layout.getFFrames();
|
127 |
for (int i = 0; i < fframes.length; i++) { |
128 |
IFFrame f = fframes[i]; |
129 |
//original, lento (NO MAC)
|
130 |
f.draw(g2, layout.getAT(), layout.getVisibleRect(),imgBase); |
131 |
// ESTILO MAC
|
132 |
/* if (f instanceof FFrameView)
|
133 |
{
|
134 |
FFrameView fframe = (FFrameView) f;
|
135 |
BufferedImage img = new BufferedImage((int) layout.getWidth(),
|
136 |
(int) layout.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
137 |
|
138 |
|
139 |
fframe.draw(img.createGraphics(), layout.getAT(), layout.getVisibleRect(), imgBase);
|
140 |
g2.drawImage(img, 0, 0, layout);
|
141 |
fframe.setBufferedImage(img);
|
142 |
}
|
143 |
else
|
144 |
{
|
145 |
f.draw(g2, layout.getAT(), layout.getVisibleRect(),imgBase);
|
146 |
} */
|
147 |
|
148 |
// Dibuja el s?mbolo de que contiene un tag.
|
149 |
if ((f.getTag() != null) && (layout.isModeDebug())) { |
150 |
f.drawSymbolTag(g2); |
151 |
} |
152 |
} |
153 |
|
154 |
if (!(fframes.length==0)) { |
155 |
layout.setStatus(Layout.ACTUALIZADO); |
156 |
} else {
|
157 |
layout.setStatus(Layout.DESACTUALIZADO); |
158 |
} |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
|
163 |
*
|
164 |
* @param g Graphics2D
|
165 |
*/
|
166 |
public void drawRectangle(Graphics2D g) { |
167 |
double unidadesX = layout.getAtributes().getUnitInPixelsX();
|
168 |
double unidadesY = layout.getAtributes().getUnitInPixelsY();
|
169 |
|
170 |
if ((unidadesX == 0) && (unidadesY == 0)) { |
171 |
return;
|
172 |
} |
173 |
|
174 |
g.setColor(Color.darkGray);
|
175 |
|
176 |
Rectangle2D.Double rectBig = new Rectangle2D.Double(layout.getRect().x, |
177 |
layout.getRect().y, layout.getRect().width, |
178 |
layout.getRect().height); |
179 |
|
180 |
g.fill3DRect((int) rectBig.x + 7, (int) rectBig.y + 7, |
181 |
(int) rectBig.width, (int) rectBig.height, true); |
182 |
|
183 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
184 |
|
185 |
if (layout.getAtributes().isMargin()) {
|
186 |
r = new Rectangle2D.Double((layout.getRect().x + |
187 |
FLayoutUtilities.fromSheetDistance( |
188 |
layout.getAtributes().m_area[2], layout.getAT())),
|
189 |
(layout.getRect().y + |
190 |
FLayoutUtilities.fromSheetDistance( |
191 |
layout.getAtributes().m_area[0], layout.getAT())),
|
192 |
(layout.getRect().width - |
193 |
FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[2] +
|
194 |
layout.getAtributes().m_area[3], layout.getAT())),
|
195 |
(layout.getRect().height - |
196 |
FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[0] +
|
197 |
layout.getAtributes().m_area[1], layout.getAT())));
|
198 |
} else {
|
199 |
r.setRect(layout.getRect()); |
200 |
} |
201 |
|
202 |
g.setColor(Color.white);
|
203 |
g.fill(layout.getRect()); |
204 |
|
205 |
g.setColor(Color.darkGray);
|
206 |
g.drawRect((int) layout.getRect().getMinX(),
|
207 |
(int) layout.getRect().getMinY(),
|
208 |
(int) layout.getRect().getWidth(),
|
209 |
(int) layout.getRect().getHeight());
|
210 |
|
211 |
if (layout.getAtributes().isMargin()) {
|
212 |
g.setColor(Color.black);
|
213 |
|
214 |
g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height); |
215 |
} |
216 |
} |
217 |
|
218 |
/**
|
219 |
* DOCUMENT ME!
|
220 |
*
|
221 |
* @param g DOCUMENT ME!
|
222 |
*/
|
223 |
public void drawGrid(Graphics2D g) { |
224 |
int unidadesMin = 6; |
225 |
double unidadesX = layout.getAtributes().getUnitInPixelsX();
|
226 |
double unidadesY = layout.getAtributes().getUnitInPixelsY();
|
227 |
|
228 |
if ((unidadesX == 0) && (unidadesY == 0)) { |
229 |
return;
|
230 |
} |
231 |
|
232 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
233 |
|
234 |
if (layout.getAtributes().isMargin()) {
|
235 |
r = new Rectangle2D.Double((layout.getRect().x + |
236 |
FLayoutUtilities.fromSheetDistance( |
237 |
layout.getAtributes().m_area[2], layout.getAT())),
|
238 |
(layout.getRect().y + |
239 |
FLayoutUtilities.fromSheetDistance( |
240 |
layout.getAtributes().m_area[0], layout.getAT())),
|
241 |
(layout.getRect().width - |
242 |
FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[2] +
|
243 |
layout.getAtributes().m_area[3], layout.getAT())),
|
244 |
(layout.getRect().height - |
245 |
FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[0] +
|
246 |
layout.getAtributes().m_area[1], layout.getAT())));
|
247 |
} else {
|
248 |
r.setRect(layout.getRect()); |
249 |
} |
250 |
|
251 |
g.setColor(Color.darkGray);
|
252 |
|
253 |
if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin)) &&
|
254 |
layout.getIsDrawGrid()) { |
255 |
double ax = r.getMinX();
|
256 |
double ay;
|
257 |
|
258 |
while (ax < (r.getMaxX())) {
|
259 |
ay = (r.getMinY()); |
260 |
|
261 |
while (ay < (r.getMaxY())) {
|
262 |
g.drawLine((int) ax, (int) ay, (int) ax, (int) ay); |
263 |
ay = ay + unidadesY; |
264 |
} |
265 |
|
266 |
ax = ax + unidadesX; |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Dibuja sobre el graphics2d las reglas.
|
273 |
*
|
274 |
* @param g graphics2d sobre el que se dibuja.
|
275 |
* @param color Color de la regla.
|
276 |
*/
|
277 |
public void drawRuler(Graphics2D g, Color color) { |
278 |
if (layout.getRuler()) {
|
279 |
int ini = 10; |
280 |
int w = 30; |
281 |
int wi = 16; |
282 |
|
283 |
g.setColor(new Color(250, 255, 250, 180)); |
284 |
g.fillRect(ini, w, wi, layout.getHeight() - w); |
285 |
g.fillRect(w, ini, layout.getWidth() - w, wi); |
286 |
|
287 |
g.setColor(new Color(100, 155, 150, 180)); |
288 |
g.fillRect(w, ini, (int) layout.getRect().x - w, wi);
|
289 |
g.fillRect((int) layout.getRect().getMaxX(), ini,
|
290 |
layout.getWidth() - (int) layout.getRect().getMaxX(), wi);
|
291 |
|
292 |
g.fillRect(ini, w, wi, (int) layout.getRect().y - w);
|
293 |
g.fillRect(ini, (int) layout.getRect().getMaxY(), wi,
|
294 |
layout.getHeight() - (int) layout.getRect().getMaxY());
|
295 |
|
296 |
if (layout.getAtributes().isMargin()) {
|
297 |
g.setColor(new Color(50, 55, 50, 180)); |
298 |
g.fillRect((int) layout.getRect().x, ini,
|
299 |
(int) FLayoutUtilities.fromSheetDistance(
|
300 |
layout.getAtributes().m_area[2], layout.getAT()), wi);
|
301 |
g.fillRect((int) layout.getRect().getMaxX() -
|
302 |
(int) FLayoutUtilities.fromSheetDistance(
|
303 |
layout.getAtributes().m_area[3], layout.getAT()), ini,
|
304 |
(int) FLayoutUtilities.fromSheetDistance(
|
305 |
layout.getAtributes().m_area[3], layout.getAT()), wi);
|
306 |
|
307 |
g.fillRect(ini, (int) layout.getRect().y, wi,
|
308 |
(int) FLayoutUtilities.fromSheetDistance(
|
309 |
layout.getAtributes().m_area[0], layout.getAT()));
|
310 |
g.fillRect(ini, |
311 |
(int) layout.getRect().getMaxY() -
|
312 |
(int) FLayoutUtilities.fromSheetDistance(
|
313 |
layout.getAtributes().m_area[1], layout.getAT()), wi,
|
314 |
(int) FLayoutUtilities.fromSheetDistance(
|
315 |
layout.getAtributes().m_area[1], layout.getAT()));
|
316 |
} |
317 |
|
318 |
g.setColor(color); |
319 |
g.drawLine(w, wi + ini, layout.getWidth(), wi + ini); |
320 |
g.drawLine(w, ini, layout.getWidth(), ini); |
321 |
g.drawLine(ini, w, ini, layout.getHeight()); |
322 |
g.drawLine(wi + ini, w, wi + ini, layout.getHeight()); |
323 |
|
324 |
drawLineY(g, 5, 12, 22, layout.getRect().y, 0); |
325 |
drawLineX(g, 5, 12, 22, layout.getRect().x, 0); |
326 |
|
327 |
if (FLayoutUtilities.fromSheetDistance(1, layout.getAT()) > 15) { |
328 |
drawLineY(g, 1, 12, 22, layout.getRect().y, 0); |
329 |
drawLineX(g, 1, 12, 22, layout.getRect().x, 0); |
330 |
|
331 |
if (FLayoutUtilities.fromSheetDistance(1, layout.getAT()) > 25) { |
332 |
drawLineY(g, 1, 18, 22, layout.getRect().y, 0.5); |
333 |
drawLineY(g, 0.1, 21, 22, layout.getRect().y, 0); |
334 |
drawLineX(g, 1, 18, 22, layout.getRect().x, 0.5); |
335 |
drawLineX(g, 0.1, 21, 22, layout.getRect().x, 0); |
336 |
} |
337 |
} |
338 |
} |
339 |
} |
340 |
|
341 |
/**
|
342 |
* Dibuja sobre el graphics2d las l?neas verticales que representan a las
|
343 |
* unidades de medida.
|
344 |
*
|
345 |
* @param g Graphics2d sobre el que se dibuja.
|
346 |
* @param dist distancia en cent?metros de una l?nea a otra.
|
347 |
* @param init inicio de la l?nea.
|
348 |
* @param end fin de la l?nea.
|
349 |
* @param y rect?ngulo, dentro del cual se dibujan las l?neas.
|
350 |
* @param desp Desplazamiento.
|
351 |
*/
|
352 |
private void drawLineY(Graphics2D g, double dist, int init, int end, |
353 |
double y, double desp) { |
354 |
double distY = FLayoutUtilities.fromSheetDistance(dist, layout.getAT());
|
355 |
double rota = Math.toRadians(90); |
356 |
|
357 |
if (distY > 4) { |
358 |
double despY = FLayoutUtilities.fromSheetDistance(desp,
|
359 |
layout.getAT()); |
360 |
|
361 |
double posUnitY = y + despY;
|
362 |
double posUnitYNeg = posUnitY;
|
363 |
int num = 0; |
364 |
double iniY = 40; |
365 |
Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double( |
366 |
layout.getWidth(), layout.getHeight()), layout.getAT()); |
367 |
|
368 |
while (posUnitY < (pfin.y - 5)) { |
369 |
posUnitYNeg -= distY; |
370 |
|
371 |
if (distY > 16) { |
372 |
if (init == 12) { |
373 |
if (posUnitY > iniY) {
|
374 |
g.rotate(-rota, 20, posUnitY - 12); |
375 |
g.drawString(String.valueOf(num), 10, |
376 |
(int) posUnitY - 12); |
377 |
g.rotate(rota, 20, posUnitY - 12); |
378 |
} |
379 |
|
380 |
if (dist == 5) { |
381 |
num = num + 5;
|
382 |
} else {
|
383 |
num++; |
384 |
} |
385 |
|
386 |
if (posUnitYNeg > iniY) {
|
387 |
g.rotate(-rota, 20, posUnitYNeg - 12); |
388 |
g.drawString(String.valueOf(-num), 10, |
389 |
(int) posUnitYNeg - 12); |
390 |
g.rotate(rota, 20, posUnitYNeg - 12); |
391 |
} |
392 |
} |
393 |
} |
394 |
|
395 |
if (posUnitY > iniY) {
|
396 |
g.drawLine( 2 + init, (int) posUnitY, 2 + end, |
397 |
(int) posUnitY);
|
398 |
} |
399 |
|
400 |
if (posUnitYNeg > iniY) {
|
401 |
g.drawLine(2 + init, (int) posUnitYNeg, |
402 |
2 + end, (int) posUnitYNeg); |
403 |
} |
404 |
|
405 |
posUnitY += distY; |
406 |
} |
407 |
} |
408 |
} |
409 |
|
410 |
/**
|
411 |
* Dibuja sobre el graphics2d las l?neas horizontales que representan a las
|
412 |
* unidades de medida.
|
413 |
*
|
414 |
* @param g Graphics2d sobre el que se dibuja.
|
415 |
* @param dist distancia en cent?metros de una l?nea a otra.
|
416 |
* @param init inicio de la l?nea.
|
417 |
* @param end fin de la l?nea.
|
418 |
* @param x rect?ngulo, dentro del cual se dibujan las l?neas.
|
419 |
* @param desp Desplazamiento.
|
420 |
*/
|
421 |
private void drawLineX(Graphics2D g, double dist, int init, int end, |
422 |
double x, double desp) { |
423 |
double distX = FLayoutUtilities.fromSheetDistance(dist, layout.getAT());
|
424 |
|
425 |
if (distX > 4) { |
426 |
double despX = FLayoutUtilities.fromSheetDistance(desp,
|
427 |
layout.getAT()); |
428 |
double posUnitX = x + despX;
|
429 |
double posUnitXNeg = posUnitX;
|
430 |
int num = 0; |
431 |
double iniX = 40; |
432 |
Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double( |
433 |
layout.getWidth(), layout.getHeight()), layout.getAT()); |
434 |
|
435 |
while (posUnitX < (pfin.x - 5)) { |
436 |
posUnitXNeg -= distX; |
437 |
|
438 |
if (init == 12) { |
439 |
if (distX > 16) { |
440 |
if (posUnitX > iniX) {
|
441 |
g.drawString(String.valueOf(num),
|
442 |
(int) posUnitX + 3, 20); |
443 |
} |
444 |
|
445 |
if (dist == 5) { |
446 |
num = num + 5;
|
447 |
} else {
|
448 |
num++; |
449 |
} |
450 |
|
451 |
if (posUnitXNeg > iniX) {
|
452 |
g.drawString(String.valueOf(-num),
|
453 |
(int) posUnitXNeg + 3, 20); |
454 |
} |
455 |
} |
456 |
} |
457 |
|
458 |
if (posUnitX > iniX) {
|
459 |
g.drawLine((int) posUnitX, 2 + init, (int) posUnitX, |
460 |
2 + end);
|
461 |
} |
462 |
|
463 |
if (posUnitXNeg > iniX) {
|
464 |
g.drawLine((int) posUnitXNeg, 2 + init, |
465 |
(int) posUnitXNeg, 2 + end); |
466 |
} |
467 |
|
468 |
posUnitX += distX; |
469 |
} |
470 |
} |
471 |
} |
472 |
|
473 |
/**
|
474 |
* Dibuja los handlers sobre los fframes que esten seleccionados.
|
475 |
*
|
476 |
* @param g Graphics sobre el que se dibuja.
|
477 |
* @param color Color de los Handlers.
|
478 |
*/
|
479 |
public void drawHandlers(Graphics2D g, Color color) { |
480 |
g.setColor(color); |
481 |
IFFrame[] fframes=layout.getFFrames();
|
482 |
for (int i = 0; i < fframes.length; i++) { |
483 |
IFFrame fframe = fframes[i]; |
484 |
|
485 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
486 |
fframe.drawHandlers(g); |
487 |
} |
488 |
} |
489 |
} |
490 |
|
491 |
/**
|
492 |
* A partir de un fichero que se pasa como par?metro se crea un pdf con el
|
493 |
* contenido del Layout.
|
494 |
*
|
495 |
* @param pdf
|
496 |
*/
|
497 |
/* public void toPS(File ps) {
|
498 |
|
499 |
try {
|
500 |
// Open the image file
|
501 |
// InputStream is = new BufferedInputStream(
|
502 |
// new FileInputStream("filename.gif"));
|
503 |
|
504 |
// Prepare the output file to receive the postscript
|
505 |
OutputStream fos = new BufferedOutputStream(
|
506 |
new FileOutputStream("filename.ps"));
|
507 |
|
508 |
// Find a factory that can do the conversion
|
509 |
//DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
|
510 |
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
|
511 |
StreamPrintServiceFactory[] factories =
|
512 |
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
|
513 |
flavor,
|
514 |
DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
|
515 |
|
516 |
if (factories.length > 0) {
|
517 |
StreamPrintService service = factories[0].getPrintService(fos);
|
518 |
|
519 |
// Create the print job
|
520 |
DocPrintJob job = service.createPrintJob();
|
521 |
Print print= (Print)PluginServices.getExtension(
|
522 |
com.iver.cit.gvsig.Print.class);
|
523 |
print.setLayout(layout);
|
524 |
Doc doc = new SimpleDoc((Printable)print, flavor, null);
|
525 |
//Doc doc = new SimpleDoc(is, flavor, null);
|
526 |
// Monitor print job events; for the implementation of PrintJobWatcher,
|
527 |
// see e702 Determining When a Print Job Has Finished
|
528 |
//PrintJobWatcher pjDone = new PrintJobWatcher(job);
|
529 |
// Actualizar attributes
|
530 |
PrintRequestAttributeSet att = layout.getAtributes()
|
531 |
.toPrintAttributes();
|
532 |
// Print it
|
533 |
job.print(doc, att);
|
534 |
|
535 |
// Wait for the print job to be done
|
536 |
//pjDone.waitForDone();
|
537 |
// It is now safe to close the streams
|
538 |
}
|
539 |
|
540 |
// is.close();
|
541 |
fos.close();
|
542 |
} catch (PrintException e) {
|
543 |
} catch (IOException e) {
|
544 |
}
|
545 |
}
|
546 |
*/
|
547 |
|
548 |
/*
|
549 |
public void printLayout(Layout layout) {
|
550 |
l = layout;
|
551 |
|
552 |
try {
|
553 |
printerJob.setPrintable((Printable) PluginServices.getExtension(
|
554 |
com.iver.cit.gvsig.Print.class));
|
555 |
|
556 |
//Actualizar attributes
|
557 |
PrintRequestAttributeSet att = layout.getAtributes()
|
558 |
.toPrintAttributes();
|
559 |
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
|
560 |
|
561 |
if (m_cachePrintServices == null) {
|
562 |
m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
|
563 |
null);
|
564 |
}
|
565 |
|
566 |
PrintService defaultService = null;
|
567 |
|
568 |
if (m_cachePrintService == null) {
|
569 |
defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
570 |
}
|
571 |
|
572 |
if (m_cachePrintService == null) {
|
573 |
m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
|
574 |
m_cachePrintServices, defaultService, flavor, att);
|
575 |
}
|
576 |
|
577 |
if (m_cachePrintService != null) {
|
578 |
DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
579 |
PrintJobListener pjlistener = new PrintJobAdapter() {
|
580 |
public void printDataTransferCompleted(PrintJobEvent e) {
|
581 |
System.out.println("Fin de impresi?n");
|
582 |
}
|
583 |
};
|
584 |
|
585 |
jobNuevo.addPrintJobListener(pjlistener);
|
586 |
|
587 |
Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
|
588 |
com.iver.cit.gvsig.Print.class), flavor, null);
|
589 |
jobNuevo.print(doc, att);
|
590 |
}
|
591 |
} catch (PrintException pe) {
|
592 |
pe.printStackTrace();
|
593 |
}
|
594 |
}
|
595 |
|
596 |
document.close();
|
597 |
|
598 |
layout.fullRect();
|
599 |
}
|
600 |
*/
|
601 |
|
602 |
|
603 |
|
604 |
/**
|
605 |
* A partir de un fichero que se pasa como par?metro se crea un ps con el
|
606 |
* contenido del Layout.
|
607 |
*
|
608 |
* @param ps
|
609 |
*/
|
610 |
public void toPS(File ps) { |
611 |
try {
|
612 |
// Prepare the output file to receive the postscript
|
613 |
OutputStream fos = new BufferedOutputStream( |
614 |
new FileOutputStream(ps)); |
615 |
|
616 |
// Find a factory that can do the conversion
|
617 |
//DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
|
618 |
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
619 |
StreamPrintServiceFactory [] factories = |
620 |
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
|
621 |
"application/postscript");
|
622 |
|
623 |
if (factories.length > 0) { |
624 |
StreamPrintService service = factories[0].getPrintService(fos); |
625 |
|
626 |
// Create the print job
|
627 |
DocPrintJob job = service.createPrintJob();
|
628 |
Print print= (Print)PluginServices.getExtension( |
629 |
com.iver.cit.gvsig.Print.class); |
630 |
print.setLayout(layout); |
631 |
Doc doc = new SimpleDoc(print, flavor, null); |
632 |
//Doc doc = new SimpleDoc(is, flavor, null);
|
633 |
// Monitor print job events; for the implementation of PrintJobWatcher,
|
634 |
// see e702 Determining When a Print Job Has Finished
|
635 |
//PrintJobWatcher pjDone = new PrintJobWatcher(job);
|
636 |
// Actualizar attributes
|
637 |
PrintRequestAttributeSet att = layout.getAtributes()
|
638 |
.toPrintAttributes(); |
639 |
// Print it
|
640 |
job.print(doc, att); |
641 |
|
642 |
// Wait for the print job to be done
|
643 |
//pjDone.waitForDone();
|
644 |
// It is now safe to close the streams
|
645 |
} |
646 |
|
647 |
// is.close();
|
648 |
fos.close(); |
649 |
} catch (PrintException e) { |
650 |
} catch (IOException e) { |
651 |
} |
652 |
/* PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
|
653 |
DocPrintJob printerJob = defaultPrintService.createPrintJob();
|
654 |
|
655 |
try {
|
656 |
DocFlavor flavor=DocFlavor.URL.POSTSCRIPT;
|
657 |
if (!defaultPrintService.isDocFlavorSupported(flavor)) {
|
658 |
System.err.println("The printer does not support the appropriate DocFlavor");
|
659 |
}else {
|
660 |
|
661 |
SimpleDoc simpleDoc;
|
662 |
simpleDoc = new SimpleDoc(ps.toURL(),flavor, null);
|
663 |
printerJob.print(simpleDoc, null);
|
664 |
}
|
665 |
} catch (MalformedURLException e) {
|
666 |
// TODO Auto-generated catch block
|
667 |
e.printStackTrace();
|
668 |
} catch (PrintException e) {
|
669 |
// TODO Auto-generated catch block
|
670 |
e.printStackTrace();
|
671 |
}
|
672 |
*/
|
673 |
layout.fullRect(); |
674 |
} |
675 |
|
676 |
|
677 |
/**
|
678 |
* A partir de un fichero que se pasa como par?metro se crea un pdf con el
|
679 |
* contenido del Layout.
|
680 |
*
|
681 |
* @param pdf
|
682 |
*/
|
683 |
public void toPDF(File pdf) { |
684 |
double w = 0; |
685 |
double h = 0; |
686 |
Document document = new Document(); |
687 |
|
688 |
if (layout.getAtributes().isLandSpace()) {
|
689 |
w = ((layout.getAtributes().m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA); |
690 |
h = ((layout.getAtributes().m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA); |
691 |
} else {
|
692 |
w = ((layout.getAtributes().m_sizePaper.getAncho() * Attributes.DPISCREEN) / Attributes.PULGADA); |
693 |
h = ((layout.getAtributes().m_sizePaper.getAlto() * Attributes.DPISCREEN) / Attributes.PULGADA); |
694 |
} |
695 |
|
696 |
document.setPageSize(new com.lowagie.text.Rectangle((float) w, (float) h)); |
697 |
|
698 |
try {
|
699 |
FileOutputStream fos=new FileOutputStream(pdf); |
700 |
PdfWriter writer = PdfWriter.getInstance(document,fos); |
701 |
document.open(); |
702 |
|
703 |
Print print = new Print();
|
704 |
print.setLayout(layout); |
705 |
|
706 |
PdfContentByte cb = writer.getDirectContent(); |
707 |
Graphics2D g2 = cb.createGraphicsShapes((float) w, (float) h); |
708 |
|
709 |
try {
|
710 |
if (layout.getAtributes().isLandSpace()) {
|
711 |
g2.rotate(Math.toRadians(-90), 0 + (w / (h / w)), |
712 |
0 + (h / 2)); |
713 |
print.print(g2, new PageFormat(), 0); |
714 |
g2.rotate(Math.toRadians(90), 0 + (w / (h / w)), 0 + |
715 |
(h / 2));
|
716 |
} else {
|
717 |
print.print(g2, new PageFormat(), 0); |
718 |
} |
719 |
} catch (PrinterException e) { |
720 |
e.printStackTrace(); |
721 |
} |
722 |
|
723 |
g2.dispose(); |
724 |
|
725 |
} catch (DocumentException de) {
|
726 |
System.err.println(de.getMessage());
|
727 |
} catch (IOException ioe) { |
728 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), |
729 |
ioe.getMessage()); |
730 |
} |
731 |
|
732 |
document.close(); |
733 |
|
734 |
layout.fullRect(); |
735 |
} |
736 |
} |