Java源码示例:com.intellij.openapi.editor.ScrollingModel

示例1
public final void gotoLine(final int pLineNumber, final Document document) {
    int lineNumber = pLineNumber;
    Editor projectEditor =
            FileEditorManager.getInstance(windowObjects.getProject()).getSelectedTextEditor();

    if (projectEditor != null) {
        CaretModel caretModel = projectEditor.getCaretModel();

        //document is 0-indexed
        if (lineNumber > document.getLineCount()) {
            lineNumber = document.getLineCount() - 1;
        } else {
            lineNumber = lineNumber - 1;
        }

        caretModel.moveToLogicalPosition(new LogicalPosition(lineNumber, 0));

        ScrollingModel scrollingModel = projectEditor.getScrollingModel();
        scrollingModel.scrollToCaret(ScrollType.CENTER);
    }
}
 
示例2
@Override
public void execute(Editor editor, DataContext dataContext) {
  editor.getCaretModel().removeSecondaryCarets();
  editor.getCaretModel().moveToOffset(0);
  editor.getSelectionModel().removeSelection();

  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.RELATIVE);
  scrollingModel.enableAnimation();

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    IdeDocumentHistory instance = IdeDocumentHistory.getInstance(project);
    if (instance != null) {
      instance.includeCurrentCommandAsNavigation();
    }
  }
}
 
示例3
@Override
public void execute(Editor editor, DataContext dataContext) {
  editor.getCaretModel().removeSecondaryCarets();
  int offset = editor.getDocument().getTextLength();
  if (editor instanceof DesktopEditorImpl) {
    editor.getCaretModel().moveToLogicalPosition(editor.offsetToLogicalPosition(offset).leanForward(true));
  }
  else {
    editor.getCaretModel().moveToOffset(offset);
  }
  editor.getSelectionModel().removeSelection();

  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.CENTER);
  scrollingModel.enableAnimation();

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    IdeDocumentHistory instance = IdeDocumentHistory.getInstance(project);
    if (instance != null) {
      instance.includeCurrentCommandAsNavigation();
    }
  }
}
 
示例4
@NotNull
public static TextRange getVisibleRangeOffset(@NotNull Editor editor) {
    ScrollingModel scrollingModel = editor.getScrollingModel();
    Rectangle visibleArea = scrollingModel.getVisibleArea();

    LogicalPosition startLog = editor.xyToLogicalPosition(new Point(0, visibleArea.y));
    LogicalPosition lastLog = editor.xyToLogicalPosition(new Point(0, visibleArea.y + visibleArea.height));

    int startOff = editor.logicalPositionToOffset(startLog);
    int endOff = editor.logicalPositionToOffset(new LogicalPosition(lastLog.line + 1, lastLog.column));
    return new TextRange(startOff, endOff);
}
 
示例5
@Override
public void setSelected(AnActionEvent e, boolean state) {

    final Editor variablesEditor = getVariablesEditor(e);
    if (variablesEditor != null) {

        final Editor queryEditor = variablesEditor.getUserData(JSGraphQLLanguageUIProjectService.GRAPH_QL_QUERY_EDITOR);
        if(queryEditor == null) {
            // not linked to a query editor
            return;
        }

        final ScrollingModel scroll = queryEditor.getScrollingModel();
        final int currentScroll = scroll.getVerticalScrollOffset();

        variablesEditor.putUserData(JS_GRAPH_QL_VARIABLES_MODEL, state ? Boolean.TRUE : Boolean.FALSE);
        variablesEditor.getComponent().setVisible(state);

        if (state) {
            variablesEditor.getContentComponent().grabFocus();
        } else {
            queryEditor.getContentComponent().grabFocus();
        }

        // restore scroll position after the editor has had a chance to re-layout
        ApplicationManager.getApplication().invokeLater(() -> {
            UIUtil.invokeLaterIfNeeded(() -> scroll.scrollVertically(currentScroll));
        });

    }

}
 
