Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / PrintTable.java @ 38564

History | View | Annotate | Download (6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.app.extension;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.print.PageFormat;
28
import java.awt.print.Printable;
29
import java.awt.print.PrinterException;
30
import java.awt.print.PrinterJob;
31

    
32
import javax.swing.JTable;
33

    
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.andami.plugins.Extension;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
40
import org.gvsig.fmap.dal.exception.DataException;
41

    
42
/**
43
 * Extensi?n para imprimir una tabla.
44
 * 
45
 * @author Vicente Caballero Navarro
46
 */
47
public class PrintTable extends Extension implements Printable {
48

    
49
    private JTable table = null;
50

    
51
    public void initialize() {
52
            IconThemeHelper.registerIcon("action", "document-print", this);
53
    }
54

    
55
    public void execute(String s) {
56
        if (s.compareTo("application-print-table") == 0) {
57
            IWindow f = PluginServices.getMDIManager().getActiveWindow();
58
            try {
59
                table =
60
                    ((FeatureTableDocumentPanel) f).getTablePanel().getTable();
61
            } catch (DataException e) {
62
                NotificationManager.showMessageError(
63
                    PluginServices.getText(this, "print_table_error"), e);
64
            }
65
            PluginServices.backgroundExecution(new Runnable() {
66

    
67
                public void run() {
68
                    PrinterJob pj = PrinterJob.getPrinterJob();
69
                    pj.setPrintable(PrintTable.this);
70
                    if (pj.printDialog()) {
71
                        try {
72
                            pj.print();
73
                        } catch (Exception PrintException) {
74
                        }
75
                    }
76
                }
77
            });
78
        }
79
    }
80

    
81
    /**
82
     * DOCUMENT ME!
83
     * 
84
     * @return DOCUMENT ME!
85
     */
86
    public boolean isEnabled() {
87
        return true;
88
    }
89

    
90
    /**
91
     * DOCUMENT ME!
92
     * 
93
     * @return DOCUMENT ME!
94
     */
95
    public boolean isVisible() {
96
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
97
        if (f == null) {
98
            return false;
99
        }
100
        if (f instanceof FeatureTableDocumentPanel) {
101
            return true;
102
        } else {
103
            return false;
104
        }
105
    }
106

    
107
    public int print(Graphics g, PageFormat pageFormat, int pageIndex)
108
        throws PrinterException {
109
        Graphics2D g2 = (Graphics2D) g;
110
        g2.setColor(Color.black);
111
        int fontHeight = g2.getFontMetrics().getHeight();
112
        int fontDesent = g2.getFontMetrics().getDescent();
113

    
114
        // leave room for page number
115
        double pageHeight = pageFormat.getImageableHeight() - fontHeight;
116
        double pageWidth = pageFormat.getImageableWidth();
117
        double tableWidth =
118
            (double) table.getColumnModel().getTotalColumnWidth();
119
        double scale = 1;
120
        if (tableWidth >= pageWidth) {
121
            scale = pageWidth / tableWidth;
122
        }
123

    
124
        double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
125
        double tableWidthOnPage = tableWidth * scale;
126

    
127
        double oneRowHeight =
128
            (table.getRowHeight() + table.getRowMargin()) * scale;
129
        int numRowsOnAPage =
130
            (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
131
        double pageHeightForTable = oneRowHeight * numRowsOnAPage;
132
        int totalNumPages =
133
            (int) Math.ceil(((double) table.getRowCount()) / numRowsOnAPage);
134
        if (pageIndex >= totalNumPages) {
135
            return NO_SUCH_PAGE;
136
        }
137

    
138
        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
139
        // bottom center
140
        g2.drawString("Page: " + (pageIndex + 1), (int) pageWidth / 2 - 35,
141
            (int) (pageHeight + fontHeight - fontDesent));
142

    
143
        g2.translate(0f, headerHeightOnPage);
144
        g2.translate(0f, -pageIndex * pageHeightForTable);
145

    
146
        // If this piece of the table is smaller
147
        // than the size available,
148
        // clip to the appropriate bounds.
149
        if (pageIndex + 1 == totalNumPages) {
150
            int lastRowPrinted = numRowsOnAPage * pageIndex;
151
            int numRowsLeft = table.getRowCount() - lastRowPrinted;
152
            g2.setClip(0, (int) (pageHeightForTable * pageIndex),
153
                (int) Math.ceil(tableWidthOnPage),
154
                (int) Math.ceil(oneRowHeight * numRowsLeft));
155
        }
156
        // else clip to the entire area available.
157
        else {
158
            g2.setClip(0, (int) (pageHeightForTable * pageIndex),
159
                (int) Math.ceil(tableWidthOnPage),
160
                (int) Math.ceil(pageHeightForTable));
161
        }
162

    
163
        g2.scale(scale, scale);
164
        table.paint(g2);
165
        g2.scale(1 / scale, 1 / scale);
166
        g2.translate(0f, pageIndex * pageHeightForTable);
167
        g2.translate(0f, -headerHeightOnPage);
168
        g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage),
169
            (int) Math.ceil(headerHeightOnPage));
170
        g2.scale(scale, scale);
171
        table.getTableHeader().paint(g2);
172
        // paint header at top
173

    
174
        return Printable.PAGE_EXISTS;
175
    }
176

    
177
}