Example usage for com.intellij.openapi.fileEditor FileEditorManager openTextEditor

List of usage examples for com.intellij.openapi.fileEditor FileEditorManager openTextEditor

Introduction

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

Prototype

@Nullable
public abstract Editor openTextEditor(@NotNull OpenFileDescriptor descriptor, boolean focusEditor);

Source Link

Document

Works as #openFile(VirtualFile,boolean) but forces opening of text editor (see TextEditor ).

Usage

From source file:ariba.ideplugin.idea.ToggleAction.java

License:Apache License

/**
 * Switch between an awl page and the corresponding java page.
 * @param event occurred//from  w  ww  . j a va  2  s .com
 */
public void actionPerformed(AnActionEvent event) {
    LOG.info("User performed ToggleAction!");
    Project project = (Project) event.getDataContext().getData(DataConstants.PROJECT);

    if (project != null) {

        VirtualFile[] selectedFile = FileEditorManager.getInstance(project).getSelectedFiles();

        if (selectedFile.length == 0) {
            error(project, "No file is currently selected");
            return;
        }
        VirtualFile currEditorFile = selectedFile[0];

        if (currEditorFile != null) {

            String extension = currEditorFile.getExtension();

            String pairName;
            VirtualFile pairFile;

            if ("awl".equals(extension) || "htm".equals(extension)) {
                pairName = currEditorFile.getNameWithoutExtension() + ".java";
                pairFile = currEditorFile.getParent().findChild(pairName);

                if (pairFile == null) {
                    pairName = currEditorFile.getNameWithoutExtension() + ".groovy";
                    pairFile = currEditorFile.getParent().findChild(pairName);
                }

                if (pairFile == null) {
                    error(project, "Could not find .java or .groovy file for " + currEditorFile.getName());
                    return;
                }
            } else if ("java".equals(extension) || "groovy".equals(extension)) {
                pairName = currEditorFile.getNameWithoutExtension() + ".awl";
                pairFile = currEditorFile.getParent().findChild(pairName);

                if (pairFile == null) {
                    pairName = currEditorFile.getNameWithoutExtension() + ".htm";
                    pairFile = currEditorFile.getParent().findChild(pairName);
                }

                if (pairFile == null) {
                    error(project, "Could not find .awl or .htm file for " + currEditorFile.getName());
                    return;
                }
            } else {
                error(project, "Unexpected file type: " + extension);
                return;
            }

            OpenFileDescriptor fd = new OpenFileDescriptor(project, pairFile);

            if (fd != null) {
                FileEditorManager manager = FileEditorManager.getInstance(project);
                Editor e = manager.openTextEditor(fd, true);
                if (e == null) {
                    error(project, "Editor is null");
                }
            } else {
                error(project, "Unable to open file: " + pairName);
            }
        } else {
            error(project, "No file is currently selected");
        }
    }
}

From source file:com.android.tools.idea.tests.gui.framework.fixture.EditorFixture.java

License:Apache License

/**
 * Opens up a different file. This will run through the "Open File..." dialog to
 * find and select the given file./*  ww  w . j  av  a  2 s  .c  o  m*/
 *
 * @param file the file to open
 * @param tab which tab to open initially, if there are multiple editors
 */
public EditorFixture open(@NotNull final VirtualFile file, @NotNull final Tab tab) {
    execute(new GuiTask() {
        @Override
        protected void executeInEDT() throws Throwable {
            // TODO: Use UI to navigate to the file instead
            Project project = myFrame.getProject();
            FileEditorManager manager = FileEditorManager.getInstance(project);
            if (tab == Tab.EDITOR) {
                manager.openTextEditor(new OpenFileDescriptor(project, file), true);
            } else {
                manager.openFile(file, true);
            }
        }
    });

    selectEditorTab(tab);

    Wait.seconds(5).expecting("file " + quote(file.getPath()) + " to be opened and loaded").until(() -> {
        if (!file.equals(getCurrentFile())) {
            return false;
        }

        FileEditor fileEditor = FileEditorManager.getInstance(myFrame.getProject()).getSelectedEditor(file);
        JComponent editorComponent = fileEditor.getComponent();
        if (editorComponent instanceof JBLoadingPanel) {
            return !((JBLoadingPanel) editorComponent).isLoading();
        }
        return true;
    });

    myFrame.requestFocusIfLost();
    robot.waitForIdle();

    return this;
}