示例6
private void updateInUI(Editor editor) {
    VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
    Point point = editor.visualPositionToXY(visualPosition);
    ScrollingModel scrollingModel = editor.getScrollingModel();
    point.x = point.x - scrollingModel.getHorizontalScrollOffset();
    point.y = point.y - scrollingModel.getVerticalScrollOffset();
    final ParticleContainer particleContainer = particleContainers.get(editor);
    if (particleContainer != null) {
        particleContainer.update(point);
    }
}
 
示例7
public static void moveCursor(Editor editor, int cursorOffset) {
	CaretModel caretModel = editor.getCaretModel();
	caretModel.moveToOffset(cursorOffset);
	ScrollingModel scrollingModel = editor.getScrollingModel();
	scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
	editor.getContentComponent().requestFocus();
}
 
示例8
private static void doScrollVertically(@Nonnull ScrollingModel model, int offset) {
  model.disableAnimation();
  try {
    model.scrollVertically(offset);
  }
  finally {
    model.enableAnimation();
  }
}
 
示例9
private static void doScrollHorizontally(@Nonnull ScrollingModel model, int offset) {
  model.disableAnimation();
  try {
    model.scrollHorizontally(offset);
  }
  finally {
    model.enableAnimation();
  }
}
 
示例10
public static void scrollEditor(@Nonnull Editor editor, int logicalLine) {
  editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(logicalLine, 0));
  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.CENTER);
  scrollingModel.enableAnimation();
}
 
示例11
public void restoreOriginalLocation() {
  for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
    Editor editor = e.getKey();
    int relativePosition = e.getValue();
    Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int scrollOffset = caretLocation.y - relativePosition;
    ScrollingModel scrollingModel = editor.getScrollingModel();
    Rectangle targetArea = scrollingModel.getVisibleAreaOnScrollingFinished();
    // when animated scrolling is in progress, we'll not stop it immediately
    boolean useAnimation = !targetArea.equals(scrollingModel.getVisibleArea());
    if (!useAnimation) scrollingModel.disableAnimation();
    scrollingModel.scroll(targetArea.x, scrollOffset);
    if (!useAnimation) scrollingModel.enableAnimation();
  }
}
 
示例12
static void navigateToError(Project project, final Editor editor, HighlightInfo info) {
  int oldOffset = editor.getCaretModel().getOffset();

  final int offset = getNavigationPositionFor(info, editor.getDocument());
  final int endOffset = info.getActualEndOffset();

  final ScrollingModel scrollingModel = editor.getScrollingModel();
  if (offset != oldOffset) {
    ScrollType scrollType = offset > oldOffset ? ScrollType.CENTER_DOWN : ScrollType.CENTER_UP;
    editor.getSelectionModel().removeSelection();
    editor.getCaretModel().removeSecondaryCarets();
    editor.getCaretModel().moveToOffset(offset);
    scrollingModel.scrollToCaret(scrollType);
  }

  scrollingModel.runActionOnScrollingFinished(
          new Runnable(){
            @Override
            public void run() {
              int maxOffset = editor.getDocument().getTextLength() - 1;
              if (maxOffset == -1) return;
              scrollingModel.scrollTo(editor.offsetToLogicalPosition(Math.min(maxOffset, endOffset)), ScrollType.MAKE_VISIBLE);
              scrollingModel.scrollTo(editor.offsetToLogicalPosition(Math.min(maxOffset, offset)), ScrollType.MAKE_VISIBLE);
            }
          }
  );

  IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
}
 
示例13
private static void doScrollVertically(@Nonnull Editor editor, int offset, boolean animated) {
  ScrollingModel model = editor.getScrollingModel();
  if (!animated) model.disableAnimation();
  model.scrollVertically(offset);
  if (!animated) model.enableAnimation();
}
 
