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

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

Introduction

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

Prototype

public abstract void setSelectedEditor(@NotNull VirtualFile file, @NotNull String fileEditorProviderId);

Source Link

Document

Selects a specified file editor tab for the specified editor.

Usage

From source file:com.android.tools.idea.actions.AndroidShowNavigationEditor.java

License:Apache License

public void showNavigationEditor(@Nullable Project project, @Nullable Module module, final String dir,
        final String file) {
    if (project == null) {
        return;//  w w w  . ja  v  a 2  s .  co  m
    }
    if (module == null) {
        return;
    }
    VirtualFile baseDir = project.getBaseDir();
    if (baseDir == null) { // this happens when we have the 'default' project; can't launch nav editor from here
        return;
    }
    VirtualFile navFile = NavigationEditorUtils.getNavigationFile(baseDir, module.getName(), dir, file);
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, navFile, 0);
    FileEditorManager manager = FileEditorManager.getInstance(project);
    manager.openEditor(descriptor, true);
    manager.setSelectedEditor(navFile, NavigationEditorProvider.ID);
}

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 w  w.j a v a2s .c om
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:org.cordovastudio.actions.ShowStoryboardEditorAction.java

License:Apache License

public void showNavigationEditor(@Nullable Project project, final String dir, final String file) {
    if (project == null) {
        return;/*from   www.  j a v  a2 s  .com*/
    }
    final VirtualFile baseDir = project.getBaseDir();
    if (baseDir == null) { // this happens when we have the 'default' project, we can't launch nav editor here
        return;
    }
    final String relativePathOfNavDir = ".navigation" + "/" + dir;
    VirtualFile navFile = baseDir.findFileByRelativePath(relativePathOfNavDir + "/" + file);
    if (navFile == null) {
        navFile = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
            @Override
            public VirtualFile compute() {
                try {
                    VirtualFile dir = mkDirs(baseDir, relativePathOfNavDir);
                    return dir.createChildData(null, file);
                } catch (IOException e) {
                    assert false;
                    return null;
                }

            }
        });
    }
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, navFile, 0);
    FileEditorManager manager = FileEditorManager.getInstance(project);
    manager.openEditor(descriptor, true);
    manager.setSelectedEditor(navFile, CordovaStoryboardEditorProvider.ID);
}

From source file:org.cordovastudio.editors.storyboard.model.CordovaRootComponent.java

License:Apache License

public void launchLayoutEditor() {
    if (myLayoutFile != null) {
        Project project = myRenderingParameters.getProject();
        VirtualFile virtualFile = myLayoutFile.getVirtualFile();
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, 0);
        FileEditorManager manager = FileEditorManager.getInstance(project);
        manager.openEditor(descriptor, true);
        manager.setSelectedEditor(virtualFile, CordovaDesignerEditorProvider.CORDOVA_DESIGNER_ID);
    }/*from  w ww  .  java2  s  .co  m*/
}

From source file:org.exbin.deltahex.intellij.DeltaHexFileEditor.java

License:Apache License

