svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / extension / Print.java @ 242
History | View | Annotate | Download (9.1 KB)
1 |
/* 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 |
import java.awt.Graphics; |
25 |
import java.awt.Graphics2D; |
26 |
import java.awt.geom.AffineTransform; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
import java.awt.print.PageFormat; |
29 |
import java.awt.print.Printable; |
30 |
import java.awt.print.PrinterException; |
31 |
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 |
import javax.swing.JOptionPane; |
46 |
|
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
import org.gvsig.andami.IconThemeHelper; |
51 |
import org.gvsig.andami.PluginServices; |
52 |
import org.gvsig.andami.plugins.Extension; |
53 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
54 |
import org.gvsig.app.ApplicationLocator; |
55 |
import org.gvsig.app.project.documents.layout.Attributes; |
56 |
import org.gvsig.app.project.documents.layout.FLayoutUtilities; |
57 |
import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
58 |
import org.gvsig.i18n.Messages; |
59 |
|
60 |
/**
|
61 |
* Extensi?n desde la que se imprime.
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class Print extends Extension implements Printable { |
66 |
|
67 |
private static Logger logger = LoggerFactory.getLogger(Print.class); |
68 |
|
69 |
public static PrinterJob printerJob = PrinterJob.getPrinterJob(); |
70 |
|
71 |
// private Paper paper;
|
72 |
Rectangle2D.Double aux = null; |
73 |
private PrintService[] m_cachePrintServices = null; |
74 |
private PrintService m_cachePrintService = null; |
75 |
|
76 |
private static LayoutPanel theLayoutPanel = null; |
77 |
|
78 |
public void execute(String s) { |
79 |
if (s.compareTo("application-print-layout") == 0) { |
80 |
doPrint((LayoutPanel) PluginServices.getMDIManager().getActiveWindow()); |
81 |
} |
82 |
} |
83 |
|
84 |
public void doPrint(final LayoutPanel layoutPanel) { |
85 |
|
86 |
theLayoutPanel = layoutPanel; |
87 |
|
88 |
try {
|
89 |
PluginServices.backgroundExecution(new Runnable() { |
90 |
|
91 |
public void run() { |
92 |
if (layoutPanel.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) { |
93 |
layoutPanel.showPrintDialog(printerJob); |
94 |
} else {
|
95 |
layoutPanel.showPrintDialog(null);
|
96 |
} |
97 |
} |
98 |
}); |
99 |
|
100 |
} catch (Exception e) { |
101 |
|
102 |
logger.info("Error while showing print dialog.", e);
|
103 |
ApplicationLocator.getManager().messageDialog( |
104 |
Messages.getText("_Error_while_showing_print_dialog"),
|
105 |
Messages.getText("Imprimir"),
|
106 |
JOptionPane.ERROR_MESSAGE);
|
107 |
} |
108 |
} |
109 |
|
110 |
public void setLayout(LayoutPanel layoutp){ |
111 |
theLayoutPanel = layoutp; |
112 |
} |
113 |
|
114 |
|
115 |
public boolean isVisible() { |
116 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
117 |
|
118 |
if (f == null) { |
119 |
return false; |
120 |
} |
121 |
|
122 |
return (f instanceof LayoutPanel); |
123 |
} |
124 |
|
125 |
public boolean isEnabled() { |
126 |
LayoutPanel f = |
127 |
(LayoutPanel) PluginServices.getMDIManager().getActiveWindow(); |
128 |
|
129 |
if (f == null) { |
130 |
return false; |
131 |
} |
132 |
|
133 |
return true; |
134 |
} |
135 |
|
136 |
|
137 |
public void initialize() { |
138 |
registerIcons(); |
139 |
} |
140 |
|
141 |
private void registerIcons() { |
142 |
IconThemeHelper.registerIcon("action", "application-print", this); |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Abre un di?logo para imprimir.
|
147 |
*
|
148 |
* @param layout
|
149 |
* Layout a imprimir.
|
150 |
*/
|
151 |
public void openDialogToPrint(LayoutPanel layoutPanel) { |
152 |
|
153 |
theLayoutPanel = layoutPanel; |
154 |
|
155 |
try {
|
156 |
if (layoutPanel.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) { |
157 |
layoutPanel.showPrintDialog(printerJob); |
158 |
} else {
|
159 |
layoutPanel.showPrintDialog(null);
|
160 |
} |
161 |
} catch (Exception e) { |
162 |
System.out.println("Excepci?n al abrir el di?logo de impresi?n: " |
163 |
+ e); |
164 |
e.printStackTrace(); |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Imprime el Layout que se pasa como par?metro.
|
170 |
*
|
171 |
* @param layout
|
172 |
* Layout a imprimir.
|
173 |
*/
|
174 |
public void printLayout(LayoutPanel layoutPanel) { |
175 |
|
176 |
theLayoutPanel = layoutPanel; |
177 |
|
178 |
try {
|
179 |
printerJob.setPrintable((Printable) PluginServices
|
180 |
.getExtension(org.gvsig.app.extension.Print.class)); |
181 |
|
182 |
// Actualizar attributes
|
183 |
PrintRequestAttributeSet att =
|
184 |
layoutPanel.getLayoutContext().getAttributes() |
185 |
.toPrintRequestAttributeSet(); |
186 |
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
187 |
|
188 |
if (m_cachePrintServices == null) { |
189 |
m_cachePrintServices = |
190 |
PrintServiceLookup.lookupPrintServices(flavor, null); |
191 |
} |
192 |
|
193 |
PrintService defaultService = null; |
194 |
|
195 |
if (m_cachePrintService == null) { |
196 |
defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
197 |
} |
198 |
|
199 |
if (m_cachePrintService == null) { |
200 |
m_cachePrintService = |
201 |
ServiceUI.printDialog(null, 200, 200, m_cachePrintServices, |
202 |
defaultService, flavor, att); |
203 |
} |
204 |
|
205 |
if (m_cachePrintService != null) { |
206 |
DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
|
207 |
PrintJobListener pjlistener = new PrintJobAdapter() { |
208 |
|
209 |
public void printDataTransferCompleted(PrintJobEvent e) { |
210 |
System.out.println("Fin de impresi?n"); |
211 |
} |
212 |
}; |
213 |
|
214 |
jobNuevo.addPrintJobListener(pjlistener); |
215 |
|
216 |
Doc doc =
|
217 |
new SimpleDoc( |
218 |
PluginServices |
219 |
.getExtension(org.gvsig.app.extension.Print.class), |
220 |
flavor, null);
|
221 |
jobNuevo.print(doc, att); |
222 |
} |
223 |
} catch (PrintException pe) { |
224 |
pe.printStackTrace(); |
225 |
} |
226 |
} |
227 |
|
228 |
/**
|
229 |
* Se dibuja sobre el graphics el Layout.
|
230 |
*
|
231 |
* @param g2 graphics sobre el que se dibuja.
|
232 |
*/
|
233 |
public void drawShapes(Graphics2D g2) { |
234 |
theLayoutPanel.drawLayoutPrint(g2); |
235 |
} |
236 |
|
237 |
|
238 |
public int print(Graphics g, PageFormat format, int pi) |
239 |
throws PrinterException { |
240 |
|
241 |
if (pi >= 1) { |
242 |
return Printable.NO_SUCH_PAGE; |
243 |
} |
244 |
|
245 |
Graphics2D g2d = (Graphics2D) g; |
246 |
|
247 |
|
248 |
AffineTransform at = g2d.getTransform();
|
249 |
g2d.translate(0, 0); |
250 |
theLayoutPanel.obtainRect(true);
|
251 |
|
252 |
g2d.scale((double) 72 / (double) (Attributes.DPI), |
253 |
(double) 72 / (double) (Attributes.DPI)); |
254 |
|
255 |
if (theLayoutPanel.getLayoutContext().getAttributes().isMargin()) {
|
256 |
g2d.setClip((int) (theLayoutPanel.getLayoutControl().getRect().getMinX() +
|
257 |
FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[2],
|
258 |
theLayoutPanel.getLayoutControl().getAT())), |
259 |
(int) (theLayoutPanel.getLayoutControl().getRect().getMinY() +
|
260 |
FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[0],
|
261 |
theLayoutPanel.getLayoutControl().getAT())), |
262 |
(int) (theLayoutPanel.getLayoutControl().getRect().getWidth() -
|
263 |
FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[2] +
|
264 |
theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[3], theLayoutPanel.getLayoutControl().getAT())),
|
265 |
(int) (theLayoutPanel.getLayoutControl().getRect().getHeight() -
|
266 |
FLayoutUtilities.fromSheetDistance(theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[0] +
|
267 |
theLayoutPanel.getLayoutContext().getAttributes().getAreaInsets()[1], theLayoutPanel.getLayoutControl().getAT())));
|
268 |
} |
269 |
|
270 |
drawShapes(g2d); |
271 |
g2d.setTransform(at); |
272 |
|
273 |
return Printable.PAGE_EXISTS; |
274 |
} |
275 |
} |