Example usage for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor

List of usage examples for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor

Introduction

In this page you can find the example usage for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor.

Prototype

public OpenFileDescriptor(@NotNull Project project, @NotNull VirtualFile file, int offset) 

Source Link

Usage

From source file:com.intellij.ide.util.EditorHelper.java

License:Apache License

@Nullable
public static FileEditor openInEditor(@NotNull PsiElement element, boolean switchToText) {
    PsiFile file;//  w  w  w .j  ava 2s .com
    int offset;
    if (element instanceof PsiFile) {
        file = (PsiFile) element;
        offset = -1;
    } else {
        file = element.getContainingFile();
        offset = element.getTextOffset();
    }
    if (file == null)
        return null;//SCR44414
    VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return null;
    OpenFileDescriptor descriptor = new OpenFileDescriptor(element.getProject(), virtualFile, offset);
    Project project = element.getProject();
    if (offset == -1 && !switchToText) {
        FileEditorManager.getInstance(project).openEditor(descriptor, false);
    } else {
        FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
    }
    return FileEditorManager.getInstance(project).getSelectedEditor(virtualFile);
}

From source file:com.intellij.ide.util.EditSourceUtil.java

License:Apache License

@Nullable
public static Navigatable getDescriptor(final PsiElement element) {
    if (!canNavigate(element)) {
        return null;
    }/*from  w  w w  . ja v  a2 s.  c  o m*/
    if (element instanceof PomTargetPsiElement) {
        return ((PomTargetPsiElement) element).getTarget();
    }
    final PsiElement navigationElement = element.getNavigationElement();
    if (navigationElement instanceof PomTargetPsiElement) {
        return ((PomTargetPsiElement) navigationElement).getTarget();
    }
    final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navigationElement);
    if (virtualFile == null || !virtualFile.isValid()) {
        return null;
    }
    OpenFileDescriptor desc = new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset);
    desc.setUseCurrentWindow(FileEditorManager.USE_CURRENT_WINDOW.isIn(navigationElement));
    return desc;
}

From source file:com.intellij.lang.ant.config.execution.TreeView.java

License:Apache License

private JPanel createPanel() {
    createModel();/*from w w w. jav a2  s.  co m*/
    myTree = new MyTree();
    myTree.setLineStyleAngled();
    myTree.setRootVisible(false);
    myTree.setShowsRootHandles(true);
    myTree.updateUI();
    myTree.setLargeModel(true);

    myTree.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                OpenSourceUtil.openSourcesFrom(DataManager.getInstance().getDataContext(myTree), false);
            }
        }
    });

    myTree.addMouseListener(new PopupHandler() {
        public void invokePopup(Component comp, int x, int y) {
            popupInvoked(comp, x, y);
        }
    });

    EditSourceOnDoubleClickHandler.install(myTree);

    myAutoScrollToSourceHandler.install(myTree);

    myOccurenceNavigatorSupport = new OccurenceNavigatorSupport(myTree) {
        protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
            if (!(node instanceof MessageNode)) {
                return null;
            }
            MessageNode messageNode = (MessageNode) node;
            AntBuildMessageView.MessageType type = messageNode.getType();

            if (type != AntBuildMessageView.MessageType.MESSAGE
                    && type != AntBuildMessageView.MessageType.ERROR) {
                return null;
            }

            if (!isValid(messageNode.getFile())) {
                return null;
            }

            return new OpenFileDescriptor(myProject, messageNode.getFile(), messageNode.getOffset());
        }

        @Nullable
        public String getNextOccurenceActionName() {
            return AntBundle.message("ant.execution.next.error.warning.action.name");
        }

        @Nullable
        public String getPreviousOccurenceActionName() {
            return AntBundle.message("ant.execution.previous.error.warning.action.name");
        }
    };

    JPanel panel = new JPanel(new BorderLayout());

    JScrollPane scrollPane = MessageTreeRenderer.install(myTree);
    panel.add(scrollPane, BorderLayout.CENTER);
    return panel;
}

From source file:com.intellij.lang.ant.config.execution.TreeView.java

License:Apache License

