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

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

Introduction

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

Prototype

@Nullable
public abstract Editor getSelectedTextEditor();

Source Link

Usage

From source file:com.android.tools.idea.configurations.ConfigurationMatcher.java

License:Apache License

@NotNull
private ConfigMatch selectConfigMatch(@NotNull List<ConfigMatch> matches) {
    // API 11-13: look for a x-large device
    Comparator<ConfigMatch> comparator = null;
    IAndroidTarget projectTarget = myManager.getProjectTarget();
    if (projectTarget != null) {
        int apiLevel = projectTarget.getVersion().getApiLevel();
        if (apiLevel >= 11 && apiLevel < 14) {
            // TODO: Maybe check the compatible-screen tag in the manifest to figure out
            // what kind of device should be used for display.
            comparator = new TabletConfigComparator();
        }//  w w  w  . ja v a  2 s . c  o m
    }

    if (comparator == null) {
        // lets look for a high density device
        comparator = new PhoneConfigComparator();
    }
    Collections.sort(matches, comparator);

    // Look at the currently active editor to see if it's a layout editor, and if so,
    // look up its configuration and if the configuration is in our match list,
    // use it. This means we "preserve" the current configuration when you open
    // new layouts.
    // TODO: This is running too late for the layout preview; the new editor has
    // already taken over so getSelectedTextEditor() returns self. Perhaps we
    // need to fish in the open editors instead.

    //Editor activeEditor = ApplicationManager.getApplication().runReadAction(new Computable<Editor>() {
    //  @Override
    //  public Editor compute() {
    //    FileEditorManager editorManager = FileEditorManager.getInstance(myManager.getProject());
    //    return editorManager.getSelectedTextEditor();
    //  }
    //});

    // TODO: How do I redispatch without risking lock?
    //Editor activeEditor = AndroidUtils.getSelectedEditor(myManager.getProject());
    if (ApplicationManager.getApplication().isDispatchThread()) {
        FileEditorManager editorManager = FileEditorManager.getInstance(myManager.getProject());
        Editor activeEditor = editorManager.getSelectedTextEditor();
        if (activeEditor != null) {
            FileDocumentManager documentManager = FileDocumentManager.getInstance();
            VirtualFile file = documentManager.getFile(activeEditor.getDocument());
            if (file != null && file != myFile && file.getFileType() == StdFileTypes.XML
                    && ResourceFolderType.LAYOUT
                            .equals(ResourceFolderType.getFolderType(file.getParent().getName()))) {
                Configuration configuration = myManager.getConfiguration(file);
                FolderConfiguration fullConfig = configuration.getFullConfig();
                for (ConfigMatch match : matches) {
                    if (fullConfig.equals(match.testConfig)) {
                        return match;
                    }
                }
            }
        }
    }

    // the list has been sorted so that the first item is the best config
    return matches.get(0);
}

From source file:com.android.tools.idea.uibuilder.actions.GenerateLayoutTestSkeletonAction.java

License:Apache License

@Nullable
private static DesignSurface getSurface(@NotNull Project project) {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    FileEditor[] editors = fileEditorManager.getSelectedEditors();
    for (FileEditor fileEditor : editors) {
        if (fileEditor instanceof NlEditor) {
            return ((NlEditor) fileEditor).getComponent().getSurface();
        }//from  ww  w . jav  a  2  s  .com
    }

    Editor editor = fileEditorManager.getSelectedTextEditor();
    if (editor == null) {
        return null;
    }

    NlPreviewManager previewManager = NlPreviewManager.getInstance(project);
    if (previewManager.isWindowVisible()) {
        return previewManager.getPreviewForm().getSurface();
    }

    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file == null) {
        return null;
    }

    for (FileEditor fileEditor : fileEditorManager.getEditors(file.getVirtualFile())) {
        if (fileEditor instanceof NlEditor) {
            return ((NlEditor) fileEditor).getComponent().getSurface();
        }
    }

    return null;
}

From source file:com.atlassian.theplugin.idea.ui.linkhiglighter.FileEditorListenerImpl.java

License:Apache License

public void fileOpened(final FileEditorManager fileEditorManager, final VirtualFile virtualFile) {
    PluginUtil.getLogger().debug(" file opened: " + virtualFile.getPath() + ", source: "
            + fileEditorManager.getSelectedTextEditor());
}

From source file:com.atlassian.theplugin.idea.ui.linkhiglighter.FileEditorListenerImpl.java

License:Apache License

public void fileClosed(final FileEditorManager fileEditorManager, final VirtualFile virtualFile) {
    removeHighlighter(virtualFile);//from  w w  w  . j ava2s . com
    linkHighlighters.remove(virtualFile);
    PluginUtil.getLogger().debug(" file closed: " + virtualFile.getPath() + ", source: "
            + fileEditorManager.getSelectedTextEditor());
}

From source file:com.atlassian.theplugin.idea.ui.linkhiglighter.FileEditorListenerImpl.java

License:Apache License

public void selectionChanged(final FileEditorManagerEvent event) {
    final FileEditorManager editorManager = event.getManager();
    if (project != editorManager.getProject()) {
        assert false : this;
        return;/*from ww  w.  ja v a2  s .c o m*/
    }
    VirtualFile newFile = event.getNewFile();

    VirtualFile oldFile = event.getOldFile();

    if (oldFile != null && newFile == null) {
        removeHighlighter(oldFile);
        linkHighlighters.remove(oldFile);

    } else if (newFile != null && !linkHighlighters.containsKey(newFile)) {
        PsiFile psiFile = safeFindFile(newFile);
        if (psiFile != null) {
            Editor editor = editorManager.getSelectedTextEditor();
            if (editor != null) {
                addHighlighter(newFile, psiFile, editor);
            }
        }
    }
}

