private static void beginCompoundChange(ITextViewer viewer) {
   if (viewer instanceof ITextViewerExtension) {
     ITextViewerExtension extension = (ITextViewerExtension) viewer;
     IRewriteTarget target = extension.getRewriteTarget();
     target.beginCompoundChange();
   }
 }
  private static void applyProposal(
      ICompletionProposal proposal,
      ITextViewer viewer,
      char trigger,
      int stateMask,
      final int offset) {
    Assert.isTrue(proposal instanceof ICompletionProposalExtension2);

    IRewriteTarget target = null;
    IEditingSupportRegistry registry = null;
    IEditingSupport helper =
        new IEditingSupport() {

          public boolean isOriginator(DocumentEvent event, IRegion focus) {
            return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset;
          }

          public boolean ownsFocusShell() {
            return false;
          }
        };

    try {
      IDocument document = viewer.getDocument();

      if (viewer instanceof ITextViewerExtension) {
        ITextViewerExtension extension = (ITextViewerExtension) viewer;
        target = extension.getRewriteTarget();
      }

      if (target != null) target.beginCompoundChange();

      if (viewer instanceof IEditingSupportRegistry) {
        registry = (IEditingSupportRegistry) viewer;
        registry.register(helper);
      }

      ((ICompletionProposalExtension2) proposal).apply(viewer, trigger, stateMask, offset);

      Point selection = proposal.getSelection(document);
      if (selection != null) {
        viewer.setSelectedRange(selection.x, selection.y);
        viewer.revealRange(selection.x, selection.y);
      }
    } finally {
      if (target != null) target.endCompoundChange();

      if (registry != null) registry.unregister(helper);
    }
  }
  @Override
  public void dispose() {
    colorManager.dispose();
    ISourceViewer sourceViewer = getSourceViewer();
    if (sourceViewer instanceof ITextViewerExtension) {
      ((ITextViewerExtension) sourceViewer).removeVerifyKeyListener(bracketInserter);
    }

    super.dispose();
  }
  @Override
  public void createPartControl(Composite parent) {
    setSourceViewerConfiguration(new Configuration(colorManager, this, getPreferenceStore()));

    super.createPartControl(parent);

    ISourceViewer sourceViewer = getSourceViewer();
    if (sourceViewer instanceof ITextViewerExtension) {
      bracketInserter.setSourceViewer(sourceViewer);
      bracketInserter.addBrackets('{', '}');
      ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(bracketInserter);
    }
  }
  /** @see AbstractTextEditor#createPartControl(Composite) */
  @Override
  public void createPartControl(Composite parent) {

    super.createPartControl(parent);

    IPreferenceStore preferenceStore = getPreferenceStore();
    boolean closeBrackets = preferenceStore.getBoolean(PreferenceConstants.EDITOR_CLOSE_BRACKETS);
    boolean closeStrings = preferenceStore.getBoolean(PreferenceConstants.EDITOR_CLOSE_STRINGS);

    this.fBracketInserter.setCloseBracketsEnabled(closeBrackets);
    this.fBracketInserter.setCloseStringsEnabled(closeStrings);

    ISourceViewer sourceViewer = getSourceViewer();

    if (sourceViewer instanceof ITextViewerExtension) {
      ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(this.fBracketInserter);
    }
  }
Exemple #6
0
 /*
  * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
  */
 @Override
 public void dragStart(DragSourceEvent event) {
   if (!fIsEnabled) {
     event.doit = false;
     return;
   }
   // convert screen coordinates to widget offest
   int offset = getOffsetAtLocation(event.x, event.y, false);
   // convert further to a document offset
   offset = getDocumentOffset(offset);
   Point selection = fViewer.getSelectedRange();
   if (selection != null && offset >= selection.x && offset < selection.x + selection.y) {
     fSelectionPosition = new Position(selection.x, selection.y);
     if (fViewer instanceof ITextViewerExtension) {
       ((ITextViewerExtension) fViewer).getRewriteTarget().beginCompoundChange();
     }
     IDocument doc = fViewer.getDocument();
     try {
       // add the drag selection position
       // the position is used to delete the selection on a DROP_MOVE
       // and it can be used by the drop target to determine if it should
       // allow the drop (e.g. if drop location overlaps selection)
       doc.addPositionCategory(DRAG_SELECTION_CATEGORY);
       fPositionUpdater = new DefaultPositionUpdater(DRAG_SELECTION_CATEGORY);
       doc.addPositionUpdater(fPositionUpdater);
       doc.addPosition(DRAG_SELECTION_CATEGORY, fSelectionPosition);
     } catch (BadLocationException e) {
       // should not happen
     } catch (BadPositionCategoryException e) {
       // cannot happen
     }
     event.doit = true;
     // this has no effect?
     event.detail = DND.DROP_COPY;
     if (isDocumentEditable()) {
       event.detail |= DND.DROP_MOVE;
     }
   } else {
     event.doit = false;
     event.detail = DND.DROP_NONE;
   }
 }
Exemple #7
0
 /*
  * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
  */
 @Override
 public void dragFinished(DragSourceEvent event) {
   IDocument doc = fViewer.getDocument();
   try {
     doc.removePositionCategory(DRAG_SELECTION_CATEGORY);
     doc.removePositionUpdater(fPositionUpdater);
   } catch (BadPositionCategoryException e1) {
     // cannot happen
   }
   if (event.doit && event.detail == DND.DROP_MOVE && isDocumentEditable()) {
     try {
       doc.replace(fSelectionPosition.offset, fSelectionPosition.length, null);
     } catch (BadLocationException e) {
       // ignore
     }
   }
   if (fViewer instanceof ITextViewerExtension) {
     ((ITextViewerExtension) fViewer).getRewriteTarget().endCompoundChange();
   }
 }