1 11 package org.eclipse.swt.custom; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 import org.eclipse.swt.events.*; 17 18 135 public class TableCursor extends Canvas { 136 Table table; 137 TableItem row = null; 138 TableColumn column = null; 139 Listener tableListener, resizeListener, disposeItemListener, disposeColumnListener; 140 141 static final int BACKGROUND = SWT.COLOR_LIST_SELECTION_TEXT; 143 static final int FOREGROUND = SWT.COLOR_LIST_SELECTION; 144 145 173 public TableCursor(Table parent, int style) { 174 super(parent, style); 175 table = parent; 176 setBackground(null); 177 setForeground(null); 178 179 Listener listener = new Listener() { 180 public void handleEvent(Event event) { 181 switch (event.type) { 182 case SWT.Dispose : 183 dispose(event); 184 break; 185 case SWT.FocusIn : 186 case SWT.FocusOut : 187 redraw(); 188 break; 189 case SWT.KeyDown : 190 keyDown(event); 191 break; 192 case SWT.Paint : 193 paint(event); 194 break; 195 case SWT.Traverse : { 196 event.doit = true; 197 switch (event.detail) { 198 case SWT.TRAVERSE_ARROW_NEXT : 199 case SWT.TRAVERSE_ARROW_PREVIOUS : 200 case SWT.TRAVERSE_RETURN : 201 event.doit = false; 202 break; 203 } 204 break; 205 } 206 } 207 } 208 }; 209 int[] events = new int[] {SWT.Dispose, SWT.FocusIn, SWT.FocusOut, SWT.KeyDown, SWT.Paint, SWT.Traverse}; 210 for (int i = 0; i < events.length; i++) { 211 addListener(events[i], listener); 212 } 213 214 tableListener = new Listener() { 215 public void handleEvent(Event event) { 216 switch (event.type) { 217 case SWT.MouseDown : 218 tableMouseDown(event); 219 break; 220 case SWT.FocusIn : 221 tableFocusIn(event); 222 break; 223 } 224 } 225 }; 226 table.addListener(SWT.FocusIn, tableListener); 227 table.addListener(SWT.MouseDown, tableListener); 228 229 disposeItemListener = new Listener() { 230 public void handleEvent(Event event) { 231 row = null; 232 column = null; 233 _resize(); 234 } 235 }; 236 disposeColumnListener = new Listener() { 237 public void handleEvent(Event event) { 238 row = null; 239 column = null; 240 _resize(); 241 } 242 }; 243 resizeListener = new Listener() { 244 public void handleEvent(Event event) { 245 _resize(); 246 } 247 }; 248 ScrollBar hBar = table.getHorizontalBar(); 249 if (hBar != null) { 250 hBar.addListener(SWT.Selection, resizeListener); 251 } 252 ScrollBar vBar = table.getVerticalBar(); 253 if (vBar != null) { 254 vBar.addListener(SWT.Selection, resizeListener); 255 } 256 } 257 258 285 public void addSelectionListener(SelectionListener listener) { 286 checkWidget(); 287 if (listener == null) 288 SWT.error(SWT.ERROR_NULL_ARGUMENT); 289 TypedListener typedListener = new TypedListener(listener); 290 addListener(SWT.Selection, typedListener); 291 addListener(SWT.DefaultSelection, typedListener); 292 } 293 294 void dispose(Event event) { 295 table.removeListener(SWT.FocusIn, tableListener); 296 table.removeListener(SWT.MouseDown, tableListener); 297 if (column != null) { 298 column.removeListener(SWT.Dispose, disposeColumnListener); 299 column.removeListener(SWT.Move, resizeListener); 300 column.removeListener(SWT.Resize, resizeListener); 301 column = null; 302 } 303 if (row != null) { 304 row.removeListener(SWT.Dispose, disposeItemListener); 305 row = null; 306 } 307 ScrollBar hBar = table.getHorizontalBar(); 308 if (hBar != null) { 309 hBar.removeListener(SWT.Selection, resizeListener); 310 } 311 ScrollBar vBar = table.getVerticalBar(); 312 if (vBar != null) { 313 vBar.removeListener(SWT.Selection, resizeListener); 314 } 315 } 316 317 void keyDown(Event event) { 318 if (row == null) return; 319 switch (event.character) { 320 case SWT.CR : 321 notifyListeners(SWT.DefaultSelection, new Event()); 322 return; 323 } 324 int rowIndex = table.indexOf(row); 325 int columnIndex = column == null ? 0 : table.indexOf(column); 326 switch (event.keyCode) { 327 case SWT.ARROW_UP : 328 setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true); 329 break; 330 case SWT.ARROW_DOWN : 331 setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true); 332 break; 333 case SWT.ARROW_LEFT : 334 case SWT.ARROW_RIGHT : 335 { 336 int columnCount = table.getColumnCount(); 337 if (columnCount == 0) break; 338 int[] order = table.getColumnOrder(); 339 int index = 0; 340 while (index < order.length) { 341 if (order[index] == columnIndex) break; 342 index++; 343 } 344 if (index == order.length) index = 0; 345 int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; 346 if (event.keyCode == leadKey) { 347 setRowColumn(rowIndex, order[Math.max(0, index - 1)], true); 348 } else { 349 setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true); 350 } 351 break; 352 } 353 case SWT.HOME : 354 setRowColumn(0, columnIndex, true); 355 break; 356 case SWT.END : 357 { 358 int i = table.getItemCount() - 1; 359 setRowColumn(i, columnIndex, true); 360 break; 361 } 362 case SWT.PAGE_UP : 363 { 364 int index = table.getTopIndex(); 365 if (index == rowIndex) { 366 Rectangle rect = table.getClientArea(); 367 TableItem item = table.getItem(index); 368 Rectangle itemRect = item.getBounds(0); 369 rect.height -= itemRect.y; 370 int height = table.getItemHeight(); 371 int page = Math.max(1, rect.height / height); 372 index = Math.max(0, index - page + 1); 373 } 374 setRowColumn(index, columnIndex, true); 375 break; 376 } 377 case SWT.PAGE_DOWN : 378 { 379 int index = table.getTopIndex(); 380 Rectangle rect = table.getClientArea(); 381 TableItem item = table.getItem(index); 382 Rectangle itemRect = item.getBounds(0); 383 rect.height -= itemRect.y; 384 int height = table.getItemHeight(); 385 int page = Math.max(1, rect.height / height); 386 int end = table.getItemCount() - 1; 387 index = Math.min(end, index + page - 1); 388 if (index == rowIndex) { 389 index = Math.min(end, index + page - 1); 390 } 391 setRowColumn(index, columnIndex, true); 392 break; 393 } 394 } 395 } 396 397 void paint(Event event) { 398 if (row == null) return; 399 int columnIndex = column == null ? 0 : table.indexOf(column); 400 GC gc = event.gc; 401 Display display = getDisplay(); 402 gc.setBackground(getBackground()); 403 gc.setForeground(getForeground()); 404 gc.fillRectangle(event.x, event.y, event.width, event.height); 405 int x = 0; 406 Point size = getSize(); 407 Image image = row.getImage(columnIndex); 408 if (image != null) { 409 Rectangle imageSize = image.getBounds(); 410 int imageY = (size.y - imageSize.height) / 2; 411 gc.drawImage(image, x, imageY); 412 x += imageSize.width; 413 } 414 String text = row.getText(columnIndex); 415 if (text.length() > 0) { 416 Rectangle bounds = row.getBounds(columnIndex); 417 Point extent = gc.stringExtent(text); 418 String platform = SWT.getPlatform(); 420 if ("win32".equals(platform)) { if (table.getColumnCount() == 0 || columnIndex == 0) { 422 x += 2; 423 } else { 424 int alignmnent = column.getAlignment(); 425 switch (alignmnent) { 426 case SWT.LEFT: 427 x += 6; 428 break; 429 case SWT.RIGHT: 430 x = bounds.width - extent.x - 6; 431 break; 432 case SWT.CENTER: 433 x += (bounds.width - x - extent.x) / 2; 434 break; 435 } 436 } 437 } else { 438 if (table.getColumnCount() == 0) { 439 x += 5; 440 } else { 441 int alignmnent = column.getAlignment(); 442 switch (alignmnent) { 443 case SWT.LEFT: 444 x += 5; 445 break; 446 case SWT.RIGHT: 447 x = bounds.width- extent.x - 2; 448 break; 449 case SWT.CENTER: 450 x += (bounds.width - x - extent.x) / 2 + 2; 451 break; 452 } 453 } 454 } 455 int textY = (size.y - extent.y) / 2; 456 gc.drawString(text, x, textY); 457 } 458 if (isFocusControl()) { 459 gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 460 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 461 gc.drawFocus(0, 0, size.x, size.y); 462 } 463 } 464 465 void tableFocusIn(Event event) { 466 if (isDisposed()) 467 return; 468 if (isVisible()) 469 setFocus(); 470 } 471 472 void tableMouseDown(Event event) { 473 if (isDisposed() || !isVisible()) return; 474 Point pt = new Point(event.x, event.y); 475 int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0; 476 TableItem item = table.getItem(pt); 477 if ((table.getStyle() & SWT.FULL_SELECTION) != 0) { 478 if (item == null) return; 479 } else { 480 int start = item != null ? table.indexOf(item) : table.getTopIndex(); 481 int end = table.getItemCount(); 482 Rectangle clientRect = table.getClientArea(); 483 for (int i = start; i < end; i++) { 484 TableItem nextItem = table.getItem(i); 485 Rectangle rect = nextItem.getBounds(0); 486 if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) { 487 item = nextItem; 488 break; 489 } 490 if (rect.y > clientRect.y + clientRect.height) return; 491 } 492 if (item == null) return; 493 } 494 TableColumn newColumn = null; 495 int columnCount = table.getColumnCount(); 496 if (columnCount > 0) { 497 for (int i = 0; i < columnCount; i++) { 498 Rectangle rect = item.getBounds(i); 499 rect.width += lineWidth; 500 rect.height += lineWidth; 501 if (rect.contains(pt)) { 502 newColumn = table.getColumn(i); 503 break; 504 } 505 } 506 if (newColumn == null) { 507 newColumn = table.getColumn(0); 508 } 509 } 510 setRowColumn(item, newColumn, true); 511 setFocus(); 512 return; 513 } 514 void setRowColumn(int row, int column, boolean notify) { 515 TableItem item = row == -1 ? null : table.getItem(row); 516 TableColumn col = column == -1 || table.getColumnCount() == 0 ? null : table.getColumn(column); 517 setRowColumn(item, col, notify); 518 } 519 void setRowColumn(TableItem row, TableColumn column, boolean notify) { 520 if (this.row == row && this.column == column) { 521 return; 522 } 523 if (this.row != null && this.row != row) { 524 this.row.removeListener(SWT.Dispose, disposeItemListener); 525 this.row = null; 526 } 527 if (this.column != null && this.column != column) { 528 this.column.removeListener(SWT.Dispose, disposeColumnListener); 529 this.column.removeListener(SWT.Move, resizeListener); 530 this.column.removeListener(SWT.Resize, resizeListener); 531 this.column = null; 532 } 533 if (row != null) { 534 if (this.row != row) { 535 this.row = row; 536 row.addListener(SWT.Dispose, disposeItemListener); 537 table.showItem(row); 538 } 539 if (this.column != column && column != null) { 540 this.column = column; 541 column.addListener(SWT.Dispose, disposeColumnListener); 542 column.addListener(SWT.Move, resizeListener); 543 column.addListener(SWT.Resize, resizeListener); 544 table.showColumn(column); 545 } 546 int columnIndex = column == null ? 0 : table.indexOf(column); 547 setBounds(row.getBounds(columnIndex)); 548 redraw(); 549 if (notify) { 550 notifyListeners(SWT.Selection, new Event()); 551 } 552 } 553 } 554 555 public void setVisible(boolean visible) { 556 checkWidget(); 557 if (visible) _resize(); 558 super.setVisible(visible); 559 } 560 561 580 public void removeSelectionListener(SelectionListener listener) { 581 checkWidget(); 582 if (listener == null) { 583 SWT.error(SWT.ERROR_NULL_ARGUMENT); 584 } 585 removeListener(SWT.Selection, listener); 586 removeListener(SWT.DefaultSelection, listener); 587 } 588 589 void _resize() { 590 if (row == null) { 591 setBounds(-200, -200, 0, 0); 592 } else { 593 int columnIndex = column == null ? 0 : table.indexOf(column); 594 setBounds(row.getBounds(columnIndex)); 595 } 596 } 597 607 public int getColumn() { 608 checkWidget(); 609 return column == null ? 0 : table.indexOf(column); 610 } 611 621 public TableItem getRow() { 622 checkWidget(); 623 return row; 624 } 625 public void setBackground (Color color) { 626 if (color == null) color = getDisplay().getSystemColor(BACKGROUND); 627 super.setBackground(color); 628 redraw(); 629 } 630 public void setForeground (Color color) { 631 if (color == null) color = getDisplay().getSystemColor(FOREGROUND); 632 super.setForeground(color); 633 redraw(); 634 } 635 647 public void setSelection(int row, int column) { 648 checkWidget(); 649 int columnCount = table.getColumnCount(); 650 int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; 651 if (row < 0 652 || row >= table.getItemCount() 653 || column < 0 654 || column > maxColumnIndex) 655 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 656 setRowColumn(row, column, false); 657 } 658 670 public void setSelection(TableItem row, int column) { 671 checkWidget(); 672 int columnCount = table.getColumnCount(); 673 int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; 674 if (row == null 675 || row.isDisposed() 676 || column < 0 677 || column > maxColumnIndex) 678 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 679 setRowColumn(table.indexOf(row), column, false); 680 } 681 } 682 | Popular Tags |