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 @ 36443

History | View | Annotate | Download (6.22 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.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
39
import org.gvsig.fmap.dal.exception.DataException;
40

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

    
48
    private JTable table = null;
49

    
50
    /**
51
     * DOCUMENT ME!
52
     */
53
    public void initialize() {
54
        registerIcons();
55
    }
56

    
57
    private void registerIcons() {
58
        PluginServices.getIconTheme().registerDefault("document-print",
59
            this.getClass().getClassLoader().getResource("images/print.png"));
60
    }
61

    
62
    /**
63
     * DOCUMENT ME!
64
     * 
65
     * @param actionCommand
66
     *            DOCUMENT ME!
67
     */
68
    public void execute(String s) {
69
        if (s.compareTo("PRINTTABLE") == 0) {
70
            IWindow f = PluginServices.getMDIManager().getActiveWindow();
71
            try {
72
                table =
73
                    ((FeatureTableDocumentPanel) f).getTablePanel().getTable();
74
            } catch (DataException e) {
75
                NotificationManager.showMessageError(
76
                    PluginServices.getText(this, "print_table_error"), e);
77
            }
78
            PluginServices.backgroundExecution(new Runnable() {
79

    
80
                public void run() {
81
                    PrinterJob pj = PrinterJob.getPrinterJob();
82
                    pj.setPrintable(PrintTable.this);
83
                    if (pj.printDialog()) {
84
                        try {
85
                            pj.print();
86
                        } catch (Exception PrintException) {
87
                        }
88
                    }
89
                }
90
            });
91
        }
92
    }
93

    
94
    /**
95
     * DOCUMENT ME!
96
     * 
97
     * @return DOCUMENT ME!
98
     */
99
    public boolean isEnabled() {
100
        return true;
101
    }
102

    
103
    /**
104
     * DOCUMENT ME!
105
     * 
106
     * @return DOCUMENT ME!
107
     */
108
    public boolean isVisible() {
109
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
110
        if (f == null) {
111
            return false;
112
        }
113
        if (f instanceof FeatureTableDocumentPanel) {
114
            return true;
115
        } else {
116
            return false;
117
        }
118
    }
119

    
120
    public int print(Graphics g, PageFormat pageFormat, int pageIndex)
121
        throws PrinterException {
122
        Graphics2D g2 = (Graphics2D) g;
123
        g2.setColor(Color.black);
124
        int fontHeight = g2.getFontMetrics().getHeight();
125
        int fontDesent = g2.getFontMetrics().getDescent();
126

    
127
        // leave room for page number
128
        double pageHeight = pageFormat.getImageableHeight() - fontHeight;
129
        double pageWidth = pageFormat.getImageableWidth();
130
        double tableWidth =
131
            (double) table.getColumnModel().getTotalColumnWidth();
132
        double scale = 1;
133
        if (tableWidth >= pageWidth) {
134
            scale = pageWidth / tableWidth;
135
        }
136

    
137
        double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
138
        double tableWidthOnPage = tableWidth * scale;
139

    
140
        double oneRowHeight =
141
            (table.getRowHeight() + table.getRowMargin()) * scale;
142
        int numRowsOnAPage =
143
            (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
144
        double pageHeightForTable = oneRowHeight * numRowsOnAPage;
145
        int totalNumPages =
146
            (int) Math.ceil(((double) table.getRowCount()) / numRowsOnAPage);
147
        if (pageIndex >= totalNumPages) {
148
            return NO_SUCH_PAGE;
149
        }
150

    
151
        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
152
        // bottom center
153
        g2.drawString("Page: " + (pageIndex + 1), (int) pageWidth / 2 - 35,
154
            (int) (pageHeight + fontHeight - fontDesent));
155

    
156
        g2.translate(0f, headerHeightOnPage);
157
        g2.translate(0f, -pageIndex * pageHeightForTable);
158

    
159
        // If this piece of the table is smaller
160
        // than the size available,
161
        // clip to the appropriate bounds.
162
        if (pageIndex + 1 == totalNumPages) {
163
            int lastRowPrinted = numRowsOnAPage * pageIndex;
164
            int numRowsLeft = table.getRowCount() - lastRowPrinted;
165
            g2.setClip(0, (int) (pageHeightForTable * pageIndex),
166
                (int) Math.ceil(tableWidthOnPage),
167
                (int) Math.ceil(oneRowHeight * numRowsLeft));
168
        }
169
        // else clip to the entire area available.
170
        else {
171
            g2.setClip(0, (int) (pageHeightForTable * pageIndex),
172
                (int) Math.ceil(tableWidthOnPage),
173
                (int) Math.ceil(pageHeightForTable));
174
        }
175

    
176
        g2.scale(scale, scale);
177
        table.paint(g2);
178
        g2.scale(1 / scale, 1 / scale);
179
        g2.translate(0f, pageIndex * pageHeightForTable);
180
        g2.translate(0f, -headerHeightOnPage);
181
        g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage),
182
            (int) Math.ceil(headerHeightOnPage));
183
        g2.scale(scale, scale);
184
        table.getTableHeader().paint(g2);
185
        // paint header at top
186

    
187
        return Printable.PAGE_EXISTS;
188
    }
189

    
190
}