Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Print.java @ 2567

History | View | Annotate | Download (8.59 KB)

1
/*
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
/* 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
package com.iver.cit.gvsig;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.plugins.Extension;
51
import com.iver.andami.ui.mdiManager.View;
52

    
53
import com.iver.cit.gvsig.gui.layout.Attributes;
54
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
55
import com.iver.cit.gvsig.gui.layout.Layout;
56

    
57
import java.awt.Graphics;
58
import java.awt.Graphics2D;
59
import java.awt.geom.AffineTransform;
60
import java.awt.geom.Rectangle2D;
61
import java.awt.print.PageFormat;
62
import java.awt.print.Printable;
63
import java.awt.print.PrinterException;
64
import java.awt.print.PrinterJob;
65

    
66
import javax.print.Doc;
67
import javax.print.DocFlavor;
68
import javax.print.DocPrintJob;
69
import javax.print.PrintException;
70
import javax.print.PrintService;
71
import javax.print.PrintServiceLookup;
72
import javax.print.ServiceUI;
73
import javax.print.SimpleDoc;
74
import javax.print.attribute.PrintRequestAttributeSet;
75
import javax.print.event.PrintJobAdapter;
76
import javax.print.event.PrintJobEvent;
77
import javax.print.event.PrintJobListener;
78

    
79

    
80
/**
81
 * Extensi?n desde la que se imprime.
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class Print implements Extension, Printable {
86
        public static PrinterJob printerJob = PrinterJob.getPrinterJob();
87
        private static Layout l = null;
88
        //private Paper paper;
89
        Rectangle2D.Double aux = null;
90
        private int veces = 0;
91
        private PrintService[] m_cachePrintServices = null;
92
        private PrintService m_cachePrintService = null;
93

    
94
        /**
95
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
96
         */
97
        public void execute(String s) {
98
                l = (Layout) PluginServices.getMDIManager().getActiveView();
99

    
100
                //PageFormat format = printerJob.defaultPage(); //new PageFormat();
101
                //paper = format.getPaper();
102

    
103
                //double margin = 72.0 / 25.4 * 5.0;
104
                try {
105
                        PluginServices.backgroundExecution(new Runnable() {
106
                                        public void run() {
107
                                                if (l.getAtributes().getType() == Attributes.CUSTOM) {
108
                                                        l.showPrintDialog(printerJob);
109
                                                } else {
110
                                                        l.showPrintDialog(null);
111
                                                }
112
                                        }
113
                                });
114

    
115
                        //Thread.sleep(10000);
116
                } catch (Exception e) {
117
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
118
                                e);
119
                }
120
        }
121
        public void setLayout(Layout l){
122
                this.l=l;
123
        }
124
        /**
125
         * Se dibuja sobre el graphics el Layout.
126
         *
127
         * @param g2 graphics sobre el que se dibuja.
128
         */
129
        public void drawShapes(Graphics2D g2) {
130
                ///double w = paper.getWidth();
131
                //System.out.println("w " + w);
132
                ///double h = paper.getHeight();
133
                //System.out.println("h " + h);
134
                //g2.translate(-l.getRect().x, -l.getRect().y);
135
                l.drawLayoutPrint(g2);
136

    
137
                // System.out.println("drawShapes = "+g2.getTransform().getScaleX());
138
                //g2.translate(l.getRect().x, l.getRect().y);
139
        }
140

    
141
        /**
142
         * @see com.iver.andami.plugins.Extension#isVisible()
143
         */
144
        public boolean isVisible() {
145
                View f = PluginServices.getMDIManager().getActiveView();
146

    
147
                if (f == null) {
148
                        return false;
149
                }
150

    
151
                return (f.getClass() == Layout.class);
152
        }
153

    
154
        /**
155
         * @see com.iver.mdiApp.plugins.Extension#isEnabled()
156
         */
157
        public boolean isEnabled() {
158
                Layout f = (Layout) PluginServices.getMDIManager().getActiveView();
159

    
160
                if (f == null) {
161
                        return false;
162
                }
163

    
164
                return true;
165
        }
166

    
167
        /* (non-Javadoc)
168
         * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
169
         */
170
        public int print(Graphics g, PageFormat format, int pi)
171
                throws PrinterException {
172
                if (pi >= 1) {
173
                        return Printable.NO_SUCH_PAGE;
174
                }
175

    
176
                System.err.println("Clip 0 = " + g.getClip());
177

    
178
                Graphics2D g2d = (Graphics2D) g;
179

    
180
                double x = format.getImageableX();
181
                double y = format.getImageableY();
182
                double w = format.getImageableWidth();
183
                double h = format.getImageableHeight();
184

    
185
                //System.err.println("Orientaci?n en Print: " + format.getOrientation());
186
                System.out.println("print:(" + x + "," + y + "," + w + "," + h + ")");
187
                System.err.println("Clip 1 = " + g2d.getClip());
188

    
189
                AffineTransform at = g2d.getTransform();
190
                g2d.translate(0, 0);
191
                l.obtainRect(true);
192

    
193
                //LWSAffineTransform at = g2d.getTransform();
194
                g2d.scale((double) 72 / (double) (Attributes.DPI),
195
                        (double) 72 / (double) (Attributes.DPI));
196
                System.err.println("Clip 2 =" + g2d.getClip());
197

    
198
                if (l.getAtributes().isMargin()) {
199
                        g2d.setClip((int) (l.getRect().getMinX() +
200
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2],
201
                                        l.getAT())),
202
                                (int) (l.getRect().getMinY() +
203
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0],
204
                                        l.getAT())),
