1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
38
39 public class PrintTable implements Printable, Pageable {
40
41 private static final int SIZE = 15;
42
43
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
51
52
53
54
55
56
57
58
59 public PrintTable(List tables, String title, PageFormat pageFormat){
60
61 this.pageFormat = pageFormat;
62
63
64 }
65
66
67
68
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
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
88 while (positionY < tableHeight) {
89 positionX = 0;
90 while (positionX < tableWidth) {
91 size = getPrintSize(table, positionX, positionY);
92 if (pageIndex == index) {
93
94
95
96
97
98 paintTable(resource.getString("nodeName") + owners.get(i), table, g, positionX, positionY, size);
99
100 } else {
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 protected Dimension getPrintSize(JTable table, int positionX, int positionY) {
120 Rectangle rect;
121 int printWidth;
122 int printHeight;
123
124
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
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
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 }