From source file:com.google.idea.blaze.android.project.BlazeBuildSystemService.java

License:Open Source License

@Override
public void addDependency(Module module, String artifact) {
    Project project = module.getProject();
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return;// w  ww . j  a v  a  2s.co m
    }
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo targetIdeInfo = blazeProjectData.targetMap.get(registry.getTargetKey(module));
    if (targetIdeInfo == null || targetIdeInfo.buildFile == null) {
        return;
    }

    // TODO: automagically edit deps instead of just opening the BUILD file?
    // Need to translate Gradle coordinates into blaze targets.
    // Will probably need to hardcode for each dependency.
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    PsiElement buildTargetPsi = BuildReferenceManager.getInstance(project)
            .resolveLabel(targetIdeInfo.key.label);
    if (buildTargetPsi != null) {
        // If we can find a PSI for the target,
        // then we can jump straight to the target in the build file.
        fileEditorManager.openTextEditor(new OpenFileDescriptor(project,
                buildTargetPsi.getContainingFile().getVirtualFile(), buildTargetPsi.getTextOffset()), true);
    } else {
        // If not, just the build file is good enough.
        File buildIoFile = blazeProjectData.artifactLocationDecoder.decode(targetIdeInfo.buildFile);
        VirtualFile buildVirtualFile = findFileByIoFile(buildIoFile);
        if (buildVirtualFile != null) {
            fileEditorManager.openFile(buildVirtualFile, true);
        }
    }
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected Editor createEditor(VirtualFile file) {
    final FileEditorManager instance = FileEditorManager.getInstance(myProject);

    if (file.getFileType().isBinary())
        return null;

    Editor editor = instance.openTextEditor(new OpenFileDescriptor(myProject, file, 0), false);
    ((EditorImpl) editor).setCaretActive();
    return editor;
}

From source file:com.intellij.codeInsight.generation.OverrideImplementUtil.java

License:Apache License

public static List<PsiGenerationInfo<PsiMethod>> overrideOrImplement(PsiClass psiClass,
        @NotNull PsiMethod baseMethod) throws IncorrectOperationException {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(baseMethod.getProject());
    List<PsiGenerationInfo<PsiMethod>> results = new ArrayList<PsiGenerationInfo<PsiMethod>>();
    try {//  w ww  . j a v a 2s .  c  o m

        List<PsiGenerationInfo<PsiMethod>> prototypes = convert2GenerationInfos(
                overrideOrImplementMethod(psiClass, baseMethod, false));
        if (prototypes.isEmpty()) {
            return null;
        }

        PsiSubstitutor substitutor = TypeConversionUtil
                .getSuperClassSubstitutor(baseMethod.getContainingClass(), psiClass, PsiSubstitutor.EMPTY);
        PsiElement anchor = getDefaultAnchorToOverrideOrImplement(psiClass, baseMethod, substitutor);
        results = GenerateMembersUtil.insertMembersBeforeAnchor(psiClass, anchor, prototypes);

        return results;
    } finally {

        PsiFile psiFile = psiClass.getContainingFile();
        Editor editor = fileEditorManager
                .openTextEditor(new OpenFileDescriptor(psiFile.getProject(), psiFile.getVirtualFile()), false);
        if (editor != null && !results.isEmpty()) {
            results.get(0).positionCaret(editor, true);
            editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
        }
    }
}

From source file:com.intellij.codeInsight.intention.impl.CopyAbstractMethodImplementationHandler.java

License:Apache License

