Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / Print.java @ 33749

History | View | Annotate | Download (7.92 KB)

1 312 fernando
/*
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 1103 fjp
/* 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 29596 jpiera
package org.gvsig.app.extension;
48 312 fernando
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 724 vcaballero
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 29596 jpiera
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 31496 jjdelcerro
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
77 312 fernando
78 5005 jorpiell
79 29596 jpiera
80 312 fernando
/**
81
 * Extensi?n desde la que se imprime.
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85 5005 jorpiell
public class Print extends Extension implements Printable {
86 1219 vcaballero
        public static PrinterJob printerJob = PrinterJob.getPrinterJob();
87 31496 jjdelcerro
        private static LayoutPanel l = null;
88 2429 caballero
        //private Paper paper;
89 1219 vcaballero
        Rectangle2D.Double aux = null;
90
        private int veces = 0;
91
        private PrintService[] m_cachePrintServices = null;
92
        private PrintService m_cachePrintService = null;
93 312 fernando
94 1219 vcaballero
        public void execute(String s) {
95 31496 jjdelcerro
                doPrint((LayoutPanel) PluginServices.getMDIManager().getActiveWindow());
96 1219 vcaballero
        }
97 9212 fdiaz
98 31496 jjdelcerro
        public void doPrint(LayoutPanel layout) {
99 9212 fdiaz
                l = layout;
100
                try {
101
                        PluginServices.backgroundExecution(new Runnable() {
102
                                        public void run() {
103 23069 vcaballero
                                                if (l.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) {
104 9212 fdiaz
                                                        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 9392 caballero
117 31496 jjdelcerro
        public void setLayout(LayoutPanel l){
118 2342 vcaballero
                this.l=l;
119
        }
120 18623 jdominguez
121 1219 vcaballero
        /**
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 312 fernando
130 1219 vcaballero
        public boolean isVisible() {
131 6880 cesar
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
132 312 fernando
133 1219 vcaballero
                if (f == null) {
134
                        return false;
135
                }
136 312 fernando
137 31496 jjdelcerro
                return (f instanceof LayoutPanel);
138 1219 vcaballero
        }
139 312 fernando
140 1219 vcaballero
        public boolean isEnabled() {
141 31496 jjdelcerro
                LayoutPanel f = (LayoutPanel) PluginServices.getMDIManager().getActiveWindow();
142 312 fernando
143 1219 vcaballero
                if (f == null) {
144
                        return false;
145
                }
146 312 fernando
147 1219 vcaballero
                return true;
148
        }
149 312 fernando
150 1219 vcaballero
        public int print(Graphics g, PageFormat format, int pi)
151
                throws PrinterException {
152
                if (pi >= 1) {
153
                        return Printable.NO_SUCH_PAGE;
154
                }
155 312 fernando
156 1219 vcaballero
                System.err.println("Clip 0 = " + g.getClip());
157 312 fernando
158 1219 vcaballero
                Graphics2D g2d = (Graphics2D) g;
159 312 fernando
160 1219 vcaballero
                double x = format.getImageableX();
161
                double y = format.getImageableY();
162
                double w = format.getImageableWidth();
163
                double h = format.getImageableHeight();
164 312 fernando
165 1219 vcaballero
                //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 312 fernando
169 1219 vcaballero
                AffineTransform at = g2d.getTransform();
170
                g2d.translate(0, 0);
171 13402 caballero
                l.obtainRect(true);
172 312 fernando
173 1219 vcaballero
                //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 312 fernando
178 23069 vcaballero
                if (l.getLayoutContext().getAttributes().isMargin()) {
179 9392 caballero
                        g2d.setClip((int) (l.getLayoutControl().getRect().getMinX() +
180 23069 vcaballero
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAttributes().m_area[2],
181 9392 caballero
                                        l.getLayoutControl().getAT())),
182
                                (int) (l.getLayoutControl().getRect().getMinY() +
183 23069 vcaballero
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAttributes().m_area[0],
184 9392 caballero
                                        l.getLayoutControl().getAT())),
185
                                (int) (l.getLayoutControl().getRect().getWidth() -
186 23069 vcaballero
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAttributes().m_area[2] +
187
                                        l.getLayoutContext().getAttributes().m_area[3], l.getLayoutControl().getAT())),
188 9392 caballero
                                (int) (l.getLayoutControl().getRect().getHeight() -
189 23069 vcaballero
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAttributes().m_area[0] +
190
                                        l.getLayoutContext().getAttributes().m_area[1], l.getLayoutControl().getAT())));
191 1219 vcaballero
                }
192 312 fernando
193 1219 vcaballero
                veces++;
194
                System.out.println("veces = " + veces);
195 312 fernando
196 1219 vcaballero
                drawShapes(g2d);
197
                g2d.setTransform(at);
198 312 fernando
199 1219 vcaballero
                return Printable.PAGE_EXISTS;
200
        }
201 312 fernando
202 5005 jorpiell
        public void initialize() {
203 14821 jmvivo
                registerIcons();
204 596 fernando
        }
205 1219 vcaballero
206 14821 jmvivo
        private void registerIcons(){
207 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
208 14821 jmvivo
                                "document-print",
209
                                this.getClass().getClassLoader().getResource("images/print.png")
210
                        );
211
        }
212
213 1219 vcaballero
        /**
214
         * Abre un di?logo para imprimir.
215
         *
216
         * @param layout Layout a imprimir.
217
         */
218 31496 jjdelcerro
        public void OpenDialogToPrint(LayoutPanel layout) {
219 1219 vcaballero
                l = layout;
220 724 vcaballero
221 1219 vcaballero
                try {
222 23069 vcaballero
                        if (layout.getLayoutContext().getAttributes().getType() == Attributes.CUSTOM) {
223 1219 vcaballero
                                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 724 vcaballero
        }
233 1219 vcaballero
234
        /**
235
         * Imprime el Layout que se pasa como par?metro.
236
         *
237
         * @param layout Layout a imprimir.
238
         */
239 31496 jjdelcerro
        public void printLayout(LayoutPanel layout) {
240 1219 vcaballero
                l = layout;
241
242
                try {
243
                        printerJob.setPrintable((Printable) PluginServices.getExtension(
244 29596 jpiera
                                        org.gvsig.app.extension.Print.class));
245 1219 vcaballero
246
                        //Actualizar attributes
247 23069 vcaballero
                        PrintRequestAttributeSet att = layout.getLayoutContext().getAttributes()
248 29333 jmvivo
                                                                                                 .toPrintRequestAttributeSet();
249 1219 vcaballero
                        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 29333 jmvivo
                                Doc doc = new SimpleDoc(PluginServices.getExtension(
278 29596 jpiera
                                                        org.gvsig.app.extension.Print.class), flavor, null);
279 1219 vcaballero
                                jobNuevo.print(doc, att);
280
                        }
281
                } catch (PrintException pe) {
282
                        pe.printStackTrace();
283
                }
284
        }
285 312 fernando
}