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.Dimension;
24  import java.awt.Graphics;
25  import java.awt.print.PageFormat;
26  import java.awt.print.Pageable;
27  import java.awt.print.Printable;
28  
29  import javax.swing.JTextArea;
30  
31  /**
32   *  <code>JTextArea</code> printing class
33   */
34  public class PrintText implements Printable, Pageable {
35  
36    protected JTextArea texto;
37    protected PageFormat pageFormat;
38  
39    public PrintText(JTextArea texto, PageFormat pf) {
40      this.texto = texto;
41      pageFormat = pf;
42    }
43  
44    /**
45     * Perform the printing here
46     */
47    public int print(Graphics g, PageFormat pf, int index) {
48      Dimension size = new Dimension();
49      //  Get the TextArea's preferred size
50      if ((texto.getWidth() == 0) || (texto.getHeight() == 0)) {
51        texto.setSize(texto.getPreferredSize());
52      }
53  //    int textWidth = texto.getWidth();
54      int textHeight = texto.getHeight();
55  
56      int positionX = 0;
57      int positionY = 0;
58  
59      //  Loop until we have printed the entire text
60      int pageIndex = 0;
61      while (positionY < textHeight) {
62        positionX = 0;
63  //      while (positionX < textWidth) {
64          size.setSize(getPrintSize(positionX, positionY));
65          if (pageIndex == index) {
66            //  Paint as much of the text as will fit on a page
67            paintText(g, positionX, positionY, size);
68            return Printable.PAGE_EXISTS;
69          }
70          pageIndex++;
71          positionX += size.width;
72  //      }
73        positionY += size.height;
74      }
75      return Printable.NO_SUCH_PAGE;
76    }
77  
78    /**
79     * Calculate how much of the table will fit on a page without
80     * causing a row or column to be split across two pages
81     */
82    protected Dimension getPrintSize(int positionX, int positionY) {
83  
84      int maxWidth = (int)(pageFormat.getImageableWidth());
85      int maxHeight = (int)(pageFormat.getImageableHeight());
86  //    int printWidth = texto.getWidth() - positionX;
87  //    int printHeight = texto.getHeight() - positionY;
88      return new Dimension(maxWidth, maxHeight);
89    }
90  
91    /**
92     * Paint / print a portion of the text
93     */
94    protected void paintText(Graphics g, int positionX, int positionY,
95        Dimension size) {
96      int offsetX = (int)(pageFormat.getImageableX());
97      int offsetY = (int)(pageFormat.getImageableY());
98      g.translate(offsetX - positionX, offsetY - positionY);
99      g.clipRect(positionX, positionY, size.width, size.height);
100     texto.paint(g);
101   }
102 
103   /**
104    * Calculate the number of pages it will take to print the entire text
105    */
106   public int getNumberOfPages() {
107     Dimension size = new Dimension();
108     int textHeight = texto.getHeight();
109     int positionX = 0;
110     int positionY = 0;
111 
112     int pageIndex = 0;
113     while (positionY < textHeight) {
114       positionX = 0;
115 //      while (positionX < textWidth) {
116           size.setSize(getPrintSize(positionX, positionY));
117           positionX += size.width;
118           pageIndex++;
119 //      }
120       positionY += size.height;
121     }
122     return pageIndex;
123   }
124 
125   public Printable getPrintable(int index) {
126     return this;
127   }
128 
129   public PageFormat getPageFormat(int index) {
130     return pageFormat;
131   }
132 }