Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / PrintTable.java @ 3547

History | View | Annotate | Download (6.21 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.print.PageFormat;
47
import java.awt.print.Printable;
48
import java.awt.print.PrinterException;
49
import java.awt.print.PrinterJob;
50

    
51
import javax.swing.JTable;
52

    
53
import org.apache.log4j.Logger;
54

    
55
import com.iver.andami.PluginServices;
56
import com.iver.andami.plugins.Extension;
57
import com.iver.andami.ui.mdiManager.View;
58
import com.iver.cit.gvsig.gui.Table;
59

    
60

    
61
/**
62
 * Extensi?n para imprimir una tabla.
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class PrintTable implements Extension, Printable {
67
        private static Logger logger = Logger.getLogger(PrintTable.class.getName());
68
        private JTable table = null;
69

    
70
        /**
71
     * DOCUMENT ME!
72
     */
73
    public void inicializar() {
74
    }
75

    
76
    /**
77
     * DOCUMENT ME!
78
     *
79
     * @param actionCommand DOCUMENT ME!
80
     */
81
    public void execute(String s) {
82
            if (s.compareTo("PRINTTABLE") == 0){
83
                    View f = PluginServices.getMDIManager().getActiveView();
84
                    table=((Table)f).getTable();
85
                    PluginServices.backgroundExecution(new Runnable() {
86
                                public void run() {
87
                                        PrinterJob pj=PrinterJob.getPrinterJob();
88
                                pj.setPrintable(PrintTable.this);
89
                                pj.printDialog();
90
                                try{ 
91
                                        pj.print();
92
                                }catch (Exception PrintException) {}
93
                                }
94

    
95
                                
96
                        });
97
            }
98
    }
99

    
100
    /**
101
     * DOCUMENT ME!
102
     *
103
     * @return DOCUMENT ME!
104
     */
105
    public boolean isEnabled() {
106
        return true;
107
    }
108

    
109
    /**
110
     * DOCUMENT ME!
111
     *
112
     * @return DOCUMENT ME!
113
     */
114
    public boolean isVisible() {
115
            View f = PluginServices.getMDIManager().getActiveView();
116
                if (f == null) {
117
                        return false;
118
                }
119
                if (f.getClass() == Table.class) {
120
                        return true; 
121
                } else {
122
                        return false;
123
                }
124
    }
125

    
126
    public int print(Graphics g, PageFormat pageFormat, 
127
            int pageIndex) throws PrinterException {
128
                 Graphics2D  g2 = (Graphics2D) g;
129
                 g2.setColor(Color.black);
130
                 int fontHeight=g2.getFontMetrics().getHeight();
131
                 int fontDesent=g2.getFontMetrics().getDescent();
132

    
133
                 //leave room for page number
134
                 double pageHeight = 
135
                   pageFormat.getImageableHeight()-fontHeight;
136
                 double pageWidth = 
137
                   pageFormat.getImageableWidth();
138
                 double tableWidth = (double) 
139
              table.getColumnModel(
140
              ).getTotalColumnWidth();
141
                 double scale = 1; 
142
                 if (tableWidth >= pageWidth) {
143
                    scale =  pageWidth / tableWidth;
144
            }
145

    
146
                 double headerHeightOnPage=
147
                     table.getTableHeader(
148
                     ).getHeight()*scale;
149
                 double tableWidthOnPage=tableWidth*scale;
150

    
151
                 double oneRowHeight=(table.getRowHeight()+
152
                          table.getRowMargin())*scale;
153
                 int numRowsOnAPage=
154
                  (int)((pageHeight-headerHeightOnPage)/
155
                                      oneRowHeight);
156
                 double pageHeightForTable=oneRowHeight*
157
                                             numRowsOnAPage;
158
                 int totalNumPages= 
159
                       (int)Math.ceil((
160
                    (double)table.getRowCount())/
161
                                        numRowsOnAPage);
162
                 if(pageIndex>=totalNumPages) {
163
                          return NO_SUCH_PAGE;
164
                 }
165

    
166
                 g2.translate(pageFormat.getImageableX(), 
167
                           pageFormat.getImageableY());
168
//    bottom center
169
                 g2.drawString("Page: "+(pageIndex+1),
170
                     (int)pageWidth/2-35, (int)(pageHeight
171
                     +fontHeight-fontDesent));
172

    
173
                 g2.translate(0f,headerHeightOnPage);
174
                 g2.translate(0f,-pageIndex*pageHeightForTable);
175

    
176
                 //If this piece of the table is smaller 
177
                 //than the size available,
178
                 //clip to the appropriate bounds.
179
                 if (pageIndex + 1 == totalNumPages) {
180
               int lastRowPrinted = 
181
                     numRowsOnAPage * pageIndex;
182
               int numRowsLeft = 
183
                     table.getRowCount() 
184
                     - lastRowPrinted;
185
               g2.setClip(0, 
186
                 (int)(pageHeightForTable * pageIndex),
187
                 (int) Math.ceil(tableWidthOnPage),
188
                 (int) Math.ceil(oneRowHeight * 
189
                                   numRowsLeft));
190
                 }
191
                 //else clip to the entire area available.
192
                 else{    
193
                 g2.setClip(0, 
194
                 (int)(pageHeightForTable*pageIndex), 
195
                 (int) Math.ceil(tableWidthOnPage),
196
                 (int) Math.ceil(pageHeightForTable));        
197
                 }
198

    
199
                 g2.scale(scale,scale);
200
                 table.paint(g2);
201
                 g2.scale(1/scale,1/scale);
202
                 g2.translate(0f,pageIndex*pageHeightForTable);
203
                 g2.translate(0f, -headerHeightOnPage);
204
                 g2.setClip(0, 0,
205
                   (int) Math.ceil(tableWidthOnPage), 
206
              (int)Math.ceil(headerHeightOnPage));
207
                 g2.scale(scale,scale);
208
                 table.getTableHeader().paint(g2);
209
                 //paint header at top
210

    
211
                 return Printable.PAGE_EXISTS;
212
       }
213

    
214

    
215
}