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

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

Introduction

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

Prototype

public FileEditor @NotNull [] openFile(@NotNull VirtualFile file, boolean focusEditor, boolean searchForOpen) 

Source Link

Document

Opens a file.

Usage

From source file:ch.mjava.intellij.tapestry.TapestrySwitcher.java

License:Apache License

public void actionPerformed(AnActionEvent e) {
    List<PsiFile> files = getPartnerFiles(e.getData(LangDataKeys.PSI_FILE));

    if (files.isEmpty()) {
        PluginHelper.showErrorBalloonWith("No partner file found", e.getDataContext());
    } else {/*from  w  ww .j a v a 2s. co m*/
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(e.getProject());
        for (PsiFile file : files) {
            fileEditorManager.openFile(file.getVirtualFile(), true, true);
        }
    }
}

From source file:com.android.tools.idea.experimental.actions.PermissionUsageQuickFix.java

License:Apache License

public static void openFile(Project project, PsiFile file) {
    String path = file.getVirtualFile().getCanonicalPath();
    assert path != null;
    FileEditorManager manager = FileEditorManager.getInstance(project);
    VirtualFile virtFile = LocalFileSystem.getInstance().findFileByPath(path);
    assert virtFile != null;
    manager.openFile(virtFile, true, true);

}

From source file:com.github.pshirshov.ShowByteCodeAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final Project project = e.getProject();
    if (project == null) {
        return;/*  w w  w. ja v a 2  s.c o  m*/
    }
    final Editor editor = e.getData(CommonDataKeys.EDITOR);

    final PsiElement psiElement = PsiUtils.getPsiElement(dataContext, project, editor);
    if (psiElement == null) {
        return;
    }

    final String psiElementTitle = PsiUtils.getTitle(psiElement);

    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(psiElement);
    if (virtualFile == null) {
        return;
    }

    final SmartPsiElementPointer element = SmartPointerManager.getInstance(project)
            .createSmartPsiElementPointer(psiElement);

    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Looking for bytecode...") {
        private String myByteCode;
        private String myErrorMessage;
        private String myErrorTitle;

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            if (ProjectRootManager.getInstance(project).getFileIndex().isInContent(virtualFile)
                    && isMarkedForCompilation(project, virtualFile)) {
                myErrorMessage = "Unable to show bytecode for '" + psiElementTitle
                        + "'. Class file does not exist or is out-of-date.";
                myErrorTitle = "Class File Out-Of-Date";
            } else {
                myByteCode = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
                    @Override
                    public String compute() {
                        return new BytecodeConverter(disassembleStrategy).getByteCode(psiElement);
                    }
                });
            }
        }

        @Override
        public void onSuccess() {
            if (project.isDisposed()) {
                return;
            }

            if ((myErrorMessage != null) && (myTitle != null)) {
                Messages.showWarningDialog(project, myErrorMessage, myErrorTitle);
                return;
            }
            final PsiElement targetElement = element.getElement();
            if (targetElement == null) {
                return;
            }

            if (myByteCode == null) {
                Messages.showErrorDialog(project, "Unable to parse class file for '" + psiElementTitle + "'.",
                        "Bytecode not Found");
                return;
            }

            PsiClass psiClass = PsiUtils.getContainingClass(psiElement);

            FileEditorManager manager = FileEditorManager.getInstance(project);

            final String filename = '/' + psiClass.getQualifiedName().replace('.', '/') + ".bc";
            BCEVirtualFile BCEVirtualFile = new BCEVirtualFile(filename, JavaClassFileType.INSTANCE,
                    myByteCode.getBytes(), psiElement, disassembleStrategy);

            for (FileEditor fileEditor : FileEditorManager.getInstance(project).getAllEditors()) {
                if (fileEditor instanceof ByteCodeEditor) {
                    final ByteCodeEditor asBce = (ByteCodeEditor) fileEditor;
                    if (asBce.getFile().getPath().equals(BCEVirtualFile.getPath())) {
                        FileEditorManager.getInstance(project).openFile(asBce.getFile(), true, true);
                        asBce.update(BCEVirtualFile);
                        return;
                    }
                }
            }

            manager.openFile(BCEVirtualFile, true, true);

        }
    });
}

From source file:com.intellij.codeInsight.navigation.NavigationUtil.java

License:Apache License

