Example usage for com.intellij.openapi.wm IdeFocusManager getLastFocusedFrame

List of usage examples for com.intellij.openapi.wm IdeFocusManager getLastFocusedFrame

Introduction

In this page you can find the example usage for com.intellij.openapi.wm IdeFocusManager getLastFocusedFrame.

Prototype

@Nullable
public abstract IdeFrame getLastFocusedFrame();

Source Link

Document

Returns last focused IdeFrame .

Usage

From source file:com.android.tools.idea.gradle.project.GradleProjectImporter.java

License:Apache License

private static void open(@NotNull final Project newProject) {
    ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
    ProjectUtil.updateLastProjectLocation(newProject.getBasePath());
    if (WindowManager.getInstance().isFullScreenSupportedInCurrentOS()) {
        IdeFocusManager instance = IdeFocusManager.findInstance();
        IdeFrame lastFocusedFrame = instance.getLastFocusedFrame();
        if (lastFocusedFrame instanceof IdeFrameEx) {
            boolean fullScreen = ((IdeFrameEx) lastFocusedFrame).isInFullScreen();
            if (fullScreen) {
                newProject.putUserData(IdeFrameImpl.SHOULD_OPEN_IN_FULL_SCREEN, Boolean.TRUE);
            }//from  ww  w. java 2s . c  om
        }
    }
    projectManager.openProject(newProject);
}

From source file:com.google.idea.blaze.clwb.wizard2.BlazeCProjectCreator.java

License:Open Source License

@Nullable
private Project doCreate() throws IOException {
    final ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
    final String projectFilePath = wizardContext.getProjectFileDirectory();

    try {//from  w  w w .  j a va  2s .  c o  m
        File projectDir = new File(projectFilePath).getParentFile();
        LOG.assertTrue(projectDir != null,
                "Cannot create project in '" + projectFilePath + "': no parent file exists");
        FileUtil.ensureExists(projectDir);
        if (wizardContext.getProjectStorageFormat() == StorageScheme.DIRECTORY_BASED) {
            final File ideaDir = new File(projectFilePath, Project.DIRECTORY_STORE_FOLDER);
            FileUtil.ensureExists(ideaDir);
        }

        String name = wizardContext.getProjectName();
        Project newProject = projectBuilder.createProject(name, projectFilePath);
        if (newProject == null) {
            return null;
        }

        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }

        if (!projectBuilder.validate(null, newProject)) {
            return null;
        }

        projectBuilder.commit(newProject, null, ModulesProvider.EMPTY_MODULES_PROVIDER);

        StartupManager.getInstance(newProject).registerPostStartupActivity(() -> {
            // ensure the dialog is shown after all startup activities are done
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(() -> {
                if (newProject.isDisposed() || ApplicationManager.getApplication().isUnitTestMode()) {
                    return;
                }
                ApplicationManager.getApplication().invokeLater(() -> {
                    if (newProject.isDisposed()) {
                        return;
                    }
                    final ToolWindow toolWindow = ToolWindowManager.getInstance(newProject)
                            .getToolWindow(ToolWindowId.PROJECT_VIEW);
                    if (toolWindow != null) {
                        toolWindow.activate(null);
                    }
                }, ModalityState.NON_MODAL);
            });
        });

        ProjectUtil.updateLastProjectLocation(projectFilePath);

        if (WindowManager.getInstance().isFullScreenSupportedInCurrentOS()) {
            IdeFocusManager instance = IdeFocusManager.findInstance();
            IdeFrame lastFocusedFrame = instance.getLastFocusedFrame();
            if (lastFocusedFrame instanceof IdeFrameEx) {
                boolean fullScreen = ((IdeFrameEx) lastFocusedFrame).isInFullScreen();
                if (fullScreen) {
                    newProject.putUserData(IdeFrameImpl.SHOULD_OPEN_IN_FULL_SCREEN, Boolean.TRUE);
                }
            }
        }

        projectManager.openProject(newProject);

        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }
        return newProject;
    } finally {
        projectBuilder.cleanup();
    }
}

From source file:com.intellij.ide.impl.DataManagerImpl.java

License:Apache License

