Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / PrintTable.java @ 25777

History | View | Annotate | Download (6.73 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
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.messages.NotificationManager;
59
import com.iver.andami.plugins.Extension;
60
import com.iver.andami.ui.mdiManager.IWindow;
61

    
62

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

    
72
        /**
73
     * DOCUMENT ME!
74
     */
75
    public void initialize() {
76
            registerIcons();
77
    }
78

    
79
    private void registerIcons(){
80
            PluginServices.getIconTheme().registerDefault(
81
                                "document-print",
82
                                this.getClass().getClassLoader().getResource("images/print.png")
83
                        );
84
    }
85
    /**
86
     * DOCUMENT ME!
87
     *
88
     * @param actionCommand DOCUMENT ME!
89
     */
90
    public void execute(String s) {
91
            if (s.compareTo("PRINTTABLE") == 0){
92
                    IWindow f = PluginServices.getMDIManager().getActiveWindow();
93
                    try {
94
                                table=((FeatureTableDocumentPanel)f).getTablePanel().getTable();
95
                        } catch (DataException e) {
96
                                NotificationManager.showMessageError(PluginServices.getText(this, "print_table_error"), e);
97
                        }
98
                    PluginServices.backgroundExecution(new Runnable() {
99
                                public void run() {
100
                                        PrinterJob pj=PrinterJob.getPrinterJob();
101
                                pj.setPrintable(PrintTable.this);
102
                                if (pj.printDialog()){
103
                                try{
104
                                        pj.print();
105
                                }catch (Exception PrintException) {}
106
                                }
107
                                }
108
                        });
109
            }
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     *
115
     * @return DOCUMENT ME!
116
     */
117
    public boolean isEnabled() {
118
        return true;
119
    }
120

    
121
    /**
122
     * DOCUMENT ME!
123
     *
124
     * @return DOCUMENT ME!
125
     */
126
    public boolean isVisible() {
127
            IWindow f = PluginServices.getMDIManager().getActiveWindow();
128
                if (f == null) {
129
                        return false;
130
                }
131
                if (f instanceof FeatureTableDocumentPanel) {
132
                        return true;
133
                } else {
134
                        return false;
135
                }
136
    }
137

    
138
    public int print(Graphics g, PageFormat pageFormat,
139
            int pageIndex) throws PrinterException {
140
                 Graphics2D  g2 = (Graphics2D) g;
141
                 g2.setColor(Color.black);
142
                 int fontHeight=g2.getFontMetrics().getHeight();
143
                 int fontDesent=g2.getFontMetrics().getDescent();
144

    
145
                 //leave room for page number
146
                 double pageHeight =
147
                   pageFormat.getImageableHeight()-fontHeight;
148
                 double pageWidth =
149
                   pageFormat.getImageableWidth();
150
                 double tableWidth = (double)
151
              table.getColumnModel(
152
              ).getTotalColumnWidth();
153
                 double scale = 1;
154
                 if (tableWidth >= pageWidth) {
155
                    scale =  pageWidth / tableWidth;
156
            }
157

    
158
                 double headerHeightOnPage=
159
                     table.getTableHeader(
160
                     ).getHeight()*scale;
161
                 double tableWidthOnPage=tableWidth*scale;
162

    
163
                 double oneRowHeight=(table.getRowHeight()+
164
                          table.getRowMargin())*scale;
165
                 int numRowsOnAPage=
166
                  (int)((pageHeight-headerHeightOnPage)/
167
                                      oneRowHeight);
168
                 double pageHeightForTable=oneRowHeight*
169
                                             numRowsOnAPage;
170
                 int totalNumPages=
171
                       (int)Math.ceil((
172
                    (double)table.getRowCount())/
173
                                        numRowsOnAPage);
174
                 if(pageIndex>=totalNumPages) {
175
                          return NO_SUCH_PAGE;
176
                 }
177

    
178
                 g2.translate(pageFormat.getImageableX(),
179
                           pageFormat.getImageableY());
180
//    bottom center
181
                 g2.drawString("Page: "+(pageIndex+1),
182
                     (int)pageWidth/2-35, (int)(pageHeight
183
                     +fontHeight-fontDesent));
184

    
185
                 g2.translate(0f,headerHeightOnPage);
186
                 g2.translate(0f,-pageIndex*pageHeightForTable);
187

    
188
                 //If this piece of the table is smaller
189
                 //than the size available,
190
                 //clip to the appropriate bounds.
191
                 if (pageIndex + 1 == totalNumPages) {
192
               int lastRowPrinted =
193
                     numRowsOnAPage * pageIndex;
194
               int numRowsLeft =
195
                     table.getRowCount()
196
                     - lastRowPrinted;
197
               g2.setClip(0,
198
                 (int)(pageHeightForTable * pageIndex),
199
                 (int) Math.ceil(tableWidthOnPage),
200
                 (int) Math.ceil(oneRowHeight *
201
                                   numRowsLeft));
202
                 }
203
                 //else clip to the entire area available.
204
                 else{
205
                 g2.setClip(0,
206
                 (int)(pageHeightForTable*pageIndex),
207
                 (int) Math.ceil(tableWidthOnPage),
208
                 (int) Math.ceil(pageHeightForTable));
209
                 }
210

    
211
                 g2.scale(scale,scale);
212
                 table.paint(g2);
213
                 g2.scale(1/scale,1/scale);
214
                 g2.translate(0f,pageIndex*pageHeightForTable);
215
                 g2.translate(0f, -headerHeightOnPage);
216
                 g2.setClip(0, 0,
217
                   (int) Math.ceil(tableWidthOnPage),
218
              (int)Math.ceil(headerHeightOnPage));
219
                 g2.scale(scale,scale);
220
                 table.getTableHeader().paint(g2);
221
                 //paint header at top
222

    
223
                 return Printable.PAGE_EXISTS;
224
       }
225

    
226

    
227
}