private static boolean activatePsiElementIfOpen(@NotNull PsiElement elt, boolean searchForOpen,
        boolean requestFocus) {
    if (!elt.isValid())
        return false;
    elt = elt.getNavigationElement();/*  w  ww.ja v  a2 s .  c  om*/
    final PsiFile file = elt.getContainingFile();
    if (file == null || !file.isValid())
        return false;

    VirtualFile vFile = file.getVirtualFile();
    if (vFile == null)
        return false;

    if (!EditorHistoryManager.getInstance(elt.getProject()).hasBeenOpen(vFile))
        return false;

    final FileEditorManager fem = FileEditorManager.getInstance(elt.getProject());
    if (!fem.isFileOpen(vFile)) {
        fem.openFile(vFile, requestFocus, searchForOpen);
    }

    final TextRange range = elt.getTextRange();
    if (range == null)
        return false;

    final FileEditor[] editors = fem.getEditors(vFile);
    for (FileEditor editor : editors) {
        if (editor instanceof TextEditor) {
            final Editor text = ((TextEditor) editor).getEditor();
            final int offset = text.getCaretModel().getOffset();

            if (range.containsOffset(offset)) {
                // select the file
                fem.openFile(vFile, requestFocus, searchForOpen);
                return true;
            }
        }
    }

    return false;
}

From source file:com.intellij.lang.jsgraphql.ide.actions.JSGraphQLEditEndpointsAction.java

License:Open Source License

@Override
public void actionPerformed(AnActionEvent e) {
    final Project myProject = e.getData(CommonDataKeys.PROJECT);
    if (myProject != null) {
        VirtualFile config = JSGraphQLConfigurationProvider.getService(myProject).getGraphQLConfigFile();
        if (config != null) {
            final FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
            fileEditorManager.openFile(config, true, true);
            final FileEditor configEditor = fileEditorManager.getSelectedEditor(config);
            if (configEditor instanceof TextEditor) {
                final TextEditor textEditor = (TextEditor) configEditor;
                final int endpointsIndex = textEditor.getEditor().getDocument().getText()
                        .indexOf("\"endpoints\"");
                if (endpointsIndex != -1) {
                    textEditor.getEditor().getCaretModel().moveToOffset(endpointsIndex);
                    textEditor.getEditor().getScrollingModel().scrollToCaret(ScrollType.CENTER);
                }/*from   w  w w . j  av a 2  s .com*/
            }
        }
    }
}

From source file:com.intellij.lang.jsgraphql.ide.editor.JSGraphQLQueryContextHighlightVisitor.java

License:Open Source License

/**
 * Locates the first operation in the specified editor and places the caret inside it
 *//*from w  w w  .  ja v  a 2 s.c o  m*/
private static void placeCaretInsideFirstOperation(Editor editor, PsiFile psiFile) {
    if (editor.isDisposed()) {
        return;
    }
    if (psiFile.isValid() && psiFile.getVirtualFile() != null && psiFile.getVirtualFile().isValid()) {
        for (PsiElement psiElement : psiFile.getChildren()) {
            if (psiElement instanceof PsiWhiteSpace) {
                continue;
            }
            PsiElement operationOrNull = asOperationOrNull(psiElement);
            if (operationOrNull != null) {
                final Project project = editor.getProject();
                if (project != null) {
                    // try to find the name of the operation
                    final JSGraphQLNamedTypePsiElement name = PsiTreeUtil.getChildOfType(operationOrNull,
                            JSGraphQLNamedTypePsiElement.class);
                    if (name != null && name.isDefinition()) {
                        operationOrNull = name;
                    }
                    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
                    fileEditorManager.openFile(psiFile.getVirtualFile(), true, true);
                    editor.getSelectionModel().removeSelection();
                    editor.getCaretModel().moveToOffset(operationOrNull.getTextOffset());
                    editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
                }
                return;
            }
        }
    }
}

From source file:com.intellij.lang.jsgraphql.ide.project.JSGraphQLLanguageUIProjectService.java

License:Open Source License

