author | peterz |
Wed, 18 Nov 2009 17:36:46 +0300 | |
changeset 4283 | 06d5378aaff3 |
parent 2658 | 43e06bc950ec |
child 4394 | 92a8ec883f5d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
28 |
import java.awt.Color; |
|
29 |
import java.awt.Component; |
|
2658 | 30 |
import java.awt.Dimension; |
2 | 31 |
import java.awt.Graphics; |
32 |
import java.awt.Point; |
|
33 |
import java.awt.Rectangle; |
|
34 |
import java.beans.PropertyChangeEvent; |
|
35 |
import java.beans.PropertyChangeListener; |
|
36 |
import java.text.DateFormat; |
|
37 |
import java.text.Format; |
|
38 |
import java.text.NumberFormat; |
|
39 |
import java.util.Date; |
|
40 |
import javax.swing.Icon; |
|
41 |
import javax.swing.ImageIcon; |
|
42 |
import javax.swing.JCheckBox; |
|
43 |
import javax.swing.JComponent; |
|
44 |
import javax.swing.JLabel; |
|
45 |
import javax.swing.JTable; |
|
46 |
import javax.swing.LookAndFeel; |
|
47 |
import javax.swing.border.Border; |
|
48 |
import javax.swing.plaf.ComponentUI; |
|
49 |
import javax.swing.plaf.UIResource; |
|
50 |
import javax.swing.plaf.basic.BasicTableUI; |
|
51 |
import javax.swing.table.DefaultTableCellRenderer; |
|
52 |
import javax.swing.table.JTableHeader; |
|
53 |
import javax.swing.table.TableCellRenderer; |
|
54 |
import javax.swing.table.TableColumn; |
|
55 |
import javax.swing.table.TableColumnModel; |
|
56 |
||
57 |
import sun.swing.plaf.synth.SynthUI; |
|
58 |
||
59 |
/** |
|
60 |
* SynthTableUI implementation |
|
61 |
* |
|
62 |
* @author Philip Milne |
|
63 |
*/ |
|
64 |
class SynthTableUI extends BasicTableUI implements SynthUI, |
|
65 |
PropertyChangeListener { |
|
66 |
// |
|
67 |
// Instance Variables |
|
68 |
// |
|
69 |
||
70 |
private SynthStyle style; |
|
71 |
||
72 |
private boolean useTableColors; |
|
73 |
private boolean useUIBorder; |
|
2658 | 74 |
private Color alternateColor; //the background color to use for cells for alternate cells |
2 | 75 |
|
76 |
// TableCellRenderer installed on the JTable at the time we're installed, |
|
77 |
// cached so that we can reinstall them at uninstallUI time. |
|
78 |
private TableCellRenderer dateRenderer; |
|
79 |
private TableCellRenderer numberRenderer; |
|
80 |
private TableCellRenderer doubleRender; |
|
81 |
private TableCellRenderer floatRenderer; |
|
82 |
private TableCellRenderer iconRenderer; |
|
83 |
private TableCellRenderer imageIconRenderer; |
|
84 |
private TableCellRenderer booleanRenderer; |
|
85 |
private TableCellRenderer objectRenderer; |
|
86 |
||
87 |
// |
|
88 |
// The installation/uninstall procedures and support |
|
89 |
// |
|
90 |
||
91 |
public static ComponentUI createUI(JComponent c) { |
|
92 |
return new SynthTableUI(); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Initialize JTable properties, e.g. font, foreground, and background. |
|
97 |
* The font, foreground, and background properties are only set if their |
|
98 |
* current value is either null or a UIResource, other properties are set |
|
99 |
* if the current value is null. |
|
100 |
* |
|
101 |
* @see #installUI |
|
102 |
*/ |
|
103 |
protected void installDefaults() { |
|
104 |
dateRenderer = installRendererIfPossible(Date.class, null); |
|
105 |
numberRenderer = installRendererIfPossible(Number.class, null); |
|
106 |
doubleRender = installRendererIfPossible(Double.class, null); |
|
107 |
floatRenderer = installRendererIfPossible(Float.class, null); |
|
108 |
iconRenderer = installRendererIfPossible(Icon.class, null); |
|
109 |
imageIconRenderer = installRendererIfPossible(ImageIcon.class, null); |
|
110 |
booleanRenderer = installRendererIfPossible(Boolean.class, |
|
111 |
new SynthBooleanTableCellRenderer()); |
|
112 |
objectRenderer = installRendererIfPossible(Object.class, |
|
113 |
new SynthTableCellRenderer()); |
|
114 |
updateStyle(table); |
|
115 |
} |
|
116 |
||
117 |
private TableCellRenderer installRendererIfPossible(Class objectClass, |
|
118 |
TableCellRenderer renderer) { |
|
119 |
TableCellRenderer currentRenderer = table.getDefaultRenderer( |
|
120 |
objectClass); |
|
121 |
if (currentRenderer instanceof UIResource) { |
|
122 |
table.setDefaultRenderer(objectClass, renderer); |
|
123 |
} |
|
124 |
return currentRenderer; |
|
125 |
} |
|
126 |
||
127 |
private void updateStyle(JTable c) { |
|
128 |
SynthContext context = getContext(c, ENABLED); |
|
129 |
SynthStyle oldStyle = style; |
|
130 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
131 |
if (style != oldStyle) { |
|
132 |
context.setComponentState(ENABLED | SELECTED); |
|
133 |
||
134 |
Color sbg = table.getSelectionBackground(); |
|
135 |
if (sbg == null || sbg instanceof UIResource) { |
|
136 |
table.setSelectionBackground(style.getColor( |
|
137 |
context, ColorType.TEXT_BACKGROUND)); |
|
138 |
} |
|
139 |
||
140 |
Color sfg = table.getSelectionForeground(); |
|
141 |
if (sfg == null || sfg instanceof UIResource) { |
|
142 |
table.setSelectionForeground(style.getColor( |
|
143 |
context, ColorType.TEXT_FOREGROUND)); |
|
144 |
} |
|
145 |
||
146 |
context.setComponentState(ENABLED); |
|
147 |
||
148 |
Color gridColor = table.getGridColor(); |
|
149 |
if (gridColor == null || gridColor instanceof UIResource) { |
|
150 |
gridColor = (Color)style.get(context, "Table.gridColor"); |
|
151 |
if (gridColor == null) { |
|
152 |
gridColor = style.getColor(context, ColorType.FOREGROUND); |
|
153 |
} |
|
154 |
table.setGridColor(gridColor); |
|
155 |
} |
|
156 |
||
157 |
useTableColors = style.getBoolean(context, |
|
158 |
"Table.rendererUseTableColors", true); |
|
159 |
useUIBorder = style.getBoolean(context, |
|
160 |
"Table.rendererUseUIBorder", true); |
|
161 |
||
162 |
Object rowHeight = style.get(context, "Table.rowHeight"); |
|
163 |
if (rowHeight != null) { |
|
164 |
LookAndFeel.installProperty(table, "rowHeight", rowHeight); |
|
165 |
} |
|
2658 | 166 |
boolean showGrid = style.getBoolean(context, "Table.showGrid", true); |
167 |
if (!showGrid) { |
|
168 |
table.setShowGrid(false); |
|
169 |
} |
|
170 |
Dimension d = table.getIntercellSpacing(); |
|
171 |
// if (d == null || d instanceof UIResource) { |
|
172 |
if (d != null) { |
|
173 |
d = (Dimension)style.get(context, "Table.intercellSpacing"); |
|
174 |
} |
|
175 |
alternateColor = (Color)style.get(context, "Table.alternateRowColor"); |
|
176 |
if (d != null) { |
|
177 |
table.setIntercellSpacing(d); |
|
178 |
} |
|
179 |
||
180 |
||
2 | 181 |
if (oldStyle != null) { |
182 |
uninstallKeyboardActions(); |
|
183 |
installKeyboardActions(); |
|
184 |
} |
|
185 |
} |
|
186 |
context.dispose(); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Attaches listeners to the JTable. |
|
191 |
*/ |
|
192 |
protected void installListeners() { |
|
193 |
super.installListeners(); |
|
194 |
table.addPropertyChangeListener(this); |
|
195 |
} |
|
196 |
||
197 |
protected void uninstallDefaults() { |
|
198 |
table.setDefaultRenderer(Date.class, dateRenderer); |
|
199 |
table.setDefaultRenderer(Number.class, numberRenderer); |
|
200 |
table.setDefaultRenderer(Double.class, doubleRender); |
|
201 |
table.setDefaultRenderer(Float.class, floatRenderer); |
|
202 |
table.setDefaultRenderer(Icon.class, iconRenderer); |
|
203 |
table.setDefaultRenderer(ImageIcon.class, imageIconRenderer); |
|
204 |
table.setDefaultRenderer(Boolean.class, booleanRenderer); |
|
205 |
table.setDefaultRenderer(Object.class, objectRenderer); |
|
206 |
||
207 |
if (table.getTransferHandler() instanceof UIResource) { |
|
208 |
table.setTransferHandler(null); |
|
209 |
} |
|
210 |
SynthContext context = getContext(table, ENABLED); |
|
211 |
style.uninstallDefaults(context); |
|
212 |
context.dispose(); |
|
213 |
style = null; |
|
214 |
} |
|
215 |
||
216 |
protected void uninstallListeners() { |
|
217 |
table.removePropertyChangeListener(this); |
|
218 |
super.uninstallListeners(); |
|
219 |
} |
|
220 |
||
221 |
// |
|
222 |
// SynthUI |
|
223 |
// |
|
224 |
public SynthContext getContext(JComponent c) { |
|
225 |
return getContext(c, getComponentState(c)); |
|
226 |
} |
|
227 |
||
228 |
private SynthContext getContext(JComponent c, int state) { |
|
229 |
return SynthContext.getContext(SynthContext.class, c, |
|
230 |
SynthLookAndFeel.getRegion(c), style, state); |
|
231 |
} |
|
232 |
||
233 |
private Region getRegion(JComponent c) { |
|
234 |
return SynthLookAndFeel.getRegion(c); |
|
235 |
} |
|
236 |
||
237 |
private int getComponentState(JComponent c) { |
|
238 |
return SynthLookAndFeel.getComponentState(c); |
|
239 |
} |
|
240 |
||
241 |
// |
|
242 |
// Paint methods and support |
|
243 |
// |
|
244 |
||
245 |
public void update(Graphics g, JComponent c) { |
|
246 |
SynthContext context = getContext(c); |
|
247 |
||
248 |
SynthLookAndFeel.update(context, g); |
|
249 |
context.getPainter().paintTableBackground(context, |
|
250 |
g, 0, 0, c.getWidth(), c.getHeight()); |
|
251 |
paint(context, g); |
|
252 |
context.dispose(); |
|
253 |
} |
|
254 |
||
255 |
public void paintBorder(SynthContext context, Graphics g, int x, |
|
256 |
int y, int w, int h) { |
|
257 |
context.getPainter().paintTableBorder(context, g, x, y, w, h); |
|
258 |
} |
|
259 |
||
260 |
public void paint(Graphics g, JComponent c) { |
|
261 |
SynthContext context = getContext(c); |
|
262 |
||
263 |
paint(context, g); |
|
264 |
context.dispose(); |
|
265 |
} |
|
266 |
||
267 |
protected void paint(SynthContext context, Graphics g) { |
|
268 |
Rectangle clip = g.getClipBounds(); |
|
269 |
||
270 |
Rectangle bounds = table.getBounds(); |
|
271 |
// account for the fact that the graphics has already been translated |
|
272 |
// into the table's bounds |
|
273 |
bounds.x = bounds.y = 0; |
|
274 |
||
275 |
if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 || |
|
276 |
// this check prevents us from painting the entire table |
|
277 |
// when the clip doesn't intersect our bounds at all |
|
278 |
!bounds.intersects(clip)) { |
|
279 |
||
280 |
paintDropLines(context, g); |
|
281 |
return; |
|
282 |
} |
|
283 |
||
284 |
boolean ltr = table.getComponentOrientation().isLeftToRight(); |
|
285 |
||
286 |
Point upperLeft = clip.getLocation(); |
|
287 |
||
288 |
Point lowerRight = new Point(clip.x + clip.width - 1, |
|
289 |
clip.y + clip.height - 1); |
|
290 |
||
291 |
int rMin = table.rowAtPoint(upperLeft); |
|
292 |
int rMax = table.rowAtPoint(lowerRight); |
|
293 |
// This should never happen (as long as our bounds intersect the clip, |
|
294 |
// which is why we bail above if that is the case). |
|
295 |
if (rMin == -1) { |
|
296 |
rMin = 0; |
|
297 |
} |
|
298 |
// If the table does not have enough rows to fill the view we'll get -1. |
|
299 |
// (We could also get -1 if our bounds don't intersect the clip, |
|
300 |
// which is why we bail above if that is the case). |
|
301 |
// Replace this with the index of the last row. |
|
302 |
if (rMax == -1) { |
|
303 |
rMax = table.getRowCount()-1; |
|
304 |
} |
|
305 |
||
306 |
int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight); |
|
307 |
int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft); |
|
308 |
// This should never happen. |
|
309 |
if (cMin == -1) { |
|
310 |
cMin = 0; |
|
311 |
} |
|
312 |
// If the table does not have enough columns to fill the view we'll get -1. |
|
313 |
// Replace this with the index of the last column. |
|
314 |
if (cMax == -1) { |
|
315 |
cMax = table.getColumnCount()-1; |
|
316 |
} |
|
317 |
||
318 |
// Paint the grid. |
|
319 |
paintGrid(context, g, rMin, rMax, cMin, cMax); |
|
320 |
||
321 |
// Paint the cells. |
|
322 |
paintCells(context, g, rMin, rMax, cMin, cMax); |
|
323 |
||
324 |
paintDropLines(context, g); |
|
325 |
} |
|
326 |
||
327 |
private void paintDropLines(SynthContext context, Graphics g) { |
|
328 |
JTable.DropLocation loc = table.getDropLocation(); |
|
329 |
if (loc == null) { |
|
330 |
return; |
|
331 |
} |
|
332 |
||
333 |
Color color = (Color)style.get(context, "Table.dropLineColor"); |
|
334 |
Color shortColor = (Color)style.get(context, "Table.dropLineShortColor"); |
|
335 |
if (color == null && shortColor == null) { |
|
336 |
return; |
|
337 |
} |
|
338 |
||
339 |
Rectangle rect; |
|
340 |
||
341 |
rect = getHDropLineRect(loc); |
|
342 |
if (rect != null) { |
|
343 |
int x = rect.x; |
|
344 |
int w = rect.width; |
|
345 |
if (color != null) { |
|
346 |
extendRect(rect, true); |
|
347 |
g.setColor(color); |
|
348 |
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
|
349 |
} |
|
350 |
if (!loc.isInsertColumn() && shortColor != null) { |
|
351 |
g.setColor(shortColor); |
|
352 |
g.fillRect(x, rect.y, w, rect.height); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
rect = getVDropLineRect(loc); |
|
357 |
if (rect != null) { |
|
358 |
int y = rect.y; |
|
359 |
int h = rect.height; |
|
360 |
if (color != null) { |
|
361 |
extendRect(rect, false); |
|
362 |
g.setColor(color); |
|
363 |
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
|
364 |
} |
|
365 |
if (!loc.isInsertRow() && shortColor != null) { |
|
366 |
g.setColor(shortColor); |
|
367 |
g.fillRect(rect.x, y, rect.width, h); |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
private Rectangle getHDropLineRect(JTable.DropLocation loc) { |
|
373 |
if (!loc.isInsertRow()) { |
|
374 |
return null; |
|
375 |
} |
|
376 |
||
377 |
int row = loc.getRow(); |
|
378 |
int col = loc.getColumn(); |
|
379 |
if (col >= table.getColumnCount()) { |
|
380 |
col--; |
|
381 |
} |
|
382 |
||
383 |
Rectangle rect = table.getCellRect(row, col, true); |
|
384 |
||
385 |
if (row >= table.getRowCount()) { |
|
386 |
row--; |
|
387 |
Rectangle prevRect = table.getCellRect(row, col, true); |
|
388 |
rect.y = prevRect.y + prevRect.height; |
|
389 |
} |
|
390 |
||
391 |
if (rect.y == 0) { |
|
392 |
rect.y = -1; |
|
393 |
} else { |
|
394 |
rect.y -= 2; |
|
395 |
} |
|
396 |
||
397 |
rect.height = 3; |
|
398 |
||
399 |
return rect; |
|
400 |
} |
|
401 |
||
402 |
private Rectangle getVDropLineRect(JTable.DropLocation loc) { |
|
403 |
if (!loc.isInsertColumn()) { |
|
404 |
return null; |
|
405 |
} |
|
406 |
||
407 |
boolean ltr = table.getComponentOrientation().isLeftToRight(); |
|
408 |
int col = loc.getColumn(); |
|
409 |
Rectangle rect = table.getCellRect(loc.getRow(), col, true); |
|
410 |
||
411 |
if (col >= table.getColumnCount()) { |
|
412 |
col--; |
|
413 |
rect = table.getCellRect(loc.getRow(), col, true); |
|
414 |
if (ltr) { |
|
415 |
rect.x = rect.x + rect.width; |
|
416 |
} |
|
417 |
} else if (!ltr) { |
|
418 |
rect.x = rect.x + rect.width; |
|
419 |
} |
|
420 |
||
421 |
if (rect.x == 0) { |
|
422 |
rect.x = -1; |
|
423 |
} else { |
|
424 |
rect.x -= 2; |
|
425 |
} |
|
426 |
||
427 |
rect.width = 3; |
|
428 |
||
429 |
return rect; |
|
430 |
} |
|
431 |
||
432 |
private Rectangle extendRect(Rectangle rect, boolean horizontal) { |
|
433 |
if (rect == null) { |
|
434 |
return rect; |
|
435 |
} |
|
436 |
||
437 |
if (horizontal) { |
|
438 |
rect.x = 0; |
|
439 |
rect.width = table.getWidth(); |
|
440 |
} else { |
|
441 |
rect.y = 0; |
|
442 |
||
443 |
if (table.getRowCount() != 0) { |
|
444 |
Rectangle lastRect = table.getCellRect(table.getRowCount() - 1, 0, true); |
|
445 |
rect.height = lastRect.y + lastRect.height; |
|
446 |
} else { |
|
447 |
rect.height = table.getHeight(); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
return rect; |
|
452 |
} |
|
453 |
||
454 |
/* |
|
455 |
* Paints the grid lines within <I>aRect</I>, using the grid |
|
456 |
* color set with <I>setGridColor</I>. Paints vertical lines |
|
457 |
* if <code>getShowVerticalLines()</code> returns true and paints |
|
458 |
* horizontal lines if <code>getShowHorizontalLines()</code> |
|
459 |
* returns true. |
|
460 |
*/ |
|
461 |
private void paintGrid(SynthContext context, Graphics g, int rMin, |
|
462 |
int rMax, int cMin, int cMax) { |
|
463 |
g.setColor(table.getGridColor()); |
|
464 |
||
465 |
Rectangle minCell = table.getCellRect(rMin, cMin, true); |
|
466 |
Rectangle maxCell = table.getCellRect(rMax, cMax, true); |
|
467 |
Rectangle damagedArea = minCell.union( maxCell ); |
|
468 |
SynthGraphicsUtils synthG = context.getStyle().getGraphicsUtils( |
|
469 |
context); |
|
470 |
||
471 |
if (table.getShowHorizontalLines()) { |
|
472 |
int tableWidth = damagedArea.x + damagedArea.width; |
|
473 |
int y = damagedArea.y; |
|
474 |
for (int row = rMin; row <= rMax; row++) { |
|
475 |
y += table.getRowHeight(row); |
|
476 |
synthG.drawLine(context, "Table.grid", |
|
477 |
g, damagedArea.x, y - 1, tableWidth - 1,y - 1); |
|
478 |
} |
|
479 |
} |
|
480 |
if (table.getShowVerticalLines()) { |
|
481 |
TableColumnModel cm = table.getColumnModel(); |
|
482 |
int tableHeight = damagedArea.y + damagedArea.height; |
|
483 |
int x; |
|
484 |
if (table.getComponentOrientation().isLeftToRight()) { |
|
485 |
x = damagedArea.x; |
|
486 |
for (int column = cMin; column <= cMax; column++) { |
|
487 |
int w = cm.getColumn(column).getWidth(); |
|
488 |
x += w; |
|
489 |
synthG.drawLine(context, "Table.grid", g, x - 1, 0, |
|
490 |
x - 1, tableHeight - 1); |
|
491 |
} |
|
492 |
} else { |
|
493 |
x = damagedArea.x; |
|
494 |
for (int column = cMax; column >= cMin; column--) { |
|
495 |
int w = cm.getColumn(column).getWidth(); |
|
496 |
x += w; |
|
497 |
synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1, |
|
498 |
tableHeight - 1); |
|
499 |
} |
|
500 |
} |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
private int viewIndexForColumn(TableColumn aColumn) { |
|
505 |
TableColumnModel cm = table.getColumnModel(); |
|
506 |
for (int column = 0; column < cm.getColumnCount(); column++) { |
|
507 |
if (cm.getColumn(column) == aColumn) { |
|
508 |
return column; |
|
509 |
} |
|
510 |
} |
|
511 |
return -1; |
|
512 |
} |
|
513 |
||
514 |
private void paintCells(SynthContext context, Graphics g, int rMin, |
|
515 |
int rMax, int cMin, int cMax) { |
|
516 |
JTableHeader header = table.getTableHeader(); |
|
517 |
TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn(); |
|
518 |
||
519 |
TableColumnModel cm = table.getColumnModel(); |
|
520 |
int columnMargin = cm.getColumnMargin(); |
|
521 |
||
522 |
Rectangle cellRect; |
|
523 |
TableColumn aColumn; |
|
524 |
int columnWidth; |
|
525 |
if (table.getComponentOrientation().isLeftToRight()) { |
|
526 |
for(int row = rMin; row <= rMax; row++) { |
|
527 |
cellRect = table.getCellRect(row, cMin, false); |
|
528 |
for(int column = cMin; column <= cMax; column++) { |
|
529 |
aColumn = cm.getColumn(column); |
|
530 |
columnWidth = aColumn.getWidth(); |
|
531 |
cellRect.width = columnWidth - columnMargin; |
|
532 |
if (aColumn != draggedColumn) { |
|
533 |
paintCell(context, g, cellRect, row, column); |
|
534 |
} |
|
535 |
cellRect.x += columnWidth; |
|
536 |
} |
|
537 |
} |
|
538 |
} else { |
|
539 |
for(int row = rMin; row <= rMax; row++) { |
|
540 |
cellRect = table.getCellRect(row, cMin, false); |
|
541 |
aColumn = cm.getColumn(cMin); |
|
542 |
if (aColumn != draggedColumn) { |
|
543 |
columnWidth = aColumn.getWidth(); |
|
544 |
cellRect.width = columnWidth - columnMargin; |
|
545 |
paintCell(context, g, cellRect, row, cMin); |
|
546 |
} |
|
547 |
for(int column = cMin+1; column <= cMax; column++) { |
|
548 |
aColumn = cm.getColumn(column); |
|
549 |
columnWidth = aColumn.getWidth(); |
|
550 |
cellRect.width = columnWidth - columnMargin; |
|
551 |
cellRect.x -= columnWidth; |
|
552 |
if (aColumn != draggedColumn) { |
|
553 |
paintCell(context, g, cellRect, row, column); |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
// Paint the dragged column if we are dragging. |
|
560 |
if (draggedColumn != null) { |
|
561 |
paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance()); |
|
562 |
} |
|
563 |
||
564 |
// Remove any renderers that may be left in the rendererPane. |
|
565 |
rendererPane.removeAll(); |
|
566 |
} |
|
567 |
||
568 |
private void paintDraggedArea(SynthContext context, Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) { |
|
569 |
int draggedColumnIndex = viewIndexForColumn(draggedColumn); |
|
570 |
||
571 |
Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true); |
|
572 |
Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true); |
|
573 |
||
574 |
Rectangle vacatedColumnRect = minCell.union(maxCell); |
|
575 |
||
576 |
// Paint a gray well in place of the moving column. |
|
577 |
g.setColor(table.getParent().getBackground()); |
|
578 |
g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, |
|
579 |
vacatedColumnRect.width, vacatedColumnRect.height); |
|
580 |
||
581 |
// Move to the where the cell has been dragged. |
|
582 |
vacatedColumnRect.x += distance; |
|
583 |
||
584 |
// Fill the background. |
|
585 |
g.setColor(context.getStyle().getColor(context, ColorType.BACKGROUND)); |
|
586 |
g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, |
|
587 |
vacatedColumnRect.width, vacatedColumnRect.height); |
|
588 |
||
589 |
SynthGraphicsUtils synthG = context.getStyle().getGraphicsUtils( |
|
590 |
context); |
|
591 |
||
592 |
||
593 |
// Paint the vertical grid lines if necessary. |
|
594 |
if (table.getShowVerticalLines()) { |
|
595 |
g.setColor(table.getGridColor()); |
|
596 |
int x1 = vacatedColumnRect.x; |
|
597 |
int y1 = vacatedColumnRect.y; |
|
598 |
int x2 = x1 + vacatedColumnRect.width - 1; |
|
599 |
int y2 = y1 + vacatedColumnRect.height - 1; |
|
600 |
// Left |
|
601 |
synthG.drawLine(context, "Table.grid", g, x1-1, y1, x1-1, y2); |
|
602 |
// Right |
|
603 |
synthG.drawLine(context, "Table.grid", g, x2, y1, x2, y2); |
|
604 |
} |
|
605 |
||
606 |
for(int row = rMin; row <= rMax; row++) { |
|
607 |
// Render the cell value |
|
608 |
Rectangle r = table.getCellRect(row, draggedColumnIndex, false); |
|
609 |
r.x += distance; |
|
610 |
paintCell(context, g, r, row, draggedColumnIndex); |
|
611 |
||
612 |
// Paint the (lower) horizontal grid line if necessary. |
|
613 |
if (table.getShowHorizontalLines()) { |
|
614 |
g.setColor(table.getGridColor()); |
|
615 |
Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true); |
|
616 |
rcr.x += distance; |
|
617 |
int x1 = rcr.x; |
|
618 |
int y1 = rcr.y; |
|
619 |
int x2 = x1 + rcr.width - 1; |
|
620 |
int y2 = y1 + rcr.height - 1; |
|
621 |
synthG.drawLine(context, "Table.grid", g, x1, y2, x2, y2); |
|
622 |
} |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
private void paintCell(SynthContext context, Graphics g, |
|
627 |
Rectangle cellRect, int row, int column) { |
|
628 |
if (table.isEditing() && table.getEditingRow()==row && |
|
629 |
table.getEditingColumn()==column) { |
|
630 |
Component component = table.getEditorComponent(); |
|
631 |
component.setBounds(cellRect); |
|
632 |
component.validate(); |
|
633 |
} |
|
634 |
else { |
|
635 |
TableCellRenderer renderer = table.getCellRenderer(row, column); |
|
636 |
Component component = table.prepareRenderer(renderer, row, column); |
|
2658 | 637 |
Color b = component.getBackground(); |
638 |
if ((b == null || b instanceof UIResource |
|
639 |
|| component instanceof SynthBooleanTableCellRenderer) |
|
640 |
&& !table.isCellSelected(row, column)) { |
|
4283
06d5378aaff3
6882917: Nimbus and DefaultTableCellRenderer: must start with normal background
peterz
parents:
2658
diff
changeset
|
641 |
if (alternateColor != null && row % 2 != 0) { |
2658 | 642 |
component.setBackground(alternateColor); |
643 |
} |
|
644 |
} |
|
2 | 645 |
rendererPane.paintComponent(g, component, table, cellRect.x, |
646 |
cellRect.y, cellRect.width, cellRect.height, true); |
|
647 |
} |
|
648 |
} |
|
649 |
||
650 |
public void propertyChange(PropertyChangeEvent event) { |
|
651 |
if (SynthLookAndFeel.shouldUpdateStyle(event)) { |
|
652 |
updateStyle((JTable)event.getSource()); |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
||
657 |
private class SynthBooleanTableCellRenderer extends JCheckBox implements |
|
658 |
TableCellRenderer { |
|
659 |
private boolean isRowSelected; |
|
660 |
||
661 |
public SynthBooleanTableCellRenderer() { |
|
662 |
setHorizontalAlignment(JLabel.CENTER); |
|
2658 | 663 |
setName("Table.cellRenderer"); |
2 | 664 |
} |
665 |
||
666 |
public Component getTableCellRendererComponent( |
|
667 |
JTable table, Object value, boolean isSelected, |
|
668 |
boolean hasFocus, int row, int column) { |
|
669 |
isRowSelected = isSelected; |
|
670 |
||
671 |
if (isSelected) { |
|
2658 | 672 |
setForeground(unwrap(table.getSelectionForeground())); |
673 |
setBackground(unwrap(table.getSelectionBackground())); |
|
2 | 674 |
} else { |
2658 | 675 |
setForeground(unwrap(table.getForeground())); |
676 |
setBackground(unwrap(table.getBackground())); |
|
2 | 677 |
} |
678 |
||
679 |
setSelected((value != null && ((Boolean)value).booleanValue())); |
|
680 |
return this; |
|
681 |
} |
|
682 |
||
2658 | 683 |
private Color unwrap(Color c) { |
684 |
if (c instanceof UIResource) { |
|
685 |
return new Color(c.getRGB()); |
|
686 |
} |
|
687 |
return c; |
|
688 |
} |
|
689 |
||
2 | 690 |
public boolean isOpaque() { |
691 |
return isRowSelected ? true : super.isOpaque(); |
|
692 |
} |
|
693 |
} |
|
694 |
||
695 |
private class SynthTableCellRenderer extends DefaultTableCellRenderer { |
|
696 |
private Object numberFormat; |
|
697 |
private Object dateFormat; |
|
698 |
private boolean opaque; |
|
699 |
||
700 |
public void setOpaque(boolean isOpaque) { |
|
701 |
opaque = isOpaque; |
|
702 |
} |
|
703 |
||
704 |
public boolean isOpaque() { |
|
705 |
return opaque; |
|
706 |
} |
|
707 |
||
708 |
public String getName() { |
|
709 |
String name = super.getName(); |
|
710 |
if (name == null) { |
|
711 |
return "Table.cellRenderer"; |
|
712 |
} |
|
713 |
return name; |
|
714 |
} |
|
715 |
||
716 |
public void setBorder(Border b) { |
|
717 |
if (useUIBorder || b instanceof SynthBorder) { |
|
718 |
super.setBorder(b); |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
public Component getTableCellRendererComponent( |
|
723 |
JTable table, Object value, boolean isSelected, |
|
724 |
boolean hasFocus, int row, int column) { |
|
725 |
if (!useTableColors && (isSelected || hasFocus)) { |
|
726 |
SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel. |
|
727 |
getUIOfType(getUI(), SynthLabelUI.class), |
|
728 |
isSelected, hasFocus, table.isEnabled(), false); |
|
729 |
} |
|
730 |
else { |
|
731 |
SynthLookAndFeel.resetSelectedUI(); |
|
732 |
} |
|
733 |
super.getTableCellRendererComponent(table, value, isSelected, |
|
734 |
hasFocus, row, column); |
|
735 |
||
736 |
setIcon(null); |
|
737 |
Class columnClass = table.getColumnClass(column); |
|
738 |
configureValue(value, columnClass); |
|
739 |
||
740 |
return this; |
|
741 |
} |
|
742 |
||
743 |
private void configureValue(Object value, Class columnClass) { |
|
744 |
if (columnClass == Object.class || columnClass == null) { |
|
745 |
setHorizontalAlignment(JLabel.LEADING); |
|
746 |
} else if (columnClass == Float.class || columnClass == Double.class) { |
|
747 |
if (numberFormat == null) { |
|
748 |
numberFormat = NumberFormat.getInstance(); |
|
749 |
} |
|
750 |
setHorizontalAlignment(JLabel.TRAILING); |
|
751 |
setText((value == null) ? "" : ((NumberFormat)numberFormat).format(value)); |
|
752 |
} |
|
753 |
else if (columnClass == Number.class) { |
|
754 |
setHorizontalAlignment(JLabel.TRAILING); |
|
755 |
// Super will have set value. |
|
756 |
} |
|
757 |
else if (columnClass == Icon.class || columnClass == ImageIcon.class) { |
|
758 |
setHorizontalAlignment(JLabel.CENTER); |
|
2658 | 759 |
setIcon((value instanceof Icon) ? (Icon)value : null); |
2 | 760 |
setText(""); |
761 |
} |
|
762 |
else if (columnClass == Date.class) { |
|
763 |
if (dateFormat == null) { |
|
764 |
dateFormat = DateFormat.getDateInstance(); |
|
765 |
} |
|
766 |
setHorizontalAlignment(JLabel.LEADING); |
|
767 |
setText((value == null) ? "" : ((Format)dateFormat).format(value)); |
|
768 |
} |
|
769 |
else { |
|
770 |
configureValue(value, columnClass.getSuperclass()); |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
public void paint(Graphics g) { |
|
775 |
super.paint(g); |
|
776 |
SynthLookAndFeel.resetSelectedUI(); |
|
777 |
} |
|
778 |
} |
|
779 |
} |