@Nullable
private Component getFocusedComponent() {
    if (myWindowManager == null) {
        return null;
    }//from   w ww.j a  v a2 s  .co m
    Window activeWindow = myWindowManager.getMostRecentFocusedWindow();
    if (activeWindow == null) {
        activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
        if (activeWindow == null) {
            activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
            if (activeWindow == null)
                return null;
        }
    }

    if (Registry.is("actionSystem.noContextComponentWhileFocusTransfer")) {
        IdeFocusManager fm = IdeFocusManager.findInstanceByComponent(activeWindow);
        if (fm.isFocusBeingTransferred()) {
            return null;
        }
    }

    // In case we have an active floating toolwindow and some component in another window focused,
    // we want this other component to receive key events.
    // Walking up the window ownership hierarchy from the floating toolwindow would have led us to the main IdeFrame
    // whereas we want to be able to type in other frames as well.
    if (activeWindow instanceof FloatingDecorator) {
        IdeFocusManager ideFocusManager = IdeFocusManager.findInstanceByComponent(activeWindow);
        IdeFrame lastFocusedFrame = ideFocusManager.getLastFocusedFrame();
        JComponent frameComponent = lastFocusedFrame != null ? lastFocusedFrame.getComponent() : null;
        Window lastFocusedWindow = frameComponent != null ? SwingUtilities.getWindowAncestor(frameComponent)
                : null;
        boolean toolWindowIsNotFocused = myWindowManager.getFocusedComponent(activeWindow) == null;
        if (toolWindowIsNotFocused && lastFocusedWindow != null) {
            activeWindow = lastFocusedWindow;
        }
    }

    // try to find first parent window that has focus
    Window window = activeWindow;
    Component focusedComponent = null;
    while (window != null) {
        focusedComponent = myWindowManager.getFocusedComponent(window);
        if (focusedComponent != null) {
            break;
        }
        window = window.getOwner();
    }
    if (focusedComponent == null) {
        focusedComponent = activeWindow;
    }

    return focusedComponent;
}

From source file:com.intellij.ide.impl.NewProjectUtil.java

License:Apache License

private static Project doCreate(final AddModuleWizard dialog, @Nullable Project projectToClose)
        throws IOException {
    final ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
    final String projectFilePath = dialog.getNewProjectFilePath();
    final ProjectBuilder projectBuilder = dialog.getProjectBuilder();

    try {/*from   www . j  a v a 2  s  .  c  o m*/
        File projectDir = new File(projectFilePath).getParentFile();
        LOGGER.assertTrue(projectDir != null,
                "Cannot create project in '" + projectFilePath + "': no parent file exists");
        FileUtil.ensureExists(projectDir);
        final File ideaDir = new File(projectFilePath, Project.DIRECTORY_STORE_FOLDER);
        FileUtil.ensureExists(ideaDir);

        final Project newProject;
        if (projectBuilder == null || !projectBuilder.isUpdate()) {
            String name = dialog.getProjectName();
            newProject = projectBuilder == null ? projectManager.newProject(name, projectFilePath, true, false)
                    : projectBuilder.createProject(name, projectFilePath);
        } else {
            newProject = projectToClose;
        }

        if (newProject == null)
            return projectToClose;

        final String compileOutput = dialog.getNewCompileOutput();
        CommandProcessor.getInstance().executeCommand(newProject, new Runnable() {
            @Override
            public void run() {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {
                    @Override
                    public void run() {
                        String canonicalPath = compileOutput;
                        try {
                            canonicalPath = FileUtil.resolveShortWindowsName(compileOutput);
                        } catch (IOException e) {
                            //file doesn't exist
                        }
                        canonicalPath = FileUtil.toSystemIndependentName(canonicalPath);
                        CompilerConfiguration.getInstance(newProject)
                                .setCompilerOutputUrl(VfsUtilCore.pathToUrl(canonicalPath));
                    }
                });
            }
        }, null, null);

        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }

        if (projectBuilder != null && !projectBuilder.validate(projectToClose, newProject)) {
            return projectToClose;
        }

        if (newProject != projectToClose && !ApplicationManager.getApplication().isUnitTestMode()) {
            closePreviousProject(projectToClose);
        }

        if (projectBuilder != null) {
            projectBuilder.commit(newProject, null, ModulesProvider.EMPTY_MODULES_PROVIDER);
        }

        final boolean need2OpenProjectStructure = projectBuilder == null
                || projectBuilder.isOpenProjectSettingsAfter();
        StartupManager.getInstance(newProject).registerPostStartupActivity(new Runnable() {
            @Override
            public void run() {
                // ensure the dialog is shown after all startup activities are done
                //noinspection SSBasedInspection
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if (newProject.isDisposed() || ApplicationManager.getApplication().isUnitTestMode())
                            return;
                        if (need2OpenProjectStructure) {
                            ModulesConfigurator.showDialog(newProject, null, null);
                        }
                        ApplicationManager.getApplication().invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                if (newProject.isDisposed())
                                    return;
                                final ToolWindow toolWindow = ToolWindowManager.getInstance(newProject)
                                        .getToolWindow(ToolWindowId.PROJECT_VIEW);
                                if (toolWindow != null) {
                                    toolWindow.activate(null);
                                }
                            }
                        }, ModalityState.NON_MODAL);
                    }
                });
            }
        });

        if (newProject != projectToClose) {
            ProjectUtil.updateLastProjectLocation(projectFilePath);

            if (WindowManager.getInstance().isFullScreenSupportedInCurrentOS()) {
                IdeFocusManager instance = IdeFocusManager.findInstance();
                IdeFrame lastFocusedFrame = instance.getLastFocusedFrame();
                if (lastFocusedFrame instanceof IdeFrameEx) {
                    boolean fullScreen = ((IdeFrameEx) lastFocusedFrame).isInFullScreen();
                    if (fullScreen) {
                        newProject.putUserData(IdeFrameImpl.SHOULD_OPEN_IN_FULL_SCREEN, Boolean.TRUE);
                    }
                }
            }

            projectManager.openProject(newProject);
        }
        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }
        return newProject;
    } finally {
        if (projectBuilder != null) {
            projectBuilder.cleanup();
        }
    }
}

