svn-document-layout / tags / 2059 / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / extension / Print.java @ 46
History | View | Annotate | Download (9.11 KB)
1 | 5 | jldominguez | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.app.extension; |
||
23 | |||
24 | 30 | jldominguez | import java.awt.Graphics; |
25 | import java.awt.Graphics2D; |
||
26 | import java.awt.geom.AffineTransform; |
||
27 | 5 | jldominguez | import java.awt.geom.Rectangle2D; |
28 | 30 | jldominguez | import java.awt.print.PageFormat; |
29 | 5 | jldominguez | import java.awt.print.Printable; |
30 | 30 | jldominguez | import java.awt.print.PrinterException; |
31 | 5 | jldominguez | import java.awt.print.PrinterJob; |
32 | |||
33 | import javax.print.Doc; |
||
34 | import javax.print.DocFlavor; |
||
35 | import javax.print.DocPrintJob; |
||
36 | import javax.print.PrintException; |
||
37 | import javax.print.PrintService; |
||
38 | import javax.print.PrintServiceLookup; |
||
39 | import javax.print.ServiceUI; |
||
40 | import javax.print.SimpleDoc; |
||
41 | import javax.print.attribute.PrintRequestAttributeSet; |
||
42 | import javax.print.event.PrintJobAdapter; |
||
43 | import javax.print.event.PrintJobEvent; |
||
44 | import javax.print.event.PrintJobListener; |
||
45 | |||
46 | 24 | jldominguez | import org.gvsig.andami.IconThemeHelper; |
47 | 5 | jldominguez | import org.gvsig.andami.PluginServices; |
48 | import org.gvsig.andami.plugins.Extension; |
||
49 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
50 | import org.gvsig.app.project.documents.layout.Attributes; |
||
51 | 30 | jldominguez | import org.gvsig.app.project.documents.layout.FLayoutUtilities; |
52 | 5 | jldominguez | import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
53 | |||
54 | /**
|
||
55 | * Extensi?n desde la que se imprime.
|
||
56 | *
|
||
57 | * @author Vicente Caballero Navarro
|
||
58 | */
|
||
59 | 30 | jldominguez | public class Print extends Extension implements Printable { |
60 | 5 | jldominguez | |
61 | public static PrinterJob printerJob = PrinterJob.getPrinterJob(); |
||
62 | |||
63 | // private Paper paper;
|
||
64 | Rectangle2D.Double aux = null; |
||
65 | private PrintService[] m_cachePrintServices = null; |
||
66 | private PrintService m_cachePrintService = null; |
||
67 | 30 | jldominguez | |
68 | private static LayoutPanel theLayoutPanel = null; |
||
69 | 5 | jldominguez | |
70 | public void execute(String s) { |
||
71 | 24 | jldominguez | if (s.compareTo("application-print-layout") == 0) { |
72 | doPrint((LayoutPanel) PluginServices.getMDIManager().getActiveWindow()); |
||
73 | } |
||
74 | 5 | jldominguez | } |
75 | |||
76 | public void doPrint(final LayoutPanel layoutPanel) { |
||
77 | 30 | jldominguez | |
78 | theLayoutPanel = layoutPanel; |
||
79 | |||
80 | 5 | jldominguez | try {
|
81 | PluginServices.backgroundExecution(new Runnable() { |
||
82 | |||
83 | public void run() { |
||
84 | if (layoutPanel.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) { |
||
85 | layoutPanel.showPrintDialog(printerJob); |
||
86 | } else {
|
||
87 | layoutPanel.showPrintDialog(null);
|
||
88 | } |
||
89 | } |
||
90 | }); |
||
91 | |||
92 | } catch (Exception e) { |
||
93 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " |
||
94 | + e); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public boolean isVisible() { |
||
99 | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
||
100 | |||
101 | if (f == null) { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | return (f instanceof LayoutPanel); |
||
106 | } |
||
107 | |||
108 | public boolean isEnabled() { |
||
109 | LayoutPanel f = |
||
110 | (LayoutPanel) PluginServices.getMDIManager().getActiveWindow(); |
||
111 | |||
112 | if (f == null) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | return true; |
||
117 | } |
||
118 | |||
119 | |||
120 | public void initialize() { |
||
121 | registerIcons(); |
||
122 | } |
||
123 | |||
124 | private void registerIcons() { |
||
125 | 24 | jldominguez | IconThemeHelper.registerIcon("action", "application-print", this); |
126 | 5 | jldominguez | } |
127 | |||
128 | /**
|
||
129 | * Abre un di?logo para imprimir.
|
||
130 | *
|
||
131 | * @param layout
|
||
132 | * Layout a imprimir.
|
||
133 | */
|
||
134 | public void openDialogToPrint(LayoutPanel layoutPanel) { |
||
135 | |||
136 | 30 | jldominguez | theLayoutPanel = layoutPanel; |
137 | |||
138 | 5 | jldominguez | try {
|
139 | if (layoutPanel.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) { |
||
140 | layoutPanel.showPrintDialog(printerJob); |
||
141 | } else {
|
||
142 | layoutPanel.showPrintDialog(null);
|
||
143 | } |
||
144 | } catch (Exception e) { |
||
145 | System.out.println("Excepci?n al abrir el di?logo de impresi?n: " |
||
146 | + e); |
||
147 | e.printStackTrace(); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /**
|
||
152 | * Imprime el Layout que se pasa como par?metro.
|
||
153 | *
|
||
154 | * @param layout
|
||
155 | * Layout a imprimir.
|
||
156 | */
|
||
157 | public void printLayout(LayoutPanel layoutPanel) { |
||
158 | |||
159 | 30 | jldominguez | theLayoutPanel = layoutPanel; |
160 | |||
161 | 5 | jldominguez | try {
|
162 | printerJob.setPrintable((Printable) PluginServices
|
||
163 | .getExtension(org.gvsig.app.extension.Print.class)); |
||
164 | |||
165 | // Actualizar attributes
|
||
166 | PrintRequestAttributeSet att =
|
||
167 | layoutPanel.getLayoutContext().getAttributes() |
||
168 | .toPrintRequestAttributeSet(); |
||
169 | DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
||
170 | |||
171 | if (m_cachePrintServices == null) { |
||
172 | m_cachePrintServices = |
||
173 | PrintServiceLookup.lookupPrintServices(flavor, null); |
||
174 | } |
||
175 | |||
176 | PrintService defaultService = null; |
||
177 | |||
178 | if (m_cachePrintService == null) { |
||
179 | defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
||
180 | } |
||
181 | |||
182 | if (m_cachePrintService == null) { |
||
183 | m_cachePrintService = |
||
184 | ServiceUI.printDialog(null, 200, 200, m_cachePrintServices, |
||
185 | defaultService, flavor, att); |
||
186 | } |
||
187 | |||
188 | if (m_cachePrintService != null) { |
||
189 | DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
||
190 | PrintJobListener pjlistener = new PrintJobAdapter() { |
||
191 | |||
192 | public void printDataTransferCompleted(PrintJobEvent e) { |
||
193 | System.out.println("Fin de impresi?n"); |
||
194 | } |
||
195 | }; |
||
196 | |||
197 | jobNuevo.addPrintJobListener(pjlistener); |
||
198 | |||
199 | Doc doc =
|
||
200 | new SimpleDoc( |
||
201 | PluginServices |
||
202 | .getExtension(org.gvsig.app.extension.Print.class), |
||
203 | flavor, null);
|
||
204 | jobNuevo.print(doc, att); |
||
205 | } |
||
206 | } catch (PrintException pe) { |
||
207 | pe.printStackTrace(); |
||
208 | } |
||
209 | } |
||
210 | 30 | jldominguez | |
211 | /**
|
||
212 | * Se dibuja sobre el graphics el Layout.
|
||
213 | *
|
||
214 | * @param g2 graphics sobre el que se dibuja.
|
||
215 | */
|
||
216 | public void drawShapes(Graphics2D g2) { |
||
217 | theLayoutPanel.drawLayoutPrint(g2); |
||
218 | } |
||
219 | |||
220 | |||
221 | public int print(Graphics g, PageFormat format, int pi) |
||
222 | throws PrinterException { |
||
223 | |||
224 | if (pi >= 1) { |
||
225 | return Printable.NO_SUCH_PAGE; |
||
226 | } |
||
227 | |||
228 | // System.err.println("Clip 0 = " + g.getClip());
|
||
229 | |||
230 | Graphics2D g2d = (Graphics2D) g; |
||
231 | |||
232 | /*
|
||
233 | double x = format.getImageableX();
|
||
234 | double y = format.getImageableY();
|
||
235 | double w = format.getImageableWidth();
|
||
236 | double h = format.getImageableHeight();
|
||
237 | */
|
||
238 | |||
239 | // System.err.println("Orientaci?n en Print: " + format.getOrientation());
|
||
240 | // System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")");
|
||
241 | // System.err.println("Clip 1 = " + g2d.getClip());
|
||
242 | |||
243 | AffineTransform at = g2d.getTransform();
|
||
244 | g2d.translate(0, 0); |
||
245 | theLayoutPanel.obtainRect(true);
|
||
246 | |||
247 | //LWSAffineTransform at = g2d.getTransform();
|
||
248 | g2d.scale((double) 72 / (double) (Attributes.DPI), |
||
249 | (double) 72 / (double) (Attributes.DPI)); |
||
250 | // System.err.println("Clip 2 =" + g2d.getClip());
|
||
251 | |||
252 | if (theLayoutPanel.getLayoutContext().getAttributes().isMargin()) {
|
||
253 | g2d.setClip((int) (theLayoutPanel.getLayoutControl().getRect().getMinX() +
|
||
254 | FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().m_area[2],
|
||
255 | theLayoutPanel.getLayoutControl().getAT())), |
||
256 | (int) (theLayoutPanel.getLayoutControl().getRect().getMinY() +
|
||
257 | FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().m_area[0],
|
||
258 | theLayoutPanel.getLayoutControl().getAT())), |
||
259 | (int) (theLayoutPanel.getLayoutControl().getRect().getWidth() -
|
||
260 | FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().m_area[2] +
|
||
261 | theLayoutPanel.getLayoutContext().getAttributes().m_area[3], theLayoutPanel.getLayoutControl().getAT())),
|
||
262 | (int) (theLayoutPanel.getLayoutControl().getRect().getHeight() -
|
||
263 | FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().m_area[0] +
|
||
264 | theLayoutPanel.getLayoutContext().getAttributes().m_area[1], theLayoutPanel.getLayoutControl().getAT())));
|
||
265 | } |
||
266 | |||
267 | drawShapes(g2d); |
||
268 | g2d.setTransform(at); |
||
269 | |||
270 | return Printable.PAGE_EXISTS; |
||
271 | } |
||
272 | 5 | jldominguez | } |