private void createToolWindowResultEditor(ToolWindow toolWindow) {

    final LightVirtualFile virtualFile = new LightVirtualFile("GraphQL.result.json", JsonFileType.INSTANCE, "");
    fileEditor = PsiAwareTextEditorProvider.getInstance().createEditor(myProject, virtualFile);

    if (fileEditor instanceof TextEditor) {
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final EditorEx editorEx = (EditorEx) editor;

        // set read-only mode
        editorEx.setViewer(true);/* w ww  . ja  va2s .c o m*/
        editorEx.getSettings().setShowIntentionBulb(false);
        editor.getSettings().setAdditionalLinesCount(0);
        editor.getSettings().setCaretRowShown(false);
        editor.getSettings().setBlinkCaret(false);

        // query result header
        final JSGraphQLEditorHeaderComponent header = new JSGraphQLEditorHeaderComponent();

        querySuccessLabel = new JBLabel();
        querySuccessLabel.setVisible(false);
        querySuccessLabel.setIconTextGap(0);
        header.add(querySuccessLabel, BorderLayout.WEST);

        queryResultLabel = new JBLabel("", null, SwingConstants.LEFT);
        queryResultLabel.setBorder(new EmptyBorder(4, 6, 4, 6));
        queryResultLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        queryResultLabel.setVisible(false);
        queryResultLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final String fileUrl = (String) queryResultLabel.getClientProperty(FILE_URL_PROPERTY);
                if (fileUrl != null) {
                    final VirtualFile queryFile = VirtualFileManager.getInstance().findFileByUrl(fileUrl);
                    if (queryFile != null) {
                        final FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
                        fileEditorManager.openFile(queryFile, true, true);
                    }
                }
            }
        });
        header.add(queryResultLabel, BorderLayout.CENTER);

        // finally set the header as permanent such that it's restored after searches
        editor.setHeaderComponent(header);
        editorEx.setPermanentHeaderComponent(header);
    }

    Disposer.register(this, fileEditor);

    final ContentImpl content = new ContentImpl(fileEditor.getComponent(), "Query result", true);
    content.setCloseable(false);
    toolWindow.getContentManager().addContent(content);

}

From source file:org.intellij.ideaplugins.tabswitch.action.SwitchTabAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent event) {
    Project project = PlatformDataKeys.PROJECT.getData(event.getDataContext());
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    VirtualFile file = getFile(project, fileEditorManager);
    if (file != null && file.isValid()) {
        fileEditorManager.openFile(file, true, true);
    }/*from  www .j  av  a2  s.c  o m*/
}

From source file:org.onehippo.intellij.freemarker.LoadFiles.java

License:Apache License

public void saveTemplates(final RepositoryConnector connector, final Project project,
        final Set<HippoTemplate> templates, final String ftlFolder) {
    try {/*from  w w w  .j  av  a 2s. c  om*/

        final File file = new File(ftlFolder);
        if (!file.exists()) {
            file.mkdir();
        }

        final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);

        for (HippoTemplate template : templates) {
            final RepositoryConnector.TemplateData path = connector.createFilePath(template.getPath(), file);
            final String fileName = path.getFileName();
            final File templateFile = new File(fileName);
            if (!templateFile.exists()) {
                templateFile.createNewFile();
            }
            final Writer writer = new BufferedWriter(new FileWriter(templateFile));
            try {
                writer.write(template.getScript());
                writer.flush();
                writer.close();
            } finally {
                writer.close();
            }
            final LocalFileSystem instance = LocalFileSystem.getInstance();
            instance.refresh(true);
            final VirtualFile vf = instance.findFileByPath(templateFile.getAbsolutePath());
            if (vf != null) {
                fileEditorManager.openFile(vf, true, true);
            }

        }
    } catch (IOException e) {
        FreemarkerEditor.error(e.getMessage(), project);
    }

}

From source file:org.onehippo.intellij.groovy.LoadFiles.java

License:Apache License

public boolean saveTemplates(final RepositoryConnector connector, final Project project,
        final Set<FileDialogData> templates, final String groovyFolder) {
    final GroovySessionComponent sessionComponent = project.getComponent(GroovySessionComponent.class);
    try {/*from  w ww . ja  va2  s  . c  o  m*/

        final File file = new File(groovyFolder);
        if (!file.exists()) {
            file.mkdir();
        }

        final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);

        for (FileDialogData template : templates) {
            final RepositoryConnector.TemplateData path = connector.createFilePath(template.getNodePath(), file,
                    template.getContent());
            final String fileName = path.getFileName();
            final File templateFile = new File(fileName);
            if (!templateFile.exists()) {
                templateFile.createNewFile();
            }
            final Writer writer = new BufferedWriter(new FileWriter(templateFile));
            try {
                writer.write(template.getContent());
                writer.flush();
                writer.close();
            } finally {
                writer.close();
            }
            final LocalFileSystem instance = LocalFileSystem.getInstance();
            instance.refresh(true);
            final VirtualFile vf = instance.findFileByPath(templateFile.getAbsolutePath());
            if (vf != null) {
                fileEditorManager.openFile(vf, true, true);
                template.setScriptName(PATTERN.matcher(vf.getName()).replaceAll(""));
                template.setLocation(path.getLocation().getName());
                sessionComponent.saveState(template, vf);
            }

        }
        return true;
    } catch (IOException e) {
        GroovyEditor.error(e.getMessage(), project);
    }

    return false;

}