View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.controller;
22  
23  import java.awt.Color;
24  import java.awt.Dimension;
25  import java.awt.Graphics;
26  import java.awt.Graphics2D;
27  import java.awt.Insets;
28  import java.awt.Rectangle;
29  import java.awt.geom.AffineTransform;
30  import java.awt.print.PageFormat;
31  import java.awt.print.Printable;
32  import java.awt.print.PrinterException;
33  
34  import javax.swing.JPanel;
35  
36  
37  /**
38   * Classe auxiliar da classe PrintPreviewer.
39   */
40  public class PrintComponent extends JPanel {
41  
42    /** Serialization runtime version number */
43    private static final long serialVersionUID = 0;		
44  	
45    /**
46     * The item to be printed
47     */
48    protected Printable printable;
49  
50    /**
51     * PageFormat to use when printing
52     */
53    protected PageFormat pageFormat;
54  
55    /**
56     * The page that is currently displayed
57     */
58    protected int displayPage;
59  
60    /**
61     * The scale factor (1.0 = 100%)
62     */
63    protected double scaleFactor;
64  
65    public PrintComponent(Printable p, PageFormat pf) {
66      setPrintable(p);
67      setPageFormat(pf);
68      setDisplayPage(0);
69      setScaleFactor(100);
70      setBackground(Color.white);
71    }
72  
73    public void setPrintable(Printable p) {
74      printable = p;
75      revalidate();
76    }
77  
78    public void setPageFormat(PageFormat pf) {
79      pageFormat = pf;
80      revalidate();
81    }
82  
83    public void setDisplayPage(int page) {
84      displayPage = page;
85      revalidate();
86    }
87  
88    public void setScaleFactor(double scale) {
89      scaleFactor = scale;
90      revalidate();
91    }
92  
93    public double getScaleFactor() {
94      return scaleFactor;
95    }
96  
97    /**
98     * Calculate the size of this component with the specified scale factor
99     */
100   public Dimension getSizeWithScale(double scale) {
101     Insets insets = getInsets();
102     int width = ((int)(pageFormat.getWidth() *
103         scale / 100d)) +
104         insets.left + insets.right;
105     int height = ((int)(pageFormat.getHeight() *
106         scale / 100d)) +
107         insets.top + insets.bottom;
108     return new Dimension(width, height);
109   }
110 
111   public Dimension getPreferredSize() {
112     return getSizeWithScale(scaleFactor);
113   }
114 
115   public Dimension getMinimumSize() {
116     return getPreferredSize();
117   }
118 
119   /**
120    * Paint this component, taking the scale factor into account and
121    * sizing it appropriately.
122    */
123   public void paintComponent(Graphics g) {
124     super.paintComponent(g);
125     Graphics2D g2 = (Graphics2D)g;
126     Rectangle clipRect = g2.getClipBounds();
127     AffineTransform at = g2.getTransform();
128     int x = (int)(pageFormat.getImageableX() *
129         scaleFactor / 100d);
130     int y = (int)(pageFormat.getImageableY() *
131         scaleFactor / 100d);
132     int w = (int)(pageFormat.getImageableWidth() *
133         scaleFactor / 100d);
134     int h = (int)(pageFormat.getImageableHeight() *
135         scaleFactor / 100d);
136     g2.clipRect(x, y, w, h);
137     g2.scale(scaleFactor / 100, scaleFactor / 100);
138     try {
139       printable.print(g, pageFormat, displayPage);
140     } catch (PrinterException pe) {};
141     g2.setTransform(at);
142     g2.setClip(clipRect);
143   }
144 
145 }