@Nullable
public Object getData(Key<?> dataId) {
    if (PlatformDataKeys.NAVIGATABLE == dataId) {
        MessageNode item = getSelectedItem();
        if (item == null)
            return null;
        if (isValid(item.getFile())) {
            return new OpenFileDescriptor(myProject, item.getFile(), item.getOffset());
        }//from  ww w.j  av  a  2  s  .  c  o m
        if (item.getType() == AntBuildMessageView.MessageType.TARGET) {
            final OpenFileDescriptor descriptor = getDescriptorForTargetNode(item);
            if (descriptor != null && isValid(descriptor.getFile())) {
                return descriptor;
            }
        }
        if (item.getType() == AntBuildMessageView.MessageType.TASK) {
            final OpenFileDescriptor descriptor = getDescriptorForTaskNode(item);
            if (descriptor != null && isValid(descriptor.getFile())) {
                return descriptor;
            }
        }
    }
    return null;
}

From source file:com.intellij.lang.ant.config.impl.AntBuildTargetImpl.java

License:Apache License

public OpenFileDescriptor getOpenFileDescriptor() {
    return (myFile == null) ? null : new OpenFileDescriptor(myProject, myFile, myTextOffset);
}

From source file:com.intellij.lang.ant.config.impl.BuildTask.java

License:Apache License

@Nullable
public OpenFileDescriptor getOpenFileDescriptor() {
    final VirtualFile vFile = myTarget.getContainingFile();
    return vFile != null ? new OpenFileDescriptor(myTarget.getProject(), vFile, myOffset) : null;
}

From source file:com.intellij.lang.javascript.inspections.qucikFixes.BaseCreateFix.java

License:Apache License

public static @Nullable Editor getEditor(final Project project, final PsiFile realFile) {
    if (!CodeInsightUtilBase.getInstance().prepareFileForWrite(realFile)) {
        return null;
    }/*from   w  w  w.  ja v a 2s.  c  om*/

    return FileEditorManager.getInstance(project)
            .openTextEditor(new OpenFileDescriptor(project, realFile.getVirtualFile(), 0), true);
}

From source file:com.intellij.packageDependencies.ui.PackageDependenciesNode.java

License:Apache License

@Nullable
private OpenFileDescriptor getDescriptor() {
    if (getProject() == null)
        return null;
    final PsiElement psiElement = getPsiElement();
    if (psiElement == null)
        return null;
    final VirtualFile virtualFile = psiElement.getContainingFile().getVirtualFile();
    if (virtualFile == null || !virtualFile.isValid())
        return null;
    return new OpenFileDescriptor(getProject(), virtualFile, psiElement.getTextOffset());
}

From source file:com.intellij.pom.NavigatableAdapter.java

License:Apache License

public static void navigate(Project project, VirtualFile file, int offset, boolean requestFocus) {
    new OpenFileDescriptor(project, file, offset).navigate(requestFocus);
}

From source file:com.intellij.psi.formatter.FormatterTestCase.java

License:Open Source License

private void checkDocument(final PsiFile file, final String text, String textAfter) {
    final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
    final EditorImpl editor;

    if (doCheckDocumentUpdate()) {
        editor = (EditorImpl) FileEditorManager.getInstance(getProject())
                .openTextEditor(new OpenFileDescriptor(getProject(), file.getVirtualFile(), 0), false);
        editor.putUserData(EditorImpl.DO_DOCUMENT_UPDATE_TEST, Boolean.TRUE);
        if (myFile != null) {
            FileEditorManager.getInstance(getProject()).closeFile(myFile.getVirtualFile());
        }/*from   w  w  w  . j av  a 2s. co  m*/
        myEditor = editor;
        myFile = file;
    } else {
        editor = null;
    }

    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                @Override
                public void run() {
                    document.replaceString(0, document.getTextLength(), text);
                    PsiDocumentManager.getInstance(getProject()).commitDocument(document);
                    assertEquals(file.getText(), document.getText());

                    if (false && doCheckDocumentUpdate()) {
                        makeFolding(file, editor);
                    }
                    try {
                        if (doReformatRangeTest) {
                            CodeStyleManager.getInstance(getProject()).reformatRange(file,
                                    file.getTextRange().getStartOffset(), file.getTextRange().getEndOffset());
                        } else if (myTextRange != null) {
                            CodeStyleManager.getInstance(getProject()).reformatText(file,
                                    myTextRange.getStartOffset(), myTextRange.getEndOffset());
                        } else {
                            CodeStyleManager.getInstance(getProject()).reformatText(file,
                                    file.getTextRange().getStartOffset(), file.getTextRange().getEndOffset());
                        }
                    } catch (IncorrectOperationException e) {
                        fail();
                    }

                }
            });
        }
    }, "", "");

    assertEquals(textAfter, document.getText());
    PsiDocumentManager.getInstance(getProject()).commitDocument(document);
    assertEquals(textAfter, file.getText());
}