Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / PrintTable.java @ 40558

History | View | Annotate | Download (6.04 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.extension;
25

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

    
34
import javax.swing.JTable;
35

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

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

    
51
    private JTable table = null;
52

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

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

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

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

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

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

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

    
126
        double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
127
        double tableWidthOnPage = tableWidth * scale;
128

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

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

    
145
        g2.translate(0f, headerHeightOnPage);
146
        g2.translate(0f, -pageIndex * pageHeightForTable);
147

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

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

    
176
        return Printable.PAGE_EXISTS;
177
    }
178

    
179
}