示例14
private static void doScrollHorizontally(@Nonnull Editor editor, int offset, boolean animated) {
  ScrollingModel model = editor.getScrollingModel();
  if (!animated) model.disableAnimation();
  model.scrollHorizontally(offset);
  if (!animated) model.enableAnimation();
}
 
示例15
/**
 * Tries to setup caret and viewport for the given editor from the selected one.
 *
 * @param toSync editor to setup caret and viewport for
 */
private void syncCaretIfPossible(@Nullable FileEditor[] toSync) {
  if (toSync == null) {
    return;
  }

  final DesktopEditorWithProviderComposite from = getSelectedEditor();
  if (from == null) {
    return;
  }

  final FileEditor caretSource = from.getSelectedEditor();
  if (!(caretSource instanceof TextEditor)) {
    return;
  }

  final Editor editorFrom = ((TextEditor)caretSource).getEditor();
  final int offset = editorFrom.getCaretModel().getOffset();
  if (offset <= 0) {
    return;
  }

  final int scrollOffset = editorFrom.getScrollingModel().getVerticalScrollOffset();

  for (FileEditor fileEditor : toSync) {
    if (!(fileEditor instanceof TextEditor)) {
      continue;
    }
    final Editor editor = ((TextEditor)fileEditor).getEditor();
    if (editorFrom.getDocument() == editor.getDocument()) {
      editor.getCaretModel().moveToOffset(offset);
      final ScrollingModel scrollingModel = editor.getScrollingModel();
      scrollingModel.scrollVertically(scrollOffset);

      SwingUtilities.invokeLater(() -> {
        if (!editor.isDisposed()) {
          scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
        }
      });
    }
  }
}
 
示例16
public void onContentChangedIn(EditorSource source) {
  myDiffUpdater.contentRemoved(source);
  final EditorEx editor = source.getEditor();
  if (myIsHorizontal && source.getSide() == FragmentSide.SIDE1 && editor != null) {
    editor.setVerticalScrollbarOrientation(EditorEx.VERTICAL_SCROLLBAR_LEFT);
  }
  DiffSideView viewSide = getSideView(source.getSide());
  viewSide.setEditorSource(getProject(), source);
  Disposer.dispose(myScrollSupport);
  if (editor == null) {
    if (!myDisposed) {
      rediff();
    }
    return;
  }

  final MouseListener mouseListener = PopupHandler.installUnknownPopupHandler(editor.getContentComponent(), new MergeActionGroup(this, source.getSide()), ActionManager.getInstance());
  myDiffUpdater.contentAdded(source);
  editor.getSettings().setLineNumbersShown(true);
  editor.getSettings().setFoldingOutlineShown(false);
  editor.getFoldingModel().setFoldingEnabled(false);
  ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);

  Editor editor1 = getEditor(FragmentSide.SIDE1);
  Editor editor2 = getEditor(FragmentSide.SIDE2);
  if (editor1 != null && editor2 != null && myIsSyncScroll) {
    myScrollSupport.install(new EditingSides[]{this});
  }

  final VisibleAreaListener visibleAreaListener = mySplitter.getVisibleAreaListener();
  final ScrollingModel scrollingModel = editor.getScrollingModel();
  if (visibleAreaListener != null) {
    scrollingModel.addVisibleAreaListener(visibleAreaListener);
    scrollingModel.addVisibleAreaListener(myVisibleAreaListener);
  }
  myFontSizeSynchronizer.synchronize(editor);
  source.addDisposable(new Disposable() {
    public void dispose() {
      myFontSizeSynchronizer.stopSynchronize(editor);
    }
  });
  source.addDisposable(new Disposable() {
    public void dispose() {
      if (visibleAreaListener != null) {
        scrollingModel.removeVisibleAreaListener(visibleAreaListener);
        scrollingModel.removeVisibleAreaListener(myVisibleAreaListener);
      }
      editor.getContentComponent().removeMouseListener(mouseListener);
    }
  });
}