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

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

Introduction

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

Prototype

@Nullable
public abstract FileEditor getSelectedEditor(@NotNull VirtualFile file);

Source Link

Usage

From source file:com.android.tools.idea.uibuilder.model.NlModel.java

License:Apache License

/**
 * Changes the configuration to use a custom device with screen size defined by xDimension and yDimension.
 *//*  w ww.  j av a  2s  . c o m*/
public void overrideConfigurationScreenSize(@AndroidCoordinate int xDimension,
        @AndroidCoordinate int yDimension) {
    Device original = myConfiguration.getDevice();
    Device.Builder deviceBuilder = new Device.Builder(original); // doesn't copy tag id
    if (original != null) {
        deviceBuilder.setTagId(original.getTagId());
    }
    deviceBuilder.setName("Custom");
    deviceBuilder.setId(Configuration.CUSTOM_DEVICE_ID);
    Device device = deviceBuilder.build();
    for (State state : device.getAllStates()) {
        Screen screen = state.getHardware().getScreen();
        screen.setXDimension(xDimension);
        screen.setYDimension(yDimension);

        double dpi = screen.getPixelDensity().getDpiValue();
        double width = xDimension / dpi;
        double height = yDimension / dpi;
        double diagonalLength = Math.sqrt(width * width + height * height);

        screen.setDiagonalLength(diagonalLength);
        screen.setSize(AvdScreenData.getScreenSize(diagonalLength));

        screen.setRatio(AvdScreenData.getScreenRatio(xDimension, yDimension));

        screen.setScreenRound(device.getDefaultHardware().getScreen().getScreenRound());
        screen.setChin(device.getDefaultHardware().getScreen().getChin());
    }

    // If a custom device already exists, remove it before adding the latest one
    List<Device> devices = myConfiguration.getConfigurationManager().getDevices();
    boolean customDeviceReplaced = false;
    for (int i = 0; i < devices.size(); i++) {
        if ("Custom".equals(devices.get(i).getId())) {
            devices.set(i, device);
            customDeviceReplaced = true;
            break;
        }
    }

    if (!customDeviceReplaced) {
        devices.add(device);
    }

    VirtualFile better;
    State newState;
    //Change the orientation of the device depending on the shape of the canvas
    if (xDimension > yDimension) {
        better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Landscape", null, null);
        newState = device.getState("Landscape");
    } else {
        better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Portrait", null, null);
        newState = device.getState("Portrait");
    }

    if (better != null) {
        VirtualFile old = myConfiguration.getFile();
        assert old != null;
        Project project = mySurface.getProject();
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, better, -1);
        FileEditorManager manager = FileEditorManager.getInstance(project);
        FileEditor selectedEditor = manager.getSelectedEditor(old);
        manager.openEditor(descriptor, true);
        // Switch to the same type of editor (XML or Layout Editor) in the target file
        if (selectedEditor instanceof NlEditor) {
            manager.setSelectedEditor(better, NlEditorProvider.DESIGNER_ID);
        } else if (selectedEditor != null) {
            manager.setSelectedEditor(better, TextEditorProvider.getInstance().getEditorTypeId());
        }

        AndroidFacet facet = AndroidFacet.getInstance(myConfiguration.getModule());
        assert facet != null;
        Configuration configuration = facet.getConfigurationManager().getConfiguration(better);
        configuration.setEffectiveDevice(device, newState);
    } else {
        myConfiguration.setEffectiveDevice(device, newState);
    }
}

From source file:com.intellij.ide.macro.MacroManager.java

License:Apache License

private static DataContext getCorrectContext(DataContext dataContext) {
    if (PlatformDataKeys.FILE_EDITOR.getData(dataContext) != null) {
        return dataContext;
    }// www . j a  v a  2s  .c om
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        return dataContext;
    }
    FileEditorManager editorManager = FileEditorManager.getInstance(project);
    VirtualFile[] files = editorManager.getSelectedFiles();
    if (files.length == 0) {
        return dataContext;
    }
    FileEditor fileEditor = editorManager.getSelectedEditor(files[0]);
    return fileEditor == null ? dataContext
            : DataManager.getInstance().getDataContext(fileEditor.getComponent());
}

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  a  v  a  2 s  .c o m
            }
        }
    }
}

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

License:Open Source License

