svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / PrintTable.java @ 28407
History | View | Annotate | Download (6.46 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.IWindow; |
58 |
import com.iver.cit.gvsig.project.documents.table.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 |
registerIcons(); |
75 |
} |
76 |
|
77 |
private void registerIcons(){ |
78 |
PluginServices.getIconTheme().registerDefault( |
79 |
"document-print",
|
80 |
this.getClass().getClassLoader().getResource("images/print.png") |
81 |
); |
82 |
} |
83 |
/**
|
84 |
* DOCUMENT ME!
|
85 |
*
|
86 |
* @param actionCommand DOCUMENT ME!
|
87 |
*/
|
88 |
public void execute(String s) { |
89 |
if (s.compareTo("PRINTTABLE") == 0){ |
90 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
91 |
table=((Table)f).getTable(); |
92 |
PluginServices.backgroundExecution(new Runnable() { |
93 |
public void run() { |
94 |
PrinterJob pj=PrinterJob.getPrinterJob(); |
95 |
pj.setPrintable(PrintTable.this); |
96 |
if (pj.printDialog()){
|
97 |
try{
|
98 |
pj.print(); |
99 |
}catch (Exception PrintException) {} |
100 |
} |
101 |
} |
102 |
}); |
103 |
} |
104 |
} |
105 |
|
106 |
/**
|
107 |
* DOCUMENT ME!
|
108 |
*
|
109 |
* @return DOCUMENT ME!
|
110 |
*/
|
111 |
public boolean isEnabled() { |
112 |
return true; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* DOCUMENT ME!
|
117 |
*
|
118 |
* @return DOCUMENT ME!
|
119 |
*/
|
120 |
public boolean isVisible() { |
121 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
122 |
if (f == null) { |
123 |
return false; |
124 |
} |
125 |
if (f instanceof Table) { |
126 |
return true; |
127 |
} else {
|
128 |
return false; |
129 |
} |
130 |
} |
131 |
|
132 |
public int print(Graphics g, PageFormat pageFormat, |
133 |
int pageIndex) throws PrinterException { |
134 |
Graphics2D g2 = (Graphics2D) g; |
135 |
g2.setColor(Color.black);
|
136 |
int fontHeight=g2.getFontMetrics().getHeight();
|
137 |
int fontDesent=g2.getFontMetrics().getDescent();
|
138 |
|
139 |
//leave room for page number
|
140 |
double pageHeight =
|
141 |
pageFormat.getImageableHeight()-fontHeight; |
142 |
double pageWidth =
|
143 |
pageFormat.getImageableWidth(); |
144 |
double tableWidth = (double) |
145 |
table.getColumnModel( |
146 |
).getTotalColumnWidth(); |
147 |
double scale = 1; |
148 |
if (tableWidth >= pageWidth) {
|
149 |
scale = pageWidth / tableWidth; |
150 |
} |
151 |
|
152 |
double headerHeightOnPage=
|
153 |
table.getTableHeader( |
154 |
).getHeight()*scale; |
155 |
double tableWidthOnPage=tableWidth*scale;
|
156 |
|
157 |
double oneRowHeight=(table.getRowHeight()+
|
158 |
table.getRowMargin())*scale; |
159 |
int numRowsOnAPage=
|
160 |
(int)((pageHeight-headerHeightOnPage)/
|
161 |
oneRowHeight); |
162 |
double pageHeightForTable=oneRowHeight*
|
163 |
numRowsOnAPage; |
164 |
int totalNumPages=
|
165 |
(int)Math.ceil(( |
166 |
(double)table.getRowCount())/
|
167 |
numRowsOnAPage); |
168 |
if(pageIndex>=totalNumPages) {
|
169 |
return NO_SUCH_PAGE;
|
170 |
} |
171 |
|
172 |
g2.translate(pageFormat.getImageableX(), |
173 |
pageFormat.getImageableY()); |
174 |
// bottom center
|
175 |
g2.drawString("Page: "+(pageIndex+1), |
176 |
(int)pageWidth/2-35, (int)(pageHeight |
177 |
+fontHeight-fontDesent)); |
178 |
|
179 |
g2.translate(0f,headerHeightOnPage);
|
180 |
g2.translate(0f,-pageIndex*pageHeightForTable);
|
181 |
|
182 |
//If this piece of the table is smaller
|
183 |
//than the size available,
|
184 |
//clip to the appropriate bounds.
|
185 |
if (pageIndex + 1 == totalNumPages) { |
186 |
int lastRowPrinted =
|
187 |
numRowsOnAPage * pageIndex; |
188 |
int numRowsLeft =
|
189 |
table.getRowCount() |
190 |
- lastRowPrinted; |
191 |
g2.setClip(0,
|
192 |
(int)(pageHeightForTable * pageIndex),
|
193 |
(int) Math.ceil(tableWidthOnPage), |
194 |
(int) Math.ceil(oneRowHeight * |
195 |
numRowsLeft)); |
196 |
} |
197 |
//else clip to the entire area available.
|
198 |
else{
|
199 |
g2.setClip(0,
|
200 |
(int)(pageHeightForTable*pageIndex),
|
201 |
(int) Math.ceil(tableWidthOnPage), |
202 |
(int) Math.ceil(pageHeightForTable)); |
203 |
} |
204 |
|
205 |
g2.scale(scale,scale); |
206 |
table.paint(g2); |
207 |
g2.scale(1/scale,1/scale); |
208 |
g2.translate(0f,pageIndex*pageHeightForTable);
|
209 |
g2.translate(0f, -headerHeightOnPage);
|
210 |
g2.setClip(0, 0, |
211 |
(int) Math.ceil(tableWidthOnPage), |
212 |
(int)Math.ceil(headerHeightOnPage)); |
213 |
g2.scale(scale,scale); |
214 |
table.getTableHeader().paint(g2); |
215 |
//paint header at top
|
216 |
|
217 |
return Printable.PAGE_EXISTS; |
218 |
} |
219 |
|
220 |
|
221 |
} |