public DeltaHexFileEditor(Project project) {
    this.project = project;
    editorPanel = new JPanel();
    initComponents();//from www  . jav a2s.  c  o m

    preferences = getPreferences();

    codeArea = new CodeArea();
    codeArea.setPainter(new HighlightNonAsciiCodeAreaPainter(codeArea));
    codeArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    codeArea.getCaret().setBlinkRate(300);
    statusPanel = new HexStatusPanel();
    registerEncodingStatus(statusPanel);
    encodingsHandler = new EncodingsHandler(new TextEncodingStatusApi() {
        @Override
        public String getEncoding() {
            return encodingStatus.getEncoding();
        }

        @Override
        public void setEncoding(String encodingName) {
            codeArea.setCharset(Charset.forName(encodingName));
            encodingStatus.setEncoding(encodingName);
            preferences.setValue(DeltaHexFileEditor.PREFERENCES_ENCODING_SELECTED, encodingName);
        }
    });

    propertyChangeSupport = new PropertyChangeSupport(this);
    // CodeAreaUndoHandler(codeArea);
    // undoHandler = new HexUndoIntelliJHandler(codeArea, project, this);
    undoHandler = new CodeAreaUndoHandler(codeArea);
    loadFromPreferences();

    undoHandler.addUndoUpdateListener(new BinaryDataUndoUpdateListener() {
        @Override
        public void undoCommandPositionChanged() {
            codeArea.repaint();
            updateUndoState();
            notifyModified();
        }

        @Override
        public void undoCommandAdded(final BinaryDataCommand command) {
            updateUndoState();
            notifyModified();
        }
    });
    updateUndoState();

    getSegmentsRepository();
    setNewData();
    CodeAreaOperationCommandHandler commandHandler = new CodeAreaOperationCommandHandler(codeArea, undoHandler);
    codeArea.setCommandHandler(commandHandler);
    editorPanel.add(codeArea, BorderLayout.CENTER);
    editorPanel.add(statusPanel, BorderLayout.SOUTH);
    registerHexStatus(statusPanel);
    goToHandler = new GoToHandler(codeArea);

    codeArea.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                JPopupMenu popupMenu = createContextMenu();
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    codeTypeComboBox.setSelectedIndex(codeArea.getCodeType().ordinal());

    editorPanel.getActionMap().put("copy-to-clipboard", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            codeArea.copy();
        }
    });
    editorPanel.getActionMap().put("cut-to-clipboard", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            codeArea.cut();
        }
    });
    editorPanel.getActionMap().put("paste-from-clipboard", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            codeArea.paste();
        }
    });

    applyFromCodeArea();

    int metaMaskValue;
    try {
        metaMaskValue = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    } catch (java.awt.HeadlessException ex) {
        metaMaskValue = java.awt.Event.CTRL_MASK;
    }

    metaMask = metaMaskValue;

    codeArea.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent keyEvent) {
            int modifiers = keyEvent.getModifiers();
            if (modifiers == metaMask) {
                int keyCode = keyEvent.getKeyCode();
                switch (keyCode) {
                case KeyEvent.VK_F: {
                    showSearchPanel(false);
                    break;
                }
                case KeyEvent.VK_G: {
                    goToHandler.getGoToLineAction().actionPerformed(null);
                    break;
                }
                case KeyEvent.VK_S: {
                    saveFileButtonActionPerformed(null);
                    break;
                }
                }
            }

            if (modifiers == InputEvent.CTRL_MASK && keyEvent.getKeyCode() == KeyEvent.VK_Z) {
                try {
                    if (undoHandler.canUndo()) {
                        undoHandler.performUndo();
                    }
                } catch (BinaryDataOperationException e) {
                    e.printStackTrace();
                }
            } else if (modifiers == (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)
                    && keyEvent.getKeyCode() == KeyEvent.VK_Z) {
                try {
                    if (undoHandler.canRedo()) {
                        undoHandler.performRedo();
                    }
                } catch (BinaryDataOperationException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    MessageBus messageBus = project.getMessageBus();
    MessageBusConnection connect = messageBus.connect();
    connect.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
        @Override
        public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
        }

        @Override
        public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
            if (virtualFile != null) {
                if (!releaseFile()) {
                    // TODO Intercept close event instead of editor recreation
                    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, 0);
                    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
                    List<FileEditor> editors = fileEditorManager.openEditor(descriptor, true);
                    fileEditorManager.setSelectedEditor(virtualFile,
                            DeltaHexWindowProvider.DELTAHEX_EDITOR_TYPE_ID);
                    for (FileEditor fileEditor : editors) {
                        if (fileEditor instanceof DeltaHexFileEditor) {
                            ((DeltaHexFileEditor) fileEditor).reopenFile(virtualFile, codeArea.getData(),
                                    undoHandler);
                        }
                    }
                    closeData(false);
                } else {
                    closeData(true);
                }
            }

            virtualFile = null;
        }

        @Override
        public void selectionChanged(@NotNull FileEditorManagerEvent event) {
        }
    });
}

From source file:org.exbin.deltahex.intellij.FileOpenAsHexAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent event) {
    final Project project = event.getProject();
    if (project == null) {
        return;/* www  . ja va 2 s.c o m*/
    }

    FileChooserDescriptor chooserDescriptor = new FileChooserDescriptor(true, false, true, false, false, false);
    VirtualFile virtualFile = FileChooser.chooseFile(chooserDescriptor, project, null);
    if (virtualFile != null && virtualFile.isValid() && !virtualFile.isDirectory()) {
        DeltaHexVirtualFile deltaHexVirtualFile = new DeltaHexVirtualFile(virtualFile);
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, deltaHexVirtualFile, 0);
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        List<FileEditor> editors = fileEditorManager.openEditor(descriptor, true);
        fileEditorManager.setSelectedEditor(virtualFile, DeltaHexWindowProvider.DELTAHEX_EDITOR_TYPE_ID);
        for (FileEditor fileEditor : editors) {
            if (fileEditor instanceof DeltaHexFileEditor) {
                ((DeltaHexFileEditor) fileEditor).openFile(deltaHexVirtualFile);
            }
        }
    }
}

From source file:org.exbin.deltahex.intellij.OpenAsHexAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent event) {
    final Project project = event.getProject();
    if (project == null) {
        return;//from www  .  j  a v  a 2  s  .c  o  m
    }
    VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
    if (virtualFile != null && virtualFile.isValid() && !virtualFile.isDirectory()) {
        DeltaHexVirtualFile deltaHexVirtualFile = new DeltaHexVirtualFile(virtualFile);
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, deltaHexVirtualFile, 0);
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        List<FileEditor> editors = fileEditorManager.openEditor(descriptor, true);
        fileEditorManager.setSelectedEditor(virtualFile, DeltaHexWindowProvider.DELTAHEX_EDITOR_TYPE_ID);
        for (FileEditor fileEditor : editors) {
            if (fileEditor instanceof DeltaHexFileEditor) {
                ((DeltaHexFileEditor) fileEditor).openFile(deltaHexVirtualFile);
            }
        }
    }
}

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 va2 s.co m*/
            }
        }
    }
}