Statistics
| Revision:

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

History | View | Annotate | Download (6.22 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 extends Extension implements 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 initialize() {
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
                                if (pj.printDialog()){
90
                                try{ 
91
                                        pj.print();
92
                                }catch (Exception PrintException) {}
93
                                }
94
                                }
95
                        });
96
            }
97
    }
98

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

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

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

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

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

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

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

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

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

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

    
210
                 return Printable.PAGE_EXISTS;
211
       }
212

    
213

    
214
}