From source file:com.intellij.ui.mac.MacMessagesImpl.java

License:Apache License

private static Window getForemostWindow(final Window window) {
    Window _window = null;/*  ww w.j a v a  2  s  . c o  m*/
    IdeFocusManager ideFocusManager = IdeFocusManager.getGlobalInstance();

    Component focusOwner = IdeFocusManager.findInstance().getFocusOwner();
    // Let's ask for a focused component first
    if (focusOwner != null) {
        _window = SwingUtilities.getWindowAncestor(focusOwner);
    }

    if (_window == null) {
        // Looks like ide lost focus, let's ask about the last focused component
        focusOwner = ideFocusManager.getLastFocusedFor(ideFocusManager.getLastFocusedFrame());
        if (focusOwner != null) {
            _window = SwingUtilities.getWindowAncestor(focusOwner);
        }
    }

    if (_window == null) {
        _window = WindowManager.getInstance().findVisibleFrame();
    }

    if (_window == null && window != null) {
        // It might be we just has not opened a frame yet.
        // So let's ask AWT
        focusOwner = window.getMostRecentFocusOwner();
        if (focusOwner != null) {
            _window = SwingUtilities.getWindowAncestor(focusOwner);
        }
    }

    if (_window != null) {
        // We have successfully found the window
        // Let's check that we have not missed a blocker
        if (ModalityHelper.isModalBlocked(_window)) {
            _window = ModalityHelper.getModalBlockerFor(_window);
        }
    }

    if (SystemInfo.isAppleJvm && MacUtil.getWindowTitle(_window) == null) {
        // With Apple JDK we cannot find a window if it does not have a title
        // Let's show a dialog instead of the message.
        throw new MacMessageException("MacMessage parent does not have a title.");
    }
    while (_window != null && MacUtil.getWindowTitle(_window) == null) {
        _window = _window.getOwner();
        //At least our frame should have a title
    }

    while (Registry.is("skip.untitled.windows.for.mac.messages") && _window != null
            && _window instanceof JDialog && !((JDialog) _window).isModal()) {
        _window = _window.getOwner();
    }

    return _window;
}