private void insertEditorHeaderComponentIfApplicable(@NotNull FileEditorManager source,
        @NotNull VirtualFile file) {/*from ww  w . j a  v a  2s  . c o m*/
    if (file.getFileType() == JSGraphQLFileType.INSTANCE
            || JSGraphQLFileType.isGraphQLScratchFile(source.getProject(), file)) {
        FileEditor fileEditor = source.getSelectedEditor(file);
        if (fileEditor instanceof TextEditor) {
            final Editor editor = ((TextEditor) fileEditor).getEditor();
            if (editor.getHeaderComponent() instanceof JSGraphQLEditorHeaderComponent) {
                return;
            }
            UIUtil.invokeLaterIfNeeded(() -> { // ensure components are created on the swing thread
                final JComponent headerComponent = createHeaderComponent(fileEditor, editor);
                editor.setHeaderComponent(headerComponent);
                if (editor instanceof EditorEx) {
                    ((EditorEx) editor).setPermanentHeaderComponent(headerComponent);
                }
                editor.getScrollingModel().scrollVertically(-1000);
            });
        }
    }
}

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

License:Open Source License

private void markSchemaFileAsViewer(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
    if (!isProjectSchemaFile(file)) {
        // only the project schema should be read only
        return;/*from w w w .  j av a2 s . com*/
    }
    if (file.getFileType() == JSGraphQLSchemaFileType.INSTANCE
            || JSGraphQLSchemaFileType.isGraphQLScratchFile(project, file)) {
        // mark the schema editor as viewer only
        final FileEditor fileEditor = source.getSelectedEditor(file);
        if (fileEditor instanceof TextEditor) {
            final Editor editor = ((TextEditor) fileEditor).getEditor();
            if (editor instanceof EditorEx) {
                ((EditorEx) editor).setViewer(true);
            }
        }
    }
}

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

License:Apache License

@Nullable
public static Editor getEditorIfSelected(Project project, PsiElement psiElement) {
    final VirtualFile elementFile = getVirtualFile(psiElement);
    if (elementFile == null) {
        return null;
    }/*  ww  w.j a  va2  s.  co m*/

    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    final FileEditor fileEditor = fileEditorManager.getSelectedEditor(elementFile);

    Editor editor = null;

    if (fileEditor != null && fileEditor instanceof TextEditor) {
        editor = ((TextEditor) fileEditor).getEditor();
    }

    return editor;
}

From source file:org.moe.designer.configurations.ConfigurationAction.java

License:Apache License

protected void pickedBetterMatch(@NotNull VirtualFile file, @NotNull VirtualFile old) {
    // Switch files, and leave this configuration alone
    Module module = myRenderContext.getModule();
    assert module != null;
    Project project = module.getProject();
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, -1);
    FileEditorManager manager = FileEditorManager.getInstance(project);
    FileEditor selectedEditor = manager.getSelectedEditor(old);
    List<FileEditor> editors = manager.openEditor(descriptor, true);

    // Switch to the same type of editor (XML or Layout Editor) in the target file
    if (selectedEditor instanceof IOSDesignerEditor) {
        manager.setSelectedEditor(file, IXMLFileEditorProvider.ANDROID_DESIGNER_ID);
    } else if (selectedEditor != null) {
        manager.setSelectedEditor(file, TextEditorProvider.getInstance().getEditorTypeId());

        // Proactively switch to the new editor right away in the layout XML preview, if applicable
        if (!editors.isEmpty()) {
            for (FileEditor editor : editors) {
                if (editor instanceof TextEditor && editor.getComponent().isShowing()) {
                    //TODO: implement preview window if it is important
                    //            AndroidLayoutPreviewToolWindowManager previewManager = AndroidLayoutPreviewToolWindowManager.getInstance(project);
                    //            previewManager.notifyFileShown((TextEditor)editor, true);
                    break;
                }/*w w w. j a  va2s .  c  o  m*/
            }
        }
    }
}

From source file:org.moe.idea.utils.FileEditorListener.java

License:Apache License

@Override
public void fileOpened(@NotNull FileEditorManager fileEditorManager, @NotNull VirtualFile virtualFile) {
    FileEditor editor = fileEditorManager.getSelectedEditor(virtualFile);

    if (editor != null) {
        String className = editor.getClass().getSimpleName();
    }/*from ww w.j av  a2 s . co m*/
}