Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 20903

History | View | Annotate | Download (7.86 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 312 fernando
package com.iver.cit.gvsig;
48
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 5005 jorpiell
import com.iver.andami.PluginServices;
72
import com.iver.andami.plugins.Extension;
73 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
74 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.Attributes;
75 7304 caballero
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
76 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
77 312 fernando
78 5005 jorpiell
79 312 fernando
/**
80
 * Extensi?n desde la que se imprime.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84 5005 jorpiell
public class Print extends Extension implements Printable {
85 1219 vcaballero
        public static PrinterJob printerJob = PrinterJob.getPrinterJob();
86
        private static Layout l = null;
87 2429 caballero
        //private Paper paper;
88 1219 vcaballero
        Rectangle2D.Double aux = null;
89
        private int veces = 0;
90
        private PrintService[] m_cachePrintServices = null;
91
        private PrintService m_cachePrintService = null;
92 312 fernando
93 1219 vcaballero
        public void execute(String s) {
94 9212 fdiaz
                doPrint((Layout) PluginServices.getMDIManager().getActiveWindow());
95 1219 vcaballero
        }
96 9212 fdiaz
97
        public void doPrint(Layout layout) {
98
                l = layout;
99
                try {
100
                        PluginServices.backgroundExecution(new Runnable() {
101
                                        public void run() {
102 9392 caballero
                                                if (l.getLayoutContext().getAtributes().getType() == Attributes.CUSTOM) {
103 9212 fdiaz
                                                        l.showPrintDialog(printerJob);
104
                                                } else {
105
                                                        l.showPrintDialog(null);
106
                                                }
107
                                        }
108
                                });
109
110
                } catch (Exception e) {
111
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
112
                                e);
113
                }
114
        }
115 9392 caballero
116 2342 vcaballero
        public void setLayout(Layout l){
117
                this.l=l;
118
        }
119 18623 jdominguez
120 1219 vcaballero
        /**
121
         * Se dibuja sobre el graphics el Layout.
122
         *
123
         * @param g2 graphics sobre el que se dibuja.
124
         */
125
        public void drawShapes(Graphics2D g2) {
126
                l.drawLayoutPrint(g2);
127
        }
128 312 fernando
129 1219 vcaballero
        public boolean isVisible() {
130 6880 cesar
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
131 312 fernando
132 1219 vcaballero
                if (f == null) {
133
                        return false;
134
                }
135 312 fernando
136 5900 jorpiell
                return (f instanceof Layout);
137 1219 vcaballero
        }
138 312 fernando
139 1219 vcaballero
        public boolean isEnabled() {
140 6880 cesar
                Layout f = (Layout) PluginServices.getMDIManager().getActiveWindow();
141 312 fernando
142 1219 vcaballero
                if (f == null) {
143
                        return false;
144
                }
145 312 fernando
146 1219 vcaballero
                return true;
147
        }
148 312 fernando
149 1219 vcaballero
        public int print(Graphics g, PageFormat format, int pi)
150
                throws PrinterException {
151
                if (pi >= 1) {
152
                        return Printable.NO_SUCH_PAGE;
153
                }
154 312 fernando
155 1219 vcaballero
                System.err.println("Clip 0 = " + g.getClip());
156 312 fernando
157 1219 vcaballero
                Graphics2D g2d = (Graphics2D) g;
158 312 fernando
159 1219 vcaballero
                double x = format.getImageableX();
160
                double y = format.getImageableY();
161
                double w = format.getImageableWidth();
162
                double h = format.getImageableHeight();
163 312 fernando
164 1219 vcaballero
                //System.err.println("Orientaci?n en Print: " + format.getOrientation());
165
                System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")");
166
                System.err.println("Clip 1 = " + g2d.getClip());
167 312 fernando
168 1219 vcaballero
                AffineTransform at = g2d.getTransform();
169
                g2d.translate(0, 0);
170 13402 caballero
                l.obtainRect(true);
171 312 fernando
172 1219 vcaballero
                //LWSAffineTransform at = g2d.getTransform();
173
                g2d.scale((double) 72 / (double) (Attributes.DPI),
174
                        (double) 72 / (double) (Attributes.DPI));
175
                System.err.println("Clip 2 =" + g2d.getClip());
176 312 fernando
177 9392 caballero
                if (l.getLayoutContext().getAtributes().isMargin()) {
178
                        g2d.setClip((int) (l.getLayoutControl().getRect().getMinX() +
179
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[2],
180
                                        l.getLayoutControl().getAT())),
181
                                (int) (l.getLayoutControl().getRect().getMinY() +
182
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[0],
183
                                        l.getLayoutControl().getAT())),
184
                                (int) (l.getLayoutControl().getRect().getWidth() -
185
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[2] +
186
                                        l.getLayoutContext().getAtributes().m_area[3], l.getLayoutControl().getAT())),
