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.Point;
26  import java.awt.Rectangle;
27  import java.awt.Shape;
28  import java.awt.print.PageFormat;
29  import java.awt.print.Pageable;
30  import java.awt.print.Printable;
31  import java.util.List;
32  import java.util.ResourceBundle;
33  
34  import javax.swing.JTable;
35  
36  /**
37   * Table printing class <code>JTable</code>
38   */
39  public class PrintTable implements Printable, Pageable {
40  
41    private static final int SIZE = 15;
42  
43    /** Load resource file from this package */
44    	private static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
45    			unbbayes.controller.resources.ControllerResources.class.getName());
46  
47    protected List tables;
48    protected List owners;
49    protected PageFormat pageFormat;
50  /**/  protected String title;
51  
52  
53    public PrintTable(List tbs, List owners, PageFormat pf) {
54      tables = tbs;
55      this.owners = owners;
56      pageFormat = pf;
57    }
58  
59  /**/  public PrintTable(List tables, String title, PageFormat pageFormat){
60  /**/    this.tables = tables;
61  /**/    this.pageFormat = pageFormat;
62  /**/    this.owners = null;
63          this.title = title;
64  /**/  }
65  /**/
66  
67    /**
68     * Perform the printing here
69     */
70    public int print(Graphics g, PageFormat pf, int index) {
71      Dimension size = null;
72      JTable table;
73      int positionX;
74      int positionY;
75      int pageIndex = 0;
76      for (int i = 0; i < tables.size(); i++) {
77          table = (JTable)tables.get(i);
78          //  Get the table's preferred size
79          if ((table.getWidth() == 0) || (table.getHeight() == 0)) {
80            table.setSize(table.getPreferredSize());
81          }
82          int tableWidth = table.getWidth();
83          int tableHeight = table.getHeight();
84          positionX = 0;
85          positionY = 0;
86  
87          //  Loop until we have printed the entire table
88          while (positionY < tableHeight) {
89            positionX = 0;
90            while (positionX < tableWidth) {
91              size = getPrintSize(table, positionX, positionY);
92              if (pageIndex == index) {
93                //  Paint as much of the table as will fit on a page
94  //              g.drawString("N: " + donos.get(i), positionX + (int)pageFormat.getImageableX(), (int)pageFormat.getImageableY() + SIZE);
95         //       paintTable(resource.getString("nodeName") + owners.get(i), table, g, positionX, positionY, size);
96  
97  /**/            if(owners != null){
98  /**/              paintTable(resource.getString("nodeName") + owners.get(i), table, g, positionX, positionY, size);
99  /**/
100 /**/            } else {
101 /**/              paintTable(title, table, g, positionX, positionY, size);
102                 }
103 
104               return Printable.PAGE_EXISTS;
105             }
106             pageIndex++;
107             positionX += size.width;
108           }
109           positionY += size.height;
110         }
111     }
112     return Printable.NO_SUCH_PAGE;
113   }
114 
115   /**
116    * Calculate how much of the table will fit on a page without
117    * causing a row or column to be split across two pages
118    */
119   protected Dimension getPrintSize(JTable table, int positionX, int positionY) {
120     Rectangle rect;
121     int printWidth;
122     int printHeight;
123 //    int firstCol = table.columnAtPoint(new Point(positionX, positionY));
124 //    int firstRow = table.rowAtPoint(new Point(positionX, positionY));
125     int maxWidth = (int)(pageFormat.getImageableWidth());
126     int maxHeight = (int)(pageFormat.getImageableHeight()-(SIZE+10));
127 
128     int lastCol = table.columnAtPoint(
129         new Point(positionX + maxWidth, positionY));
130     if (lastCol == -1) {
131       printWidth = table.getWidth() - positionX;
132     }
133     else {
134       rect = table.getCellRect(0, lastCol - 1, true);
135       printWidth = rect.x + rect.width - positionX;
136     }
137 
138     int lastRow = table.rowAtPoint(new Point(
139         positionX, positionY + maxHeight));
140     if (lastRow == -1) {
141       printHeight = table.getHeight() - positionY;
142     }
143     else {
144       rect = table.getCellRect(lastRow - 1, 0, true);
145       printHeight = rect.y + rect.height - positionY;
146     }
147     return new Dimension(printWidth, printHeight);
148   }
149 
150   /**
151    * Paint / print a portion of the table
152    */
153   protected void paintTable(String title, JTable table, Graphics g, int positionX, int positionY, Dimension size) {
154     int offsetX = (int)(pageFormat.getImageableX());
155     int offsetY = (int)(pageFormat.getImageableY());
156     Shape lastClip = g.getClip();
157     g.drawString(title, offsetX, offsetY + SIZE);
158     g.translate(offsetX - positionX, offsetY - positionY + (SIZE+10));
159     g.clipRect(positionX, positionY, size.width, size.height);
160     table.paint(g);
161     g.setClip(lastClip);
162     g.translate(-(offsetX - positionX), -(offsetY - positionY + (SIZE+10)));
163   }
164 
165   /**
166    * Calculate the number of pages it will take to print the entire table
167    */
168   public int getNumberOfPages() {
169     JTable table;
170     Dimension size = null;
171     int positionX;
172     int positionY;
173     int pageIndex = 0;
174 
175     for (int i = 0; i < tables.size(); i++) {
176         table = (JTable)tables.get(i);
177         if ((table.getWidth() == 0) || (table.getHeight() == 0)) {
178           table.setSize(table.getPreferredSize());
179         }
180         int tableWidth = table.getWidth();
181         int tableHeight = table.getHeight();
182         positionX = 0;
183         positionY = 0;
184         while (positionY < tableHeight) {
185           positionX = 0;
186           while (positionX < tableWidth) {
187             size = getPrintSize(table, positionX, positionY);
188             positionX += size.width;
189             pageIndex++;
190           }
191           positionY += size.height;
192         }
193     }
194     return pageIndex;
195   }
196 
197   public Printable getPrintable(int index) {
198     return this;
199   }
200 
201   public PageFormat getPageFormat(int index) {
202     return pageFormat;
203   }
204 
205 }