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