187
                                (int) (l.getLayoutControl().getRect().getHeight() -
188
                                FLayoutUtilities.fromSheetDistance(l.getLayoutContext().getAtributes().m_area[0] +
189
                                        l.getLayoutContext().getAtributes().m_area[1], l.getLayoutControl().getAT())));
190 1219 vcaballero
                }
191 312 fernando
192 1219 vcaballero
                veces++;
193
                System.out.println("veces = " + veces);
194 312 fernando
195 1219 vcaballero
                drawShapes(g2d);
196
                g2d.setTransform(at);
197 312 fernando
198 1219 vcaballero
                return Printable.PAGE_EXISTS;
199
        }
200 312 fernando
201 5005 jorpiell
        public void initialize() {
202 14821 jmvivo
                registerIcons();
203 596 fernando
        }
204 1219 vcaballero
205 14821 jmvivo
        private void registerIcons(){
206 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
207 14821 jmvivo
                                "document-print",
208
                                this.getClass().getClassLoader().getResource("images/print.png")
209
                        );
210
        }
211
212 1219 vcaballero
        /**
213
         * Abre un di?logo para imprimir.
214
         *
215
         * @param layout Layout a imprimir.
216
         */
217 724 vcaballero
        public void OpenDialogToPrint(Layout layout) {
218 1219 vcaballero
                l = layout;
219 724 vcaballero
220 1219 vcaballero
                try {
221 9392 caballero
                        if (layout.getLayoutContext().getAtributes().getType() == Attributes.CUSTOM) {
222 1219 vcaballero
                                layout.showPrintDialog(printerJob);
223
                        } else {
224
                                layout.showPrintDialog(null);
225
                        }
226
                } catch (Exception e) {
227
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
228
                                e);
229
                        e.printStackTrace();
230
                }
231 724 vcaballero
        }
232 1219 vcaballero
233
        /**
234
         * Imprime el Layout que se pasa como par?metro.
235
         *
236
         * @param layout Layout a imprimir.
237
         */
238
        public void printLayout(Layout layout) {
239
                l = layout;
240
241
                try {
242
                        printerJob.setPrintable((Printable) PluginServices.getExtension(
243
                                        com.iver.cit.gvsig.Print.class));
244
245
                        //Actualizar attributes
246 9392 caballero
                        PrintRequestAttributeSet att = layout.getLayoutContext().getAtributes()
247 1219 vcaballero
                                                                                                 .toPrintAttributes();
248
                        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
249
250
                        if (m_cachePrintServices == null) {
251
                                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
252
                                                null);
253
                        }
254
255
                        PrintService defaultService = null;
256
257
                        if (m_cachePrintService == null) {
258
                                defaultService = PrintServiceLookup.lookupDefaultPrintService();
259
                        }
260
261
                        if (m_cachePrintService == null) {
262
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
263
                                                m_cachePrintServices, defaultService, flavor, att);
264
                        }
265
266
                        if (m_cachePrintService != null) {
267
                                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
268
                                PrintJobListener pjlistener = new PrintJobAdapter() {
269
                                                public void printDataTransferCompleted(PrintJobEvent e) {
270
                                                        System.out.println("Fin de impresi?n");
271
                                                }
272
                                        };
273
274
                                jobNuevo.addPrintJobListener(pjlistener);
275
276
                                Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
277
                                                        com.iver.cit.gvsig.Print.class), flavor, null);
278
                                jobNuevo.print(doc, att);
279
                        }
280
                } catch (PrintException pe) {
281
                        pe.printStackTrace();
282
                }
283
        }
284 312 fernando
}