1 15 package org.eclipse.ui.texteditor; 16 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.ResourceBundle ; 25 26 import org.osgi.framework.Bundle; 27 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.custom.BusyIndicator; 30 import org.eclipse.swt.custom.ST; 31 import org.eclipse.swt.custom.StyledText; 32 import org.eclipse.swt.custom.VerifyKeyListener; 33 import org.eclipse.swt.dnd.DND; 34 import org.eclipse.swt.dnd.DragSource; 35 import org.eclipse.swt.dnd.DragSourceAdapter; 36 import org.eclipse.swt.dnd.DragSourceEvent; 37 import org.eclipse.swt.dnd.DropTargetAdapter; 38 import org.eclipse.swt.dnd.DropTargetEvent; 39 import org.eclipse.swt.dnd.DropTargetListener; 40 import org.eclipse.swt.dnd.TextTransfer; 41 import org.eclipse.swt.dnd.Transfer; 42 import org.eclipse.swt.events.KeyEvent; 43 import org.eclipse.swt.events.KeyListener; 44 import org.eclipse.swt.events.MouseEvent; 45 import org.eclipse.swt.events.MouseListener; 46 import org.eclipse.swt.events.VerifyEvent; 47 import org.eclipse.swt.events.VerifyListener; 48 import org.eclipse.swt.graphics.Color; 49 import org.eclipse.swt.graphics.Font; 50 import org.eclipse.swt.graphics.FontData; 51 import org.eclipse.swt.graphics.GC; 52 import org.eclipse.swt.graphics.Image; 53 import org.eclipse.swt.graphics.ImageData; 54 import org.eclipse.swt.graphics.PaletteData; 55 import org.eclipse.swt.graphics.Point; 56 import org.eclipse.swt.graphics.RGB; 57 import org.eclipse.swt.widgets.Caret; 58 import org.eclipse.swt.widgets.Composite; 59 import org.eclipse.swt.widgets.Control; 60 import org.eclipse.swt.widgets.Display; 61 import org.eclipse.swt.widgets.Menu; 62 import org.eclipse.swt.widgets.Shell; 63 64 import org.eclipse.core.commands.operations.IOperationApprover; 65 import org.eclipse.core.commands.operations.IOperationHistory; 66 import org.eclipse.core.commands.operations.IUndoContext; 67 import org.eclipse.core.commands.operations.OperationHistoryFactory; 68 69 import org.eclipse.core.runtime.Assert; 70 import org.eclipse.core.runtime.CoreException; 71 import org.eclipse.core.runtime.IConfigurationElement; 72 import org.eclipse.core.runtime.ILog; 73 import org.eclipse.core.runtime.IProgressMonitor; 74 import org.eclipse.core.runtime.IStatus; 75 import org.eclipse.core.runtime.NullProgressMonitor; 76 import org.eclipse.core.runtime.Platform; 77 import org.eclipse.core.runtime.SafeRunner; 78 import org.eclipse.core.runtime.Status; 79 80 import org.eclipse.text.undo.DocumentUndoManagerRegistry; 81 import org.eclipse.text.undo.IDocumentUndoManager; 82 83 import org.eclipse.jface.action.Action; 84 import org.eclipse.jface.action.GroupMarker; 85 import org.eclipse.jface.action.IAction; 86 import org.eclipse.jface.action.IMenuListener; 87 import org.eclipse.jface.action.IMenuManager; 88 import org.eclipse.jface.action.IStatusLineManager; 89 import org.eclipse.jface.action.MenuManager; 90 import org.eclipse.jface.action.Separator; 91 import org.eclipse.jface.dialogs.ErrorDialog; 92 import org.eclipse.jface.dialogs.MessageDialog; 93 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 94 import org.eclipse.jface.operation.IRunnableWithProgress; 95 import org.eclipse.jface.preference.IPreferenceStore; 96 import org.eclipse.jface.preference.PreferenceConverter; 97 import org.eclipse.jface.resource.ImageDescriptor; 98 import org.eclipse.jface.resource.JFaceResources; 99 import org.eclipse.jface.util.IPropertyChangeListener; 100 import org.eclipse.jface.util.PropertyChangeEvent; 101 import org.eclipse.jface.util.SafeRunnable; 102 import org.eclipse.jface.viewers.IPostSelectionProvider; 103 import org.eclipse.jface.viewers.ISelection; 104 import org.eclipse.jface.viewers.ISelectionChangedListener; 105 import org.eclipse.jface.viewers.ISelectionProvider; 106 import org.eclipse.jface.viewers.SelectionChangedEvent; 107 import org.eclipse.jface.viewers.StructuredSelection; 108 import org.eclipse.jface.window.IShellProvider; 109 110 import org.eclipse.jface.text.AbstractInformationControlManager; 111 import org.eclipse.jface.text.BadLocationException; 112 import org.eclipse.jface.text.DefaultInformationControl; 113 import org.eclipse.jface.text.DefaultLineTracker; 114 import org.eclipse.jface.text.DocumentEvent; 115 import org.eclipse.jface.text.IDocument; 116 import org.eclipse.jface.text.IDocumentListener; 117 import org.eclipse.jface.text.IFindReplaceTarget; 118 import org.eclipse.jface.text.IFindReplaceTargetExtension; 119 import org.eclipse.jface.text.IInformationControl; 120 import org.eclipse.jface.text.IInformationControlCreator; 121 import org.eclipse.jface.text.IMarkRegionTarget; 122 import org.eclipse.jface.text.IRegion; 123 import org.eclipse.jface.text.IRewriteTarget; 124 import org.eclipse.jface.text.ISelectionValidator; 125 import org.eclipse.jface.text.ITextHover; 126 import org.eclipse.jface.text.ITextInputListener; 127 import org.eclipse.jface.text.ITextListener; 128 import org.eclipse.jface.text.ITextOperationTarget; 129 import org.eclipse.jface.text.ITextSelection; 130 import org.eclipse.jface.text.ITextViewer; 131 import org.eclipse.jface.text.ITextViewerExtension; 132 import org.eclipse.jface.text.ITextViewerExtension2; 133 import org.eclipse.jface.text.ITextViewerExtension4; 134 import org.eclipse.jface.text.ITextViewerExtension5; 135 import org.eclipse.jface.text.ITextViewerExtension6; 136 import org.eclipse.jface.text.ITextViewerExtension7; 137 import org.eclipse.jface.text.IUndoManager; 138 import org.eclipse.jface.text.IUndoManagerExtension; 139 import org.eclipse.jface.text.Position; 140 import org.eclipse.jface.text.Region; 141 import org.eclipse.jface.text.TabsToSpacesConverter; 142 import org.eclipse.jface.text.TextEvent; 143 import org.eclipse.jface.text.TextSelection; 144 import org.eclipse.jface.text.TextUtilities; 145 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; 146 import org.eclipse.jface.text.information.IInformationProvider; 147 import org.eclipse.jface.text.information.IInformationProviderExtension; 148 import org.eclipse.jface.text.information.IInformationProviderExtension2; 149 import org.eclipse.jface.text.information.InformationPresenter; 150 import org.eclipse.jface.text.link.LinkedModeModel; 151 import org.eclipse.jface.text.link.LinkedPosition; 152 import org.eclipse.jface.text.revisions.RevisionInformation; 153 import org.eclipse.jface.text.source.Annotation; 154 import org.eclipse.jface.text.source.CompositeRuler; 155 import org.eclipse.jface.text.source.IAnnotationHover; 156 import org.eclipse.jface.text.source.IAnnotationHoverExtension; 157 import org.eclipse.jface.text.source.IAnnotationModel; 158 import org.eclipse.jface.text.source.ILineRange; 159 import org.eclipse.jface.text.source.ISourceViewer; 160 import org.eclipse.jface.text.source.ISourceViewerExtension3; 161 import org.eclipse.jface.text.source.IVerticalRuler; 162 import org.eclipse.jface.text.source.IVerticalRulerColumn; 163 import org.eclipse.jface.text.source.IVerticalRulerExtension; 164 import org.eclipse.jface.text.source.IVerticalRulerInfo; 165 import org.eclipse.jface.text.source.SourceViewer; 166 import org.eclipse.jface.text.source.SourceViewerConfiguration; 167 import org.eclipse.jface.text.source.VerticalRuler; 168 169 import org.eclipse.ui.IActionBars; 170 import org.eclipse.ui.IEditorDescriptor; 171 import org.eclipse.ui.IEditorInput; 172 import org.eclipse.ui.IEditorPart; 173 import org.eclipse.ui.IEditorRegistry; 174 import org.eclipse.ui.IEditorSite; 175 import org.eclipse.ui.IKeyBindingService; 176 import org.eclipse.ui.IMemento; 177 import org.eclipse.ui.INavigationLocation; 178 import org.eclipse.ui.INavigationLocationProvider; 179 import org.eclipse.ui.IPartListener; 180 import org.eclipse.ui.IPartService; 181 import org.eclipse.ui.IPersistableEditor; 182 import org.eclipse.ui.IReusableEditor; 183 import org.eclipse.ui.ISaveablesLifecycleListener; 184 import org.eclipse.ui.ISaveablesSource; 185 import org.eclipse.ui.IWindowListener; 186 import org.eclipse.ui.IWorkbenchActionConstants; 187 import org.eclipse.ui.IWorkbenchPart; 188 import org.eclipse.ui.IWorkbenchWindow; 189 import org.eclipse.ui.PartInitException; 190 import org.eclipse.ui.PlatformUI; 191 import org.eclipse.ui.Saveable; 192 import org.eclipse.ui.SaveablesLifecycleEvent; 193 import org.eclipse.ui.actions.CommandNotMappedException; 194 import org.eclipse.ui.actions.ContributedAction; 195 import org.eclipse.ui.dialogs.PropertyDialogAction; 196 import org.eclipse.ui.dnd.IDragAndDropService; 197 import org.eclipse.ui.internal.texteditor.EditPosition; 198 import org.eclipse.ui.internal.texteditor.NLSUtility; 199 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 200 import org.eclipse.ui.internal.texteditor.rulers.StringSetSerializer; 201 import org.eclipse.ui.operations.LinearUndoViolationUserApprover; 202 import org.eclipse.ui.operations.NonLocalUndoUserApprover; 203 import org.eclipse.ui.operations.OperationHistoryActionHandler; 204 import org.eclipse.ui.operations.RedoActionHandler; 205 import org.eclipse.ui.operations.UndoActionHandler; 206 import org.eclipse.ui.part.EditorPart; 207 import org.eclipse.ui.texteditor.rulers.IColumnSupport; 208 import org.eclipse.ui.texteditor.rulers.IContributedRulerColumn; 209 import org.eclipse.ui.texteditor.rulers.RulerColumnDescriptor; 210 import org.eclipse.ui.texteditor.rulers.RulerColumnPreferenceAdapter; 211 import org.eclipse.ui.texteditor.rulers.RulerColumnRegistry; 212 213 214 238 public abstract class AbstractTextEditor extends EditorPart implements ITextEditor, IReusableEditor, ITextEditorExtension, ITextEditorExtension2, ITextEditorExtension3, ITextEditorExtension4, INavigationLocationProvider, ISaveablesSource, IPersistableEditor { 239 240 245 private static final String TAG_CONTRIBUTION_TYPE= "editorContribution"; 247 255 protected static final String TAG_SELECTION_OFFSET= "selectionOffset"; protected static final String TAG_SELECTION_LENGTH= "selectionLength"; private static final String TAG_SELECTION_HPIXEL= "selectionHPixel"; 260 261 267 private static final int WIDE_CARET_WIDTH= 2; 268 269 275 private static final int SINGLE_CARET_WIDTH= 1; 276 277 283 private static class TextInputListener implements ITextInputListener { 284 285 public boolean inputChanged; 286 287 288 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {} 289 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { inputChanged= true; } 290 } 291 292 295 class ElementStateListener implements IElementStateListener, IElementStateListenerExtension { 296 297 302 class Validator implements VerifyListener { 303 306 public void verifyText(VerifyEvent e) { 307 IDocument document= getDocumentProvider().getDocument(getEditorInput()); 308 final boolean[] documentChanged= new boolean[1]; 309 IDocumentListener listener= new IDocumentListener() { 310 public void documentAboutToBeChanged(DocumentEvent event) { 311 } 312 public void documentChanged(DocumentEvent event) { 313 documentChanged[0]= true; 314 } 315 }; 316 try { 317 if (document != null) 318 document.addDocumentListener(listener); 319 if (! validateEditorInputState() || documentChanged[0]) 320 e.doit= false; 321 } finally { 322 if (document != null) 323 document.removeDocumentListener(listener); 324 } 325 } 326 } 327 328 332 private Validator fValidator; 333 337 private Display fDisplay; 338 339 343 public void elementStateValidationChanged(final Object element, final boolean isStateValidated) { 344 if (element != null && element.equals(getEditorInput())) { 345 Runnable r= new Runnable () { 346 public void run() { 347 enableSanityChecking(true); 348 if (isStateValidated && fValidator != null) { 349 ISourceViewer viewer= fSourceViewer; 350 if (viewer != null) { 351 StyledText textWidget= viewer.getTextWidget(); 352 if (textWidget != null && !textWidget.isDisposed()) 353 textWidget.removeVerifyListener(fValidator); 354 fValidator= null; 355 enableStateValidation(false); 356 } 357 } else if (!isStateValidated && fValidator == null) { 358 ISourceViewer viewer= fSourceViewer; 359 if (viewer != null) { 360 StyledText textWidget= viewer.getTextWidget(); 361 if (textWidget != null && !textWidget.isDisposed()) { 362 fValidator= new Validator(); 363 enableStateValidation(true); 364 textWidget.addVerifyListener(fValidator); 365 } 366 } 367 } 368 } 369 }; 370 execute(r, false); 371 } 372 } 373 374 375 378 public void elementDirtyStateChanged(Object element, boolean isDirty) { 379 if (element != null && element.equals(getEditorInput())) { 380 Runnable r= new Runnable () { 381 public void run() { 382 enableSanityChecking(true); 383 firePropertyChange(PROP_DIRTY); 384 } 385 }; 386 execute(r, false); 387 } 388 } 389 390 393 public void elementContentAboutToBeReplaced(Object element) { 394 if (element != null && element.equals(getEditorInput())) { 395 Runnable r= new Runnable () { 396 public void run() { 397 enableSanityChecking(true); 398 rememberSelection(); 399 resetHighlightRange(); 400 } 401 }; 402 execute(r, false); 403 } 404 } 405 406 409 public void elementContentReplaced(Object element) { 410 if (element != null && element.equals(getEditorInput())) { 411 Runnable r= new Runnable () { 412 public void run() { 413 enableSanityChecking(true); 414 firePropertyChange(PROP_DIRTY); 415 restoreSelection(); 416 handleElementContentReplaced(); 417 } 418 }; 419 execute(r, false); 420 } 421 } 422 423 426 public void elementDeleted(Object deletedElement) { 427 if (deletedElement != null && deletedElement.equals(getEditorInput())) { 428 Runnable r= new Runnable () { 429 public void run() { 430 enableSanityChecking(true); 431 close(false); 432 } 433 }; 434 execute(r, false); 435 } 436 } 437 438 441 public void elementMoved(final Object originalElement, final Object movedElement) { 442 if (originalElement != null && originalElement.equals(getEditorInput())) { 443 final boolean doValidationAsync= Display.getCurrent() != null; 444 Runnable r= new Runnable () { 445 public void run() { 446 enableSanityChecking(true); 447 448 if (fSourceViewer == null) 449 return; 450 451 if (!canHandleMove((IEditorInput) originalElement, (IEditorInput) movedElement)) { 452 close(true); 453 return; 454 } 455 456 if (movedElement == null || movedElement instanceof IEditorInput) { 457 rememberSelection(); 458 459 final IDocumentProvider d= getDocumentProvider(); 460 final String previousContent; 461 IDocumentUndoManager previousUndoManager=null; 462 IDocument changed= null; 463 boolean wasDirty= isDirty(); 464 changed= d.getDocument(getEditorInput()); 465 if (changed != null) { 466 if (wasDirty) 467 previousContent= changed.get(); 468 else 469 previousContent= null; 470 471 previousUndoManager= DocumentUndoManagerRegistry.getDocumentUndoManager(changed); 472 if (previousUndoManager != null) 473 previousUndoManager.connect(this); 474 } 475 else 476 previousContent= null; 477 478 setInput((IEditorInput) movedElement); 479 480 if (previousUndoManager != null) { 483 IDocument newDocument= getDocumentProvider().getDocument(movedElement); 484 if (newDocument != null) { 485 IDocumentUndoManager newUndoManager= DocumentUndoManagerRegistry.getDocumentUndoManager(newDocument); 486 if (newUndoManager != null) 487 newUndoManager.transferUndoHistory(previousUndoManager); 488 } 489 previousUndoManager.disconnect(this); 490 } 491 492 if (wasDirty && changed != null) { 493 Runnable r2= new Runnable () { 494 public void run() { 495 validateState(getEditorInput()); 496 d.getDocument(getEditorInput()).set(previousContent); 497 updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE); 498 restoreSelection(); 499 } 500 }; 501 execute(r2, doValidationAsync); 502 } else 503 restoreSelection(); 504 505 } 506 } 507 }; 508 execute(r, false); 509 } 510 } 511 512 516 public void elementStateChanging(Object element) { 517 if (element != null && element.equals(getEditorInput())) 518 enableSanityChecking(false); 519 } 520 521 525 public void elementStateChangeFailed(Object element) { 526 if (element != null && element.equals(getEditorInput())) 527 enableSanityChecking(true); 528 } 529 530 541 private void execute(Runnable runnable, boolean postAsync) { 542 if (postAsync || Display.getCurrent() == null) { 543 if (fDisplay == null) 544 fDisplay= getSite().getShell().getDisplay(); 545 fDisplay.asyncExec(runnable); 546 } else 547 runnable.run(); 548 } 549 } 550 551 555 class TextListener implements ITextListener, ITextInputListener { 556 557 558 private Runnable fRunnable= new Runnable () { 559 public void run() { 560 fIsRunnablePosted= false; 561 562 if (fSourceViewer != null) { 563 updateContentDependentActions(); 564 565 if (isDirty() && fUpdateLastEditPosition) { 567 fUpdateLastEditPosition= false; 568 ISelection sel= getSelectionProvider().getSelection(); 569 IEditorInput input= getEditorInput(); 570 IDocument document= getDocumentProvider().getDocument(input); 571 572 if (fLocalLastEditPosition != null) { 573 document.removePosition(fLocalLastEditPosition); 574 fLocalLastEditPosition= null; 575 } 576 577 if (sel instanceof ITextSelection && !sel.isEmpty()) { 578 ITextSelection s= (ITextSelection) sel; 579 fLocalLastEditPosition= new Position(s.getOffset(), s.getLength()); 580 try { 581 document.addPosition(fLocalLastEditPosition); 582 } catch (BadLocationException ex) { 583 fLocalLastEditPosition= null; 584 } 585 } 586 TextEditorPlugin.getDefault().setLastEditPosition(new EditPosition(input, getEditorSite().getId(), fLocalLastEditPosition)); 587 } 588 } 589 } 590 }; 591 592 593 private Display fDisplay; 594 598 private Position fLocalLastEditPosition; 599 603 private boolean fIsRunnablePosted= false; 604 608 private boolean fUpdateLastEditPosition= false; 609 610 613 public void textChanged(TextEvent event) { 614 615 620 621 if (fDisplay == null) 622 fDisplay= getSite().getShell().getDisplay(); 623 624 if (event.getDocumentEvent() != null) 625 fUpdateLastEditPosition= true; 626 627 if (!fIsRunnablePosted) { 628 fIsRunnablePosted= true; 629 fDisplay.asyncExec(fRunnable); 630 } 631 } 632 633 636 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { 637 if (oldInput != null && fLocalLastEditPosition != null) { 638 oldInput.removePosition(fLocalLastEditPosition); 639 fLocalLastEditPosition= null; 640 } 641 } 642 643 646 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { 647 } 648 } 649 650 653 class PropertyChangeListener implements IPropertyChangeListener { 654 657 public void propertyChange(PropertyChangeEvent event) { 658 handlePreferenceStoreChanged(event); 659 } 660 } 661 662 666 class FontPropertyChangeListener implements IPropertyChangeListener { 667 670 public void propertyChange(PropertyChangeEvent event) { 671 if (fSourceViewer == null) 672 return; 673 674 String property= event.getProperty(); 675 676 if (getFontPropertyPreferenceKey().equals(property)) { 677 initializeViewerFont(fSourceViewer); 678 updateCaret(); 679 } 680 } 681 } 682 683 686 class ActivationCodeTrigger implements VerifyKeyListener { 687 688 689 private boolean fIsInstalled= false; 690 694 private IKeyBindingService fKeyBindingService; 695 696 699 public void verifyKey(VerifyEvent event) { 700 701 ActionActivationCode code= null; 702 int size= fActivationCodes.size(); 703 for (int i= 0; i < size; i++) { 704 code= (ActionActivationCode) fActivationCodes.get(i); 705 if (code.matches(event)) { 706 IAction action= getAction(code.fActionId); 707 if (action != null) { 708 709 if (action instanceof IUpdate) 710 ((IUpdate) action).update(); 711 712 if (!action.isEnabled() && action instanceof IReadOnlyDependent) { 713 IReadOnlyDependent dependent= (IReadOnlyDependent) action; 714 boolean writable= dependent.isEnabled(true); 715 if (writable) { 716 event.doit= false; 717 return; 718 } 719 } else if (action.isEnabled()) { 720 event.doit= false; 721 action.run(); 722 return; 723 } 724 } 725 } 726 } 727 } 728 729 733 public void install() { 734 if (!fIsInstalled) { 735 736 if (fSourceViewer instanceof ITextViewerExtension) { 737 ITextViewerExtension e= (ITextViewerExtension) fSourceViewer; 738 e.prependVerifyKeyListener(this); 739 } else { 740 StyledText text= fSourceViewer.getTextWidget(); 741 text.addVerifyKeyListener(this); 742 } 743 744 fKeyBindingService= getEditorSite().getKeyBindingService(); 745 fIsInstalled= true; 746 } 747 } 748 749 753 public void uninstall() { 754 if (fIsInstalled) { 755 756 if (fSourceViewer instanceof ITextViewerExtension) { 757 ITextViewerExtension e= (ITextViewerExtension) fSourceViewer; 758 e.removeVerifyKeyListener(this); 759 } else if (fSourceViewer != null) { 760 StyledText text= fSourceViewer.getTextWidget(); 761 if (text != null && !text.isDisposed()) 762 text.removeVerifyKeyListener(fActivationCodeTrigger); 763 } 764 765 fIsInstalled= false; 766 fKeyBindingService= null; 767 } 768 } 769 770 775 public void registerActionForKeyActivation(IAction action) { 776 if (action.getActionDefinitionId() != null) 777 fKeyBindingService.registerAction(action); 778 } 779 780 785 public void unregisterActionFromKeyActivation(IAction action) { 786 if (action.getActionDefinitionId() != null) 787 fKeyBindingService.unregisterAction(action); 788 } 789 790 795 public void setScopes(String [] keyBindingScopes) { 796 if (keyBindingScopes != null && keyBindingScopes.length > 0) 797 fKeyBindingService.setScopes(keyBindingScopes); 798 } 799 } 800 801 804 static class ActionActivationCode { 805 806 807 public String fActionId; 808 809 public char fCharacter; 810 811 public int fKeyCode= -1; 812 813 public int fStateMask= SWT.DEFAULT; 814 815 819 public ActionActivationCode(String actionId) { 820 fActionId= actionId; 821 } 822 823 828 public boolean matches(VerifyEvent event) { 829 return (event.character == fCharacter && 830 (fKeyCode == -1 || event.keyCode == fKeyCode) && 831 (fStateMask == SWT.DEFAULT || event.stateMask == fStateMask)); 832 } 833 } 834 835 839 class ActivationListener implements IPartListener, IWindowListener { 840 841 842 private IWorkbenchPart fActivePart; 843 844 private boolean fIsHandlingActivation= false; 845 849 private IPartService fPartService; 850 851 857 public ActivationListener(IPartService partService) { 858 fPartService= partService; 859 fPartService.addPartListener(this); 860 PlatformUI.getWorkbench().addWindowListener(this); 861 } 862 863 868 public void dispose() { 869 fPartService.removePartListener(this); 870 PlatformUI.getWorkbench().removeWindowListener(this); 871 fPartService= null; 872 } 873 874 877 public void partActivated(IWorkbenchPart part) { 878 fActivePart= part; 879 handleActivation(); 880 } 881 882 885 public void partBroughtToTop(IWorkbenchPart part) { 886 } 887 888 891 public void partClosed(IWorkbenchPart part) { 892 } 893 894 897 public void partDeactivated(IWorkbenchPart part) { 898 fActivePart= null; 899 } 900 901 904 public void partOpened(IWorkbenchPart part) { 905 if (part == AbstractTextEditor.this && fMementoToRestore != null && containsSavedState(fMementoToRestore)) 907 doRestoreState(fMementoToRestore); 908 fMementoToRestore= null; 909 } 910 911 914 private void handleActivation() { 915 if (fIsHandlingActivation) 916 return; 917 918 if (fActivePart == AbstractTextEditor.this) { 919 fIsHandlingActivation= true; 920 try { 921 safelySanityCheckState(getEditorInput()); 922 } finally { 923 fIsHandlingActivation= false; 924 } 925 } 926 } 927 928 932 public void windowActivated(IWorkbenchWindow window) { 933 if (window == getEditorSite().getWorkbenchWindow()) { 934 939 window.getShell().getDisplay().asyncExec(new Runnable () { 940 public void run() { 941 handleActivation(); 942 } 943 }); 944 } 945 } 946 947 951 public void windowDeactivated(IWorkbenchWindow window) { 952 } 953 954 958 public void windowClosed(IWorkbenchWindow window) { 959 } 960 961 965 public void windowOpened(IWorkbenchWindow window) { 966 } 967 } 968 969 974 interface ICursorListener extends MouseListener, KeyListener { 975 } 976 977 981 protected static final class IdMapEntry { 982 983 984 private String fActionId; 985 986 private int fAction; 987 988 993 public IdMapEntry(String actionId, int action) { 994 fActionId= actionId; 995 fAction= action; 996 } 997 998 1002 public String getActionId() { 1003 return fActionId; 1004 } 1005 1006 1010 public int getAction() { 1011 return fAction; 1012 } 1013 } 1014 1015 1019 class ScrollLinesAction extends Action { 1020 1021 1022 private int fScrollIncrement; 1023 1024 1029 public ScrollLinesAction(int scrollIncrement) { 1030 fScrollIncrement= scrollIncrement; 1031 } 1032 1033 1036 public void run() { 1037 if (fSourceViewer instanceof ITextViewerExtension5) { 1038 ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer; 1039 StyledText textWidget= fSourceViewer.getTextWidget(); 1040 int topIndex= textWidget.getTopIndex(); 1041 int newTopIndex= Math.max(0, topIndex + fScrollIncrement); 1042 fSourceViewer.setTopIndex(extension.widgetLine2ModelLine(newTopIndex)); 1043 } else { 1044 int topIndex= fSourceViewer.getTopIndex(); 1045 int newTopIndex= Math.max(0, topIndex + fScrollIncrement); 1046 fSourceViewer.setTopIndex(newTopIndex); 1047 } 1048 } 1049 } 1050 1051 1057 class ToggleInsertModeAction extends ResourceAction { 1058 1059 public ToggleInsertModeAction(ResourceBundle bundle, String prefix) { 1060 super(bundle, prefix, IAction.AS_CHECK_BOX); 1061 } 1062 1063 1066 public void run() { 1067 switchToNextInsertMode(); 1068 } 1069 1070 1074 public boolean isChecked() { 1075 return fInsertMode == SMART_INSERT; 1076 } 1077 } 1078 1079 1084 class ToggleOverwriteModeAction extends ResourceAction { 1085 1086 public ToggleOverwriteModeAction(ResourceBundle bundle, String prefix) { 1087 super(bundle, prefix); 1088 } 1089 1090 1093 public void run() { 1094 toggleOverwriteMode(); 1095 } 1096 } 1097 1098 1106 protected class LineEndAction extends TextNavigationAction { 1107 1108 1109 private boolean fDoSelect; 1110 1111 1117 public LineEndAction(StyledText textWidget, boolean doSelect) { 1118 super(textWidget, ST.LINE_END); 1119 fDoSelect= doSelect; 1120 } 1121 1122 1135 int getLineEndPosition(final IDocument document, final String line, final int length, final int offset) { 1136 int index= length - 1; 1137 while (index > -1 && Character.isWhitespace(line.charAt(index))) 1138 index--; 1139 index++; 1140 1141 LinkedModeModel model= LinkedModeModel.getModel(document, offset); 1142 if (model != null) { 1143 LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, offset, 0)); 1144 if (linkedPosition != null) { 1145 int linkedPositionEnd= linkedPosition.getOffset() + linkedPosition.getLength(); 1146 int lineOffset; 1147 try { 1148 lineOffset= document.getLineInformationOfOffset(offset).getOffset(); 1149 if (offset != linkedPositionEnd && linkedPositionEnd - lineOffset < index) 1150 index= linkedPositionEnd - lineOffset; 1151 } catch (BadLocationException e) { 1152 } 1154 } 1155 } 1156 return index; 1157 } 1158 1159 1162 public void run() { 1163 boolean isSmartHomeEndEnabled= false; 1164 IPreferenceStore store= getPreferenceStore(); 1165 if (store != null) 1166 isSmartHomeEndEnabled= store.getBoolean(AbstractTextEditor.PREFERENCE_NAVIGATION_SMART_HOME_END); 1167 1168 StyledText st= fSourceViewer.getTextWidget(); 1169 if (st == null || st.isDisposed()) 1170 return; 1171 int caretOffset= st.getCaretOffset(); 1172 int lineNumber= st.getLineAtOffset(caretOffset); 1173 int lineOffset= st.getOffsetAtLine(lineNumber); 1174 1175 int lineLength; 1176 int caretOffsetInDocument; 1177 final IDocument document= fSourceViewer.getDocument(); 1178 1179 try { 1180 caretOffsetInDocument= widgetOffset2ModelOffset(fSourceViewer, caretOffset); 1181 lineLength= document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); 1182 } catch (BadLocationException ex) { 1183 return; 1184 } 1185 int lineEndOffset= lineOffset + lineLength; 1186 1187 int delta= lineEndOffset - st.getCharCount(); 1188 if (delta > 0) { 1189 lineEndOffset -= delta; 1190 lineLength -= delta; 1191 } 1192 1193 String line= ""; if (lineLength > 0) 1195 line= st.getText(lineOffset, lineEndOffset - 1); 1196 1197 Point oldSelection= st.getSelection(); 1199 1200 int newCaretOffset= -1; 1202 1203 if (isSmartHomeEndEnabled) { 1204 int i= getLineEndPosition(document, line, lineLength, caretOffsetInDocument); 1206 1207 if (caretOffset - lineOffset == i) 1208 newCaretOffset= lineEndOffset; 1210 else 1211 newCaretOffset= lineOffset + i; 1213 1214 } else { 1215 1216 if (caretOffset < lineEndOffset) 1217 newCaretOffset= lineEndOffset; 1219 1220 } 1221 1222 if (newCaretOffset == -1) 1223 newCaretOffset= caretOffset; 1224 else 1225 st.setCaretOffset(newCaretOffset); 1226 1227 st.setCaretOffset(newCaretOffset); 1228 if (fDoSelect) { 1229 if (caretOffset < oldSelection.y) 1230 st.setSelection(oldSelection.y, newCaretOffset); 1231 else 1232 st.setSelection(oldSelection.x, newCaretOffset); 1233 } else 1234 st.setSelection(newCaretOffset); 1235 1236 fireSelectionChanged(oldSelection); 1237 } 1238 } 1239 1240 1248 protected class LineStartAction extends TextNavigationAction { 1249 1250 1251 private final boolean fDoSelect; 1252 1253 1259 public LineStartAction(final StyledText textWidget, final boolean doSelect) { 1260 super(textWidget, ST.LINE_START); 1261 fDoSelect= doSelect; 1262 } 1263 1264 1274 protected int getLineStartPosition(final IDocument document, final String line, final int length, final int offset) { 1275 int index= 0; 1276 while (index < length && Character.isWhitespace(line.charAt(index))) 1277 index++; 1278 1279 LinkedModeModel model= LinkedModeModel.getModel(document, offset); 1280 if (model != null) { 1281 LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, offset, 0)); 1282 if (linkedPosition != null) { 1283 int linkedPositionOffset= linkedPosition.getOffset(); 1284 int lineOffset; 1285 try { 1286 lineOffset= document.getLineInformationOfOffset(offset).getOffset(); 1287 if (offset != linkedPositionOffset && index < linkedPositionOffset - lineOffset) 1288 index= linkedPositionOffset - lineOffset; 1289 } catch (BadLocationException e) { 1290 } 1292 } 1293 } 1294 return index; 1295 } 1296 1297 1300 public void run() { 1301 boolean isSmartHomeEndEnabled= false; 1302 IPreferenceStore store= getPreferenceStore(); 1303 if (store != null) 1304 isSmartHomeEndEnabled= store.getBoolean(AbstractTextEditor.PREFERENCE_NAVIGATION_SMART_HOME_END); 1305 1306 StyledText st= fSourceViewer.getTextWidget(); 1307 if (st == null || st.isDisposed()) 1308 return; 1309 1310 int caretOffset= st.getCaretOffset(); 1311 int lineNumber= st.getLineAtOffset(caretOffset); 1312 int lineOffset= st.getOffsetAtLine(lineNumber); 1313 1314 int lineLength; 1315 int caretOffsetInDocument; 1316 final IDocument document= fSourceViewer.getDocument(); 1317 1318 try { 1319 caretOffsetInDocument= widgetOffset2ModelOffset(fSourceViewer, caretOffset); 1320 lineLength= document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); 1321 } catch (BadLocationException ex) { 1322 return; 1323 } 1324 1325 String line= ""; if (lineLength > 0) { 1327 int end= lineOffset + lineLength - 1; 1328 end= Math.min(end, st.getCharCount() -1); 1329 line= st.getText(lineOffset, end); 1330 } 1331 1332 Point oldSelection= st.getSelection(); 1334 1335 int newCaretOffset= -1; 1337 1338 if (isSmartHomeEndEnabled) { 1339 1340 int index= getLineStartPosition(document, line, lineLength, caretOffsetInDocument); 1342 1343 if (caretOffset - lineOffset == index) 1344 newCaretOffset= lineOffset; 1346 else 1347 newCaretOffset= lineOffset + index; 1349 1350 } else { 1351 1352 if (caretOffset > lineOffset) 1353 newCaretOffset= lineOffset; 1355 } 1356 1357 if (newCaretOffset == -1) 1358 newCaretOffset= caretOffset; 1359 else 1360 st.setCaretOffset(newCaretOffset); 1361 1362 if (fDoSelect) { 1363 if (caretOffset < oldSelection.y) 1364 st.setSelection(oldSelection.y, newCaretOffset); 1365 else 1366 st.setSelection(oldSelection.x, newCaretOffset); 1367 } else 1368 st.setSelection(newCaretOffset); 1369 1370 fireSelectionChanged(oldSelection); 1371 } 1372 1373 } 1374 1375 1379 class ShowRulerContextMenuAction extends Action { 1380 1383 public void run() { 1384 if (fSourceViewer == null) 1385 return; 1386 1387 StyledText text= fSourceViewer.getTextWidget(); 1388 if (text == null || text.isDisposed()) 1389 return; 1390 1391 Point location= text.getLocationAtOffset(text.getCaretOffset()); 1392 location.x= 0; 1393 1394 if (fVerticalRuler instanceof IVerticalRulerExtension) 1395 ((IVerticalRulerExtension) fVerticalRuler).setLocationOfLastMouseButtonActivity(location.x, location.y); 1396 1397 location= text.toDisplay(location); 1398 fRulerContextMenu.setLocation(location.x, location.y); 1399 fRulerContextMenu.setVisible(true); 1400 } 1401 } 1402 1403 1404 1408 class SelectionProvider implements IPostSelectionProvider, ISelectionValidator { 1409 1410 1413 public void addSelectionChangedListener(ISelectionChangedListener listener) { 1414 if (fSourceViewer != null) 1415 fSourceViewer.getSelectionProvider().addSelectionChangedListener(listener); 1416 } 1417 1418 1421 public ISelection getSelection() { 1422 return doGetSelection(); 1423 } 1424 1425 1428 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 1429 if (fSourceViewer != null) 1430 fSourceViewer.getSelectionProvider().removeSelectionChangedListener(listener); 1431 } 1432 1433 1436 public void setSelection(ISelection selection) { 1437 doSetSelection(selection); 1438 } 1439 1440 1444 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { 1445 if (fSourceViewer != null) { 1446 if (fSourceViewer.getSelectionProvider() instanceof IPostSelectionProvider) { 1447 IPostSelectionProvider provider= (IPostSelectionProvider) fSourceViewer.getSelectionProvider(); 1448 provider.addPostSelectionChangedListener(listener); 1449 } 1450 } 1451 } 1452 1453 1457 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { 1458 if (fSourceViewer != null) { 1459 if (fSourceViewer.getSelectionProvider() instanceof IPostSelectionProvider) { 1460 IPostSelectionProvider provider= (IPostSelectionProvider) fSourceViewer.getSelectionProvider(); 1461 provider.removePostSelectionChangedListener(listener); 1462 } 1463 } 1464 } 1465 1466 1470 public boolean isValid(ISelection postSelection) { 1471 return fSelectionListener != null && fSelectionListener.isValid(postSelection); 1472 } 1473 } 1474 1475 1479 protected abstract class AbstractSelectionChangedListener implements ISelectionChangedListener { 1480 1481 1489 public void install(ISelectionProvider selectionProvider) { 1490 if (selectionProvider == null) 1491 return; 1492 1493 if (selectionProvider instanceof IPostSelectionProvider) { 1494 IPostSelectionProvider provider= (IPostSelectionProvider) selectionProvider; 1495 provider.addPostSelectionChangedListener(this); 1496 } else { 1497 selectionProvider.addSelectionChangedListener(this); 1498 } 1499 } 1500 1501 1506 public void uninstall(ISelectionProvider selectionProvider) { 1507 if (selectionProvider == null) 1508 return; 1509 1510 if (selectionProvider instanceof IPostSelectionProvider) { 1511 IPostSelectionProvider provider= (IPostSelectionProvider) selectionProvider; 1512 provider.removePostSelectionChangedListener(this); 1513 } else { 1514 selectionProvider.removeSelectionChangedListener(this); 1515 } 1516 } 1517 } 1518 1519 1524 private class SelectionListener extends AbstractSelectionChangedListener implements IDocumentListener { 1525 1526 private IDocument fDocument; 1527 private final Object INVALID_SELECTION= new Object (); 1528 private Object fPostSelection= INVALID_SELECTION; 1529 1530 1533 public synchronized void selectionChanged(SelectionChangedEvent event) { 1534 fPostSelection= event.getSelection(); 1535 } 1536 1537 1541 public synchronized void documentAboutToBeChanged(DocumentEvent event) { 1542 fPostSelection= INVALID_SELECTION; 1543 } 1544 1545 1549 public void documentChanged(DocumentEvent event) { 1550 } 1551 1552 public synchronized boolean isValid(ISelection selection) { 1553 return fPostSelection != INVALID_SELECTION && fPostSelection == selection; 1554 } 1555 1556 public void setDocument(IDocument document) { 1557 if (fDocument != null) 1558 fDocument.removeDocumentListener(this); 1559 1560 fDocument= document; 1561 if (fDocument != null) 1562 fDocument.addDocumentListener(this); 1563 } 1564 1565 1569 public void install(ISelectionProvider selectionProvider) { 1570 super.install(selectionProvider); 1571 1572 if (selectionProvider != null) 1573 selectionProvider.addSelectionChangedListener(this); 1574 } 1575 1576 1580 public void uninstall(ISelectionProvider selectionProvider) { 1581 if (selectionProvider != null) 1582 selectionProvider.removeSelectionChangedListener(this); 1583 1584 if (fDocument != null) { 1585 fDocument.removeDocumentListener(this); 1586 fDocument= null; 1587 } 1588 super.uninstall(selectionProvider); 1589 } 1590 } 1591 1592 1593 1601 protected static class ColumnSupport implements IColumnSupport { 1602 private final AbstractTextEditor fEditor; 1603 private final RulerColumnRegistry fRegistry; 1604 private final List fColumns; 1605 1606 1613 public ColumnSupport(AbstractTextEditor editor, RulerColumnRegistry registry) { 1614 Assert.isLegal(editor != null); 1615 Assert.isLegal(registry != null); 1616 fEditor= editor; 1617 fRegistry= registry; 1618 fColumns= new ArrayList (); 1619 } 1620 1621 1624 public final void setColumnVisible(RulerColumnDescriptor descriptor, boolean visible) { 1625 Assert.isLegal(descriptor != null); 1626 1627 final CompositeRuler ruler= getRuler(); 1628 if (ruler == null) 1629 return; 1630 1631 if (!isColumnSupported(descriptor)) 1632 visible= false; 1633 1634 if (isColumnVisible(descriptor)) { 1635 if (!visible) 1636 removeColumn(ruler, descriptor); 1637 } else { 1638 if (visible) 1639 addColumn(ruler, descriptor); 1640 } 1641 } 1642 1643 private void addColumn(final CompositeRuler ruler, final RulerColumnDescriptor descriptor) { 1644 1645 final int idx= computeIndex(ruler, descriptor); 1646 1647 SafeRunnable runnable= new SafeRunnable() { 1648 public void run() throws Exception { 1649 IContributedRulerColumn column= descriptor.createColumn(fEditor); 1650 fColumns.add(column); 1651 initializeColumn(column); 1652 ruler.addDecorator(idx, column); 1653 } 1654 }; 1655 SafeRunner.run(runnable); 1656 } 1657 1658 1665 protected void initializeColumn(IContributedRulerColumn column) { 1666 } 1667 1668 private void removeColumn(final CompositeRuler ruler, final RulerColumnDescriptor descriptor) { 1669 removeColumn(ruler, getVisibleColumn(ruler, descriptor)); 1670 } 1671 1672 private void removeColumn(final CompositeRuler ruler, final IContributedRulerColumn rulerColumn) { 1673 if (rulerColumn != null) { 1674 SafeRunnable runnable= new SafeRunnable() { 1675 public void run() throws Exception { 1676 if (ruler != null) 1677 ruler.removeDecorator(rulerColumn); 1678 rulerColumn.columnRemoved(); 1679 } 1680 }; 1681 SafeRunner.run(runnable); 1682 } 1683 } 1684 1685 1693 private IContributedRulerColumn getVisibleColumn(CompositeRuler ruler, RulerColumnDescriptor descriptor) { 1694 for (Iterator it= ruler.getDecoratorIterator(); it.hasNext();) { 1695 IVerticalRulerColumn column= (IVerticalRulerColumn)it.next(); 1696 if (column instanceof IContributedRulerColumn) { 1697 IContributedRulerColumn rulerColumn= (IContributedRulerColumn)column; 1698 RulerColumnDescriptor rcd= rulerColumn.getDescriptor(); 1699 if (descriptor.equals(rcd)) 1700 return rulerColumn; 1701 } 1702 } 1703 return null; 1704 } 1705 1706 1713 private int computeIndex(CompositeRuler ruler, RulerColumnDescriptor descriptor) { 1714 int index= 0; 1715 List all= fRegistry.getColumnDescriptors(); 1716 int newPos= all.indexOf(descriptor); 1717 for (Iterator it= ruler.getDecoratorIterator(); it.hasNext();) { 1718 IVerticalRulerColumn column= (IVerticalRulerColumn) it.next(); 1719 if (column instanceof IContributedRulerColumn) { 1720 RulerColumnDescriptor rcd= ((IContributedRulerColumn)column).getDescriptor(); 1721 if (rcd != null && all.indexOf(rcd) > newPos) 1722 break; 1723 } else if ("org.eclipse.jface.text.source.projection.ProjectionRulerColumn".equals(column.getClass().getName())) { break; 1726 } 1727 index++; 1728 } 1729 return index; 1730 } 1731 1732 1735 public final boolean isColumnVisible(RulerColumnDescriptor descriptor) { 1736 Assert.isLegal(descriptor != null); 1737 CompositeRuler ruler= getRuler(); 1738 return ruler != null && getVisibleColumn(ruler, descriptor) != null; 1739 } 1740 1741 1744 public final boolean isColumnSupported(RulerColumnDescriptor descriptor) { 1745 Assert.isLegal(descriptor != null); 1746 if (getRuler() == null) 1747 return false; 1748 1749 if (descriptor == null) 1750 return false; 1751 1752 return descriptor.matchesEditor(fEditor); 1753 } 1754 1755 1761 private CompositeRuler getRuler() { 1762 Object ruler= fEditor.getAdapter(IVerticalRulerInfo.class); 1763 if (ruler instanceof CompositeRuler) 1764 return (CompositeRuler) ruler; 1765 return null; 1766 } 1767 1768 1774 public void dispose() { 1775 for (Iterator iter= new ArrayList (fColumns).iterator(); iter.hasNext();) 1776 removeColumn(getRuler(), (IContributedRulerColumn)iter.next()); 1777 fColumns.clear(); 1778 } 1779 } 1780 1781 1782 1783 1788 private static final class InformationProvider implements IInformationProvider, IInformationProviderExtension, IInformationProviderExtension2 { 1789 1790 private IRegion fHoverRegion; 1791 private Object fHoverInfo; 1792 private IInformationControlCreator fControlCreator; 1793 1794 InformationProvider(IRegion hoverRegion, Object hoverInfo, IInformationControlCreator controlCreator) { 1795 fHoverRegion= hoverRegion; 1796 fHoverInfo= hoverInfo; 1797 fControlCreator= controlCreator; 1798 } 1799 1802 public IRegion getSubject(ITextViewer textViewer, int invocationOffset) { 1803 return fHoverRegion; 1804 } 1805 1808 public String getInformation(ITextViewer textViewer, IRegion subject) { 1809 return fHoverInfo.toString(); 1810 } 1811 1815 public Object getInformation2(ITextViewer textViewer, IRegion subject) { 1816 return fHoverInfo; 1817 } 1818 1821 public IInformationControlCreator getInformationPresenterControlCreator() { 1822 return fControlCreator; 1823 } 1824 } 1825 1826 1834 private final class InformationDispatchAction extends TextEditorAction { 1835 1836 1837 private final TextOperationAction fTextOperationAction; 1838 1839 1846 public InformationDispatchAction(ResourceBundle resourceBundle, String prefix, final TextOperationAction textOperationAction) { 1847 super(resourceBundle, prefix, AbstractTextEditor.this); 1848 if (textOperationAction == null) 1849 throw new IllegalArgumentException (); 1850 fTextOperationAction= textOperationAction; 1851 } 1852 1853 1856 public void run() { 1857 1858 ISourceViewer sourceViewer= getSourceViewer(); 1859 if (sourceViewer == null) { 1860 if (fTextOperationAction.isEnabled()) 1861 fTextOperationAction.run(); 1862 return; 1863 } 1864 1865 if (sourceViewer instanceof ITextViewerExtension4) { 1866 ITextViewerExtension4 extension4= (ITextViewerExtension4) sourceViewer; 1867 if (extension4.moveFocusToWidgetToken()) 1868 return; 1869 } 1870 1871 if (sourceViewer instanceof ITextViewerExtension2) { 1872 ITextHover textHover= ((ITextViewerExtension2) sourceViewer).getCurrentTextHover(); 1874 if (textHover != null && makeTextHoverFocusable(sourceViewer, textHover)) 1875 return; 1876 } 1877 1878 if (sourceViewer instanceof ISourceViewerExtension3) { 1879 IAnnotationHover annotationHover= ((ISourceViewerExtension3) sourceViewer).getCurrentAnnotationHover(); 1881 if (annotationHover != null && makeAnnotationHoverFocusable(sourceViewer, annotationHover)) 1882 return; 1883 } 1884 1885 if (fTextOperationAction.isEnabled()) 1887 fTextOperationAction.run(); 1888 } 1889 1890 1897 private boolean makeTextHoverFocusable(ISourceViewer sourceViewer, ITextHover textHover) { 1898 Point hoverEventLocation= ((ITextViewerExtension2) sourceViewer).getHoverEventLocation(); 1899 int offset= computeOffsetAtLocation(sourceViewer, hoverEventLocation.x, hoverEventLocation.y); 1900 if (offset == -1) 1901 return false; 1902 1903 try { 1904 IRegion hoverRegion= textHover.getHoverRegion(sourceViewer, offset); 1905 if (hoverRegion == null) 1906 return false; 1907 1908 String hoverInfo= textHover.getHoverInfo(sourceViewer, hoverRegion); 1909 1910 IInformationControlCreator controlCreator= null; 1911 if (textHover instanceof IInformationProviderExtension2) 1912 controlCreator= ((IInformationProviderExtension2)textHover).getInformationPresenterControlCreator(); 1913 1914 IInformationProvider informationProvider= new InformationProvider(hoverRegion, hoverInfo, controlCreator); 1915 1916 fInformationPresenter.setOffset(offset); 1917 fInformationPresenter.setAnchor(AbstractInformationControlManager.ANCHOR_BOTTOM); 1918 fInformationPresenter.setMargins(6, 6); String contentType= TextUtilities.getContentType(sourceViewer.getDocument(), getSourceViewerConfiguration().getConfiguredDocumentPartitioning(getSourceViewer()), offset, true); 1920 fInformationPresenter.setInformationProvider(informationProvider, contentType); 1921 fInformationPresenter.showInformation(); 1922 1923 return true; 1924 1925 } catch (BadLocationException e) { 1926 return false; 1927 } 1928 } 1929 1930 1937 private boolean makeAnnotationHoverFocusable(ISourceViewer sourceViewer, IAnnotationHover annotationHover) { 1938 IVerticalRulerInfo info= getVerticalRuler(); 1939 int line= info.getLineOfLastMouseButtonActivity(); 1940 if (line == -1) 1941 return false; 1942 1943 try { 1944 1945 Object hoverInfo; 1947 if (annotationHover instanceof IAnnotationHoverExtension) { 1948 IAnnotationHoverExtension extension= (IAnnotationHoverExtension) annotationHover; 1949 ILineRange hoverLineRange= extension.getHoverLineRange(sourceViewer, line); 1950 if (hoverLineRange == null) 1951 return false; 1952 final int maxVisibleLines= Integer.MAX_VALUE; hoverInfo= extension.getHoverInfo(sourceViewer, hoverLineRange, maxVisibleLines); 1954 } else { 1955 hoverInfo= annotationHover.getHoverInfo(sourceViewer, line); 1956 } 1957 1958 IDocument document= sourceViewer.getDocument(); 1960 int offset= document.getLineOffset(line); 1961 String contentType= TextUtilities.getContentType(document, getSourceViewerConfiguration().getConfiguredDocumentPartitioning(getSourceViewer()), offset, true); 1962 1963 IInformationControlCreator controlCreator= null; 1964 if (annotationHover instanceof IInformationProviderExtension2) 1965 controlCreator= ((IInformationProviderExtension2) annotationHover).getInformationPresenterControlCreator(); 1966 else if (annotationHover instanceof IAnnotationHoverExtension) 1967 controlCreator= ((IAnnotationHoverExtension) annotationHover).getHoverControlCreator(); 1968 1969 IInformationProvider informationProvider= new InformationProvider(new Region(offset, 0), hoverInfo, controlCreator); 1970 1971 fInformationPresenter.setOffset(offset); 1972 fInformationPresenter.setAnchor(AbstractInformationControlManager.ANCHOR_RIGHT); 1973 fInformationPresenter.setMargins(4, 0); fInformationPresenter.setInformationProvider(informationProvider, contentType); 1975 fInformationPresenter.showInformation(); 1976 1977 return true; 1978 1979 } catch (BadLocationException e) { 1980 return false; 1981 } 1982 } 1983 1984 private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) { 1986 1987 StyledText styledText= textViewer.getTextWidget(); 1988 IDocument document= textViewer.getDocument(); 1989 1990 if (document == null) 1991 return -1; 1992 1993 try { 1994 int widgetOffset= styledText.getOffsetAtLocation(new Point(x, y)); 1995 Point p= styledText.getLocationAtOffset(widgetOffset); 1996 if (p.x > x) 1997 widgetOffset--; 1998 1999 if (textViewer instanceof ITextViewerExtension5) { 2000 ITextViewerExtension5 extension= (ITextViewerExtension5) textViewer; 2001 return extension.widgetOffset2ModelOffset(widgetOffset); 2002 } 2003 IRegion visibleRegion= textViewer.getVisibleRegion(); 2004 return widgetOffset + visibleRegion.getOffset(); 2005 } catch (IllegalArgumentException e) { 2006 return -1; 2007 } 2008 2009 } 2010 } 2011 2012 2013 2019 public final static String PREFERENCE_FONT= JFaceResources.TEXT_FONT; 2020 2025 public final static String PREFERENCE_COLOR_FOREGROUND= "AbstractTextEditor.Color.Foreground"; 2031 public final static String PREFERENCE_COLOR_BACKGROUND= "AbstractTextEditor.Color.Background"; 2037 public final static String PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT= "AbstractTextEditor.Color.Foreground.SystemDefault"; 2043 public final static String PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT= "AbstractTextEditor.Color.Background.SystemDefault"; 2049 public final static String PREFERENCE_COLOR_SELECTION_FOREGROUND= "AbstractTextEditor.Color.SelectionForeground"; 2055 public final static String PREFERENCE_COLOR_SELECTION_BACKGROUND= "AbstractTextEditor.Color.SelectionBackground"; 2061 public final static String PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT= "AbstractTextEditor.Color.SelectionForeground.SystemDefault"; 2067 public final static String PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT= "AbstractTextEditor.Color.SelectionBackground.SystemDefault"; 2073 public final static String PREFERENCE_COLOR_FIND_SCOPE= "AbstractTextEditor.Color.FindScope"; 2079 public final static String PREFERENCE_NAVIGATION_SMART_HOME_END= "AbstractTextEditor.Navigation.SmartHomeEnd"; 2085 public final static String PREFERENCE_USE_CUSTOM_CARETS= "AbstractTextEditor.Accessibility.UseCustomCarets"; 2091 public final static String PREFERENCE_WIDE_CARET= "AbstractTextEditor.Accessibility.WideCaret"; 2100 public static final String PREFERENCE_HYPERLINKS_ENABLED= "hyperlinksEnabled"; 2102 2110 public static final String PREFERENCE_HYPERLINK_KEY_MODIFIER= "hyperlinkKeyModifier"; 2122 public static final String PREFERENCE_HYPERLINK_KEY_MODIFIER_MASK= "hyperlinkKeyModifierMask"; 2131 public static final String PREFERENCE_RULER_CONTRIBUTIONS= "rulerContributions"; 2140 public static final String PREFERENCE_SHOW_WHITESPACE_CHARACTERS= "showWhitespaceCharacters"; 2149 public static final String PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED= "textDragAndDropEnabled"; 2151 2152 2153 public final static String DEFAULT_EDITOR_CONTEXT_MENU_ID= "#EditorContext"; 2155 public final static String DEFAULT_RULER_CONTEXT_MENU_ID= "#RulerContext"; 2157 2158 protected final static int VERTICAL_RULER_WIDTH= 12; 2159 2160 2165 protected final static IdMapEntry[] ACTION_MAP= new IdMapEntry[] { 2166 new IdMapEntry(ITextEditorActionDefinitionIds.LINE_UP, ST.LINE_UP), 2168 new IdMapEntry(ITextEditorActionDefinitionIds.LINE_DOWN, ST.LINE_DOWN), 2169 new IdMapEntry(ITextEditorActionDefinitionIds.LINE_START, ST.LINE_START), 2170 new IdMapEntry(ITextEditorActionDefinitionIds.LINE_END, ST.LINE_END), 2171 new IdMapEntry(ITextEditorActionDefinitionIds.COLUMN_PREVIOUS, ST.COLUMN_PREVIOUS), 2172 new IdMapEntry(ITextEditorActionDefinitionIds.COLUMN_NEXT, ST.COLUMN_NEXT), 2173 new IdMapEntry(ITextEditorActionDefinitionIds.PAGE_UP, ST.PAGE_UP), 2174 new IdMapEntry(ITextEditorActionDefinitionIds.PAGE_DOWN, ST.PAGE_DOWN), 2175 new IdMapEntry(ITextEditorActionDefinitionIds.WORD_PREVIOUS, ST.WORD_PREVIOUS), 2176 new IdMapEntry(ITextEditorActionDefinitionIds.WORD_NEXT, ST.WORD_NEXT), 2177 new IdMapEntry(ITextEditorActionDefinitionIds.TEXT_START, ST.TEXT_START), 2178 new IdMapEntry(ITextEditorActionDefinitionIds.TEXT_END, ST.TEXT_END), 2179 new IdMapEntry(ITextEditorActionDefinitionIds.WINDOW_START, ST.WINDOW_START), 2180 new IdMapEntry(ITextEditorActionDefinitionIds.WINDOW_END, ST.WINDOW_END), 2181 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_LINE_UP, ST.SELECT_LINE_UP), 2183 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_LINE_DOWN, ST.SELECT_LINE_DOWN), 2184 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_LINE_START, ST.SELECT_LINE_START), 2185 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_LINE_END, ST.SELECT_LINE_END), 2186 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_COLUMN_PREVIOUS, ST.SELECT_COLUMN_PREVIOUS), 2187 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_COLUMN_NEXT, ST.SELECT_COLUMN_NEXT), 2188 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_PAGE_UP, ST.SELECT_PAGE_UP), 2189 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_PAGE_DOWN, ST.SELECT_PAGE_DOWN), 2190 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS, ST.SELECT_WORD_PREVIOUS), 2191 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_WORD_NEXT, ST.SELECT_WORD_NEXT), 2192 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_TEXT_START, ST.SELECT_TEXT_START), 2193 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_TEXT_END, ST.SELECT_TEXT_END), 2194 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_WINDOW_START, ST.SELECT_WINDOW_START), 2195 new IdMapEntry(ITextEditorActionDefinitionIds.SELECT_WINDOW_END, ST.SELECT_WINDOW_END), 2196 new IdMapEntry(IWorkbenchActionDefinitionIds.CUT, ST.CUT), 2198 new IdMapEntry(IWorkbenchActionDefinitionIds.COPY, ST.COPY), 2199 new IdMapEntry(IWorkbenchActionDefinitionIds.PASTE, ST.PASTE), 2200 new IdMapEntry(ITextEditorActionDefinitionIds.DELETE_PREVIOUS, ST.DELETE_PREVIOUS), 2201 new IdMapEntry(ITextEditorActionDefinitionIds.DELETE_NEXT, ST.DELETE_NEXT), 2202 new IdMapEntry(ITextEditorActionDefinitionIds.DELETE_PREVIOUS_WORD, ST.DELETE_WORD_PREVIOUS), 2203 new IdMapEntry(ITextEditorActionDefinitionIds.DELETE_NEXT_WORD, ST.DELETE_WORD_NEXT), 2204 new IdMapEntry(ITextEditorActionDefinitionIds.TOGGLE_OVERWRITE, ST.TOGGLE_OVERWRITE) 2206 }; 2207 2208 private final String fReadOnlyLabel= EditorMessages.Editor_statusline_state_readonly_label; 2209 private final String fWritableLabel= EditorMessages.Editor_statusline_state_writable_label; 2210 private final String fInsertModeLabel= EditorMessages.Editor_statusline_mode_insert_label; 2211 private final String fOverwriteModeLabel= EditorMessages.Editor_statusline_mode_overwrite_label; 2212 private final String fSmartInsertModeLabel= EditorMessages.Editor_statusline_mode_smartinsert_label; 2213 2214 2215 protected final String fErrorLabel= EditorMessages.Editor_statusline_error_label; 2216 2217 2220 private static class PositionLabelValue { 2221 2222 public int fValue; 2223 2224 public String toString() { 2225 return String.valueOf(fValue); 2226 } 2227 } 2228 2229 private final String fPositionLabelPattern= EditorMessages.Editor_statusline_position_pattern; 2230 2231 private final PositionLabelValue fLineLabel= new PositionLabelValue(); 2232 2233 private final PositionLabelValue fColumnLabel= new PositionLabelValue(); 2234 2235 private final Object [] fPositionLabelPatternArguments= new Object [] { fLineLabel, fColumnLabel }; 2236 2240 private IColumnSupport fColumnSupport; 2241 2242 2243 private IDocumentProvider fExplicitDocumentProvider; 2244 2245 private IPreferenceStore fPreferenceStore; 2246 2247 private Annotation fRangeIndicator; 2248 2249 private SourceViewerConfiguration fConfiguration; 2250 2251 private ISourceViewer fSourceViewer; 2252 2256 private SelectionProvider fSelectionProvider= new SelectionProvider(); 2257 2261 private SelectionListener fSelectionListener; 2262 2263 private Font fFont; 2267 private Color fForegroundColor; 2268 2272 private Color fBackgroundColor; 2273 2277 private Color fSelectionForegroundColor; 2278 2282 private Color fSelectionBackgroundColor; 2283 2287 private Color fFindScopeHighlightColor; 2288 2289 2293 private IEditorStatusLine fEditorStatusLine; 2294 2295 private IVerticalRuler fVerticalRuler; 2296 2297 private String fEditorContextMenuId; 2298 2299 private String fRulerContextMenuId; 2300 2301 private String fHelpContextId; 2302 2303 private boolean fShowHighlightRangeOnly; 2304 2305 private Map fActions= new HashMap (10); 2306 2307 private List fSelectionActions= new ArrayList (5); 2308 2309 private List fContentActions= new ArrayList (5); 2310 2314 private List fPropertyActions= new ArrayList (5); 2315 2319 private List fStateActions= new ArrayList (5); 2320 2321 private List fActivationCodes= new ArrayList (2); 2322 2323 private ActivationCodeTrigger fActivationCodeTrigger= new ActivationCodeTrigger(); 2324 2325 private IMenuListener fMenuListener; 2326 2327 private MouseListener fMouseListener; 2328 2329 private ISelectionChangedListener fSelectionChangedListener; 2330 2331 private Image fTitleImage; 2332 2333 private Menu fTextContextMenu; 2334 2335 private Menu fRulerContextMenu; 2336 2337 private IElementStateListener fElementStateListener= new ElementStateListener(); 2338 2342 private TextInputListener fTextInputListener= new TextInputListener(); 2343 2344 private TextListener fTextListener= new TextListener(); 2345 2346 private IPropertyChangeListener fPropertyChangeListener= new PropertyChangeListener(); 2347 2351 private IPropertyChangeListener fFontPropertyChangeListener= new FontPropertyChangeListener(); 2352 2353 2357 private ActivationListener fActivationListener; 2358 2362 private Map fStatusFields; 2363 2367 private ICursorListener fCursorListener; 2368 2372 private ISelection fRememberedSelection; 2373 2377 private boolean fCompatibilityMode= true; 2378 2382 private int fErrorCorrectionOnSave; 2383 2387 private DeleteLineTarget fDeleteLineTarget; 2388 2392 private IncrementalFindTarget fIncrementalFindTarget; 2393 2397 private IMarkRegionTarget fMarkRegionTarget; 2398 2402 private long fModificationStamp= -1; 2403 2407 private List fRulerContextMenuListeners= new ArrayList (); 2408 2412 private boolean fIsSanityCheckEnabled= true; 2413 2417 private FindReplaceTarget fFindReplaceTarget; 2418 2422 private boolean fIsStateValidationEnabled= true; 2423 2427 private String [] fKeyBindingScopes; 2428 2432 private boolean fIsOverwriteModeEnabled= true; 2433 2437 private boolean fIsOverwriting= false; 2438 2442 private InsertMode fInsertMode= SMART_INSERT; 2443 2447 private List fLegalInsertModes= null; 2448 2452 private Caret fNonDefaultCaret; 2453 2457 private Image fNonDefaultCaretImage; 2458 2462 private Caret fInitialCaret; 2463 2467 private IOperationApprover fNonLocalOperationApprover; 2468 2472 private IOperationApprover fLinearUndoViolationApprover; 2473 2477 private IMemento fMementoToRestore; 2478 2482 private TextEditorSavable fSavable; 2483 2487 private boolean fIsTextDragAndDropEnabled= false; 2488 2492 private boolean fIsTextDragAndDropInstalled= false; 2493 2498 private Object fTextDragAndDropToken; 2499 2503 private InformationPresenter fInformationPresenter; 2504 2505 2506 2513 protected AbstractTextEditor() { 2514 super(); 2515 fEditorContextMenuId= null; 2516 fRulerContextMenuId= null; 2517 fHelpContextId= null; 2518 } 2519 2520 2523 public IDocumentProvider getDocumentProvider() { 2524 return fExplicitDocumentProvider; 2525 } 2526 2527 2533 protected final Annotation getRangeIndicator() { 2534 return fRangeIndicator; 2535 } 2536 2537 2543 protected final SourceViewerConfiguration getSourceViewerConfiguration() { 2544 return fConfiguration; 2545 } 2546 2547 2553 protected final ISourceViewer getSourceViewer() { 2554 return fSourceViewer; 2555 } 2556 2557 2563 protected final IVerticalRuler getVerticalRuler() { 2564 return fVerticalRuler; 2565 } 2566 2567 2573 protected final String getEditorContextMenuId() { 2574 return fEditorContextMenuId; 2575 } 2576 2577 2583 protected final String getRulerContextMenuId() { 2584 return fRulerContextMenuId; 2585 } 2586 2587 2593 protected final String getHelpContextId() { 2594 return fHelpContextId; 2595 } 2596 2597 2603 protected final IPreferenceStore getPreferenceStore() { 2604 return fPreferenceStore; 2605 } 2606 2607 2613 protected void setDocumentProvider(IDocumentProvider provider) { 2614 fExplicitDocumentProvider= provider; 2615 } 2616 2617 2624 protected void setSourceViewerConfiguration(SourceViewerConfiguration configuration) { 2625 Assert.isNotNull(configuration); 2626 fConfiguration= configuration; 2627 } 2628 2629 2636 protected void setRangeIndicator(Annotation rangeIndicator) { 2637 Assert.isNotNull(rangeIndicator); 2638 fRangeIndicator= rangeIndicator; 2639 } 2640 2641 2646 protected void setEditorContextMenuId(String contextMenuId) { 2647 Assert.isNotNull(contextMenuId); 2648 fEditorContextMenuId= contextMenuId; 2649 } 2650 2651 2656 protected void setRulerContextMenuId(String contextMenuId) { 2657 Assert.isNotNull(contextMenuId); 2658 fRulerContextMenuId= contextMenuId; 2659 } 2660 2661 2668 protected final void setCompatibilityMode(boolean compatible) { 2669 fCompatibilityMode= compatible; 2670 } 2671 2672 2677 protected void setHelpContextId(String helpContextId) { 2678 Assert.isNotNull(helpContextId); 2679 fHelpContextId= helpContextId; 2680 } 2681 2682 2688 protected void setKeyBindingScopes(String [] scopes) { 2689 Assert.isTrue(scopes != null && scopes.length > 0); 2690 fKeyBindingScopes= scopes; 2691 } 2692 2693 2700 protected void setPreferenceStore(IPreferenceStore store) { 2701 if (fPreferenceStore != null) 2702 fPreferenceStore.removePropertyChangeListener(fPropertyChangeListener); 2703 2704 fPreferenceStore= store; 2705 2706 if (fPreferenceStore != null) 2707 fPreferenceStore.addPropertyChangeListener(fPropertyChangeListener); 2708 } 2709 2710 2713 public boolean isEditable() { 2714 IDocumentProvider provider= getDocumentProvider(); 2715 if (provider instanceof IDocumentProviderExtension) { 2716 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 2717 return extension.isModifiable(getEditorInput()); 2718 } 2719 return false; 2720 } 2721 2722 2731 public ISelectionProvider getSelectionProvider() { 2732 return fSelectionProvider; 2733 } 2734 2735 2749 protected void rememberSelection() { 2750 fRememberedSelection= doGetSelection(); 2751 } 2752 2753 2758 protected ISelection doGetSelection() { 2759 ISelectionProvider sp= null; 2760 if (fSourceViewer != null) 2761 sp= fSourceViewer.getSelectionProvider(); 2762 return (sp == null ? null : sp.getSelection()); 2763 } 2764 2765 2774 protected void restoreSelection() { 2775 if (fRememberedSelection instanceof ITextSelection) { 2776 ITextSelection textSelection= (ITextSelection)fRememberedSelection; 2777 if (isValidSelection(textSelection.getOffset(), textSelection.getLength())) 2778 doSetSelection(fRememberedSelection); 2779 } 2780 fRememberedSelection= null; 2781 } 2782 2783 2791 private boolean isValidSelection(int offset, int length) { 2792 IDocumentProvider provider= getDocumentProvider(); 2793 if (provider != null) { 2794 IDocument document= provider.getDocument(getEditorInput()); 2795 if (document != null) { 2796 int end= offset + length; 2797 int documentLength= document.getLength(); 2798 return 0 <= offset && offset <= documentLength && 0 <= end && end <= documentLength && length >= 0; 2799 } 2800 } 2801 return false; 2802 } 2803 2804 2809 protected void doSetSelection(ISelection selection) { 2810 if (selection instanceof ITextSelection) { 2811 ITextSelection textSelection= (ITextSelection) selection; 2812 selectAndReveal(textSelection.getOffset(), textSelection.getLength()); 2813 } 2814 } 2815 2816 2821 protected final IMenuListener getContextMenuListener() { 2822 if (fMenuListener == null) { 2823 fMenuListener= new IMenuListener() { 2824 2825 public void menuAboutToShow(IMenuManager menu) { 2826 String id= menu.getId(); 2827 if (getRulerContextMenuId().equals(id)) { 2828 setFocus(); 2829 rulerContextMenuAboutToShow(menu); 2830 } else if (getEditorContextMenuId().equals(id)) { 2831 setFocus(); 2832 editorContextMenuAboutToShow(menu); 2833 } 2834 } 2835 }; 2836 } 2837 return fMenuListener; 2838 } 2839 2840 2845 protected final MouseListener getRulerMouseListener() { 2846 if (fMouseListener == null) { 2847 fMouseListener= new MouseListener() { 2848 2849 private boolean fDoubleClicked= false; 2850 private final int fDoubleClickTime= Display.getDefault().getDoubleClickTime(); 2851 private long fMouseUpDelta= 0; 2852 2853 private void triggerAction(String actionID) { 2854 IAction action= getAction(actionID); 2855 if (action != null) { 2856 if (action instanceof IUpdate) 2857 ((IUpdate) action).update(); 2858 if (action.isEnabled()) 2859 action.run(); 2860 } 2861 } 2862 2863 public void mouseUp(final MouseEvent e) { 2864 setFocus(); 2865 final int delay= fDoubleClickTime - (int)(System.currentTimeMillis() - fMouseUpDelta); 2866 if (1 != e.button) 2867 return; 2868 2869 Runnable runnable= new Runnable () { 2870 public void run() { 2871 if (!fDoubleClicked) 2872 triggerAction(ITextEditorActionConstants.RULER_CLICK); 2873 } 2874 }; 2875 if (delay <= 0) 2876 runnable.run(); 2877 else 2878 Display.getDefault().timerExec(delay, runnable); 2879 } 2880 2881 public void mouseDoubleClick(MouseEvent e) { 2882 if (1 == e.button) { 2883 fDoubleClicked= true; 2884 triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK); 2885 } 2886 } 2887 2888 public void mouseDown(MouseEvent e) { 2889 fMouseUpDelta= System.currentTimeMillis(); 2890 fDoubleClicked= false; 2891 StyledText text= fSourceViewer.getTextWidget(); 2892 if (text != null && !text.isDisposed()) { 2893 Display display= text.getDisplay(); 2894 Point location= display.getCursorLocation(); 2895 fRulerContextMenu.setLocation(location.x, location.y); 2896 } 2897 } 2898 }; 2899 } 2900 return fMouseListener; 2901 } 2902 2903 2909 protected final ISelectionChangedListener getSelectionChangedListener() { 2910 if (fSelectionChangedListener == null) { 2911 fSelectionChangedListener= new ISelectionChangedListener() { 2912 2913 private Runnable fRunnable= new Runnable () { 2914 public void run() { 2915 if (fSourceViewer != null && fSourceViewer.getDocument() != null) { 2917 updateSelectionDependentActions(); 2918 } 2919 } 2920 }; 2921 2922 private Display fDisplay; 2923 2924 public void selectionChanged(SelectionChangedEvent event) { 2925 if (fDisplay == null) 2926 fDisplay= getSite().getShell().getDisplay(); 2927 fDisplay.asyncExec(fRunnable); 2928 handleCursorPositionChanged(); 2929 } 2930 }; 2931 } 2932 2933 return fSelectionChangedListener; 2934 } 2935 2936 2945 protected final ICursorListener getCursorListener() { 2946 if (fCursorListener == null) { 2947 fCursorListener= new ICursorListener() { 2948 2949 public void keyPressed(KeyEvent e) { 2950 handleCursorPositionChanged(); 2951 } 2952 2953 public void keyReleased(KeyEvent e) { 2954 } 2955 2956 public void mouseDoubleClick(MouseEvent e) { 2957 } 2958 2959 public void mouseDown(MouseEvent e) { 2960 } 2961 2962 public void mouseUp(MouseEvent e) { 2963 handleCursorPositionChanged(); 2964 } 2965 }; 2966 } 2967 return fCursorListener; 2968 } 2969 2970 2983 protected final void internalInit(IWorkbenchWindow window, final IEditorSite site, final IEditorInput input) throws PartInitException { 2984 2985 IRunnableWithProgress runnable= new IRunnableWithProgress() { 2986 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 2987 try { 2988 2989 if (getDocumentProvider() instanceof IDocumentProviderExtension2) { 2990 IDocumentProviderExtension2 extension= (IDocumentProviderExtension2) getDocumentProvider(); 2991 extension.setProgressMonitor(monitor); 2992 } 2993 2994 doSetInput(input); 2995 2996 } catch (CoreException x) { 2997 throw new InvocationTargetException (x); 2998 } finally { 2999 if (getDocumentProvider() instanceof IDocumentProviderExtension2) { 3000 IDocumentProviderExtension2 extension= (IDocumentProviderExtension2) getDocumentProvider(); 3001 extension.setProgressMonitor(null); 3002 } 3003 } 3004 } 3005 }; 3006 3007 try { 3008 3012 getSite().getWorkbenchWindow().run(false, true, runnable); 3013 3014 } catch (InterruptedException x) { 3015 } catch (InvocationTargetException x) { 3016 Throwable t= x.getTargetException(); 3017 if (t instanceof CoreException) { 3018 3022 CoreException e= (CoreException)t; 3023 IStatus status= e.getStatus(); 3024 if (status.getException() != null) 3025 throw new PartInitException(status); 3026 throw new PartInitException(new Status(status.getSeverity(), status.getPlugin(), status.getCode(), status.getMessage(), t)); 3027 } 3028 throw new PartInitException(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, EditorMessages.Editor_error_init, t)); 3029 } 3030 } 3031 3032 3035 public void init(final IEditorSite site, final IEditorInput input) throws PartInitException { 3036 3037 setSite(site); 3038 3039 internalInit(site.getWorkbenchWindow(), site, input); 3040 fActivationListener= new ActivationListener(site.getWorkbenchWindow().getPartService()); 3041 } 3042 3043 3049 protected IVerticalRuler createVerticalRuler() { 3050 return new VerticalRuler(VERTICAL_RULER_WIDTH); 3051 } 3052 3053 3061 protected void updateContributedRulerColumns(CompositeRuler ruler) { 3062 IColumnSupport support= (IColumnSupport)getAdapter(IColumnSupport.class); 3063 if (support == null) 3064 return; 3065 3066 RulerColumnPreferenceAdapter adapter= null; 3067 if (fPreferenceStore != null) 3068 adapter= new RulerColumnPreferenceAdapter(getPreferenceStore(), PREFERENCE_RULER_CONTRIBUTIONS); 3069 3070 RulerColumnRegistry registry= RulerColumnRegistry.getDefault(); 3071 List descriptors= registry.getColumnDescriptors(); 3072 for (Iterator it= descriptors.iterator(); it.hasNext();) { 3073 final RulerColumnDescriptor descriptor= (RulerColumnDescriptor) it.next(); 3074 support.setColumnVisible(descriptor, adapter == null || adapter.isEnabled(descriptor)); 3075 } 3076 } 3077 3078 3093 protected IColumnSupport createColumnSupport() { 3094 return null; 3095 } 3096 3097 3106 protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) { 3107 return new SourceViewer(parent, ruler, styles); 3108 } 3109 3110 3117 protected void initializeDragAndDrop(ISourceViewer viewer) { 3118 IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class); 3119 if (dndService == null) 3120 return; 3121 3122 ITextEditorDropTargetListener listener= (ITextEditorDropTargetListener) getAdapter(ITextEditorDropTargetListener.class); 3123 3124 if (listener == null) { 3125 Object object= Platform.getAdapterManager().loadAdapter(this, "org.eclipse.ui.texteditor.ITextEditorDropTargetListener"); if (object instanceof ITextEditorDropTargetListener) 3127 listener= (ITextEditorDropTargetListener)object; 3128 } 3129 3130 if (listener != null) 3131 dndService.addMergedDropTarget(viewer.getTextWidget(), DND.DROP_MOVE | DND.DROP_COPY, listener.getTransfers(), listener); 3132 3133 IPreferenceStore store= getPreferenceStore(); 3134 if (store != null && store.getBoolean(PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED)) 3135 installTextDragAndDrop(viewer); 3136 3137 } 3138 3139 3192 public void createPartControl(Composite parent) { 3193 3194 fVerticalRuler= createVerticalRuler(); 3195 3196 int styles= SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION; 3197 fSourceViewer= createSourceViewer(parent, fVerticalRuler, styles); 3198 3199 if (fConfiguration == null) 3200 fConfiguration= new SourceViewerConfiguration(); 3201 fSourceViewer.configure(fConfiguration); 3202 3203 if (fRangeIndicator != null) 3204 fSourceViewer.setRangeIndicator(fRangeIndicator); 3205 3206 fSourceViewer.addTextListener(fTextListener); 3207 fSourceViewer.addTextInputListener(fTextListener); 3208 getSelectionProvider().addSelectionChangedListener(getSelectionChangedListener()); 3209 3210 initializeViewerFont(fSourceViewer); 3211 initializeViewerColors(fSourceViewer); 3212 initializeFindScopeColor(fSourceViewer); 3213 initializeDragAndDrop(fSourceViewer); 3214 3215 StyledText styledText= fSourceViewer.getTextWidget(); 3216 3217 3263 3264 styledText.addMouseListener(getCursorListener()); 3265 styledText.addKeyListener(getCursorListener()); 3266 3267 if (getHelpContextId() != null) 3268 PlatformUI.getWorkbench().getHelpSystem().setHelp(styledText, getHelpContextId()); 3269 3270 3271 String id= fEditorContextMenuId != null ? fEditorContextMenuId : DEFAULT_EDITOR_CONTEXT_MENU_ID; 3272 3273 MenuManager manager= new MenuManager(id, id); 3274 manager.setRemoveAllWhenShown(true); 3275 manager.addMenuListener(getContextMenuListener()); 3276 fTextContextMenu= manager.createContextMenu(styledText); 3277 3278 styledText.setMenu(fTextContextMenu); 3280 3281 if (fEditorContextMenuId != null) 3282 getEditorSite().registerContextMenu(fEditorContextMenuId, manager, getSelectionProvider(), isEditorInputIncludedInContextMenu()); 3283 else if (fCompatibilityMode) 3284 getEditorSite().registerContextMenu(DEFAULT_EDITOR_CONTEXT_MENU_ID, manager, getSelectionProvider(), isEditorInputIncludedInContextMenu()); 3285 3286 if ((fEditorContextMenuId != null && fCompatibilityMode) || fEditorContextMenuId == null) { 3287 String partId= getEditorSite().getId(); 3288 if (partId != null) 3289 getEditorSite().registerContextMenu(partId + ".EditorContext", manager, getSelectionProvider(), isEditorInputIncludedInContextMenu()); } 3291 3292 if (fEditorContextMenuId == null) 3293 fEditorContextMenuId= DEFAULT_EDITOR_CONTEXT_MENU_ID; 3294 3295 3296 id= fRulerContextMenuId != null ? fRulerContextMenuId : DEFAULT_RULER_CONTEXT_MENU_ID; 3297 manager= new MenuManager(id, id); 3298 manager.setRemoveAllWhenShown(true); 3299 manager.addMenuListener(getContextMenuListener()); 3300 3301 Control rulerControl= fVerticalRuler.getControl(); 3302 fRulerContextMenu= manager.createContextMenu(rulerControl); 3303 rulerControl.setMenu(fRulerContextMenu); 3304 rulerControl.addMouseListener(getRulerMouseListener()); 3305 3306 if (fRulerContextMenuId != null) 3307 getEditorSite().registerContextMenu(fRulerContextMenuId, manager, getSelectionProvider(), false); 3308 else if (fCompatibilityMode) 3309 getEditorSite().registerContextMenu(DEFAULT_RULER_CONTEXT_MENU_ID, manager, getSelectionProvider(), false); 3310 3311 if ((fRulerContextMenuId != null && fCompatibilityMode) || fRulerContextMenuId == null) { 3312 String partId= getSite().getId(); 3313 if (partId != null) 3314 getEditorSite().registerContextMenu(partId + ".RulerContext", manager, getSelectionProvider(), false); } 3316 3317 if (fRulerContextMenuId == null) 3318 fRulerContextMenuId= DEFAULT_RULER_CONTEXT_MENU_ID; 3319 3320 getSite().setSelectionProvider(getSelectionProvider()); 3321 3322 fSelectionListener= new SelectionListener(); 3323 fSelectionListener.install(getSelectionProvider()); 3324 fSelectionListener.setDocument(getDocumentProvider().getDocument(getEditorInput())); 3325 3326 initializeActivationCodeTrigger(); 3327 3328 createNavigationActions(); 3329 createAccessibilityActions(); 3330 createActions(); 3331 3332 initializeSourceViewer(getEditorInput()); 3333 3334 3338 createUndoRedoActions(); 3339 3340 JFaceResources.getFontRegistry().addListener(fFontPropertyChangeListener); 3341 3342 IVerticalRuler ruler= getVerticalRuler(); 3343 if (ruler instanceof CompositeRuler) 3344 updateContributedRulerColumns((CompositeRuler) ruler); 3345 3346 IInformationControlCreator informationControlCreator= new IInformationControlCreator() { 3347 public IInformationControl createInformationControl(Shell shell) { 3348 boolean cutDown= false; 3349 int style= cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL); 3350 return new DefaultInformationControl(shell, SWT.RESIZE | SWT.TOOL, style, new HTMLTextPresenter(cutDown)); 3351 } 3352 }; 3353 3354 fInformationPresenter= new InformationPresenter(informationControlCreator); 3355 fInformationPresenter.setSizeConstraints(60, 10, true, true); 3356 fInformationPresenter.install(getSourceViewer()); 3357 fInformationPresenter.setDocumentPartitioning(getSourceViewerConfiguration().getConfiguredDocumentPartitioning(getSourceViewer())); 3358 3359 } 3360 3361 3367 protected void installTextDragAndDrop(final ISourceViewer viewer) { 3368 if (fIsTextDragAndDropEnabled || viewer == null) 3369 return; 3370 3371 if (fIsTextDragAndDropInstalled) { 3372 fIsTextDragAndDropEnabled= true; 3373 return; 3374 } 3375 3376 final IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class); 3377 if (dndService == null) 3378 return; 3379 3380 fIsTextDragAndDropEnabled= true; 3381 3382 final StyledText st= viewer.getTextWidget(); 3383 3384 final ISelectionProvider selectionProvider= viewer.getSelectionProvider(); 3386 final DragSource source= new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE); 3387 source.setTransfer(new Transfer[] {TextTransfer.getInstance()}); 3388 source.addDragListener(new DragSourceAdapter() { 3389 String fSelectedText; 3390 Point fSelection; 3391 public void dragStart(DragSourceEvent event) { 3392 fTextDragAndDropToken= null; 3393 3394 if (!fIsTextDragAndDropEnabled) { 3396 event.doit= false; 3397 event.image= null; 3398 return; 3399 } 3400 3401 try { 3402 fSelection= st.getSelection(); 3403 int offset= st.getOffsetAtLocation(new Point(event.x, event.y)); 3404 Point p= st.getLocationAtOffset(offset); 3405 if (p.x > event.x) 3406 offset--; 3407 event.doit= offset > fSelection.x && offset < fSelection.y; 3408 3409 ISelection selection= selectionProvider.getSelection(); 3410 if (selection instanceof ITextSelection) 3411 fSelectedText= ((ITextSelection)selection).getText(); 3412 else fSelectedText= st.getSelectionText(); 3414 } catch (IllegalArgumentException ex) { 3415 event.doit= false; 3416 } 3417 } 3418 3419 public void dragSetData(DragSourceEvent event) { 3420 event.data= fSelectedText; 3421 fTextDragAndDropToken= this; } 3423 3424 public void dragFinished(DragSourceEvent event) { 3425 try { 3426 if (event.detail == DND.DROP_MOVE && validateEditorInputState()) { 3427 Point newSelection= st.getSelection(); 3428 int length= fSelection.y - fSelection.x; 3429 int delta= 0; 3430 if (newSelection.x < fSelection.x) 3431 delta= length; 3432 st.replaceTextRange(fSelection.x + delta, length, ""); 3434 if (fTextDragAndDropToken == null) { 3435 IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class); 3437 if (target != null) 3438 target.endCompoundChange(); 3439 } 3440 3441 } 3442 } finally { 3443 fTextDragAndDropToken= null; 3444 } 3445 } 3446 }); 3447 3448 DropTargetListener dropTargetListener= new DropTargetAdapter() { 3450 3451 private Point fSelection; 3452 3453 public void dragEnter(DropTargetEvent event) { 3454 fTextDragAndDropToken= null; 3455 fSelection= st.getSelection(); 3456 3457 if (!fIsTextDragAndDropEnabled) { 3459 event.detail= DND.DROP_NONE; 3460 event.feedback= DND.FEEDBACK_NONE; 3461 return; 3462 } 3463 3464 if (event.detail == DND.DROP_DEFAULT) 3465 event.detail= DND.DROP_MOVE; 3466 } 3467 3468 public void dragOperationChanged(DropTargetEvent event) { 3469 if (!fIsTextDragAndDropEnabled) { 3470 event.detail= DND.DROP_NONE; 3471 event.feedback= DND.FEEDBACK_NONE; 3472 return; 3473 } 3474 3475 if (event.detail == DND.DROP_DEFAULT) 3476 event.detail= DND.DROP_MOVE; 3477 } 3478 3479 public void dragOver(DropTargetEvent event) { 3480 3481 if (!fIsTextDragAndDropEnabled) { 3483 event.feedback= DND.FEEDBACK_NONE; 3484 return; 3485 } 3486 3487 event.feedback |= DND.FEEDBACK_SCROLL; 3488 } 3489 3490 public void drop(DropTargetEvent event) { 3491 try { 3492 if (!fIsTextDragAndDropEnabled) 3493 return; 3494 3495 if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) { 3496 int caretOffset= st.getCaretOffset(); 3498 if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) { 3499 event.detail= DND.DROP_NONE; 3500 return; 3501 } 3502 3503 IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class); 3505 if (target != null) 3506 target.beginCompoundChange(); 3507 } 3508 3509 if (!validateEditorInputState()) { 3510 event.detail= DND.DROP_NONE; 3511 return; 3512 } 3513 3514 String text= (String )event.data; 3515 Point newSelection= st.getSelection(); 3516 try { 3517 int modelOffset= widgetOffset2ModelOffset(viewer, newSelection.x); 3518 viewer.getDocument().replace(modelOffset, 0, text); 3519 } catch (BadLocationException e) { 3520 return; 3521 } 3522 st.setSelectionRange(newSelection.x, text.length()); 3523 } finally { 3524 fTextDragAndDropToken= null; 3525 } 3526 } 3527 }; 3528 dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] {TextTransfer.getInstance()}, dropTargetListener); 3529 3530 fIsTextDragAndDropInstalled= true; 3531 fIsTextDragAndDropEnabled= true; 3532 } 3533 3534 3540 protected void uninstallTextDragAndDrop(ISourceViewer viewer) { 3541 fIsTextDragAndDropEnabled= false; 3542 } 3543 3544 3554 protected boolean isEditorInputIncludedInContextMenu() { 3555 return true; 3556 } 3557 3558 3563 private void initializeActivationCodeTrigger() { 3564 fActivationCodeTrigger.install(); 3565 fActivationCodeTrigger.setScopes(fKeyBindingScopes); 3566 } 3567 3568 3574 private void initializeViewerFont(ISourceViewer viewer) { 3575 3576 boolean isSharedFont= true; 3577 Font font= null; 3578 String symbolicFontName= getSymbolicFontName(); 3579 3580 if (symbolicFontName != null) 3581 font= JFaceResources.getFont(symbolicFontName); 3582 else if (fPreferenceStore != null) { 3583 if (fPreferenceStore.contains(JFaceResources.TEXT_FONT) && !fPreferenceStore.isDefault(JFaceResources.TEXT_FONT)) { 3585 FontData data= PreferenceConverter.getFontData(fPreferenceStore, JFaceResources.TEXT_FONT); 3586 3587 if (data != null) { 3588 isSharedFont= false; 3589 font= new Font(viewer.getTextWidget().getDisplay(), data); 3590 } 3591 } 3592 } 3593 if (font == null) 3594 font= JFaceResources.getTextFont(); 3595 3596 setFont(viewer, font); 3597 3598 if (fFont != null) { 3599 fFont.dispose(); 3600 fFont= null; 3601 } 3602 3603 if (!isSharedFont) 3604 fFont= font; 3605 } 3606 3607 3614 private void setFont(ISourceViewer sourceViewer, Font font) { 3615 if (sourceViewer.getDocument() != null) { 3616 3617 Point selection= sourceViewer.getSelectedRange(); 3618 int topIndex= sourceViewer.getTopIndex(); 3619 3620 StyledText styledText= sourceViewer.getTextWidget(); 3621 Control parent= styledText; 3622 if (sourceViewer instanceof ITextViewerExtension) { 3623 ITextViewerExtension extension= (ITextViewerExtension) sourceViewer; 3624 parent= extension.getControl(); 3625 } 3626 3627 parent.setRedraw(false); 3628 3629 styledText.setFont(font); 3630 3631 if (fVerticalRuler instanceof IVerticalRulerExtension) { 3632 IVerticalRulerExtension e= (IVerticalRulerExtension) fVerticalRuler; 3633 e.setFont(font); 3634 } 3635 3636 sourceViewer.setSelectedRange(selection.x , selection.y); 3637 sourceViewer.setTopIndex(topIndex); 3638 3639 if (parent instanceof Composite) { 3640 Composite composite= (Composite) parent; 3641 composite.layout(true); 3642 } 3643 3644 parent.setRedraw(true); 3645 3646 3647 } else { 3648 3649 StyledText styledText= sourceViewer.getTextWidget(); 3650 styledText.setFont(font); 3651 3652 if (fVerticalRuler instanceof IVerticalRulerExtension) { 3653 IVerticalRulerExtension e= (IVerticalRulerExtension) fVerticalRuler; 3654 e.setFont(font); 3655 } 3656 } 3657 } 3658 3659 3669 private Color createColor(IPreferenceStore store, String key, Display display) { 3670 3671 RGB rgb= null; 3672 3673 if (store.contains(key)) { 3674 3675 if (store.isDefault(key)) 3676 rgb= PreferenceConverter.getDefaultColor(store, key); 3677 else 3678 rgb= PreferenceConverter.getColor(store, key); 3679 3680 if (rgb != null) 3681 return new Color(display, rgb); 3682 } 3683 3684 return null; 3685 } 3686 3687 3694 protected void initializeViewerColors(ISourceViewer viewer) { 3695 3696 IPreferenceStore store= getPreferenceStore(); 3697 if (store != null) { 3698 3699 StyledText styledText= viewer.getTextWidget(); 3700 3701 Color color= store.getBoolean(PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) 3703 ? null 3704 : createColor(store, PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay()); 3705 styledText.setForeground(color); 3706 3707 if (fForegroundColor != null) 3708 fForegroundColor.dispose(); 3709 3710 fForegroundColor= color; 3711 3712 color= store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) 3714 ? null 3715 : createColor(store, PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay()); 3716 styledText.setBackground(color); 3717 3718 if (fBackgroundColor != null) 3719 fBackgroundColor.dispose(); 3720 3721 fBackgroundColor= color; 3722 3723 color= store.getBoolean(PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT) 3725 ? null 3726 : createColor(store, PREFERENCE_COLOR_SELECTION_FOREGROUND, styledText.getDisplay()); 3727 styledText.setSelectionForeground(color); 3728 3729 if (fSelectionForegroundColor != null) 3730 fSelectionForegroundColor.dispose(); 3731 3732 fSelectionForegroundColor= color; 3733 3734 color= store.getBoolean(PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT) 3736 ? null 3737 : createColor(store, PREFERENCE_COLOR_SELECTION_BACKGROUND, styledText.getDisplay()); 3738 styledText.setSelectionBackground(color); 3739 3740 if (fSelectionBackgroundColor != null) 3741 fSelectionBackgroundColor.dispose(); 3742 3743 fSelectionBackgroundColor= color; 3744 } 3745 } 3746 3747 3754 private void initializeFindScopeColor(ISourceViewer viewer) { 3755 3756 IPreferenceStore store= getPreferenceStore(); 3757 if (store != null) { 3758 3759 StyledText styledText= viewer.getTextWidget(); 3760 3761 Color color= createColor(store, PREFERENCE_COLOR_FIND_SCOPE, styledText.getDisplay()); 3762 3763 IFindReplaceTarget target= viewer.getFindReplaceTarget(); 3764 if (target != null && target instanceof IFindReplaceTargetExtension) 3765 ((IFindReplaceTargetExtension) target).setScopeHighlightColor(color); 3766 3767 if (fFindScopeHighlightColor != null) 3768 fFindScopeHighlightColor.dispose(); 3769 3770 fFindScopeHighlightColor= color; 3771 } 3772 } 3773 3774 3775 3780 private void initializeSourceViewer(IEditorInput input) { 3781 3782 IAnnotationModel model= getDocumentProvider().getAnnotationModel(input); 3783 IDocument document= getDocumentProvider().getDocument(input); 3784 3785 if (document != null) { 3786 fSourceViewer.setDocument(document, model); 3787 fSourceViewer.setEditable(isEditable()); 3788 fSourceViewer.showAnnotations(model != null); 3789 } 3790 3791 if (fElementStateListener instanceof IElementStateListenerExtension) { 3792 IElementStateListenerExtension extension= (IElementStateListenerExtension) fElementStateListener; 3793 extension.elementStateValidationChanged(input, false); 3794 } 3795 3796 if (fInitialCaret == null) 3797 fInitialCaret= fSourceViewer.getTextWidget().getCaret(); 3798 3799 if (fIsOverwriting) 3800 fSourceViewer.getTextWidget().invokeAction(ST.TOGGLE_OVERWRITE); 3801 handleInsertModeChanged(); 3802 3803 if (isTabsToSpacesConversionEnabled()) 3804 installTabsToSpacesConverter(); 3805 3806 } 3807 3808 3813 private void initializeTitle(IEditorInput input) { 3814 3815 Image oldImage= fTitleImage; 3816 fTitleImage= null; 3817 String title= ""; 3819 if (input != null) { 3820 IEditorRegistry editorRegistry= PlatformUI.getWorkbench().getEditorRegistry(); 3821 IEditorDescriptor editorDesc= editorRegistry.findEditor(getSite().getId()); 3822 ImageDescriptor imageDesc= editorDesc != null ? editorDesc.getImageDescriptor() : null; 3823 3824 fTitleImage= imageDesc != null ? imageDesc.createImage() : null; 3825 title= input.getName(); 3826 } 3827 3828 setTitleImage(fTitleImage); 3829 setPartName(title); 3830 3831 firePropertyChange(PROP_DIRTY); 3832 3833 if (oldImage != null && !oldImage.isDisposed()) 3834 oldImage.dispose(); 3835 } 3836 3837 3845 protected void setDocumentProvider(IEditorInput input) { 3846 } 3847 3848 3854 private void updateDocumentProvider(IEditorInput input) { 3855 3856 IProgressMonitor rememberedProgressMonitor= null; 3857 3858 IDocumentProvider provider= getDocumentProvider(); 3859 if (provider != null) { 3860 provider.removeElementStateListener(fElementStateListener); 3861 if (provider instanceof IDocumentProviderExtension2) { 3862 IDocumentProviderExtension2 extension= (IDocumentProviderExtension2) provider; 3863 rememberedProgressMonitor= extension.getProgressMonitor(); 3864 extension.setProgressMonitor(null); 3865 } 3866 } 3867 3868 setDocumentProvider(input); 3869 3870 provider= getDocumentProvider(); 3871 if (provider != null) { 3872 provider.addElementStateListener(fElementStateListener); 3873 if (provider instanceof IDocumentProviderExtension2) { 3874 IDocumentProviderExtension2 extension= (IDocumentProviderExtension2) provider; 3875 extension.setProgressMonitor(rememberedProgressMonitor); 3876 } 3877 } 3878 } 3879 3880 3894 protected void doSetInput(IEditorInput input) throws CoreException { 3895 ISaveablesLifecycleListener listener= (ISaveablesLifecycleListener)getSite().getService(ISaveablesLifecycleListener.class); 3896 if (listener == null) 3897 fSavable= null; 3898 3899 if (input == null) { 3900 close(isSaveOnCloseNeeded()); 3901 3902 if (fSavable != null) { 3903 listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false)); 3904 fSavable.disconnectEditor(); 3905 fSavable= null; 3906 } 3907 3908 } else { 3909 boolean mustSendLifeCycleEvent= false; 3910 if (fSavable != null) { 3911 listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false)); 3912 fSavable.disconnectEditor(); 3913 fSavable= null; 3914 mustSendLifeCycleEvent= true; 3915 } 3916 3917 IEditorInput oldInput= getEditorInput(); 3918 if (oldInput != null) 3919 getDocumentProvider().disconnect(oldInput); 3920 3921 super.setInput(input); 3922 3923 updateDocumentProvider(input); 3924 3925 IDocumentProvider provider= getDocumentProvider(); 3926 if (provider == null) { 3927 IStatus s= new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, EditorMessages.Editor_error_no_provider, null); 3928 throw new CoreException(s); 3929 } 3930 3931 provider.connect(input); 3932 3933 initializeTitle(input); 3934 3935 if (fSourceViewer != null) { 3936 initializeSourceViewer(input); 3937 3938 IAction undoAction= getAction(ITextEditorActionConstants.UNDO); 3940 IAction redoAction= getAction(ITextEditorActionConstants.REDO); 3941 boolean areOperationActionHandlersInstalled= undoAction instanceof OperationHistoryActionHandler && redoAction instanceof OperationHistoryActionHandler; 3942 IUndoContext undoContext= getUndoContext(); 3943 if (undoContext != null && areOperationActionHandlersInstalled) { 3944 ((OperationHistoryActionHandler)undoAction).setContext(undoContext); 3945 ((OperationHistoryActionHandler)redoAction).setContext(undoContext); 3946 } else { 3947 createUndoRedoActions(); 3948 } 3949 } 3950 3951 if (fIsOverwriting) 3952 toggleOverwriteMode(); 3953 setInsertMode((InsertMode) getLegalInsertModes().get(0)); 3954 updateCaret(); 3955 3956 updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE); 3957 3958 if (fSelectionListener != null) 3959 fSelectionListener.setDocument(getDocumentProvider().getDocument(input)); 3960 3961 IVerticalRuler ruler= getVerticalRuler(); 3962 if (ruler instanceof CompositeRuler) 3963 updateContributedRulerColumns((CompositeRuler) ruler); 3964 3965 if (mustSendLifeCycleEvent && listener != null) 3967 listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, getSaveables(), false)); 3968 3969 } 3970 3971 } 3972 3973 3979 private IUndoContext getUndoContext() { 3980 if (fSourceViewer instanceof ITextViewerExtension6) { 3981 IUndoManager undoManager= ((ITextViewerExtension6)fSourceViewer).getUndoManager(); 3982 if (undoManager instanceof IUndoManagerExtension) 3983 return ((IUndoManagerExtension)undoManager).getUndoContext(); 3984 } 3985 return null; 3986 } 3987 3988 3992 protected final void setInputWithNotify(IEditorInput input) { 3993 try { 3994 3995 doSetInput(input); 3996 3997 4003 firePropertyChange(IEditorPart.PROP_INPUT); 4004 4005 } catch (CoreException x) { 4006 String title= EditorMessages.Editor_error_setinput_title; 4007 String msg= EditorMessages.Editor_error_setinput_message; 4008 Shell shell= getSite().getShell(); 4009 ErrorDialog.openError(shell, title, msg, x.getStatus()); 4010 } 4011 } 4012 4013 4016 public final void setInput(IEditorInput input) { 4017 setInputWithNotify(input); 4018 } 4019 4020 4023 public void close(final boolean save) { 4024 4025 enableSanityChecking(false); 4026 4027 Display display= getSite().getShell().getDisplay(); 4028 display.asyncExec(new Runnable () { 4029 public void run() { 4030 if (fSourceViewer != null) 4031 getSite().getPage().closeEditor(AbstractTextEditor.this, save); 4032 } 4033 }); 4034 } 4035 4036 4045 public void dispose() { 4046 4047 if (fActivationListener != null) { 4048 fActivationListener.dispose(); 4049 fActivationListener= null; 4050 } 4051 4052 if (fTitleImage != null) { 4053 fTitleImage.dispose(); 4054 fTitleImage= null; 4055 } 4056 4057 if (fFont != null) { 4058 fFont.dispose(); 4059 fFont= null; 4060 } 4061 4062 disposeNonDefaultCaret(); 4063 fInitialCaret= null; 4064 4065 if (fForegroundColor != null) { 4066 fForegroundColor.dispose(); 4067 fForegroundColor= null; 4068 } 4069 4070 if (fBackgroundColor != null) { 4071 fBackgroundColor.dispose(); 4072 fBackgroundColor= null; 4073 } 4074 4075 if (fSelectionForegroundColor != null) { 4076 fSelectionForegroundColor.dispose(); 4077 fSelectionForegroundColor= null; 4078 } 4079 4080 if (fSelectionBackgroundColor != null) { 4081 fSelectionBackgroundColor.dispose(); 4082 fSelectionBackgroundColor= null; 4083 } 4084 4085 if (fFindScopeHighlightColor != null) { 4086 fFindScopeHighlightColor.dispose(); 4087 fFindScopeHighlightColor= null; 4088 } 4089 4090 if (fFontPropertyChangeListener != null) { 4091 JFaceResources.getFontRegistry().removeListener(fFontPropertyChangeListener); 4092 fFontPropertyChangeListener= null; 4093 } 4094 4095 if (fPropertyChangeListener != null) { 4096 if (fPreferenceStore != null) { 4097 fPreferenceStore.removePropertyChangeListener(fPropertyChangeListener); 4098 fPreferenceStore= null; 4099 } 4100 fPropertyChangeListener= null; 4101 } 4102 4103 if (fActivationCodeTrigger != null) { 4104 fActivationCodeTrigger.uninstall(); 4105 fActivationCodeTrigger= null; 4106 } 4107 4108 if (fSelectionListener != null) { 4109 fSelectionListener.uninstall(getSelectionProvider()); 4110 fSelectionListener= null; 4111 } 4112 4113 if (fSavable != null) { 4114 fSavable.disconnectEditor(); 4115 fSavable= null; 4116 } 4117 4118 disposeDocumentProvider(); 4119 4120 if (fSourceViewer != null) { 4121 4122 if (fTextListener != null) { 4123 fSourceViewer.removeTextListener(fTextListener); 4124 fSourceViewer.removeTextInputListener(fTextListener); 4125 fTextListener= null; 4126 } 4127 4128 uninstallTabsToSpacesConverter(); 4129 4130 fTextInputListener= null; 4131 fSelectionProvider= null; 4132 fSourceViewer= null; 4133 } 4134 4135 if (fTextContextMenu != null) { 4136 fTextContextMenu.dispose(); 4137 fTextContextMenu= null; 4138 } 4139 4140 if (fRulerContextMenu != null) { 4141 fRulerContextMenu.dispose(); 4142 fRulerContextMenu= null; 4143 } 4144 4145 if (fActions != null) { 4146 fActions.clear(); 4147 fActions= null; 4148 } 4149 4150 if (fSelectionActions != null) { 4151 fSelectionActions.clear(); 4152 fSelectionActions= null; 4153 } 4154 4155 if (fContentActions != null) { 4156 fContentActions.clear(); 4157 fContentActions= null; 4158 } 4159 4160 if (fPropertyActions != null) { 4161 fPropertyActions.clear(); 4162 fPropertyActions= null; 4163 } 4164 4165 if (fStateActions != null) { 4166 fStateActions.clear(); 4167 fStateActions= null; 4168 } 4169 4170 if (fActivationCodes != null) { 4171 fActivationCodes.clear(); 4172 fActivationCodes= null; 4173 } 4174 4175 if (fEditorStatusLine != null) 4176 fEditorStatusLine= null; 4177 4178 if (fConfiguration != null) 4179 fConfiguration= null; 4180 4181 if (fColumnSupport != null) { 4182 fColumnSupport.dispose(); 4183 fColumnSupport= null; 4184 } 4185 4186 if (fVerticalRuler != null) 4187 fVerticalRuler= null; 4188 4189 IOperationHistory history= OperationHistoryFactory.getOperationHistory(); 4190 if (history != null) { 4191 if (fNonLocalOperationApprover != null) 4192 history.removeOperationApprover(fNonLocalOperationApprover); 4193 if (fLinearUndoViolationApprover != null) 4194 history.removeOperationApprover(fLinearUndoViolationApprover); 4195 } 4196 fNonLocalOperationApprover= null; 4197 fLinearUndoViolationApprover= null; 4198 4199 super.dispose(); 4200 } 4201 4202 4208 protected void disposeDocumentProvider() { 4209 IDocumentProvider provider= getDocumentProvider(); 4210 if (provider != null) { 4211 4212 IEditorInput input= getEditorInput(); 4213 if (input != null) 4214 provider.disconnect(input); 4215 4216 if (fElementStateListener != null) { 4217 provider.removeElementStateListener(fElementStateListener); 4218 fElementStateListener= null; 4219 } 4220 4221 } 4222 fExplicitDocumentProvider= null; 4223 } 4224 4225 4234 protected boolean affectsTextPresentation(PropertyChangeEvent event) { 4235 return false; 4236 } 4237 4238 4245 private String getSymbolicFontName() { 4246 if (getConfigurationElement() != null) 4247 return getConfigurationElement().getAttribute("symbolicFontName"); return null; 4249 } 4250 4251 4258 protected final String getFontPropertyPreferenceKey() { 4259 String symbolicFontName= getSymbolicFontName(); 4260 if (symbolicFontName != null) 4261 return symbolicFontName; 4262 return JFaceResources.TEXT_FONT; 4263 } 4264 4265 4274 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) { 4275 4276 if (fSourceViewer == null) 4277 return; 4278 4279 String property= event.getProperty(); 4280 4281 if (getFontPropertyPreferenceKey().equals(property)) 4282 return; 4284 4285 if (PREFERENCE_COLOR_FOREGROUND.equals(property) || PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(property) || 4286 PREFERENCE_COLOR_BACKGROUND.equals(property) || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) || 4287 PREFERENCE_COLOR_SELECTION_FOREGROUND.equals(property) || PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT.equals(property) || 4288 PREFERENCE_COLOR_SELECTION_BACKGROUND.equals(property) || PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT.equals(property)) 4289 { 4290 initializeViewerColors(fSourceViewer); 4291 } else if (PREFERENCE_COLOR_FIND_SCOPE.equals(property)) { 4292 initializeFindScopeColor(fSourceViewer); 4293 } else if (PREFERENCE_USE_CUSTOM_CARETS.equals(property)) { 4294 updateCaret(); 4295 } else if (PREFERENCE_WIDE_CARET.equals(property)) { 4296 updateCaret(); 4297 } 4298 4299 if (affectsTextPresentation(event)) 4300 fSourceViewer.invalidateTextPresentation(); 4301 4302 if (PREFERENCE_HYPERLINKS_ENABLED.equals(property)) { 4303 if (fSourceViewer instanceof ITextViewerExtension6) { 4304 IHyperlinkDetector[] detectors= getSourceViewerConfiguration().getHyperlinkDetectors(fSourceViewer); 4305 int stateMask= getSourceViewerConfiguration().getHyperlinkStateMask(fSourceViewer); 4306 ITextViewerExtension6 textViewer6= (ITextViewerExtension6)fSourceViewer; 4307 textViewer6.setHyperlinkDetectors(detectors, stateMask); 4308 } 4309 return; 4310 } 4311 4312 if (PREFERENCE_HYPERLINK_KEY_MODIFIER.equals(property)) { 4313 if (fSourceViewer instanceof ITextViewerExtension6) { 4314 ITextViewerExtension6 textViewer6= (ITextViewerExtension6)fSourceViewer; 4315 IHyperlinkDetector[] detectors= getSourceViewerConfiguration().getHyperlinkDetectors(fSourceViewer); 4316 int stateMask= getSourceViewerConfiguration().getHyperlinkStateMask(fSourceViewer); 4317 textViewer6.setHyperlinkDetectors(detectors, stateMask); 4318 } 4319 return; 4320 } 4321 4322 if (PREFERENCE_RULER_CONTRIBUTIONS.equals(property)) { 4323 String [] difference= StringSetSerializer.getDifference((String ) event.getOldValue(), (String ) event.getNewValue()); 4324 IColumnSupport support= (IColumnSupport) getAdapter(IColumnSupport.class); 4325 for (int i= 0; i < difference.length; i++) { 4326 RulerColumnDescriptor desc= RulerColumnRegistry.getDefault().getColumnDescriptor(difference[i]); 4327 if (desc != null && support.isColumnSupported(desc)) { 4328 boolean newState= !support.isColumnVisible(desc); 4329 support.setColumnVisible(desc, newState); 4330 } 4331 } 4332 return; 4333 } 4334 4335 if (PREFERENCE_SHOW_WHITESPACE_CHARACTERS.equals(property)) { 4336 IAction action= getAction(ITextEditorActionConstants.SHOW_WHITESPACE_CHARACTERS); 4337 if (action instanceof IUpdate) 4338 ((IUpdate)action).update(); 4339 return; 4340 } 4341 4342 if (PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED.equals(property)) { 4343 IPreferenceStore store= getPreferenceStore(); 4344 if (store != null && store.getBoolean(PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED)) 4345 installTextDragAndDrop(getSourceViewer()); 4346 else 4347 uninstallTextDragAndDrop(getSourceViewer()); 4348 return; 4349 } 4350 4351 } 4352 4353 4360 protected IProgressMonitor getProgressMonitor() { 4361 4362 IProgressMonitor pm= null; 4363 4364 IStatusLineManager manager= getStatusLineManager(); 4365 if (manager != null) 4366 pm= manager.getProgressMonitor(); 4367 4368 return pm != null ? pm : new NullProgressMonitor(); 4369 } 4370 4371 4375 protected void handleEditorInputChanged() { 4376 4377 String title; 4378 String msg; 4379 Shell shell= getSite().getShell(); 4380 4381 final IDocumentProvider provider= getDocumentProvider(); 4382 if (provider == null) { 4383 close(false); 4385 return; 4386 } 4387 4388 final IEditorInput input= getEditorInput(); 4389 if (provider.isDeleted(input)) { 4390 4391 if (isSaveAsAllowed()) { 4392 4393 title= EditorMessages.Editor_error_activated_deleted_save_title; 4394 msg= EditorMessages.Editor_error_activated_deleted_save_message; 4395 4396 String [] buttons= { 4397 EditorMessages.Editor_error_activated_deleted_save_button_save, 4398 EditorMessages.Editor_error_activated_deleted_save_button_close, 4399 }; 4400 4401 MessageDialog dialog= new MessageDialog(shell, title, null, msg, MessageDialog.QUESTION, buttons, 0); 4402 4403 if (dialog.open() == 0) { 4404 IProgressMonitor pm= getProgressMonitor(); 4405 performSaveAs(pm); 4406 if (pm.isCanceled()) 4407 handleEditorInputChanged(); 4408 } else { 4409 close(false); 4410 } 4411 4412 } else { 4413 4414 title= EditorMessages.Editor_error_activated_deleted_close_title; 4415 msg= EditorMessages.Editor_error_activated_deleted_close_message; 4416 if (MessageDialog.openConfirm(shell, title, msg)) 4417 close(false); 4418 } 4419 4420 } else { 4421 4422 title= EditorMessages.Editor_error_activated_outofsync_title; 4423 msg= EditorMessages.Editor_error_activated_outofsync_message; 4424 4425 if (MessageDialog.openQuestion(shell, title, msg)) { 4426 4427 4428 try { 4429 if (provider instanceof IDocumentProviderExtension) { 4430 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 4431 extension.synchronize(input); 4432 } else { 4433 doSetInput(input); 4434 } 4435 } catch (CoreException x) { 4436 IStatus status= x.getStatus(); 4437 if (status == null || status.getSeverity() != IStatus.CANCEL) { 4438 title= EditorMessages.Editor_error_refresh_outofsync_title; 4439 msg= EditorMessages.Editor_error_refresh_outofsync_message; 4440 ErrorDialog.openError(shell, title, msg, x.getStatus()); 4441 } 4442 } 4443 } 4444 } 4445 } 4446 4447 4452 public void doSaveAs() { 4453 4458 performSaveAs(getProgressMonitor()); 4459 } 4460 4461 4468 protected void performSaveAs(IProgressMonitor progressMonitor) { 4469 } 4470 4471 4477 public void doSave(IProgressMonitor progressMonitor) { 4478 4479 IDocumentProvider p= getDocumentProvider(); 4480 if (p == null) 4481 return; 4482 4483 if (p.isDeleted(getEditorInput())) { 4484 4485 if (isSaveAsAllowed()) { 4486 4487 4492 performSaveAs(progressMonitor); 4493 4494 } else { 4495 4496 Shell shell= getSite().getShell(); 4497 String title= EditorMessages.Editor_error_save_deleted_title; 4498 String msg= EditorMessages.Editor_error_save_deleted_message; 4499 MessageDialog.openError(shell, title, msg); 4500 } 4501 4502 } else { 4503 updateState(getEditorInput()); 4504 validateState(getEditorInput()); 4505 performSave(false, progressMonitor); 4506 } 4507 } 4508 4509 4514 protected void enableSanityChecking(boolean enable) { 4515 synchronized (this) { 4516 fIsSanityCheckEnabled= enable; 4517 } 4518 } 4519 4520 4525 protected void safelySanityCheckState(IEditorInput input) { 4526 boolean enabled= false; 4527 4528 synchronized (this) { 4529 enabled= fIsSanityCheckEnabled; 4530 } 4531 4532 if (enabled) 4533 sanityCheckState(input); 4534 } 4535 4536 4541 protected void sanityCheckState(IEditorInput input) { 4542 4543 IDocumentProvider p= getDocumentProvider(); 4544 if (p == null) 4545 return; 4546 4547 if (p instanceof IDocumentProviderExtension3) { 4548 4549 IDocumentProviderExtension3 p3= (IDocumentProviderExtension3) p; 4550 4551 long stamp= p.getModificationStamp(input); 4552 if (stamp != fModificationStamp) { 4553 fModificationStamp= stamp; 4554 if (!p3.isSynchronized(input)) 4555 handleEditorInputChanged(); 4556 } 4557 4558 } else { 4559 4560 if (fModificationStamp == -1) 4561 fModificationStamp= p.getSynchronizationStamp(input); 4562 4563 long stamp= p.getModificationStamp(input); 4564 if (stamp != fModificationStamp) { 4565 fModificationStamp= stamp; 4566 if (stamp != p.getSynchronizationStamp(input)) 4567 handleEditorInputChanged(); 4568 } 4569 } 4570 4571 updateState(getEditorInput()); 4572 updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE); 4573 } 4574 4575 4580 protected void enableStateValidation(boolean enable) { 4581 synchronized (this) { 4582 fIsStateValidationEnabled= enable; 4583 } 4584 } 4585 4586 4594 protected void validateState(IEditorInput input) { 4595 4596 IDocumentProvider provider= getDocumentProvider(); 4597 if (! (provider instanceof IDocumentProviderExtension)) 4598 return; 4599 4600 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 4601 4602 try { 4603 4604 extension.validateState(input, getSite().getShell()); 4605 4606 } catch (CoreException x) { 4607 IStatus status= x.getStatus(); 4608 if (status == null || status.getSeverity() != IStatus.CANCEL) { 4609 Bundle bundle= Platform.getBundle(PlatformUI.PLUGIN_ID); 4610 ILog log= Platform.getLog(bundle); 4611 log.log(x.getStatus()); 4612 4613 Shell shell= getSite().getShell(); 4614 String title= EditorMessages.Editor_error_validateEdit_title; 4615 String msg= EditorMessages.Editor_error_validateEdit_message; 4616 ErrorDialog.openError(shell, title, msg, x.getStatus()); 4617 } 4618 return; 4619 } 4620 4621 if (fSourceViewer != null) 4622 fSourceViewer.setEditable(isEditable()); 4623 4624 updateStateDependentActions(); 4625 } 4626 4627 4631 public boolean validateEditorInputState() { 4632 4633 boolean enabled= false; 4634 4635 synchronized (this) { 4636 enabled= fIsStateValidationEnabled; 4637 } 4638 4639 if (enabled) { 4640 4641 ISourceViewer viewer= fSourceViewer; 4642 if (viewer == null) 4643 return false; 4644 4645 fTextInputListener.inputChanged= false; 4646 viewer.addTextInputListener(fTextInputListener); 4647 4648 try { 4649 final IEditorInput input= getEditorInput(); 4650 BusyIndicator.showWhile(getSite().getShell().getDisplay(), new Runnable () { 4651 4654 public void run() { 4655 validateState(input); 4656 } 4657 }); 4658 sanityCheckState(input); 4659 return !isEditorInputReadOnly() && !fTextInputListener.inputChanged; 4660 4661 } finally { 4662 viewer.removeTextInputListener(fTextInputListener); 4663 } 4664 4665 } 4666 4667 return !isEditorInputReadOnly(); 4668 } 4669 4670 4676 protected void updateState(IEditorInput input) { 4677 IDocumentProvider provider= getDocumentProvider(); 4678 if (provider instanceof IDocumentProviderExtension) { 4679 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 4680 try { 4681 4682 boolean wasReadOnly= isEditorInputReadOnly(); 4683 extension.updateStateCache(input); 4684 4685 if (fSourceViewer != null) 4686 fSourceViewer.setEditable(isEditable()); 4687 4688 if (wasReadOnly != isEditorInputReadOnly()) 4689 updateStateDependentActions(); 4690 4691 } catch (CoreException x) { 4692 Bundle bundle= Platform.getBundle(PlatformUI.PLUGIN_ID); 4693 ILog log= Platform.getLog(bundle); 4694 log.log(x.getStatus()); 4695 } 4696 } 4697 } 4698 4699 4706 protected void performSave(boolean overwrite, IProgressMonitor progressMonitor) { 4707 4708 IDocumentProvider provider= getDocumentProvider(); 4709 if (provider == null) 4710 return; 4711 4712 try { 4713 4714 provider.aboutToChange(getEditorInput()); 4715 IEditorInput input= getEditorInput(); 4716 provider.saveDocument(progressMonitor, input, getDocumentProvider().getDocument(input), overwrite); 4717 editorSaved(); 4718 4719 } catch (CoreException x) { 4720 IStatus status= x.getStatus(); 4721 if (status == null || status.getSeverity() != IStatus.CANCEL) 4722 handleExceptionOnSave(x, progressMonitor); 4723 } finally { 4724 provider.changed(getEditorInput()); 4725 } 4726 } 4727 4728 4736 protected void handleExceptionOnSave(CoreException exception, IProgressMonitor progressMonitor) { 4737 4738 try { 4739 ++ fErrorCorrectionOnSave; 4740 4741 boolean isSynchronized= false; 4742 IDocumentProvider p= getDocumentProvider(); 4743 4744 if (p instanceof IDocumentProviderExtension3) { 4745 IDocumentProviderExtension3 p3= (IDocumentProviderExtension3) p; 4746 isSynchronized= p3.isSynchronized(getEditorInput()); 4747 } else { 4748 long modifiedStamp= p.getModificationStamp(getEditorInput()); 4749 long synchStamp= p.getSynchronizationStamp(getEditorInput()); 4750 isSynchronized= (modifiedStamp == synchStamp); 4751 } 4752 4753 if (isNotSynchronizedException(exception) && fErrorCorrectionOnSave == 1 && !isSynchronized) { 4754 String title= EditorMessages.Editor_error_save_outofsync_title; 4755 String msg= NLSUtility.format(EditorMessages.Editor_error_save_outofsync_message, getEditorInput().getToolTipText()); 4756 4757 if (MessageDialog.openQuestion(getSite().getShell(), title, msg)) 4758 performSave(true, progressMonitor); 4759 else { 4760 4765 if (progressMonitor != null) 4766 progressMonitor.setCanceled(true); 4767 } 4768 } else { 4769 String title= EditorMessages.Editor_error_save_title; 4770 String msg= EditorMessages.Editor_error_save_message; 4771 openSaveErrorDialog(title, msg, exception); 4772 4773 4778 if (progressMonitor != null) 4779 progressMonitor.setCanceled(true); 4780 } 4781 } finally { 4782 -- fErrorCorrectionOnSave; 4783 } 4784 } 4785 4786 4798 protected void openSaveErrorDialog(String title, String message, CoreException exception) { 4799 ErrorDialog.openError(getSite().getShell(), title, message, exception.getStatus()); 4800 } 4801 4802 4811 private boolean isNotSynchronizedException(CoreException ex) { 4812 IDocumentProvider provider= getDocumentProvider(); 4813 if (provider instanceof IDocumentProviderExtension5) 4814 return ((IDocumentProviderExtension5)provider).isNotSynchronizedException(getEditorInput(), ex); 4815 return false; 4816 } 4817 4818 4825 public boolean isSaveAsAllowed() { 4826 return false; 4827 } 4828 4829 4832 public boolean isDirty() { 4833 IDocumentProvider p= getDocumentProvider(); 4834 return p == null ? false : p.canSaveDocument(getEditorInput()); 4835 } 4836 4837 4841 public void doRevertToSaved() { 4842 IDocumentProvider p= getDocumentProvider(); 4843 if (p == null) 4844 return; 4845 4846 performRevert(); 4847 } 4848 4849 4857 protected void performRevert() { 4858 4859 IDocumentProvider provider= getDocumentProvider(); 4860 if (provider == null) 4861 return; 4862 4863 try { 4864 4865 provider.aboutToChange(getEditorInput()); 4866 provider.resetDocument(getEditorInput()); 4867 editorSaved(); 4868 4869 } catch (CoreException x) { 4870 IStatus status= x.getStatus(); 4871 if (status == null || status.getSeverity() != IStatus.CANCEL ) { 4872 Shell shell= getSite().getShell(); 4873 String title= EditorMessages.Editor_error_revert_title; 4874 String msg= EditorMessages.Editor_error_revert_message; 4875 ErrorDialog.openError(shell, title, msg, x.getStatus()); 4876 } 4877 } finally { 4878 provider.changed(getEditorInput()); 4879 } 4880 } 4881 4882 4890 protected void handleElementContentReplaced() { 4891 } 4892 4893 4896 public void setAction(String actionID, IAction action) { 4897 Assert.isNotNull(actionID); 4898 if (action == null) { 4899 action= (IAction) fActions.remove(actionID); 4900 if (action != null) 4901 fActivationCodeTrigger.unregisterActionFromKeyActivation(action); 4902 } else { 4903 fActions.put(actionID, action); 4904 fActivationCodeTrigger.registerActionForKeyActivation(action); 4905 } 4906 } 4907 4908 4911 public void setActionActivationCode(String actionID, char activationCharacter, int activationKeyCode, int activationStateMask) { 4912 4913 Assert.isNotNull(actionID); 4914 4915 ActionActivationCode found= findActionActivationCode(actionID); 4916 if (found == null) { 4917 found= new ActionActivationCode(actionID); 4918 fActivationCodes.add(found); 4919 } 4920 4921 found.fCharacter= activationCharacter; 4922 found.fKeyCode= activationKeyCode; 4923 found.fStateMask= activationStateMask; 4924 } 4925 4926 4932 private ActionActivationCode findActionActivationCode(String actionID) { 4933 int size= fActivationCodes.size(); 4934 for (int i= 0; i < size; i++) { 4935 ActionActivationCode code= (ActionActivationCode) fActivationCodes.get(i); 4936 if (actionID.equals(code.fActionId)) 4937 return code; 4938 } 4939 return null; 4940 } 4941 4942 4945 public void removeActionActivationCode(String actionID) { 4946 Assert.isNotNull(actionID); 4947 ActionActivationCode code= findActionActivationCode(actionID); 4948 if (code != null) 4949 fActivationCodes.remove(code); 4950 } 4951 4952 4955 public IAction getAction(String actionID) { 4956 Assert.isNotNull(actionID); 4957 IAction action= (IAction) fActions.get(actionID); 4958 4959 if (action == null) { 4960 action= findContributedAction(actionID); 4961 if (action != null) 4962 setAction(actionID, action); 4963 } 4964 4965 return action; 4966 } 4967 4968 4976 private IAction findContributedAction(String actionID) { 4977 List actions= new ArrayList (); 4978 IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(PlatformUI.PLUGIN_ID, "editorActions"); for (int i= 0; i < elements.length; i++) { 4980 IConfigurationElement element= elements[i]; 4981 if (TAG_CONTRIBUTION_TYPE.equals(element.getName())) { 4982 if (!getSite().getId().equals(element.getAttribute("targetID"))) continue; 4984 4985 IConfigurationElement[] children= element.getChildren("action"); for (int j= 0; j < children.length; j++) { 4987 IConfigurationElement child= children[j]; 4988 if (actionID.equals(child.getAttribute("actionID"))) actions.add(child); 4990 } 4991 } 4992 } 4993 int actionSize= actions.size(); 4994 if (actionSize > 0) { 4995 IConfigurationElement element; 4996 if (actionSize > 1) { 4997 IConfigurationElement[] actionArray= (IConfigurationElement[])actions.toArray(new IConfigurationElement[actionSize]); 4998 ConfigurationElementSorter sorter= new ConfigurationElementSorter() { 4999 5002 public IConfigurationElement getConfigurationElement(Object object) { 5003 return (IConfigurationElement)object; 5004 } 5005 }; 5006 sorter.sort(actionArray); 5007 element= actionArray[0]; 5008 } else 5009 element= (IConfigurationElement)actions.get(0); 5010 5011 try { 5012 return new ContributedAction(getSite(), element); 5013 } catch (CommandNotMappedException e) { 5014 } 5016 } 5017 5018 return null; 5019 } 5020 5021 5027 private void updateAction(String actionId) { 5028 Assert.isNotNull(actionId); 5029 if (fActions != null) { 5030 IAction action= (IAction) fActions.get(actionId); 5031 if (action instanceof IUpdate) 5032 ((IUpdate) action).update(); 5033 } 5034 } 5035 5036 5042 public void markAsSelectionDependentAction(String actionId, boolean mark) { 5043 Assert.isNotNull(actionId); 5044 if (mark) { 5045 if (!fSelectionActions.contains(actionId)) 5046 fSelectionActions.add(actionId); 5047 } else 5048 fSelectionActions.remove(actionId); 5049 } 5050 5051 5057 public void markAsContentDependentAction(String actionId, boolean mark) { 5058 Assert.isNotNull(actionId); 5059 if (mark) { 5060 if (!fContentActions.contains(actionId)) 5061 fContentActions.add(actionId); 5062 } else 5063 fContentActions.remove(actionId); 5064 } 5065 5066 5073 public void markAsPropertyDependentAction(String actionId, boolean mark) { 5074 Assert.isNotNull(actionId); 5075 if (mark) { 5076 if (!fPropertyActions.contains(actionId)) 5077 fPropertyActions.add(actionId); 5078 } else 5079 fPropertyActions.remove(actionId); 5080 } 5081 5082 5089 public void markAsStateDependentAction(String actionId, boolean mark) { 5090 Assert.isNotNull(actionId); 5091 if (mark) { 5092 if (!fStateActions.contains(actionId)) 5093 fStateActions.add(actionId); 5094 } else 5095 fStateActions.remove(actionId); 5096 } 5097 5098 5101 protected void updateSelectionDependentActions() { 5102 if (fSelectionActions != null) { 5103 Iterator e= fSelectionActions.iterator(); 5104 while (e.hasNext()) 5105 updateAction((String ) e.next()); 5106 } 5107 } 5108 5109 5112 protected void updateContentDependentActions() { 5113 if (fContentActions != null) { 5114 Iterator e= fContentActions.iterator(); 5115 while (e.hasNext()) 5116 updateAction((String ) e.next()); 5117 } 5118 } 5119 5120 5124 protected void updatePropertyDependentActions() { 5125 if (fPropertyActions != null) { 5126 Iterator e= fPropertyActions.iterator(); 5127 while (e.hasNext()) 5128 updateAction((String ) e.next()); 5129 } 5130 } 5131 5132 5136 protected void updateStateDependentActions() { 5137 if (fStateActions != null) { 5138 Iterator e= fStateActions.iterator(); 5139 while (e.hasNext()) 5140 updateAction((String ) e.next()); 5141 } 5142 } 5143 5144 5153 protected void createNavigationActions() { 5154 5155 IAction action; 5156 5157 StyledText textWidget= fSourceViewer.getTextWidget(); 5158 for (int i= 0; i < ACTION_MAP.length; i++) { 5159 IdMapEntry entry= ACTION_MAP[i]; 5160 action= new TextNavigationAction(textWidget, entry.getAction()); 5161 action.setActionDefinitionId(entry.getActionId()); 5162 setAction(entry.getActionId(), action); 5163 } 5164 5165 action= new ToggleOverwriteModeAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ToggleOverwriteMode."); action.setActionDefinitionId(ITextEditorActionDefinitionIds.TOGGLE_OVERWRITE); 5167 setAction(ITextEditorActionDefinitionIds.TOGGLE_OVERWRITE, action); 5168 textWidget.setKeyBinding(SWT.INSERT, SWT.NULL); 5169 5170 action= new ScrollLinesAction(-1); 5171 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SCROLL_LINE_UP); 5172 setAction(ITextEditorActionDefinitionIds.SCROLL_LINE_UP, action); 5173 5174 action= new ScrollLinesAction(1); 5175 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SCROLL_LINE_DOWN); 5176 setAction(ITextEditorActionDefinitionIds.SCROLL_LINE_DOWN, action); 5177 5178 action= new LineEndAction(textWidget, false); 5179 action.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_END); 5180 setAction(ITextEditorActionDefinitionIds.LINE_END, action); 5181 5182 action= new LineStartAction(textWidget, false); 5183 action.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_START); 5184 setAction(ITextEditorActionDefinitionIds.LINE_START, action); 5185 5186 action= new LineEndAction(textWidget, true); 5187 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SELECT_LINE_END); 5188 setAction(ITextEditorActionDefinitionIds.SELECT_LINE_END, action); 5189 5190 action= new LineStartAction(textWidget, true); 5191 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SELECT_LINE_START); 5192 setAction(ITextEditorActionDefinitionIds.SELECT_LINE_START, action); 5193 5194 textWidget.setKeyBinding(SWT.DEL, SWT.NULL); 5197 } 5198 5199 5203 private void createAccessibilityActions() { 5204 IAction action= new ShowRulerContextMenuAction(); 5205 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_RULER_CONTEXT_MENU); 5206 setAction(ITextEditorActionDefinitionIds.SHOW_RULER_CONTEXT_MENU, action); 5207 } 5208 5209 5216 protected void createUndoRedoActions() { 5217 IUndoContext undoContext= getUndoContext(); 5218 if (undoContext != null) { 5219 5221 OperationHistoryActionHandler undoAction= new UndoActionHandler(getEditorSite(), undoContext); 5223 PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION); 5224 undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO); 5225 registerUndoRedoAction(ITextEditorActionConstants.UNDO, undoAction); 5226 5227 OperationHistoryActionHandler redoAction= new RedoActionHandler(getEditorSite(), undoContext); 5229 PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION); 5230 redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO); 5231 registerUndoRedoAction(ITextEditorActionConstants.REDO, redoAction); 5232 5233 IOperationHistory history= OperationHistoryFactory.getOperationHistory(); 5235 5236 if (fNonLocalOperationApprover != null) 5238 history.removeOperationApprover(fNonLocalOperationApprover); 5239 fNonLocalOperationApprover= getUndoRedoOperationApprover(undoContext); 5240 history.addOperationApprover(fNonLocalOperationApprover); 5241 5242 if (fLinearUndoViolationApprover != null) 5245 history.removeOperationApprover(fLinearUndoViolationApprover); 5246 fLinearUndoViolationApprover= new LinearUndoViolationUserApprover(undoContext, this); 5247 history.addOperationApprover(fLinearUndoViolationApprover); 5248 5249 } else { 5250 5252 ResourceAction action; 5253 5254 if (getAction(ITextEditorActionConstants.UNDO) == null) { 5255 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Undo.", this, ITextOperationTarget.UNDO); action.setHelpContextId(IAbstractTextEditorHelpContextIds.UNDO_ACTION); 5257 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO); 5258 setAction(ITextEditorActionConstants.UNDO, action); 5259 } 5260 5261 if (getAction(ITextEditorActionConstants.REDO) == null) { 5262 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Redo.", this, ITextOperationTarget.REDO); action.setHelpContextId(IAbstractTextEditorHelpContextIds.REDO_ACTION); 5264 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO); 5265 setAction(ITextEditorActionConstants.REDO, action); 5266 } 5267 } 5268 } 5269 5270 5280 private void registerUndoRedoAction(String actionId, OperationHistoryActionHandler action) { 5281 IAction oldAction= getAction(actionId); 5282 if (oldAction instanceof OperationHistoryActionHandler) 5283 ((OperationHistoryActionHandler)oldAction).dispose(); 5284 5285 setAction(actionId, action); 5286 5287 IActionBars actionBars= getEditorSite().getActionBars(); 5288 if (actionBars != null) 5289 actionBars.setGlobalActionHandler(actionId, action); 5290 } 5291 5292 5305 protected IOperationApprover getUndoRedoOperationApprover(IUndoContext undoContext) { 5306 return new NonLocalUndoUserApprover(undoContext, this, new Object [] { getEditorInput() }, Object .class); 5307 } 5308 5309 5315 protected void createActions() { 5316 5317 ResourceAction action; 5318 5319 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Cut.", this, ITextOperationTarget.CUT); action.setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_ACTION); 5321 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.CUT); 5322 setAction(ITextEditorActionConstants.CUT, action); 5323 5324 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Copy.", this, ITextOperationTarget.COPY, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.COPY_ACTION); 5326 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY); 5327 setAction(ITextEditorActionConstants.COPY, action); 5328 5329 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Paste.", this, ITextOperationTarget.PASTE); action.setHelpContextId(IAbstractTextEditorHelpContextIds.PASTE_ACTION); 5331 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE); 5332 setAction(ITextEditorActionConstants.PASTE, action); 5333 5334 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Delete.", this, ITextOperationTarget.DELETE); action.setHelpContextId(IAbstractTextEditorHelpContextIds.DELETE_ACTION); 5336 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.DELETE); 5337 setAction(ITextEditorActionConstants.DELETE, action); 5338 5339 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.DeleteLine.", this, DeleteLineAction.WHOLE, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.DELETE_LINE_ACTION); 5341 action.setActionDefinitionId(ITextEditorActionDefinitionIds.DELETE_LINE); 5342 setAction(ITextEditorActionConstants.DELETE_LINE, action); 5343 5344 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CutLine.", this, DeleteLineAction.WHOLE, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_LINE_ACTION); 5346 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CUT_LINE); 5347 setAction(ITextEditorActionConstants.CUT_LINE, action); 5348 5349 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.DeleteLineToBeginning.", this, DeleteLineAction.TO_BEGINNING, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.DELETE_LINE_TO_BEGINNING_ACTION); 5351 action.setActionDefinitionId(ITextEditorActionDefinitionIds.DELETE_LINE_TO_BEGINNING); 5352 setAction(ITextEditorActionConstants.DELETE_LINE_TO_BEGINNING, action); 5353 5354 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CutLineToBeginning.", this, DeleteLineAction.TO_BEGINNING, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_LINE_TO_BEGINNING_ACTION); 5356 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CUT_LINE_TO_BEGINNING); 5357 setAction(ITextEditorActionConstants.CUT_LINE_TO_BEGINNING, action); 5358 5359 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.DeleteLineToEnd.", this, DeleteLineAction.TO_END, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.DELETE_LINE_TO_END_ACTION); 5361 action.setActionDefinitionId(ITextEditorActionDefinitionIds.DELETE_LINE_TO_END); 5362 setAction(ITextEditorActionConstants.DELETE_LINE_TO_END, action); 5363 5364 action= new DeleteLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CutLineToEnd.", this, DeleteLineAction.TO_END, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_LINE_TO_END_ACTION); 5366 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CUT_LINE_TO_END); 5367 setAction(ITextEditorActionConstants.CUT_LINE_TO_END, action); 5368 5369 action= new JoinLinesAction(EditorMessages.getBundleForConstructedKeys(), "Editor.JoinLines.", this, " "); action.setHelpContextId(IAbstractTextEditorHelpContextIds.JOIN_LINES_ACTION); 5371 action.setActionDefinitionId(ITextEditorActionDefinitionIds.JOIN_LINES); 5372 setAction(ITextEditorActionConstants.JOIN_LINES, action); 5373 5374 action= new MarkAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SetMark.", this, MarkAction.SET_MARK); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SET_MARK_ACTION); 5376 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SET_MARK); 5377 setAction(ITextEditorActionConstants.SET_MARK, action); 5378 5379 action= new MarkAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ClearMark.", this, MarkAction.CLEAR_MARK); action.setHelpContextId(IAbstractTextEditorHelpContextIds.CLEAR_MARK_ACTION); 5381 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CLEAR_MARK); 5382 setAction(ITextEditorActionConstants.CLEAR_MARK, action); 5383 5384 action= new MarkAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SwapMark.", this, MarkAction.SWAP_MARK); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SWAP_MARK_ACTION); 5386 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SWAP_MARK); 5387 setAction(ITextEditorActionConstants.SWAP_MARK, action); 5388 5389 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SelectAll.", this, ITextOperationTarget.SELECT_ALL, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SELECT_ALL_ACTION); 5391 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.SELECT_ALL); 5392 setAction(ITextEditorActionConstants.SELECT_ALL, action); 5393 5394 action= new ShiftAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShiftRight.", this, ITextOperationTarget.SHIFT_RIGHT); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SHIFT_RIGHT_ACTION); 5396 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SHIFT_RIGHT); 5397 setAction(ITextEditorActionConstants.SHIFT_RIGHT, action); 5398 5399 action= new ShiftAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShiftRight.", this, ITextOperationTarget.SHIFT_RIGHT) { public void update() { 5401 updateForTab(); 5402 } 5403 }; 5404 setAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, action); 5405 5406 action= new ShiftAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShiftLeft.", this, ITextOperationTarget.SHIFT_LEFT); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SHIFT_LEFT_ACTION); 5408 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SHIFT_LEFT); 5409 setAction(ITextEditorActionConstants.SHIFT_LEFT, action); 5410 5411 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Print.", this, ITextOperationTarget.PRINT, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.PRINT_ACTION); 5413 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.PRINT); 5414 setAction(ITextEditorActionConstants.PRINT, action); 5415 5416 action= new FindReplaceAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindReplace.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_ACTION); 5418 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE); 5419 setAction(ITextEditorActionConstants.FIND, action); 5420 5421 action= new FindNextAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindNext.", this, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_NEXT_ACTION); 5423 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_NEXT); 5424 setAction(ITextEditorActionConstants.FIND_NEXT, action); 5425 5426 action= new FindNextAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindPrevious.", this, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_PREVIOUS_ACTION); 5428 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_PREVIOUS); 5429 setAction(ITextEditorActionConstants.FIND_PREVIOUS, action); 5430 5431 action= new IncrementalFindAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncremental.", this, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_INCREMENTAL_ACTION); 5433 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL); 5434 setAction(ITextEditorActionConstants.FIND_INCREMENTAL, action); 5435 5436 action= new IncrementalFindAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncrementalReverse.", this, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_INCREMENTAL_REVERSE_ACTION); 5438 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL_REVERSE); 5439 setAction(ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE, action); 5440 5441 action= new SaveAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Save.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SAVE_ACTION); 5443 5447 setAction(ITextEditorActionConstants.SAVE, action); 5449 5450 action= new RevertToSavedAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Revert.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.REVERT_TO_SAVED_ACTION); 5452 action.setActionDefinitionId(IWorkbenchActionDefinitionIds.REVERT_TO_SAVED); 5453 setAction(ITextEditorActionConstants.REVERT_TO_SAVED, action); 5454 5455 action= new GotoLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.GotoLine.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.GOTO_LINE_ACTION); 5457 action.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_GOTO); 5458 setAction(ITextEditorActionConstants.GOTO_LINE, action); 5459 5460 action= new MoveLinesAction(EditorMessages.getBundleForConstructedKeys(), "Editor.MoveLinesUp.", this, true, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.MOVE_LINES_ACTION); 5462 action.setActionDefinitionId(ITextEditorActionDefinitionIds.MOVE_LINES_UP); 5463 setAction(ITextEditorActionConstants.MOVE_LINE_UP, action); 5464 5465 action= new MoveLinesAction(EditorMessages.getBundleForConstructedKeys(), "Editor.MoveLinesDown.", this, false, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.MOVE_LINES_ACTION); 5467 action.setActionDefinitionId(ITextEditorActionDefinitionIds.MOVE_LINES_DOWN); 5468 setAction(ITextEditorActionConstants.MOVE_LINE_DOWN, action); 5469 5470 action= new MoveLinesAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CopyLineUp.", this, true, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.COPY_LINES_ACTION); 5472 action.setActionDefinitionId(ITextEditorActionDefinitionIds.COPY_LINES_UP); 5473 setAction(ITextEditorActionConstants.COPY_LINE_UP, action); 5474 5475 action= new MoveLinesAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CopyLineDown.", this, false, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.COPY_LINES_ACTION); 5477 action.setActionDefinitionId(ITextEditorActionDefinitionIds.COPY_LINES_DOWN); 5478 setAction(ITextEditorActionConstants.COPY_LINE_DOWN, action); 5479 5480 action= new CaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.UpperCase.", this, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.UPPER_CASE_ACTION); 5482 action.setActionDefinitionId(ITextEditorActionDefinitionIds.UPPER_CASE); 5483 setAction(ITextEditorActionConstants.UPPER_CASE, action); 5484 5485 action= new CaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.LowerCase.", this, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.LOWER_CASE_ACTION); 5487 action.setActionDefinitionId(ITextEditorActionDefinitionIds.LOWER_CASE); 5488 setAction(ITextEditorActionConstants.LOWER_CASE, action); 5489 5490 action= new InsertLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SmartEnter.", this, false); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SMART_ENTER_ACTION); 5492 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SMART_ENTER); 5493 setAction(ITextEditorActionConstants.SMART_ENTER, action); 5494 5495 action= new InsertLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SmartEnterInverse.", this, true); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SMART_ENTER_ACTION); 5497 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SMART_ENTER_INVERSE); 5498 setAction(ITextEditorActionConstants.SMART_ENTER_INVERSE, action); 5499 5500 action= new ToggleInsertModeAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ToggleInsertMode."); action.setHelpContextId(IAbstractTextEditorHelpContextIds.TOGGLE_INSERT_MODE_ACTION); 5502 action.setActionDefinitionId(ITextEditorActionDefinitionIds.TOGGLE_INSERT_MODE); 5503 setAction(ITextEditorActionConstants.TOGGLE_INSERT_MODE, action); 5504 5505 action= new HippieCompleteAction(EditorMessages.getBundleForConstructedKeys(), "Editor.HippieCompletion.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.HIPPIE_COMPLETION_ACTION); 5507 action.setActionDefinitionId(ITextEditorActionDefinitionIds.HIPPIE_COMPLETION); 5508 setAction(ITextEditorActionConstants.HIPPIE_COMPLETION, action); 5509 5510 action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.QuickAssist.", this, ISourceViewer.QUICK_ASSIST); action.setHelpContextId(IAbstractTextEditorHelpContextIds.QUICK_ASSIST_ACTION); 5512 action.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST); 5513 setAction(ITextEditorActionConstants.QUICK_ASSIST, action); 5514 markAsStateDependentAction(ITextEditorActionConstants.QUICK_ASSIST, true); 5515 5516 action= new GotoAnnotationAction(this, true); 5517 setAction(ITextEditorActionConstants.NEXT, action); 5518 action= new GotoAnnotationAction(this, false); 5519 setAction(ITextEditorActionConstants.PREVIOUS, action); 5520 5521 action= new RecenterAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Recenter.", this); action.setHelpContextId(IAbstractTextEditorHelpContextIds.RECENTER_ACTION); 5523 action.setActionDefinitionId(ITextEditorActionDefinitionIds.RECENTER); 5524 setAction(ITextEditorActionConstants.RECENTER, action); 5525 5526 action= new ShowWhitespaceCharactersAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShowWhitespaceCharacters.", this, getPreferenceStore()); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SHOW_WHITESPACE_CHARACTERS_ACTION); 5528 action.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_WHITESPACE_CHARACTERS); 5529 setAction(ITextEditorActionConstants.SHOW_WHITESPACE_CHARACTERS, action); 5530 5531 ResourceAction resAction= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShowInformation.", this, ISourceViewer.INFORMATION, true); resAction= new InformationDispatchAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ShowInformation.", (TextOperationAction) resAction); action.setHelpContextId(IAbstractTextEditorHelpContextIds.SHOW_INFORMATION_ACTION); 5534 resAction.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_INFORMATION); 5535 setAction(ITextEditorActionConstants.SHOW_INFORMATION, resAction); 5536 5537 5538 PropertyDialogAction openProperties= new PropertyDialogAction( 5539 new IShellProvider() { 5540 public Shell getShell() { 5541 return getSite().getShell(); 5542 } 5543 }, 5544 new ISelectionProvider() { 5545 public void addSelectionChangedListener(ISelectionChangedListener listener) { 5546 } 5547 public ISelection getSelection() { 5548 return new StructuredSelection(getEditorInput()); 5549 } 5550 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 5551 } 5552 public void setSelection(ISelection selection) { 5553 } 5554 }); 5555 openProperties.setActionDefinitionId(IWorkbenchActionDefinitionIds.PROPERTIES); 5556 setAction(ITextEditorActionConstants.PROPERTIES, openProperties); 5557 5558 markAsContentDependentAction(ITextEditorActionConstants.UNDO, true); 5559 markAsContentDependentAction(ITextEditorActionConstants.REDO, true); 5560 markAsContentDependentAction(ITextEditorActionConstants.FIND, true); 5561 markAsContentDependentAction(ITextEditorActionConstants.FIND_NEXT, true); 5562 markAsContentDependentAction(ITextEditorActionConstants.FIND_PREVIOUS, true); 5563 markAsContentDependentAction(ITextEditorActionConstants.FIND_INCREMENTAL, true); 5564 markAsContentDependentAction(ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE, true); 5565 5566 markAsSelectionDependentAction(ITextEditorActionConstants.CUT, true); 5567 markAsSelectionDependentAction(ITextEditorActionConstants.COPY, true); 5568 markAsSelectionDependentAction(ITextEditorActionConstants.PASTE, true); 5569 markAsSelectionDependentAction(ITextEditorActionConstants.DELETE, true); 5570 markAsSelectionDependentAction(ITextEditorActionConstants.SHIFT_RIGHT, true); 5571 markAsSelectionDependentAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, true); 5572 markAsSelectionDependentAction(ITextEditorActionConstants.UPPER_CASE, true); 5573 markAsSelectionDependentAction(ITextEditorActionConstants.LOWER_CASE, true); 5574 5575 markAsPropertyDependentAction(ITextEditorActionConstants.UNDO, true); 5576 markAsPropertyDependentAction(ITextEditorActionConstants.REDO, true); 5577 markAsPropertyDependentAction(ITextEditorActionConstants.REVERT_TO_SAVED, true); 5578 5579 markAsStateDependentAction(ITextEditorActionConstants.UNDO, true); 5580 markAsStateDependentAction(ITextEditorActionConstants.REDO, true); 5581 markAsStateDependentAction(ITextEditorActionConstants.CUT, true); 5582 markAsStateDependentAction(ITextEditorActionConstants.PASTE, true); 5583 markAsStateDependentAction(ITextEditorActionConstants.DELETE, true); 5584 markAsStateDependentAction(ITextEditorActionConstants.SHIFT_RIGHT, true); 5585 markAsStateDependentAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, true); 5586 markAsStateDependentAction(ITextEditorActionConstants.SHIFT_LEFT, true); 5587 markAsStateDependentAction(ITextEditorActionConstants.FIND, true); 5588 markAsStateDependentAction(ITextEditorActionConstants.DELETE_LINE, true); 5589 markAsStateDependentAction(ITextEditorActionConstants.DELETE_LINE_TO_BEGINNING, true); 5590 markAsStateDependentAction(ITextEditorActionConstants.DELETE_LINE_TO_END, true); 5591 markAsStateDependentAction(ITextEditorActionConstants.MOVE_LINE_UP, true); 5592 markAsStateDependentAction(ITextEditorActionConstants.MOVE_LINE_DOWN, true); 5593 markAsStateDependentAction(ITextEditorActionConstants.CUT_LINE, true); 5594 markAsStateDependentAction(ITextEditorActionConstants.CUT_LINE_TO_BEGINNING, true); 5595 markAsStateDependentAction(ITextEditorActionConstants.CUT_LINE_TO_END, true); 5596 5597 setActionActivationCode(ITextEditorActionConstants.SHIFT_RIGHT_TAB,'\t', -1, SWT.NONE); 5598 setActionActivationCode(ITextEditorActionConstants.SHIFT_LEFT, '\t', -1, SWT.SHIFT); 5599 } 5600 5601 5606 protected final void addAction(IMenuManager menu, String actionId) { 5607 IAction action= getAction(actionId); 5608 if (action != null) { 5609 if (action instanceof IUpdate) 5610 ((IUpdate) action).update(); 5611 menu.add(action); 5612 } 5613 } 5614 5615 5621 protected final void addAction(IMenuManager menu, String group, String actionId) { 5622 IAction action= getAction(actionId); 5623 if (action != null) { 5624 if (action instanceof IUpdate) 5625 ((IUpdate) action).update(); 5626 5627 IMenuManager subMenu= menu.findMenuUsingPath(group); 5628 if (subMenu != null) 5629 subMenu.add(action); 5630 else 5631 menu.appendToGroup(group, action); 5632 } 5633 } 5634 5635 5641 protected final void addGroup(IMenuManager menu, String existingGroup, String newGroup) { 5642 IMenuManager subMenu= menu.findMenuUsingPath(existingGroup); 5643 if (subMenu != null) 5644 subMenu.add(new Separator(newGroup)); 5645 else 5646 menu.appendToGroup(existingGroup, new Separator(newGroup)); 5647 } 5648 5649 5656 protected void rulerContextMenuAboutToShow(IMenuManager menu) { 5657 5658 menu.add(new Separator(ITextEditorActionConstants.GROUP_REST)); 5659 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 5660 5661 for (Iterator i= fRulerContextMenuListeners.iterator(); i.hasNext();) 5662 ((IMenuListener) i.next()).menuAboutToShow(menu); 5663 5664 addAction(menu, ITextEditorActionConstants.RULER_MANAGE_BOOKMARKS); 5665 addAction(menu, ITextEditorActionConstants.RULER_MANAGE_TASKS); 5666 } 5667 5668 5675 protected void editorContextMenuAboutToShow(IMenuManager menu) { 5676 5677 menu.add(new Separator(ITextEditorActionConstants.GROUP_UNDO)); 5678 menu.add(new GroupMarker(ITextEditorActionConstants.GROUP_SAVE)); 5679 menu.add(new Separator(ITextEditorActionConstants.GROUP_COPY)); 5680 menu.add(new Separator(ITextEditorActionConstants.GROUP_PRINT)); 5681 menu.add(new Separator(ITextEditorActionConstants.GROUP_EDIT)); 5682 menu.add(new Separator(ITextEditorActionConstants.GROUP_FIND)); 5683 menu.add(new Separator(IWorkbenchActionConstants.GROUP_ADD)); 5684 menu.add(new Separator(ITextEditorActionConstants.GROUP_REST)); 5685 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 5686 5687 if (isEditable()) { 5688 addAction(menu, ITextEditorActionConstants.GROUP_UNDO, ITextEditorActionConstants.UNDO); 5689 addAction(menu, ITextEditorActionConstants.GROUP_UNDO, ITextEditorActionConstants.REVERT_TO_SAVED); 5690 addAction(menu, ITextEditorActionConstants.GROUP_SAVE, ITextEditorActionConstants.SAVE); 5691 addAction(menu, ITextEditorActionConstants.GROUP_COPY, ITextEditorActionConstants.CUT); 5692 addAction(menu, ITextEditorActionConstants.GROUP_COPY, ITextEditorActionConstants.COPY); 5693 addAction(menu, ITextEditorActionConstants.GROUP_COPY, ITextEditorActionConstants.PASTE); 5694 } else { 5695 addAction(menu, ITextEditorActionConstants.GROUP_COPY, ITextEditorActionConstants.COPY); 5696 } 5697 } 5698 5699 5705 protected IStatusLineManager getStatusLineManager() { 5706 return getEditorSite().getActionBars().getStatusLineManager(); 5707 } 5708 5709 5712 public Object getAdapter(Class required) { 5713 5714 if (IEditorStatusLine.class.equals(required)) { 5715 if (fEditorStatusLine == null) { 5716 IStatusLineManager statusLineManager= getStatusLineManager(); 5717 ISelectionProvider selectionProvider= getSelectionProvider(); 5718 if (statusLineManager != null && selectionProvider != null) 5719 fEditorStatusLine= new EditorStatusLine(statusLineManager, selectionProvider); 5720 } 5721 return fEditorStatusLine; 5722 } 5723 5724 if (IVerticalRulerInfo.class.equals(required)) { 5725 if (fVerticalRuler != null) 5726 return fVerticalRuler; 5727 } 5728 5729 if (IMarkRegionTarget.class.equals(required)) { 5730 if (fMarkRegionTarget == null) { 5731 IStatusLineManager manager= getStatusLineManager(); 5732 if (manager != null) 5733 fMarkRegionTarget= (fSourceViewer == null ? null : new MarkRegionTarget(fSourceViewer, manager)); 5734 } 5735 return fMarkRegionTarget; 5736 } 5737 5738 if (DeleteLineTarget.class.equals(required)){ 5739 if (fDeleteLineTarget == null) { 5740 fDeleteLineTarget= new DeleteLineTarget(fSourceViewer); 5741 } 5742 return fDeleteLineTarget; 5743 } 5744 5745 if (IncrementalFindTarget.class.equals(required)) { 5746 if (fIncrementalFindTarget == null) { 5747 IStatusLineManager manager= getStatusLineManager(); 5748 if (manager != null) 5749 fIncrementalFindTarget= (fSourceViewer == null ? null : new IncrementalFindTarget(fSourceViewer, manager)); 5750 } 5751 return fIncrementalFindTarget; 5752 } 5753 5754 if (IFindReplaceTarget.class.equals(required)) { 5755 if (fFindReplaceTarget == null) { 5756 IFindReplaceTarget target= (fSourceViewer == null ? null : fSourceViewer.getFindReplaceTarget()); 5757 if (target != null) { 5758 fFindReplaceTarget= new FindReplaceTarget(this, target); 5759 if (fFindScopeHighlightColor != null) 5760 fFindReplaceTarget.setScopeHighlightColor(fFindScopeHighlightColor); 5761 } 5762 } 5763 return fFindReplaceTarget; 5764 } 5765 5766 if (ITextOperationTarget.class.equals(required)) 5767 return (fSourceViewer == null ? null : fSourceViewer.getTextOperationTarget()); 5768 5769 if (IRewriteTarget.class.equals(required)) { 5770 if (fSourceViewer instanceof ITextViewerExtension) { 5771 ITextViewerExtension extension= (ITextViewerExtension) fSourceViewer; 5772 return extension.getRewriteTarget(); 5773 } 5774 return null; 5775 } 5776 5777 if (Control.class.equals(required)) 5778 return fSourceViewer != null ? fSourceViewer.getTextWidget() : null; 5779 5780 if (IColumnSupport.class.equals(required)) { 5781 if (fColumnSupport == null) 5782 fColumnSupport= createColumnSupport(); 5783 return fColumnSupport; 5784 } 5785 5786 return super.getAdapter(required); 5787 } 5788 5789 5792 public void setFocus() { 5793 if (fSourceViewer != null && fSourceViewer.getTextWidget() != null) 5794 fSourceViewer.getTextWidget().setFocus(); 5795 } 5796 5797 5800 public boolean showsHighlightRangeOnly() { 5801 return fShowHighlightRangeOnly; 5802 } 5803 5804 5807 public void showHighlightRangeOnly(boolean showHighlightRangeOnly) { 5808 fShowHighlightRangeOnly= showHighlightRangeOnly; 5809 } 5810 5811 5814 public void setHighlightRange(int offset, int length, boolean moveCursor) { 5815 if (fSourceViewer == null) 5816 return; 5817 5818 if (fShowHighlightRangeOnly) { 5819 if (moveCursor) 5820 fSourceViewer.setVisibleRegion(offset, length); 5821 } else { 5822 IRegion rangeIndication= fSourceViewer.getRangeIndication(); 5823 if (rangeIndication == null || offset != rangeIndication.getOffset() || length != rangeIndication.getLength()) 5824 fSourceViewer.setRangeIndication(offset, length, moveCursor); 5825 } 5826 } 5827 5828 5831 public IRegion getHighlightRange() { 5832 if (fSourceViewer == null) 5833 return null; 5834 5835 if (fShowHighlightRangeOnly) 5836 return getCoverage(fSourceViewer); 5837 5838 return fSourceViewer.getRangeIndication(); 5839 } 5840 5841 5844 public void resetHighlightRange() { 5845 if (fSourceViewer == null) 5846 return; 5847 5848 if (fShowHighlightRangeOnly) 5849 fSourceViewer.resetVisibleRegion(); 5850 else 5851 fSourceViewer.removeRangeIndication(); 5852 } 5853 5854 5863 protected void adjustHighlightRange(int offset, int length) { 5864 if (fSourceViewer == null) 5865 return; 5866 5867 if (fSourceViewer instanceof ITextViewerExtension5) { 5868 ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer; 5869 extension.exposeModelRange(new Region(offset, length)); 5870 } else if (!isVisible(fSourceViewer, offset, length)) { 5871 fSourceViewer.resetVisibleRegion(); 5872 } 5873 } 5874 5875 5878 public void selectAndReveal(int start, int length) { 5879 selectAndReveal(start, length, start, length); 5880 } 5881 5882 5891 protected void selectAndReveal(int selectionStart, int selectionLength, int revealStart, int revealLength) { 5892 if (fSourceViewer == null) 5893 return; 5894 5895 ISelection selection= getSelectionProvider().getSelection(); 5896 if (selection instanceof ITextSelection) { 5897 ITextSelection textSelection= (ITextSelection) selection; 5898 if (textSelection.getOffset() != 0 || textSelection.getLength() != 0) 5899 markInNavigationHistory(); 5900 } 5901 5902 StyledText widget= fSourceViewer.getTextWidget(); 5903 widget.setRedraw(false); 5904 { 5905 adjustHighlightRange(revealStart, revealLength); 5906 fSourceViewer.revealRange(revealStart, revealLength); 5907 5908 fSourceViewer.setSelectedRange(selectionStart, selectionLength); 5909 5910 markInNavigationHistory(); 5911 } 5912 widget.setRedraw(true); 5913 } 5914 5915 5919 public INavigationLocation createEmptyNavigationLocation() { 5920 return new TextSelectionNavigationLocation(this, false); 5921 } 5922 5923 5926 public INavigationLocation createNavigationLocation() { 5927 return new TextSelectionNavigationLocation(this, true); 5928 } 5929 5930 5934 protected void markInNavigationHistory() { 5935 getSite().getPage().getNavigationHistory().markLocation(this); 5936 } 5937 5938 5943 protected void editorSaved() { 5944 INavigationLocation[] locations= getSite().getPage().getNavigationHistory().getLocations(); 5945 IEditorInput input= getEditorInput(); 5946 for (int i= 0; i < locations.length; i++) { 5947 if (locations[i] instanceof TextSelectionNavigationLocation) { 5948 if(input.equals(locations[i].getInput())) { 5949 TextSelectionNavigationLocation location= (TextSelectionNavigationLocation) locations[i]; 5950 location.partSaved(this); 5951 } 5952 } 5953 } 5954 } 5955 5956 5959 protected void firePropertyChange(int property) { 5960 super.firePropertyChange(property); 5961 updatePropertyDependentActions(); 5962 } 5963 5964 5968 public void setStatusField(IStatusField field, String category) { 5969 Assert.isNotNull(category); 5970 if (field != null) { 5971 5972 if (fStatusFields == null) 5973 fStatusFields= new HashMap (3); 5974 5975 fStatusFields.put(category, field); 5976 updateStatusField(category); 5977 5978 } else if (fStatusFields != null) 5979 fStatusFields.remove(category); 5980 5981 if (fIncrementalFindTarget != null && ITextEditorActionConstants.STATUS_CATEGORY_FIND_FIELD.equals(category)) 5982 fIncrementalFindTarget.setStatusField(field); 5983 } 5984 5985 5992 protected IStatusField getStatusField(String category) { 5993 if (category != null && fStatusFields != null) 5994 return (IStatusField) fStatusFields.get(category); 5995 return null; 5996 } 5997 5998 6004 protected boolean isInInsertMode() { 6005 return !fIsOverwriting; 6006 } 6007 6008 6012 public InsertMode getInsertMode() { 6013 return fInsertMode; 6014 } 6015 6016 6020 public void setInsertMode(InsertMode newMode) { 6021 List legalModes= getLegalInsertModes(); 6022 if (!legalModes.contains(newMode)) 6023 throw new IllegalArgumentException (); 6024 6025 fInsertMode= newMode; 6026 6027 handleInsertModeChanged(); 6028 } 6029 6030 6037 protected List getLegalInsertModes() { 6038 if (fLegalInsertModes == null) { 6039 fLegalInsertModes= new ArrayList (); 6040 fLegalInsertModes.add(SMART_INSERT); 6041 fLegalInsertModes.add(INSERT); 6042 } 6043 return fLegalInsertModes; 6044 } 6045 6046 private void switchToNextInsertMode() { 6047 6048 InsertMode mode= getInsertMode(); 6049 List legalModes= getLegalInsertModes(); 6050 6051 int i= 0; 6052 while (i < legalModes.size()) { 6053 if (legalModes.get(i) == mode) break; 6054 ++ i; 6055 } 6056 6057 i= (i + 1) % legalModes.size(); 6058 InsertMode newMode= (InsertMode) legalModes.get(i); 6059 setInsertMode(newMode); 6060 } 6061 6062 private void toggleOverwriteMode() { 6063 if (fIsOverwriteModeEnabled) { 6064 fIsOverwriting= !fIsOverwriting; 6065 fSourceViewer.getTextWidget().invokeAction(ST.TOGGLE_OVERWRITE); 6066 handleInsertModeChanged(); 6067 } 6068 } 6069 6070 6078 protected void configureInsertMode(InsertMode mode, boolean legal) { 6079 List legalModes= getLegalInsertModes(); 6080 if (legal) { 6081 if (!legalModes.contains(mode)) 6082 legalModes.add(mode); 6083 } else if (legalModes.size() > 1) { 6084 if (getInsertMode() == mode) 6085 switchToNextInsertMode(); 6086 legalModes.remove(mode); 6087 } 6088 } 6089 6090 6097 protected void enableOverwriteMode(boolean enable) { 6098 if (fIsOverwriting && !enable) 6099 toggleOverwriteMode(); 6100 fIsOverwriteModeEnabled= enable; 6101 } 6102 6103 private Caret createOverwriteCaret(StyledText styledText) { 6104 Caret caret= new Caret(styledText, SWT.NULL); 6105 GC gc= new GC(styledText); 6106 Point charSize= gc.stringExtent("a"); 6110 caret.setSize(charSize.x, styledText.getLineHeight()); 6112 caret.setFont(styledText.getFont()); 6113 6114 gc.dispose(); 6115 6116 return caret; 6117 } 6118 6119 private Caret createInsertCaret(StyledText styledText) { 6120 Caret caret= new Caret(styledText, SWT.NULL); 6121 6122 caret.setSize(getCaretWidthPreference(), styledText.getLineHeight()); 6124 caret.setFont(styledText.getFont()); 6125 6126 return caret; 6127 } 6128 6129 private Image createRawInsertModeCaretImage(StyledText styledText) { 6130 6131 PaletteData caretPalette= new PaletteData(new RGB[] {new RGB (0,0,0), new RGB (255,255,255)}); 6132 int width= getCaretWidthPreference(); 6133 int widthOffset= width - 1; 6134 6135 ImageData imageData= new ImageData(4 + widthOffset, styledText.getLineHeight(), 1, caretPalette); 6137 6138 Display display= styledText.getDisplay(); 6139 Image bracketImage= new Image(display, imageData); 6140 GC gc= new GC (bracketImage); 6141 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 6142 gc.setLineWidth(0); int height= imageData.height / 3; 6144 for (int i= 0; i < width ; i++) { 6148 gc.drawLine(i, 0, i, height - 1); 6149 gc.drawLine(i, imageData.height - height, i, imageData.height - 1); 6150 } 6151 6152 gc.dispose(); 6153 6154 return bracketImage; 6155 } 6156 6157 private Caret createRawInsertModeCaret(StyledText styledText) { 6158 if (!getLegalInsertModes().contains(SMART_INSERT)) 6160 return createInsertCaret(styledText); 6161 6162 Caret caret= new Caret(styledText, SWT.NULL); 6163 Image image= createRawInsertModeCaretImage(styledText); 6164 if (image != null) 6165 caret.setImage(image); 6166 else { 6167 caret.setSize(getCaretWidthPreference(), styledText.getLineHeight()); 6169 } 6170 6171 caret.setFont(styledText.getFont()); 6172 6173 return caret; 6174 } 6175 6176 private int getCaretWidthPreference() { 6177 if (getPreferenceStore() != null && getPreferenceStore().getBoolean(PREFERENCE_WIDE_CARET)) 6178 return WIDE_CARET_WIDTH; 6179 6180 return SINGLE_CARET_WIDTH; 6181 } 6182 6183 private void updateCaret() { 6184 6185 if (fSourceViewer == null) 6186 return; 6187 6188 StyledText styledText= fSourceViewer.getTextWidget(); 6189 6190 InsertMode mode= getInsertMode(); 6191 6192 styledText.setCaret(null); 6193 disposeNonDefaultCaret(); 6194 6195 if (getPreferenceStore() == null || !getPreferenceStore().getBoolean(PREFERENCE_USE_CUSTOM_CARETS)) 6196 Assert.isTrue(fNonDefaultCaret == null); 6197 else if (fIsOverwriting) 6198 fNonDefaultCaret= createOverwriteCaret(styledText); 6199 else if (SMART_INSERT == mode) 6200 fNonDefaultCaret= createInsertCaret(styledText); 6201 else if (INSERT == mode) 6202 fNonDefaultCaret= createRawInsertModeCaret(styledText); 6203 6204 if (fNonDefaultCaret != null) { 6205 styledText.setCaret(fNonDefaultCaret); 6206 fNonDefaultCaretImage= fNonDefaultCaret.getImage(); 6207 } else if (fInitialCaret != styledText.getCaret()) 6208 styledText.setCaret(fInitialCaret); 6209 } 6210 6211 private void disposeNonDefaultCaret() { 6212 if (fNonDefaultCaretImage != null) { 6213 fNonDefaultCaretImage.dispose(); 6214 fNonDefaultCaretImage= null; 6215 } 6216 6217 if (fNonDefaultCaret != null) { 6218 fNonDefaultCaret.dispose(); 6219 fNonDefaultCaret= null; 6220 } 6221 } 6222 6223 6229 protected void handleInsertModeChanged() { 6230 updateInsertModeAction(); 6231 updateCaret(); 6232 updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_MODE); 6233 } 6234 6235 private void updateInsertModeAction() { 6236 6237 if (getSite() == null) 6240 return; 6241 6242 IAction action= getAction(ITextEditorActionConstants.TOGGLE_INSERT_MODE); 6243 if (action != null) { 6244 action.setEnabled(!fIsOverwriting); 6245 action.setChecked(fInsertMode == SMART_INSERT); 6246 } 6247 } 6248 6249 6255 protected void handleCursorPositionChanged() { 6256 updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION); 6257 } 6258 6259 6265 protected void updateStatusField(String category) { 6266 6267 if (category == null) 6268 return; 6269 6270 IStatusField field= getStatusField(category); 6271 if (field != null) { 6272 6273 String text= null; 6274 6275 if (ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION.equals(category)) 6276 text= getCursorPosition(); 6277 else if (ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE.equals(category)) 6278 text= isEditorInputReadOnly() ? fReadOnlyLabel : fWritableLabel; 6279 else if (ITextEditorActionConstants.STATUS_CATEGORY_INPUT_MODE.equals(category)) { 6280 InsertMode mode= getInsertMode(); 6281 if (fIsOverwriting) 6282 text= fOverwriteModeLabel; 6283 else if (INSERT == mode) 6284 text= fInsertModeLabel; 6285 else if (SMART_INSERT == mode) 6286 text= fSmartInsertModeLabel; 6287 } 6288 6289 field.setText(text == null ? fErrorLabel : text); 6290 } 6291 } 6292 6293 6298 protected void updateStatusFields() { 6299 if (fStatusFields != null) { 6300 Iterator e= fStatusFields.keySet().iterator(); 6301 while (e.hasNext()) 6302 updateStatusField((String ) e.next()); 6303 } 6304 } 6305 6306 6312 protected String getCursorPosition() { 6313 6314 if (fSourceViewer == null) 6315 return fErrorLabel; 6316 6317 StyledText styledText= fSourceViewer.getTextWidget(); 6318 int caret= widgetOffset2ModelOffset(fSourceViewer, styledText.getCaretOffset()); 6319 IDocument document= fSourceViewer.getDocument(); 6320 6321 if (document == null) 6322 return fErrorLabel; 6323 6324 try { 6325 6326 int line= document.getLineOfOffset(caret); 6327 6328 int lineOffset= document.getLineOffset(line); 6329 int tabWidth= styledText.getTabs(); 6330 int column= 0; 6331 for (int i= lineOffset; i < caret; i++) 6332 if ('\t' == document.getChar(i)) 6333 column += tabWidth - (tabWidth == 0 ? 0 : column % tabWidth); 6334 else 6335 column++; 6336 6337 fLineLabel.fValue= line + 1; 6338 fColumnLabel.fValue= column + 1; 6339 return NLSUtility.format(fPositionLabelPattern, fPositionLabelPatternArguments); 6340 6341 } catch (BadLocationException x) { 6342 return fErrorLabel; 6343 } 6344 } 6345 6346 6350 public boolean isEditorInputReadOnly() { 6351 IDocumentProvider provider= getDocumentProvider(); 6352 if (provider instanceof IDocumentProviderExtension) { 6353 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 6354 return extension.isReadOnly(getEditorInput()); 6355 } 6356 return true; 6357 } 6358 6359 6363 public boolean isEditorInputModifiable() { 6364 IDocumentProvider provider= getDocumentProvider(); 6365 if (provider instanceof IDocumentProviderExtension) { 6366 IDocumentProviderExtension extension= (IDocumentProviderExtension) provider; 6367 return extension.isModifiable(getEditorInput()); 6368 } 6369 return true; 6370 } 6371 6372 6376 public void addRulerContextMenuListener(IMenuListener listener) { 6377 fRulerContextMenuListeners.add(listener); 6378 } 6379 6380 6384 public void removeRulerContextMenuListener(IMenuListener listener) { 6385 fRulerContextMenuListeners.remove(listener); 6386 } 6387 6388 6399 protected boolean canHandleMove(IEditorInput originalElement, IEditorInput movedElement) { 6400 return true; 6401 } 6402 6403 6412 protected final static int widgetOffset2ModelOffset(ISourceViewer viewer, int widgetOffset) { 6413 if (viewer instanceof ITextViewerExtension5) { 6414 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; 6415 return extension.widgetOffset2ModelOffset(widgetOffset); 6416 } 6417 return widgetOffset + viewer.getVisibleRegion().getOffset(); 6418 } 6419 6420 6429 protected final static int modelOffset2WidgetOffset(ISourceViewer viewer, int modelOffset) { 6430 if (viewer instanceof ITextViewerExtension5) { 6431 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; 6432 return extension.modelOffset2WidgetOffset(modelOffset); 6433 } 6434 return modelOffset - viewer.getVisibleRegion().getOffset(); 6435 } 6436 6437 6445 protected final static IRegion getCoverage(ISourceViewer viewer) { 6446 if (viewer instanceof ITextViewerExtension5) { 6447 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; 6448 return extension.getModelCoverage(); 6449 } 6450 return viewer.getVisibleRegion(); 6451 } 6452 6453 6462 protected static final boolean isVisible(ISourceViewer viewer, int offset, int length) { 6463 if (viewer instanceof ITextViewerExtension5) { 6464 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; 6465 IRegion overlap= extension.modelRange2WidgetRange(new Region(offset, length)); 6466 return overlap != null; 6467 } 6468 return viewer.overlapsWithVisibleRegion(offset, length); 6469 } 6470 6471 6475 public void showChangeInformation(boolean show) { 6476 } 6478 6479 6483 public boolean isChangeInformationShowing() { 6484 return false; 6485 } 6486 6487 6493 protected void setStatusLineErrorMessage(String message) { 6494 IEditorStatusLine statusLine= (IEditorStatusLine)getAdapter(IEditorStatusLine.class); 6495 if (statusLine != null) 6496 statusLine.setMessage(true, message, null); 6497 } 6498 6499 6505 protected void setStatusLineMessage(String message) { 6506 IEditorStatusLine statusLine= (IEditorStatusLine)getAdapter(IEditorStatusLine.class); 6507 if (statusLine != null) 6508 statusLine.setMessage(false, message, null); 6509 } 6510 6511 6520 public Annotation gotoAnnotation(boolean forward) { 6521 ITextSelection selection= (ITextSelection) getSelectionProvider().getSelection(); 6522 Position position= new Position(0, 0); 6523 Annotation annotation= findAnnotation(selection.getOffset(), selection.getLength(), forward, position); 6524 setStatusLineErrorMessage(null); 6525 setStatusLineMessage(null); 6526 6527 if (annotation != null) { 6528 selectAndReveal(position.getOffset(), position.getLength()); 6529 setStatusLineMessage(annotation.getText()); 6530 } 6531 return annotation; 6532 } 6533 6534 6546 protected Annotation findAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) { 6547 6548 Annotation nextAnnotation= null; 6549 Position nextAnnotationPosition= null; 6550 Annotation containingAnnotation= null; 6551 Position containingAnnotationPosition= null; 6552 boolean currentAnnotation= false; 6553 6554 IDocument document= getDocumentProvider().getDocument(getEditorInput()); 6555 int endOfDocument= document.getLength(); 6556 int distance= Integer.MAX_VALUE; 6557 6558 IAnnotationModel model= getDocumentProvider().getAnnotationModel(getEditorInput()); 6559 Iterator e= model.getAnnotationIterator(); 6560 while (e.hasNext()) { 6561 Annotation a= (Annotation) e.next(); 6562 if (!isNavigationTarget(a)) 6563 continue; 6564 6565 Position p= model.getPosition(a); 6566 if (p == null) 6567 continue; 6568 6569 if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) { if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) { 6571 containingAnnotation= a; 6572 containingAnnotationPosition= p; 6573 currentAnnotation= p.length == length; 6574 } 6575 } else { 6576 int currentDistance= 0; 6577 6578 if (forward) { 6579 currentDistance= p.getOffset() - offset; 6580 if (currentDistance < 0) 6581 currentDistance= endOfDocument + currentDistance; 6582 6583 if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) { 6584 distance= currentDistance; 6585 nextAnnotation= a; 6586 nextAnnotationPosition= p; 6587 } 6588 } else { 6589 currentDistance= offset + length - (p.getOffset() + p.length); 6590 if (currentDistance < 0) 6591 currentDistance= endOfDocument + currentDistance; 6592 6593 if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) { 6594 distance= currentDistance; 6595 nextAnnotation= a; 6596 nextAnnotationPosition= p; 6597 } 6598 } 6599 } 6600 } 6601 if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) { 6602 annotationPosition.setOffset(containingAnnotationPosition.getOffset()); 6603 annotationPosition.setLength(containingAnnotationPosition.getLength()); 6604 return containingAnnotation; 6605 } 6606 if (nextAnnotationPosition != null) { 6607 annotationPosition.setOffset(nextAnnotationPosition.getOffset()); 6608 annotationPosition.setLength(nextAnnotationPosition.getLength()); 6609 } 6610 6611 return nextAnnotation; 6612 } 6613 6614 6625 protected boolean isNavigationTarget(Annotation annotation) { 6626 return true; 6627 } 6628 6629 6633 public void showRevisionInformation(RevisionInformation info, String quickDiffProviderId) { 6634 } 6636 6637 6641 public void restoreState(IMemento memento) { 6642 fMementoToRestore= memento; 6643 } 6644 6645 6649 public void saveState(IMemento memento) { 6650 ISelection selection= doGetSelection(); 6651 if (selection instanceof ITextSelection) { 6652 memento.putInteger(TAG_SELECTION_OFFSET, ((ITextSelection)selection).getOffset()); 6653 memento.putInteger(TAG_SELECTION_LENGTH, ((ITextSelection)selection).getLength()); 6654 memento.putInteger(TAG_SELECTION_HPIXEL, getSourceViewer().getTextWidget().getHorizontalPixel()); 6656 } 6657 } 6658 6659 6668 protected boolean containsSavedState(IMemento memento) { 6669 return memento.getInteger(TAG_SELECTION_OFFSET) != null && memento.getInteger(TAG_SELECTION_LENGTH) != null; 6670 } 6671 6672 6680 protected void doRestoreState(IMemento memento) { 6681 Integer offset= memento.getInteger(TAG_SELECTION_OFFSET); 6682 if (offset == null) 6683 return; 6684 6685 Integer length= memento.getInteger(TAG_SELECTION_LENGTH); 6686 if (length == null) 6687 return; 6688 6689 doSetSelection(new TextSelection(offset.intValue(), length.intValue())); 6690 6691 Integer horizontalPixel= memento.getInteger(TAG_SELECTION_HPIXEL); 6693 if (horizontalPixel == null) 6694 return; 6695 StyledText textWidget= getSourceViewer().getTextWidget(); 6696 if (!textWidget.isVisible()) 6697 textWidget.setHorizontalPixel(horizontalPixel.intValue()); 6698 } 6699 6700 6704 public Saveable[] getSaveables() { 6705 if (fSavable == null) 6706 fSavable= new TextEditorSavable(this); 6707 6708 return new Saveable[] { fSavable }; 6709 } 6710 6711 6715 public Saveable[] getActiveSaveables() { 6716 return getSaveables(); 6717 } 6718 6719 6724 protected static class TextEditorSavable extends Saveable { 6725 6726 6727 private ITextEditor fTextEditor; 6728 6729 private IEditorInput fEditorInput; 6730 6731 private IDocument fDocument; 6732 6733 6738 public TextEditorSavable(ITextEditor textEditor) { 6739 Assert.isLegal(textEditor != null); 6740 fTextEditor= textEditor; 6741 fEditorInput= fTextEditor.getEditorInput(); 6742 Assert.isLegal(fEditorInput != null); 6743 } 6744 6745 6748 public void disconnectEditor() { 6749 getAdapter(IDocument.class); fTextEditor= null; 6751 } 6752 6753 6756 public String getName() { 6757 return fEditorInput.getName(); 6758 } 6759 6760 6763 public String getToolTipText() { 6764 return fEditorInput.getToolTipText(); 6765 } 6766 6767 6770 public ImageDescriptor getImageDescriptor() { 6771 return fEditorInput.getImageDescriptor(); 6772 } 6773 6774 6778 public void doSave(IProgressMonitor monitor) throws CoreException { 6779 fTextEditor.doSave(monitor); 6780 } 6781 6782 public boolean isDirty() { 6783 return fTextEditor.isDirty(); 6784 } 6785 6786 6789 public boolean supportsBackgroundSave() { 6790 return false; 6791 } 6792 6793 6796 public int hashCode() { 6797 Object document= getAdapter(IDocument.class); 6798 if (document == null) 6799 return 0; 6800 return document.hashCode(); 6801 } 6802 6803 6806 public boolean equals(Object obj) { 6807 if (this == obj) 6808 return true; 6809 6810 if (!(obj instanceof Saveable)) 6811 return false; 6812 6813 Object thisDocument= getAdapter(IDocument.class); 6814 Object otherDocument= ((Saveable)obj).getAdapter(IDocument.class); 6815 6816 if (thisDocument == null && otherDocument == null) 6817 return true; 6818 6819 return thisDocument != null && thisDocument.equals(otherDocument); 6820 } 6821 6822 6828 public Object getAdapter(Class adapter) { 6829 if (adapter == IDocument.class) { 6830 if (fDocument == null) { 6831 IDocumentProvider documentProvider= fTextEditor.getDocumentProvider(); 6832 if (documentProvider != null) 6833 fDocument= documentProvider.getDocument(fEditorInput); 6834 } 6835 return fDocument; 6836 } 6837 return super.getAdapter(adapter); 6838 } 6839 } 6840 6841 6843 6850 protected void installTabsToSpacesConverter() { 6851 SourceViewerConfiguration config= getSourceViewerConfiguration(); 6852 if (config != null && fSourceViewer instanceof ITextViewerExtension7) { 6853 int tabWidth= config.getTabWidth(fSourceViewer); 6854 TabsToSpacesConverter tabToSpacesConverter= new TabsToSpacesConverter(); 6855 tabToSpacesConverter.setLineTracker(new DefaultLineTracker()); 6856 tabToSpacesConverter.setNumberOfSpacesPerTab(tabWidth); 6857 ((ITextViewerExtension7)fSourceViewer).setTabsToSpacesConverter(tabToSpacesConverter); 6858 updateIndentPrefixes(); 6859 } 6860 } 6861 6862 6869 protected void uninstallTabsToSpacesConverter() { 6870 if (fSourceViewer instanceof ITextViewerExtension7) { 6871 ((ITextViewerExtension7)fSourceViewer).setTabsToSpacesConverter(null); 6872 if (fSourceViewer.getTextWidget() != null) 6873 updateIndentPrefixes(); 6874 } 6875 } 6876 6877 6886 protected boolean isTabsToSpacesConversionEnabled() { 6887 return false; 6888 } 6889 6890 6896 protected final void updateIndentPrefixes() { 6897 SourceViewerConfiguration configuration= getSourceViewerConfiguration(); 6898 String [] types= configuration.getConfiguredContentTypes(fSourceViewer); 6899 for (int i= 0; i < types.length; i++) { 6900 String [] prefixes= configuration.getIndentPrefixes(fSourceViewer, types[i]); 6901 if (prefixes != null && prefixes.length > 0) 6902 fSourceViewer.setIndentPrefixes(prefixes, types[i]); 6903 } 6904 } 6905 6906} 6907 | Popular Tags |