root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / PrintTable.java @ 24962
History | View | Annotate | Download (6.49 KB)
1 | 3547 | caballero | /* 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 | 24962 | vcaballero | import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel; |
55 | 3547 | caballero | |
56 | import com.iver.andami.PluginServices; |
||
57 | import com.iver.andami.plugins.Extension; |
||
58 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
59 | 3547 | caballero | |
60 | |||
61 | /**
|
||
62 | * Extensi?n para imprimir una tabla.
|
||
63 | *
|
||
64 | * @author Vicente Caballero Navarro
|
||
65 | */
|
||
66 | 5005 | jorpiell | public class PrintTable extends Extension implements Printable { |
67 | 3547 | caballero | private static Logger logger = Logger.getLogger(PrintTable.class.getName()); |
68 | private JTable table = null; |
||
69 | |||
70 | /**
|
||
71 | * DOCUMENT ME!
|
||
72 | */
|
||
73 | 5005 | jorpiell | public void initialize() { |
74 | 14821 | jmvivo | registerIcons(); |
75 | 3547 | caballero | } |
76 | |||
77 | 14821 | jmvivo | private void registerIcons(){ |
78 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
79 | 14821 | jmvivo | "document-print",
|
80 | this.getClass().getClassLoader().getResource("images/print.png") |
||
81 | ); |
||
82 | } |
||
83 | 3547 | caballero | /**
|
84 | * DOCUMENT ME!
|
||
85 | *
|
||
86 | * @param actionCommand DOCUMENT ME!
|
||
87 | */
|
||
88 | public void execute(String s) { |
||
89 | if (s.compareTo("PRINTTABLE") == 0){ |
||
90 | 6880 | cesar | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
91 | 24962 | vcaballero | table=((FeatureTableDocumentPanel)f).getTablePanel().getTable(); |
92 | 3547 | caballero | PluginServices.backgroundExecution(new Runnable() { |
93 | public void run() { |
||
94 | PrinterJob pj=PrinterJob.getPrinterJob(); |
||
95 | pj.setPrintable(PrintTable.this); |
||
96 | 3574 | caballero | if (pj.printDialog()){
|
97 | 24962 | vcaballero | try{
|
98 | 3547 | caballero | pj.print(); |
99 | }catch (Exception PrintException) {} |
||
100 | } |
||
101 | 3574 | caballero | } |
102 | 3547 | caballero | }); |
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 | 6880 | cesar | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
122 | 3547 | caballero | if (f == null) { |
123 | return false; |
||
124 | } |
||
125 | 24962 | vcaballero | if (f instanceof FeatureTableDocumentPanel) { |
126 | return true; |
||
127 | 3547 | caballero | } else {
|
128 | return false; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | 24962 | vcaballero | public int print(Graphics g, PageFormat pageFormat, |
133 | 3547 | caballero | 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 | 24962 | vcaballero | double pageHeight =
|
141 | 3547 | caballero | pageFormat.getImageableHeight()-fontHeight; |
142 | 24962 | vcaballero | double pageWidth =
|
143 | 3547 | caballero | pageFormat.getImageableWidth(); |
144 | 24962 | vcaballero | double tableWidth = (double) |
145 | 3547 | caballero | table.getColumnModel( |
146 | ).getTotalColumnWidth(); |
||
147 | 24962 | vcaballero | double scale = 1; |
148 | 3547 | caballero | 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 | 24962 | vcaballero | int totalNumPages=
|
165 | 3547 | caballero | (int)Math.ceil(( |
166 | (double)table.getRowCount())/
|
||
167 | numRowsOnAPage); |
||
168 | if(pageIndex>=totalNumPages) {
|
||
169 | return NO_SUCH_PAGE;
|
||
170 | } |
||
171 | |||
172 | 24962 | vcaballero | g2.translate(pageFormat.getImageableX(), |
173 | 3547 | caballero | 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 | 24962 | vcaballero | //If this piece of the table is smaller
|
183 | 3547 | caballero | //than the size available,
|
184 | //clip to the appropriate bounds.
|
||
185 | if (pageIndex + 1 == totalNumPages) { |
||
186 | 24962 | vcaballero | int lastRowPrinted =
|
187 | 3547 | caballero | numRowsOnAPage * pageIndex; |
188 | 24962 | vcaballero | int numRowsLeft =
|
189 | table.getRowCount() |
||
190 | 3547 | caballero | - lastRowPrinted; |
191 | 24962 | vcaballero | g2.setClip(0,
|
192 | 3547 | caballero | (int)(pageHeightForTable * pageIndex),
|
193 | (int) Math.ceil(tableWidthOnPage), |
||
194 | 24962 | vcaballero | (int) Math.ceil(oneRowHeight * |
195 | 3547 | caballero | numRowsLeft)); |
196 | } |
||
197 | //else clip to the entire area available.
|
||
198 | 24962 | vcaballero | else{
|
199 | g2.setClip(0,
|
||
200 | (int)(pageHeightForTable*pageIndex),
|
||
201 | 3547 | caballero | (int) Math.ceil(tableWidthOnPage), |
202 | 24962 | vcaballero | (int) Math.ceil(pageHeightForTable)); |
203 | 3547 | caballero | } |
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 | 24962 | vcaballero | (int) Math.ceil(tableWidthOnPage), |
212 | 3547 | caballero | (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 | } |