private void copyImplementation(final PsiMethod sourceMethod) {
    final List<PsiMethod> generatedMethods = new ArrayList<PsiMethod>();
    new WriteCommandAction(myProject, getTargetFiles()) {
        @Override/*from w w w .  ja  v a  2s.  c om*/
        protected void run(final Result result) throws Throwable {
            for (PsiEnumConstant enumConstant : myTargetEnumConstants) {
                PsiClass initializingClass = enumConstant.getOrCreateInitializingClass();
                myTargetClasses.add(initializingClass);
            }
            for (PsiClass psiClass : myTargetClasses) {
                final Collection<PsiMethod> methods = OverrideImplementUtil.overrideOrImplementMethod(psiClass,
                        myMethod, true);
                final Iterator<PsiMethod> iterator = methods.iterator();
                if (!iterator.hasNext())
                    continue;
                PsiMethod overriddenMethod = iterator.next();
                final PsiCodeBlock body = overriddenMethod.getBody();
                final PsiCodeBlock sourceBody = sourceMethod.getBody();
                assert body != null && sourceBody != null;
                ChangeContextUtil.encodeContextInfo(sourceBody, true);
                final PsiElement newBody = body.replace(sourceBody.copy());
                ChangeContextUtil.decodeContextInfo(newBody, psiClass, null);

                PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(mySourceClass,
                        psiClass, PsiSubstitutor.EMPTY);
                PsiElement anchor = OverrideImplementUtil.getDefaultAnchorToOverrideOrImplement(psiClass,
                        sourceMethod, substitutor);
                try {
                    if (anchor != null) {
                        overriddenMethod = (PsiMethod) anchor.getParent().addBefore(overriddenMethod, anchor);
                    } else {
                        overriddenMethod = (PsiMethod) psiClass.addBefore(overriddenMethod,
                                psiClass.getRBrace());
                    }
                    generatedMethods.add(overriddenMethod);
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            }
        }
    }.execute();
    if (generatedMethods.size() > 0) {
        PsiMethod target = generatedMethods.get(0);
        PsiFile psiFile = target.getContainingFile();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(psiFile.getProject());
        Editor editor = fileEditorManager
                .openTextEditor(new OpenFileDescriptor(psiFile.getProject(), psiFile.getVirtualFile()), false);
        if (editor != null) {
            GenerateMembersUtil.positionCaret(editor, target, true);
            editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
        }
    }
}

From source file:com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.java

License:Apache License

@Nullable
private Editor createEditor(VirtualFile file) {
    final Project project = getProject();
    final FileEditorManager instance = FileEditorManager.getInstance(project);
    if (file.getFileType().isBinary()) {
        return null;
    }//from  www  .ja v  a  2s. co  m
    return instance.openTextEditor(new OpenFileDescriptor(project, file, 0), false);
}

From source file:com.sixrr.guiceyidea.utils.EditorCaretMover.java

License:Apache License

@Nullable
public Editor openInEditor(PsiElement element) {
    final PsiFile psiFile;
    final int i;
    if (element instanceof PsiFile) {
        psiFile = (PsiFile) element;/*from w  w  w  .j  a  v a 2s. com*/
        i = -1;
    } else {
        psiFile = element.getContainingFile();
        i = element.getTextOffset();
    }
    if (psiFile == null) {
        return null;
    }
    final VirtualFile virtualFile = psiFile.getVirtualFile();
    if (virtualFile != null) {
        final OpenFileDescriptor fileDesc = new OpenFileDescriptor(project, virtualFile, i);
        final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        return fileEditorManager.openTextEditor(fileDesc, true);
    } else {
        Logger.getInstance("Foo").error("Virtual file doesn't exist");
        return null;
    }
}

From source file:com.sixrr.metrics.utils.EditorCaretMover.java

License:Apache License

@Nullable
public Editor openInEditor(PsiElement element) {

    final PsiFile psiFile;
    final int i;
    if (element instanceof PsiFile) {
        psiFile = (PsiFile) element;//www .j  a v  a2 s. c  om
        i = -1;
    } else {
        psiFile = element.getContainingFile();
        i = element.getTextOffset();
    }
    if (psiFile == null) {
        return null;
    }
    final OpenFileDescriptor fileDesc = new OpenFileDescriptor(project, psiFile.getVirtualFile(), i);
    disableMovementOneTime();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    return fileEditorManager.openTextEditor(fileDesc, false);
}

From source file:com.twitter.intellij.pants.highlighting.PantsHighlightingIntegrationTest.java

License:Apache License

@Nullable
protected Editor createEditor(@NotNull VirtualFile file) {
    final FileEditorManager instance = FileEditorManager.getInstance(myProject);
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    Editor editor = instance.openTextEditor(new OpenFileDescriptor(myProject, file), false);
    if (editor != null) {
        editor.getCaretModel().moveToOffset(0);
        DaemonCodeAnalyzer.getInstance(myProject).restart();
    }/*  ww  w .ja  va 2  s.co  m*/
    return editor;
}