205
                                (int) (l.getRect().getWidth() -
206
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[2] +
207
                                        l.getAtributes().m_area[3], l.getAT())),
208
                                (int) (l.getRect().getHeight() -
209
                                FLayoutUtilities.fromSheetDistance(l.getAtributes().m_area[0] +
210
                                        l.getAtributes().m_area[1], l.getAT())));
211
                } else {
212
                        /*
213
                           Rectangle rec=(Rectangle)g2d.getClipBounds();
214
                           Rectangle2D.Double rec2d=new Rectangle2D.Double();
215
                           rec2d.setRect(rec.x,rec.y,l.getRect().width,rec.getHeight());
216
                           g2d.setClip((int)rec2d.x,(int)rec2d.y,(int)rec2d.width,(int)rec2d.height);
217
                         */
218
                        /* g2d.setClip((int) l.getRect().getMinX(),
219
                           (int) l.getRect().getMinY(), (int) l.getRect().getWidth(),
220
                           (int) l.getRect().getHeight()); */
221
                }
222

    
223
                veces++;
224
                System.out.println("veces = " + veces);
225

    
226
                drawShapes(g2d);
227
                g2d.setTransform(at);
228

    
229
                return Printable.PAGE_EXISTS;
230
        }
231

    
232
        /**
233
         * @see com.iver.andami.plugins.Extension#inicializar()
234
         */
235
        public void inicializar() {
236
        }
237

    
238
        /**
239
         * Abre un di?logo para imprimir.
240
         *
241
         * @param layout Layout a imprimir.
242
         */
243
        public void OpenDialogToPrint(Layout layout) {
244
                l = layout;
245

    
246
                //PageFormat format = printerJob.defaultPage(); //new PageFormat();
247
                //paper = format.getPaper();
248

    
249
                try {
250
                        if (layout.getAtributes().getType() == Attributes.CUSTOM) {
251
                                layout.showPrintDialog(printerJob);
252
                        } else {
253
                                layout.showPrintDialog(null);
254
                        }
255
                } catch (Exception e) {
256
                        System.out.println("Excepci?n al abrir el di?logo de impresi?n: " +
257
                                e);
258
                        e.printStackTrace();
259
                }
260
        }
261

    
262
        /**
263
         * Imprime el Layout que se pasa como par?metro.
264
         *
265
         * @param layout Layout a imprimir.
266
         */
267
        public void printLayout(Layout layout) {
268
                l = layout;
269

    
270
                try {
271
                        printerJob.setPrintable((Printable) PluginServices.getExtension(
272
                                        com.iver.cit.gvsig.Print.class));
273

    
274
                        //Actualizar attributes
275
                        PrintRequestAttributeSet att = layout.getAtributes()
276
                                                                                                 .toPrintAttributes();
277
                        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
278

    
279
                        if (m_cachePrintServices == null) {
280
                                m_cachePrintServices = PrintServiceLookup.lookupPrintServices(flavor,
281
                                                null);
282
                        }
283

    
284
                        PrintService defaultService = null;
285

    
286
                        if (m_cachePrintService == null) {
287
                                defaultService = PrintServiceLookup.lookupDefaultPrintService();
288
                        }
289

    
290
                        if (m_cachePrintService == null) {
291
                                m_cachePrintService = ServiceUI.printDialog(null, 200, 200,
292
                                                m_cachePrintServices, defaultService, flavor, att);
293
                        }
294

    
295
                        if (m_cachePrintService != null) {
296
                                DocPrintJob jobNuevo = m_cachePrintService.createPrintJob();
297
                                PrintJobListener pjlistener = new PrintJobAdapter() {
298
                                                public void printDataTransferCompleted(PrintJobEvent e) {
299
                                                        System.out.println("Fin de impresi?n");
300
                                                }
301
                                        };
302

    
303
                                jobNuevo.addPrintJobListener(pjlistener);
304

    
305
                                Doc doc = new SimpleDoc((Printable) PluginServices.getExtension(
306
                                                        com.iver.cit.gvsig.Print.class), flavor, null);
307
                                jobNuevo.print(doc, att);
308
                        }
309
                } catch (PrintException pe) {
310
                        pe.printStackTrace();
311
                }
312
        }
313
}