1 11 12 package org.eclipse.debug.internal.ui.views.memory; 13 14 import java.math.BigInteger ; 15 16 import org.eclipse.debug.core.DebugException; 17 import org.eclipse.debug.internal.core.memory.IExtendedMemoryBlock; 18 import org.eclipse.debug.internal.ui.DebugUIMessages; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.resource.JFaceResources; 23 import org.eclipse.jface.viewers.IBaseLabelProvider; 24 import org.eclipse.jface.viewers.ICellModifier; 25 import org.eclipse.jface.viewers.TableViewer; 26 import org.eclipse.jface.viewers.TextCellEditor; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.custom.TableCursor; 29 import org.eclipse.swt.custom.TableEditor; 30 import org.eclipse.swt.events.DisposeEvent; 31 import org.eclipse.swt.events.DisposeListener; 32 import org.eclipse.swt.events.FocusEvent; 33 import org.eclipse.swt.events.FocusListener; 34 import org.eclipse.swt.events.KeyEvent; 35 import org.eclipse.swt.events.KeyListener; 36 import org.eclipse.swt.events.MouseEvent; 37 import org.eclipse.swt.events.MouseListener; 38 import org.eclipse.swt.events.SelectionEvent; 39 import org.eclipse.swt.events.SelectionListener; 40 import org.eclipse.swt.events.TraverseEvent; 41 import org.eclipse.swt.events.TraverseListener; 42 import org.eclipse.swt.graphics.Font; 43 import org.eclipse.swt.graphics.Rectangle; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Display; 46 import org.eclipse.swt.widgets.Menu; 47 import org.eclipse.swt.widgets.Table; 48 import org.eclipse.swt.widgets.TableItem; 49 import org.eclipse.swt.widgets.Text; 50 51 52 61 public class ViewTabCursorManager 62 { 63 private TableCursor fTableCursor; 64 private MemoryViewTab fViewTab; 65 private MenuManager fMenuManager; 66 protected int fRow; 67 protected int fCol; 68 private TableEditor editor; 69 private boolean fMenuDisposed = false; 70 71 private Table fTable; 72 private TableViewer fTableViewer; 73 private String fRenderingId; 74 private IFixedLengthOutputRenderer fRenderer; 75 76 private TraverseEventListener fTraverseEvtListener; 77 private TextFocusListener fTextFocusListener; 78 private TextKeyListener fTextKeyListener; 79 private MouseEventListener fMouseEventListener; 80 81 class MouseEventListener implements MouseListener 82 { 83 84 87 public void mouseDoubleClick(MouseEvent e) 88 { 89 } 90 91 94 public void mouseDown(MouseEvent e) 95 { 96 if (e.getSource() instanceof Table) 97 handleTableMouseEvent(e); 98 else if (e.getSource() instanceof TableCursor) 99 handleCursorMouseEvent(e); 100 } 101 102 private void handleCursorMouseEvent(MouseEvent e){ 103 if (e.button == 1) 104 { 105 if (fCol > 0 && fCol <= (getNumCol())) 106 activateCellEditor(null); 107 } 108 } 109 110 113 private void handleTableMouseEvent(MouseEvent e) { 114 TableItem[] selections = fTableViewer.getTable().getSelection(); 116 TableItem selectedRow = null; 117 int colNum = -1; 118 119 if (selections.length > 0) 120 { 121 selectedRow = selections[0]; 122 123 int numCol = fTableViewer.getColumnProperties().length; 124 125 for (int i=0; i<numCol; i++) 126 { 127 Rectangle bound = selectedRow.getBounds(i); 128 if (bound.contains(e.x, e.y)) 129 { 130 colNum = i; 131 break; 132 } 133 } 134 } 135 136 if (colNum < 1) 138 return; 139 140 141 if (selectedRow != null) 144 { 145 int row = fTableViewer.getTable().indexOf(selectedRow); 146 147 updateCursorPosition(row, colNum, true); 148 } 149 150 fViewTab.updateSyncTopAddress(true); 151 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 153 154 fViewTab.updateTableSelection(); 156 157 setCursorFocus(); 158 } 159 160 163 public void mouseUp(MouseEvent e) 164 { 165 166 } 167 } 168 169 173 class TraverseEventListener implements TraverseListener 174 { 175 178 public void keyTraversed(TraverseEvent event) 179 { 180 if (event.getSource() instanceof Text) 181 handleTextTraverseEvt(event); 182 else if (event.getSource() instanceof TableCursor) 183 handleCursorTraverseEvt(event); 184 } 185 186 189 private void handleTextTraverseEvt(TraverseEvent event) { 190 final TraverseEvent e = event; 191 Display.getDefault().asyncExec(new Runnable () 192 { 193 public void run() 194 { 195 try 196 { 197 if (fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock) 199 { 200 if (fRow+1 >= fTable.getItemCount() || fRow-1 <= 0) 201 { 202 BigInteger topAddress = fViewTab.getTopVisibleAddress(); 203 fViewTab.reloadTable(topAddress, false); 204 } 205 } 206 } 207 catch (DebugException e1) 208 { 209 DebugUIPlugin.log(e1); 210 return; 211 } 212 213 if (e.detail == SWT.TRAVERSE_TAB_NEXT) 214 { 215 if (!(fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock)) 216 { 217 if (fRow+1 >= fTable.getItemCount() && 218 fCol == getNumCol()) 219 { 220 return; 221 } 222 } 223 224 if (fCol == getNumCol()) 226 { 227 fCol = 1; 228 fRow++; 229 } 230 else 231 { 232 fCol++; 233 } 234 } 235 else if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS) 236 { 237 if (!(fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock)) 238 { 239 if (fRow-1 < 0 && fCol == 1) 240 { 241 return; 242 } 243 } 244 245 if (fCol == 1) 247 { 248 fCol = getNumCol(); 249 fRow--; 250 } 251 else 252 { 253 fCol--; 254 } 255 } 256 else 257 return; 258 259 updateCursorPosition(fRow, fCol, true); 261 262 fViewTab.updateSyncTopAddress(true); 263 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 264 fViewTab.updateTableSelection(); 265 266 Text text = (Text)e.getSource(); 267 removeListeners(text); 268 269 activateCellEditor(null); 270 } 271 272 273 }); 274 } 275 276 private void handleCursorTraverseEvt(TraverseEvent e){ 277 if (fCol == getNumCol() && e.keyCode == SWT.ARROW_RIGHT) 278 { 279 if (fRow + 1>= fTable.getItemCount()) 280 { 281 return; 282 } 283 284 fRow = fRow +1; 285 fCol = 0; 286 287 updateCursorPosition(fRow, fCol, true); 288 } 289 if (fCol == 1 && e.keyCode == SWT.ARROW_LEFT) 290 { 291 if (fRow-1 < 0) 292 { 293 return; 294 } 295 296 fRow = fRow - 1; 297 fCol = getNumCol()+1; 298 299 updateCursorPosition(fRow, fCol, true); 300 } 301 } 302 } 303 304 308 class TextFocusListener implements FocusListener 309 { 310 311 314 public void focusGained(FocusEvent e) 315 { 316 } 317 318 321 public void focusLost(FocusEvent event) 322 { 323 324 final FocusEvent e = event; 325 326 Display.getDefault().syncExec(new Runnable () { 327 328 public void run() 329 { 330 try 331 { 332 Text text = (Text)e.getSource(); 333 removeListeners(text); 334 335 String newValue = text.getText(); 337 338 modifyValue(fRow, fCol, newValue); 340 341 showCursor(); 343 } 344 catch (NumberFormatException e1) 345 { 346 MemoryViewUtil.openError(DebugUIMessages.getString(MemoryViewCellModifier.TITLE), 347 DebugUIMessages.getString(MemoryViewCellModifier.DATA_IS_INVALID), null); 348 } 349 } 350 }); 351 } 352 } 353 354 361 class TextKeyListener implements KeyListener 362 { 363 364 367 public void keyTyped(KeyEvent e) 368 { 369 } 370 371 374 public void keyPressed(KeyEvent event) 375 { 376 if (event.getSource() instanceof Text) 377 handleTextKeyEvt(event); 378 } 379 380 383 private void handleTextKeyEvt(KeyEvent event) { 384 final KeyEvent e = event; 385 Display.getDefault().asyncExec(new Runnable () 386 { 387 public void run() 388 { 389 Text text = (Text)e.getSource(); 390 391 try 392 { 393 switch (e.keyCode) 394 { 395 case SWT.ARROW_UP : 396 397 399 if (fRow-1 < 0) 400 return; 401 402 modifyValue(fRow, fCol, text.getText()); 404 405 fRow--; 406 407 updateCursorPosition(fRow, fCol, true); 409 410 fViewTab.updateSyncTopAddress(true); 411 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 412 fViewTab.updateTableSelection(); 413 414 removeListeners(text); 416 activateCellEditor(null); 417 break; 418 case SWT.ARROW_DOWN : 419 420 422 if (fRow+1 >= fTable.getItemCount()) 423 return; 424 425 modifyValue(fRow, fCol, text.getText()); 427 428 fRow++; 429 430 updateCursorPosition(fRow, fCol, true); 432 433 fViewTab.updateSyncTopAddress(true); 434 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 435 fViewTab.updateTableSelection(); 436 437 removeListeners(text); 439 activateCellEditor(null); 440 break; 441 case 0: 442 443 446 if (fRenderer != null) 447 { 448 if (text.getText().length() > fViewTab.getColumnSize()*fRenderer.getNumCharPerByte()) 449 { 450 String newValue = text.getText(); 451 text.setText(newValue.substring(0, fViewTab.getColumnSize()*fRenderer.getNumCharPerByte())); 452 453 modifyValue(fRow, fCol, text.getText()); 454 455 if (fCol >= getNumCol()) 457 { 458 fCol = 1; 459 fRow++; 460 } 461 else 462 { 463 fCol++; 465 } 466 467 updateCursorPosition(fRow, fCol, true); 469 470 fViewTab.updateSyncTopAddress(true); 471 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 472 fViewTab.updateTableSelection(); 473 474 removeListeners(text); 475 476 activateCellEditor(newValue.substring(fViewTab.getColumnSize()*fRenderer.getNumCharPerByte())); 478 } 479 } 480 break; 481 case SWT.ESC: 482 483 updateCursorPosition(fRow, fCol, true); 486 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 487 fViewTab.updateTableSelection(); 488 489 removeListeners(text); 490 491 setCursorFocus(); 493 break; 494 default : 495 if (fRenderer != null) 496 { 497 if (text.getText().length()> fViewTab.getColumnSize()* fRenderer.getNumCharPerByte()) 498 { 499 String newValue = text.getText(); 500 text.setText(newValue.substring(0,fViewTab.getColumnSize()* fRenderer.getNumCharPerByte())); 501 modifyValue(fRow, fCol, text.getText()); 502 if (fCol >= getNumCol()) 504 { 505 fCol = 1; 506 fRow++; 507 } 508 else 509 { 510 fCol++; 511 } 512 513 updateCursorPosition(fRow, fCol, true); 514 515 fViewTab.updateSyncTopAddress(true); 516 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 517 fViewTab.updateTableSelection(); 518 removeListeners(text); 519 activateCellEditor( 520 newValue.substring( 521 fViewTab.getColumnSize() 522 * fRenderer.getNumCharPerByte())); 523 } 524 } 525 break; 526 } 527 } 528 catch (NumberFormatException e1) 529 { 530 MemoryViewUtil.openError(DebugUIMessages.getString(MemoryViewCellModifier.TITLE), 531 DebugUIMessages.getString(MemoryViewCellModifier.DATA_IS_INVALID), null); 532 533 updateCursorPosition(fRow, fCol, true); 534 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 535 fViewTab.updateTableSelection(); 536 537 removeListeners(text); 538 showCursor(); 539 } 540 } 541 }); 542 } 543 544 547 public void keyReleased(KeyEvent e) 548 { 549 550 } 551 } 552 553 560 public ViewTabCursorManager(MemoryViewTab viewTab, int initialRow, int initialCol, MenuManager menuManager) 561 { 562 fViewTab = viewTab; 563 fMenuManager = menuManager; 564 fRow = initialRow; 565 fCol = initialCol; 566 fTableViewer = viewTab.getTableViewer(); 567 fTable = fTableViewer.getTable(); 568 editor = new TableEditor (fTable); 569 fRenderingId = fViewTab.getRenderingId(); 570 571 IBaseLabelProvider labelProvider = fTableViewer.getLabelProvider(); 572 if (labelProvider instanceof AbstractTableViewTabLabelProvider) 573 { 574 AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer(); 575 if (renderer instanceof IFixedLengthOutputRenderer) 576 fRenderer = (IFixedLengthOutputRenderer)renderer; 577 } 578 579 fTraverseEvtListener = new TraverseEventListener(); 580 fTextFocusListener = new TextFocusListener(); 581 fTextKeyListener = new TextKeyListener(); 582 583 fMouseEventListener = new MouseEventListener(); 584 fTable.addMouseListener(fMouseEventListener); 585 586 createCursor(); 587 } 588 589 private void createCursor() 590 { 591 fTableCursor = new TableCursor(fTable, SWT.NONE); 592 593 Display display = fTableCursor.getDisplay(); 594 595 fTableCursor.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION)); 597 fTableCursor.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT)); 598 599 updateCursorPosition(fRow, fCol, true); 600 601 fTableCursor.setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME)); 602 fTableCursor.setVisible(true); 603 fTableCursor.setFocus(); 604 605 fTableCursor.addSelectionListener(new SelectionListener() { 606 607 public void widgetSelected(SelectionEvent e) 609 { 610 if (fTableCursor != null) 611 { 612 fViewTab.updateSyncTopAddress(true); 613 614 fRow = fTable.indexOf(fTableCursor.getRow()); 615 fCol = fTableCursor.getColumn(); 616 617 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn()); 619 fViewTab.updateTableSelection(); 620 } 621 } 622 623 public void widgetDefaultSelected(SelectionEvent e) 624 { 625 626 }}); 627 628 fTableCursor.addTraverseListener(fTraverseEvtListener); 629 630 if (fMenuManager != null) 633 { 634 createContextMenu(); 635 } 636 637 fTableCursor.addKeyListener(fViewTab); 640 fTableCursor.addMouseListener(fMouseEventListener); 641 } 642 643 644 650 private Menu createContextMenu() 651 { 652 fMenuDisposed = false; 653 654 Menu menu = fMenuManager.createContextMenu(fTable); 655 656 fTable.setMenu(menu); 657 658 if (fTableCursor != null) 659 { 660 Control menuControl = fTableCursor; 661 menuControl.setMenu(menu); 662 663 menu.addDisposeListener(new DisposeListener(){ 664 665 public void widgetDisposed(DisposeEvent e) 666 { 667 fMenuDisposed = true; 668 669 Menu disposedMenu = (Menu)e.getSource(); 670 disposedMenu.removeDisposeListener(this); 671 }}); 672 } 673 674 return menu; 675 } 676 677 680 public void redrawCursors() 681 { 682 if (fTableCursor != null && fTableCursor.isVisible()) 683 { 684 fTableCursor.setSelection(fRow, fCol); 685 fTableCursor.redraw(); 686 } 687 } 688 689 692 public void dispose() 693 { 694 if (fTableCursor != null) 695 { 696 fTableCursor = null; 701 } 702 703 if (editor != null) 704 { 705 editor.dispose(); 706 editor = null; 707 } 708 709 if (fTable != null) 710 fTable.removeMouseListener(fMouseEventListener); 711 } 712 713 716 public TableCursor getLeadCursor() 717 { 718 return fTableCursor; 719 } 720 721 724 public void hideCursor() 725 { 726 fTableCursor.setVisible(false); 727 } 728 729 733 public void showCursor() 734 { 735 if (fTableCursor == null) 736 { 737 createCursor(); 738 } 739 if (fMenuDisposed) 740 { 741 createContextMenu(); 742 } 743 744 if (!fTableCursor.isVisible()) 745 { 746 fTableCursor.setVisible(true); 747 } 748 } 749 750 753 public void setCursorFocus() 754 { 755 if (fTableCursor != null) 756 fTableCursor.setFocus(); 757 } 758 759 760 766 public void updateCursorPosition(int row, int col, boolean showCursor) 767 { 768 if (row < 0 769 || row >= fTable.getItemCount() 770 || col < 0 771 || col >= fTable.getColumnCount()) { 772 return; 773 } 774 775 this.fRow = row; 776 this.fCol = col; 777 778 fTableCursor.setSelection(row, col); 779 780 if (showCursor) 781 showCursor(); 782 } 783 784 789 public void activateCellEditor(String initialValue) { 790 791 if (fCol == 0 || fCol > getNumCol()) 793 { 794 return; 795 } 796 797 ICellModifier cellModifier = null; 798 799 if (fTableViewer != null) 800 { 801 cellModifier = fTableViewer.getCellModifier(); 802 } 803 804 TableItem tableItem = fTable.getItem(fRow); 805 806 Object element = tableItem.getData(); 807 Object property = fTableViewer.getColumnProperties()[fCol]; 808 Object value = cellModifier.getValue(element, (String )property); 809 810 ((MemoryViewCellModifier)cellModifier).setEditActionInvoked(true); 817 boolean canEdit = cellModifier.canModify(element, (String )property); 818 ((MemoryViewCellModifier)cellModifier).setEditActionInvoked(false); 819 820 if (!canEdit) 821 return; 822 823 TextCellEditor selectedEditor = (TextCellEditor)fTableViewer.getCellEditors()[fCol]; 825 826 827 if (fTableViewer != null && cellModifier != null && selectedEditor != null && tableItem != null) 828 { 829 Text text = (Text)selectedEditor.getControl(); 831 832 String cellValue = null; 833 834 if (initialValue != null) 835 { 836 cellValue = initialValue; 837 } 838 else 839 { 840 cellValue = ((String )value); 841 } 842 843 if (fRenderingId.equals(IMemoryViewConstants.RENDERING_RAW_MEMORY)) 844 cellValue = cellValue.toUpperCase(); 845 846 text.setText(cellValue); 847 848 editor.horizontalAlignment = SWT.LEFT; 849 editor.grabHorizontal = true; 850 851 editor.setEditor (text, tableItem, fCol); 853 854 selectedEditor.setFocus(); 856 857 if (initialValue != null) 858 { 859 text.clearSelection(); 860 } 861 862 text.setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME)); 863 864 addListeners(text); 866 867 fTableCursor.moveBelow(text); 869 } 870 } 871 872 875 private int getNumCol() { 876 877 int bytesPerLine = fViewTab.getBytesPerLine(); 878 int columnSize = fViewTab.getColumnSize(); 879 880 return bytesPerLine/columnSize; 881 } 882 883 890 private void modifyValue(int row, int col, String newValue) throws NumberFormatException 891 { 892 if (newValue.length() == 0) 893 { 894 return; 896 } 897 898 TableItem tableItem = fTable.getItem(row); 899 900 Object property = fTableViewer.getColumnProperties()[col]; 901 902 Object element = tableItem.getData(); 904 905 ICellModifier cellModifier = fTableViewer.getCellModifier(); 906 String oldValue = (String )cellModifier.getValue(element, (String )property); 907 908 if (fRenderingId.equals(IMemoryViewConstants.RENDERING_RAW_MEMORY)) 909 { 910 oldValue = oldValue.toUpperCase(); 911 newValue = newValue.toUpperCase(); 912 913 if (newValue.length() <= oldValue.length()) 915 { 916 oldValue = oldValue.substring(0, newValue.length()); 917 } 918 919 if (!oldValue.equals(newValue)) 920 { 921 fTableViewer.getCellModifier().modify(tableItem, (String )property, newValue); 922 } 923 } 924 else 925 { 926 fTableViewer.getCellModifier().modify(tableItem, (String )property, newValue); 928 } 929 } 930 931 934 protected void setFont(Font font) 935 { 936 if (fTableCursor != null) 937 { 938 fTableCursor.setFont(font); 939 } 940 } 941 942 945 private void addListeners(Text text) { 946 text.addFocusListener(fTextFocusListener); 948 text.addTraverseListener(fTraverseEvtListener); 949 text.addKeyListener(fTextKeyListener); 950 text.addKeyListener(fViewTab); 951 } 952 953 956 private void removeListeners(Text text) { 957 text.removeTraverseListener(fTraverseEvtListener); 959 text.removeFocusListener(fTextFocusListener); 960 text.removeKeyListener(fTextKeyListener); 961 text.removeKeyListener(fViewTab); 962 } 963 } 964 | Popular Tags |