From source file:com.atlassian.theplugin.idea.ui.linkhiglighter.FileEditorListenerImpl.java

License:Apache License

public void scanOpenEditors() {
    final FileEditorManager editorManager = FileEditorManager.getInstance(project);
    if (editorManager == null) {
        return;//from   w  w  w  .j  a v  a2  s .  c  o m
    }
    for (VirtualFile openFile : editorManager.getSelectedFiles()) {
        if (!linkHighlighters.containsKey(openFile)) {
            PsiFile psiFile = safeFindFile(openFile);
            if (psiFile != null) {
                Editor editor = editorManager.getSelectedTextEditor();
                if (editor != null) {
                    addHighlighter(openFile, psiFile, editor);
                }
            }

        } else {
            linkHighlighters.get(openFile).reparseAll();
            linkHighlighters.get(openFile).checkComments();
        }
    }

}

From source file:com.boxysystems.libraryfinder.view.intellij.ui.FindLibraryDialog.java

License:Apache License

public void setEditorSelectedTextToFileName() {
    final FileEditorManager editorManager = FileEditorManager.getInstance(project);
    final Editor editor = editorManager.getSelectedTextEditor();

    if (editor != null && hasSelectedText(editor)) {
        String selectedText = editor.getSelectionModel().getSelectedText();
        if (selectedText != null) {
            txtFileNamePattern.setText(selectedText.trim());
            txtFileNamePattern.selectText();
        }/*from w w  w . j a v a2s  . co  m*/
    }
}

From source file:com.dvd.intellijdea.materialcolorpalette.UtilsEnvironment.java

License:Apache License

private static Editor getEditor(Project curProject) {
    if (curProject == null) {
        curProject = getOpenProject();/*from   www.  j av  a2s . c o m*/
    }
    if (curProject != null) {
        FileEditorManager manager = FileEditorManager.getInstance(curProject);

        return manager.getSelectedTextEditor();
    }

    return null;
}

From source file:com.facebook.buck.intellij.ideabuck.fixup.MoveResourceFiles.java

License:Apache License

@Override
public void onFileCreate(VFileCreateEvent event, Facet facet) {
    Project project = facet.getModule().getProject();

    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    VirtualFile[] selectedFiles = fileEditorManager.getSelectedFiles();
    if (selectedFiles.length != 1) {
        log("Oh, dear. We have a new file in a Project View resource directory, but we have %d selected files",
                selectedFiles.length);/*from   w w  w  .j av  a  2 s .c  o  m*/
        ErrorDialog.showErrorDialog(project, "Error moving new file",
                "We have a new file in a Project View resource directory, but we have %d selected files and so don't know which BUCK file to examine",
                selectedFiles.length);
        return; // we are expecting a SINGLE file, here
    }
    VirtualFile selection = selectedFiles[0];

    Editor editor = fileEditorManager.getSelectedTextEditor();

    moveResourceFile(event.getPath(), project, selection, editor);
}

From source file:com.intellij.codeInsight.highlighting.BraceHighlightingHandler.java

License:Apache License

private void highlightBraces(@Nullable TextRange lBrace, @Nullable TextRange rBrace, boolean matched,
        boolean scopeHighlighting, @NotNull FileType fileType) {
    if (!matched && fileType == PlainTextFileType.INSTANCE) {
        return;// w w  w.j  a va  2  s.  c  om
    }

    EditorColorsScheme scheme = myEditor.getColorsScheme();
    final TextAttributes attributes = matched ? scheme.getAttributes(CodeInsightColors.MATCHED_BRACE_ATTRIBUTES)
            : scheme.getAttributes(CodeInsightColors.UNMATCHED_BRACE_ATTRIBUTES);

    if (rBrace != null && !scopeHighlighting) {
        highlightBrace(rBrace, matched);
    }

    if (lBrace != null && !scopeHighlighting) {
        highlightBrace(lBrace, matched);
    }

    FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject); // null in default project
    if (fileEditorManager == null || !myEditor.equals(fileEditorManager.getSelectedTextEditor())) {
        return;
    }

    if (lBrace != null && rBrace != null) {
        final int startLine = myEditor.offsetToLogicalPosition(lBrace.getStartOffset()).line;
        final int endLine = myEditor.offsetToLogicalPosition(rBrace.getEndOffset()).line;
        if (endLine - startLine > 0) {
            final Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (myProject.isDisposed() || myEditor.isDisposed())
                        return;
                    Color color = attributes.getBackgroundColor();
                    if (color == null)
                        return;
                    color = UIUtil.isUnderDarcula() ? ColorUtil.shift(color, 1.1d) : color.darker();
                    lineMarkFragment(startLine, endLine, color);
                }
            };

            if (!scopeHighlighting) {
                myAlarm.addRequest(runnable, 300);
            } else {
                runnable.run();
            }
        } else {
            if (!myCodeInsightSettings.HIGHLIGHT_SCOPE) {
                removeLineMarkers();
            }
        }

        if (!scopeHighlighting) {
            showScopeHint(lBrace.getStartOffset(), lBrace.getEndOffset());
        }
    } else {
        if (!myCodeInsightSettings.HIGHLIGHT_SCOPE) {
            removeLineMarkers();
        }
    }
}