17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.ui;
|
|
|
15 |
|
|
|
16 |
import java.awt.Color;
|
|
|
17 |
import java.awt.Component;
|
|
|
18 |
import java.awt.Dimension;
|
|
|
19 |
import java.awt.Graphics;
|
|
|
20 |
import java.awt.Point;
|
|
|
21 |
import java.awt.Rectangle;
|
|
|
22 |
import java.util.Enumeration;
|
|
|
23 |
|
|
|
24 |
import javax.swing.JComponent;
|
|
|
25 |
import javax.swing.plaf.basic.BasicTableUI;
|
|
|
26 |
import javax.swing.table.JTableHeader;
|
|
|
27 |
import javax.swing.table.TableCellRenderer;
|
|
|
28 |
import javax.swing.table.TableColumn;
|
|
|
29 |
import javax.swing.table.TableColumnModel;
|
|
|
30 |
|
|
|
31 |
public class EnhancedTableUI extends BasicTableUI {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Return the minimum size of the table. The minimum height is the row height (plus inter-cell
|
|
|
35 |
* spacing) times the number of rows. The minimum width is the sum of the minimum widths of each
|
|
|
36 |
* column (plus inter-cell spacing).
|
|
|
37 |
*/
|
|
|
38 |
public Dimension getMinimumSize(JComponent c) {
|
|
|
39 |
long width = 0;
|
|
|
40 |
Enumeration enumeration = table.getColumnModel().getColumns();
|
|
|
41 |
while (enumeration.hasMoreElements()) {
|
|
|
42 |
TableColumn aColumn = (TableColumn) enumeration.nextElement();
|
|
|
43 |
width = width + aColumn.getMinWidth();
|
|
|
44 |
}
|
|
|
45 |
return createTableSize(width);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Return the preferred size of the table. The preferred height is the row height (plus
|
|
|
50 |
* inter-cell spacing) times the number of rows. The preferred width is the sum of the preferred
|
|
|
51 |
* widths of each column (plus inter-cell spacing).
|
|
|
52 |
*/
|
|
|
53 |
public Dimension getPreferredSize(JComponent c) {
|
|
|
54 |
long width = 0;
|
|
|
55 |
Enumeration enumeration = table.getColumnModel().getColumns();
|
|
|
56 |
while (enumeration.hasMoreElements()) {
|
|
|
57 |
TableColumn aColumn = (TableColumn) enumeration.nextElement();
|
|
|
58 |
width = width + aColumn.getPreferredWidth();
|
|
|
59 |
}
|
|
|
60 |
return createTableSize(width);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Return the maximum size of the table. The maximum height is the row height (plus inter-cell
|
|
|
65 |
* spacing) times the number of rows. The maximum width is the sum of the maximum widths of each
|
|
|
66 |
* column (plus inter-cell spacing).
|
|
|
67 |
*/
|
|
|
68 |
public Dimension getMaximumSize(JComponent c) {
|
|
|
69 |
long width = 0;
|
|
|
70 |
Enumeration enumeration = table.getColumnModel().getColumns();
|
|
|
71 |
while (enumeration.hasMoreElements()) {
|
|
|
72 |
TableColumn aColumn = (TableColumn) enumeration.nextElement();
|
|
|
73 |
width = width + aColumn.getMaxWidth();
|
|
|
74 |
}
|
|
|
75 |
return createTableSize(width);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private Dimension createTableSize(long width) {
|
|
|
79 |
final int row = table.getRowCount() - 1;
|
|
|
80 |
final EnhancedTable enhancedTable = (EnhancedTable) table;
|
|
|
81 |
int height = enhancedTable.getCellRect(row, 0, true).y + enhancedTable.getRowHeight(row);
|
|
|
82 |
// int totalMarginWidth = table.getColumnModel().getColumnMargin() * table.getColumnCount();
|
|
|
83 |
long widthWithMargin = Math.abs(width);// + totalMarginWidth;
|
|
|
84 |
if (widthWithMargin > Integer.MAX_VALUE) {
|
|
|
85 |
widthWithMargin = Integer.MAX_VALUE;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return new Dimension((int) widthWithMargin, height);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Paint a representation of the <code>table</code> instance that was set in installUI().
|
|
|
93 |
*/
|
|
|
94 |
public void paint(Graphics g, JComponent c) {
|
|
|
95 |
if (table.getRowCount() <= 0 || table.getColumnCount() <= 0) {
|
|
|
96 |
return;
|
|
|
97 |
}
|
|
|
98 |
// g.setColor(Color.GREEN);
|
|
|
99 |
// g.fillRect(0, 0, 600, 600);
|
|
|
100 |
Rectangle clip = g.getClipBounds();
|
|
|
101 |
Point upperLeft = clip.getLocation();
|
|
|
102 |
Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1);
|
156 |
ilm |
103 |
int rMin = table.rowAtPoint(upperLeft);
|
|
|
104 |
int rMax = table.rowAtPoint(lowerRight);
|
|
|
105 |
if (rMin < 0) {
|
17 |
ilm |
106 |
rMin = 0;
|
|
|
107 |
}
|
|
|
108 |
// If the table does not have enough rows to fill the view we'll get -1.
|
|
|
109 |
// Replace this with the index of the last row.
|
156 |
ilm |
110 |
if (rMax == -1 || rMax >= table.getRowCount()) {
|
17 |
ilm |
111 |
rMax = table.getRowCount() - 1;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
boolean ltr = table.getComponentOrientation().isLeftToRight();
|
|
|
115 |
int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight);
|
|
|
116 |
int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);
|
|
|
117 |
// This should never happen.
|
|
|
118 |
if (cMin == -1) {
|
|
|
119 |
cMin = 0;
|
|
|
120 |
}
|
|
|
121 |
// If the table does not have enough columns to fill the view we'll get -1.
|
|
|
122 |
// Replace this with the index of the last column.
|
|
|
123 |
if (cMax == -1) {
|
|
|
124 |
cMax = table.getColumnCount() - 1;
|
|
|
125 |
}
|
156 |
ilm |
126 |
// System.out.println("paint:" + rMin + " /" + rMax + " - col:" + cMin + " / " + cMax);
|
17 |
ilm |
127 |
for (int i = rMin; i <= rMax; i++) {
|
|
|
128 |
for (int j = cMin; j <= cMax; j++) {
|
|
|
129 |
if (table.isEditing() && table.getEditingRow() == i && table.getEditingColumn() == j) {
|
|
|
130 |
Component component = table.getEditorComponent();
|
|
|
131 |
|
|
|
132 |
component.validate();
|
|
|
133 |
} else {
|
|
|
134 |
TableCellRenderer renderer = table.getCellRenderer(i, j);
|
|
|
135 |
if (renderer == null) {
|
|
|
136 |
throw new IllegalStateException("Renderer null for " + i + "," + j);
|
|
|
137 |
}
|
|
|
138 |
table.prepareRenderer(renderer, i, j);
|
|
|
139 |
// System.out.println("Paint: "+row+" / "+column +" rect: "+cellRect);
|
|
|
140 |
// rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
|
|
|
141 |
// cellRect.width, cellRect.height, true);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
// TableCellRenderer renderer = table.getCellRenderer(i, j);
|
|
|
145 |
// if(renderer instanceof TextAreaRenderer)
|
|
|
146 |
// renderer.getTableCellRendererComponent(table, table.getValueAt(i, j), false,
|
|
|
147 |
// false, i, j);
|
|
|
148 |
// System.out.println("update renderer: "+i+","+j);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
// Paint the grid.
|
|
|
152 |
paintGrid(g, rMin, rMax, cMin, cMax);
|
|
|
153 |
|
|
|
154 |
// Paint the cells.
|
|
|
155 |
paintCells(g, rMin, rMax, cMin, cMax);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
public void paintold(Graphics g, JComponent c) {
|
|
|
159 |
System.out.println("\nEnhancedTableUI.paint()---------------------------- Line count:" + table.getRowCount());
|
|
|
160 |
g.setColor(Color.GREEN);
|
|
|
161 |
g.fillRect(0, 0, 600, 600);
|
|
|
162 |
/*
|
|
|
163 |
* Rectangle oldClipBounds = g.getClipBounds(); Rectangle clipBounds = new
|
|
|
164 |
* Rectangle(oldClipBounds); int tableWidth = table.getColumnModel().getTotalColumnWidth();
|
|
|
165 |
* clipBounds.width = Math.min(clipBounds.width, tableWidth); g.setClip(clipBounds);
|
|
|
166 |
*/
|
|
|
167 |
// Paint the grid
|
|
|
168 |
// paintGrid(g);
|
|
|
169 |
System.out.println("Update Cells");
|
|
|
170 |
// updateCellsSize(g, 0, table.getRowCount() - 1, 0, table.getColumnCount() - 1);
|
|
|
171 |
for (int i = 0; i < table.getRowCount(); i++) {
|
|
|
172 |
for (int j = 0; j < table.getColumnCount(); j++) {
|
|
|
173 |
TableCellRenderer renderer = table.getCellRenderer(i, j);
|
|
|
174 |
System.out.println(renderer.getTableCellRendererComponent(table, table.getValueAt(i, j), false, false, i, j));
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
System.out.println("Paint Grids");
|
|
|
179 |
paintGrid(g, 0, table.getRowCount() - 1, 0, table.getColumnCount() - 1);
|
|
|
180 |
// Paint the rows
|
|
|
181 |
// paintCells(g, clipBounds, tableWidth);
|
|
|
182 |
System.out.println("Paint Cells");
|
|
|
183 |
paintCells(g, 0, table.getRowCount() - 1, 0, table.getColumnCount() - 1);
|
|
|
184 |
// g.setClip(oldClipBounds);
|
|
|
185 |
System.out.println("EnhancedTableUI.paint()---------------------------- done");
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// From Sun
|
|
|
189 |
/*
|
|
|
190 |
* Paints the grid lines within <I>aRect</I>, using the grid color set with <I>setGridColor</I>.
|
|
|
191 |
* Paints vertical lines if <code>getShowVerticalLines()</code> returns true and paints
|
|
|
192 |
* horizontal lines if <code>getShowHorizontalLines()</code> returns true.
|
|
|
193 |
*/
|
|
|
194 |
private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) {
|
|
|
195 |
g.setColor(table.getGridColor());
|
|
|
196 |
|
|
|
197 |
Rectangle minCell = table.getCellRect(rMin, cMin, true);
|
|
|
198 |
Rectangle maxCell = table.getCellRect(rMax, cMax, true);
|
|
|
199 |
Rectangle damagedArea = minCell.union(maxCell);
|
|
|
200 |
|
|
|
201 |
if (table.getShowHorizontalLines()) {
|
|
|
202 |
int tableWidth = damagedArea.x + damagedArea.width;
|
|
|
203 |
int y = damagedArea.y;
|
|
|
204 |
for (int row = rMin; row <= rMax; row++) {
|
|
|
205 |
y += table.getRowHeight(row);
|
|
|
206 |
// System.out.println("paintGrid at y=:"+(y-1));
|
|
|
207 |
g.drawLine(damagedArea.x, y - 1, tableWidth - 1, y - 1);
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
if (table.getShowVerticalLines()) {
|
|
|
211 |
TableColumnModel cm = table.getColumnModel();
|
|
|
212 |
int tableHeight = damagedArea.y + damagedArea.height;
|
|
|
213 |
int x;
|
|
|
214 |
if (table.getComponentOrientation().isLeftToRight()) {
|
|
|
215 |
x = damagedArea.x;
|
|
|
216 |
for (int column = cMin; column <= cMax; column++) {
|
|
|
217 |
int w = cm.getColumn(column).getWidth();
|
|
|
218 |
x += w;
|
|
|
219 |
g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
|
|
|
220 |
}
|
|
|
221 |
} else {
|
|
|
222 |
x = damagedArea.x + damagedArea.width;
|
|
|
223 |
for (int column = cMin; column < cMax; column++) {
|
|
|
224 |
int w = cm.getColumn(column).getWidth();
|
|
|
225 |
x -= w;
|
|
|
226 |
g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
|
|
|
227 |
}
|
|
|
228 |
x -= cm.getColumn(cMax).getWidth();
|
|
|
229 |
g.drawLine(x, 0, x, tableHeight - 1);
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
// From Sun + fix
|
|
|
235 |
private void paintCells(Graphics g, int rMin, int rMax, int cMin, int cMax) {
|
|
|
236 |
JTableHeader header = table.getTableHeader();
|
|
|
237 |
TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
|
|
|
238 |
|
|
|
239 |
TableColumnModel cm = table.getColumnModel();
|
|
|
240 |
int columnMargin = cm.getColumnMargin();
|
|
|
241 |
|
|
|
242 |
Rectangle cellRect;
|
|
|
243 |
TableColumn aColumn;
|
|
|
244 |
int columnWidth;
|
|
|
245 |
if (table.getComponentOrientation().isLeftToRight()) {
|
|
|
246 |
for (int row = rMin; row <= rMax; row++) {
|
|
|
247 |
cellRect = table.getCellRect(row, cMin, false);
|
|
|
248 |
// Mon fix
|
|
|
249 |
// cellRect.height = ((EnhancedTable) table).getRowHeight(row);
|
|
|
250 |
for (int column = cMin; column <= cMax; column++) {
|
|
|
251 |
aColumn = cm.getColumn(column);
|
|
|
252 |
columnWidth = aColumn.getWidth();
|
|
|
253 |
cellRect.width = columnWidth - columnMargin;
|
|
|
254 |
if (aColumn != draggedColumn) {
|
|
|
255 |
paintCell(g, cellRect, row, column);
|
|
|
256 |
}
|
|
|
257 |
cellRect.x += columnWidth;
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
} else {
|
|
|
261 |
for (int row = rMin; row <= rMax; row++) {
|
|
|
262 |
cellRect = table.getCellRect(row, cMin, false);
|
|
|
263 |
aColumn = cm.getColumn(cMin);
|
|
|
264 |
if (aColumn != draggedColumn) {
|
|
|
265 |
columnWidth = aColumn.getWidth();
|
|
|
266 |
cellRect.width = columnWidth - columnMargin;
|
|
|
267 |
paintCell(g, cellRect, row, cMin);
|
|
|
268 |
}
|
|
|
269 |
for (int column = cMin + 1; column <= cMax; column++) {
|
|
|
270 |
aColumn = cm.getColumn(column);
|
|
|
271 |
columnWidth = aColumn.getWidth();
|
|
|
272 |
cellRect.width = columnWidth - columnMargin;
|
|
|
273 |
cellRect.x -= columnWidth;
|
|
|
274 |
if (aColumn != draggedColumn) {
|
|
|
275 |
paintCell(g, cellRect, row, column);
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
// Paint the dragged column if we are dragging.
|
|
|
282 |
if (draggedColumn != null) {
|
|
|
283 |
paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance());
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
// Remove any renderers that may be left in the rendererPane.
|
|
|
287 |
rendererPane.removeAll();
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
// From Sun 100%
|
|
|
291 |
private int viewIndexForColumn(TableColumn aColumn) {
|
|
|
292 |
TableColumnModel cm = table.getColumnModel();
|
|
|
293 |
for (int column = 0; column < cm.getColumnCount(); column++) {
|
|
|
294 |
if (cm.getColumn(column) == aColumn) {
|
|
|
295 |
return column;
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
return -1;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
// From Sun 100%
|
|
|
302 |
private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
|
|
|
303 |
int draggedColumnIndex = viewIndexForColumn(draggedColumn);
|
|
|
304 |
|
|
|
305 |
Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
|
|
|
306 |
Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
|
|
|
307 |
|
|
|
308 |
Rectangle vacatedColumnRect = minCell.union(maxCell);
|
|
|
309 |
|
|
|
310 |
// Paint a gray well in place of the moving column.
|
|
|
311 |
g.setColor(table.getParent().getBackground());
|
|
|
312 |
g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, vacatedColumnRect.width, vacatedColumnRect.height);
|
|
|
313 |
|
|
|
314 |
// Move to the where the cell has been dragged.
|
|
|
315 |
vacatedColumnRect.x += distance;
|
|
|
316 |
|
|
|
317 |
// Fill the background.
|
|
|
318 |
g.setColor(table.getBackground());
|
|
|
319 |
g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, vacatedColumnRect.width, vacatedColumnRect.height);
|
|
|
320 |
|
|
|
321 |
// Paint the vertical grid lines if necessary.
|
|
|
322 |
if (table.getShowVerticalLines()) {
|
|
|
323 |
g.setColor(table.getGridColor());
|
|
|
324 |
int x1 = vacatedColumnRect.x;
|
|
|
325 |
int y1 = vacatedColumnRect.y;
|
|
|
326 |
int x2 = x1 + vacatedColumnRect.width - 1;
|
|
|
327 |
int y2 = y1 + vacatedColumnRect.height - 1;
|
|
|
328 |
// Left
|
|
|
329 |
g.drawLine(x1 - 1, y1, x1 - 1, y2);
|
|
|
330 |
// Right
|
|
|
331 |
g.drawLine(x2, y1, x2, y2);
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
for (int row = rMin; row <= rMax; row++) {
|
|
|
335 |
// Render the cell value
|
|
|
336 |
Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
|
|
|
337 |
r.x += distance;
|
|
|
338 |
paintCell(g, r, row, draggedColumnIndex);
|
|
|
339 |
|
|
|
340 |
// Paint the (lower) horizontal grid line if necessary.
|
|
|
341 |
if (table.getShowHorizontalLines()) {
|
|
|
342 |
g.setColor(table.getGridColor());
|
|
|
343 |
Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true);
|
|
|
344 |
rcr.x += distance;
|
|
|
345 |
int x1 = rcr.x;
|
|
|
346 |
int y1 = rcr.y;
|
|
|
347 |
int x2 = x1 + rcr.width - 1;
|
|
|
348 |
int y2 = y1 + rcr.height - 1;
|
|
|
349 |
g.drawLine(x1, y2, x2, y2);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
// From Sun 100%
|
|
|
355 |
private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
|
|
|
356 |
if (table.isEditing() && table.getEditingRow() == row && table.getEditingColumn() == column) {
|
|
|
357 |
Component component = table.getEditorComponent();
|
|
|
358 |
component.setBounds(cellRect);
|
|
|
359 |
component.validate();
|
|
|
360 |
} else {
|
|
|
361 |
TableCellRenderer renderer = table.getCellRenderer(row, column);
|
|
|
362 |
Component component = table.prepareRenderer(renderer, row, column);
|
|
|
363 |
// System.out.println("Paint: "+row+" / "+column +" rect: "+cellRect);
|
|
|
364 |
rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
}
|