use of com.intellij.openapi.fileEditor.TextEditor in project checkstyle-idea by jshiell.
the class CheckStyleToolWindowPanel method scrollToError.
/**
* Scroll to the error specified by the given tree path, or do nothing
* if no error is specified.
*
* @param treePath the tree path to scroll to.
*/
private void scrollToError(final TreePath treePath) {
final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) {
return;
}
final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject();
if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) {
// no problem here :-)
return;
}
final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile();
if (virtualFile == null || !virtualFile.exists()) {
return;
}
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
ApplicationManager.getApplication().invokeLater(() -> {
final FileEditor[] editor = fileEditorManager.openFile(virtualFile, true);
if (editor.length > 0 && editor[0] instanceof TextEditor) {
final LogicalPosition problemPos = new LogicalPosition(Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0));
final Editor textEditor = ((TextEditor) editor[0]).getEditor();
textEditor.getCaretModel().moveToLogicalPosition(problemPos);
textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
}
}, ModalityState.NON_MODAL);
}
Aggregations