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