Search in sources :

Example 76 with DataContext

use of com.intellij.openapi.actionSystem.DataContext in project intellij-community by JetBrains.

the class ChooseRunConfigurationPopup method createSettingsList.

public static ItemWrapper[] createSettingsList(@NotNull Project project, @NotNull ExecutorProvider executorProvider, boolean createEditAction) {
    List<ItemWrapper> result = new ArrayList<>();
    if (createEditAction) {
        ItemWrapper<Void> edit = new ItemWrapper<Void>(null) {

            @Override
            public Icon getIcon() {
                return AllIcons.Actions.EditSource;
            }

            @Override
            public String getText() {
                return UIUtil.removeMnemonic(ActionsBundle.message("action.editRunConfigurations.text"));
            }

            @Override
            public void perform(@NotNull final Project project, @NotNull final Executor executor, @NotNull DataContext context) {
                if (new EditConfigurationsDialog(project) {

                    @Override
                    protected void init() {
                        setOKButtonText(executor.getStartActionText());
                        setOKButtonIcon(executor.getIcon());
                        myExecutor = executor;
                        super.init();
                    }
                }.showAndGet()) {
                    ApplicationManager.getApplication().invokeLater(() -> {
                        RunnerAndConfigurationSettings configuration = RunManager.getInstance(project).getSelectedConfiguration();
                        if (configuration != null) {
                            ExecutionUtil.runConfiguration(configuration, executor);
                        }
                    }, project.getDisposed());
                }
            }

            @Override
            public boolean available(Executor executor) {
                return true;
            }
        };
        edit.setMnemonic(0);
        result.add(edit);
    }
    RunManagerEx manager = RunManagerEx.getInstanceEx(project);
    final RunnerAndConfigurationSettings selectedConfiguration = manager.getSelectedConfiguration();
    if (selectedConfiguration != null) {
        boolean isFirst = true;
        final ExecutionTarget activeTarget = ExecutionTargetManager.getActiveTarget(project);
        for (ExecutionTarget eachTarget : ExecutionTargetManager.getTargetsToChooseFor(project, selectedConfiguration)) {
            result.add(new ItemWrapper<ExecutionTarget>(eachTarget, isFirst) {

                {
                    setChecked(getValue().equals(activeTarget));
                }

                @Override
                public Icon getIcon() {
                    return getValue().getIcon();
                }

                @Override
                public String getText() {
                    return getValue().getDisplayName();
                }

                @Override
                public void perform(@NotNull final Project project, @NotNull final Executor executor, @NotNull DataContext context) {
                    ExecutionTargetManager.setActiveTarget(project, getValue());
                    ExecutionUtil.runConfiguration(selectedConfiguration, executor);
                }

                @Override
                public boolean available(Executor executor) {
                    return true;
                }
            });
            isFirst = false;
        }
    }
    Map<RunnerAndConfigurationSettings, ItemWrapper> wrappedExisting = new LinkedHashMap<>();
    for (ConfigurationType type : manager.getConfigurationFactories()) {
        if (!(type instanceof UnknownConfigurationType)) {
            Map<String, List<RunnerAndConfigurationSettings>> structure = manager.getStructure(type);
            for (Map.Entry<String, List<RunnerAndConfigurationSettings>> entry : structure.entrySet()) {
                if (entry.getValue().isEmpty()) {
                    continue;
                }
                final String key = entry.getKey();
                if (key != null) {
                    boolean isSelected = entry.getValue().contains(selectedConfiguration);
                    if (isSelected) {
                        assert selectedConfiguration != null;
                    }
                    FolderWrapper folderWrapper = new FolderWrapper(project, executorProvider, key + (isSelected ? "  (mnemonic is to \"" + selectedConfiguration.getName() + "\")" : ""), entry.getValue());
                    if (isSelected) {
                        folderWrapper.setMnemonic(1);
                    }
                    result.add(folderWrapper);
                } else {
                    for (RunnerAndConfigurationSettings configuration : entry.getValue()) {
                        final ItemWrapper wrapped = ItemWrapper.wrap(project, configuration);
                        if (configuration == selectedConfiguration) {
                            wrapped.setMnemonic(1);
                        }
                        wrappedExisting.put(configuration, wrapped);
                    }
                }
            }
        }
    }
    if (!DumbService.isDumb(project)) {
        populateWithDynamicRunners(result, wrappedExisting, project, manager, selectedConfiguration);
    }
    result.addAll(wrappedExisting.values());
    return result.toArray(new ItemWrapper[result.size()]);
}
Also used : NotNull(org.jetbrains.annotations.NotNull) DataContext(com.intellij.openapi.actionSystem.DataContext) UnknownConfigurationType(com.intellij.execution.configurations.UnknownConfigurationType) List(java.util.List) ConfigurationType(com.intellij.execution.configurations.ConfigurationType) UnknownConfigurationType(com.intellij.execution.configurations.UnknownConfigurationType) EditConfigurationsDialog(com.intellij.execution.impl.EditConfigurationsDialog) Project(com.intellij.openapi.project.Project)

Example 77 with DataContext

use of com.intellij.openapi.actionSystem.DataContext in project intellij-community by JetBrains.

the class ChooseRunConfigurationPopup method populateWithDynamicRunners.

@NotNull
private static List<RunnerAndConfigurationSettings> populateWithDynamicRunners(final List<ItemWrapper> result, Map<RunnerAndConfigurationSettings, ItemWrapper> existing, final Project project, final RunManagerEx manager, final RunnerAndConfigurationSettings selectedConfiguration) {
    final ArrayList<RunnerAndConfigurationSettings> contextConfigurations = new ArrayList<>();
    if (!EventQueue.isDispatchThread()) {
        return Collections.emptyList();
    }
    final DataContext dataContext = DataManager.getInstance().getDataContext();
    final ConfigurationContext context = ConfigurationContext.getFromContext(dataContext);
    final List<ConfigurationFromContext> producers = PreferredProducerFind.getConfigurationsFromContext(context.getLocation(), context, false);
    if (producers == null)
        return Collections.emptyList();
    Collections.sort(producers, ConfigurationFromContext.NAME_COMPARATOR);
    final RunnerAndConfigurationSettings[] preferred = { null };
    // selectedConfiguration == null ? 1 : 2;
    int i = 2;
    for (final ConfigurationFromContext fromContext : producers) {
        final RunnerAndConfigurationSettings configuration = fromContext.getConfigurationSettings();
        if (existing.keySet().contains(configuration)) {
            final ItemWrapper wrapper = existing.get(configuration);
            if (wrapper.getMnemonic() != 1) {
                wrapper.setMnemonic(i);
                i++;
            }
        } else {
            if (selectedConfiguration != null && configuration.equals(selectedConfiguration))
                continue;
            contextConfigurations.add(configuration);
            if (preferred[0] == null) {
                preferred[0] = configuration;
            }
            //noinspection unchecked
            final ItemWrapper wrapper = new ItemWrapper(configuration) {

                @Override
                public Icon getIcon() {
                    return RunManagerEx.getInstanceEx(project).getConfigurationIcon(configuration);
                }

                @Override
                public String getText() {
                    return configuration.getName();
                }

                @Override
                public boolean available(Executor executor) {
                    return canRun(executor, configuration);
                }

                @Override
                public void perform(@NotNull Project project, @NotNull Executor executor, @NotNull DataContext context) {
                    manager.setTemporaryConfiguration(configuration);
                    RunManagerEx.getInstanceEx(project).setSelectedConfiguration(configuration);
                    ExecutionUtil.runConfiguration(configuration, executor);
                }

                @Override
                public PopupStep getNextStep(@NotNull final Project project, @NotNull final ChooseRunConfigurationPopup action) {
                    return new ConfigurationActionsStep(project, action, configuration, isDynamic());
                }

                @Override
                public boolean hasActions() {
                    return true;
                }
            };
            wrapper.setDynamic(true);
            wrapper.setMnemonic(i);
            result.add(wrapper);
            i++;
        }
    }
    return contextConfigurations;
}
Also used : NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with DataContext

use of com.intellij.openapi.actionSystem.DataContext in project intellij-community by JetBrains.

the class ChooseRunConfigurationPopup method execute.

static void execute(final ItemWrapper itemWrapper, final Executor executor) {
    if (executor == null) {
        return;
    }
    final DataContext dataContext = DataManager.getInstance().getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project != null) {
        itemWrapper.perform(project, executor, dataContext);
    }
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext)

Example 79 with DataContext

use of com.intellij.openapi.actionSystem.DataContext in project intellij-community by JetBrains.

the class ProjectUtil method guessCurrentProject.

@NotNull
public static Project guessCurrentProject(@Nullable JComponent component) {
    Project project = null;
    if (component != null) {
        project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(component));
    }
    if (project == null) {
        Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
        if (openProjects.length > 0)
            project = openProjects[0];
        if (project == null) {
            DataContext dataContext = DataManager.getInstance().getDataContext();
            project = CommonDataKeys.PROJECT.getData(dataContext);
        }
        if (project == null) {
            project = ProjectManager.getInstance().getDefaultProject();
        }
    }
    return project;
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with DataContext

use of com.intellij.openapi.actionSystem.DataContext in project intellij-community by JetBrains.

the class RefactoringQuickFix method doFix.

default default void doFix(@NotNull PsiElement element) {
    final PsiElement elementToRefactor = getElementToRefactor(element);
    if (elementToRefactor == null) {
        return;
    }
    final Consumer<DataContext> consumer = dataContext -> {
        dataContext = enhanceDataContext(dataContext);
        final RefactoringActionHandler handler = getHandler();
        handler.invoke(element.getProject(), new PsiElement[] { elementToRefactor }, dataContext);
    };
    DataManager.getInstance().getDataContextFromFocus().doWhenDone(consumer);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) NonNls(org.jetbrains.annotations.NonNls) PsiNamedElement(com.intellij.psi.PsiNamedElement) NotNull(org.jetbrains.annotations.NotNull) DataManager(com.intellij.ide.DataManager) Consumer(com.intellij.util.Consumer) DataContext(com.intellij.openapi.actionSystem.DataContext) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) PsiElement(com.intellij.psi.PsiElement)

Aggregations

DataContext (com.intellij.openapi.actionSystem.DataContext)204 Project (com.intellij.openapi.project.Project)73 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 NotNull (org.jetbrains.annotations.NotNull)23 IpnbFileEditor (org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)23 Editor (com.intellij.openapi.editor.Editor)22 Nullable (org.jetbrains.annotations.Nullable)21 FileEditor (com.intellij.openapi.fileEditor.FileEditor)20 PsiFile (com.intellij.psi.PsiFile)17 IpnbFilePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel)16 Module (com.intellij.openapi.module.Module)13 PsiElement (com.intellij.psi.PsiElement)13 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)11 Presentation (com.intellij.openapi.actionSystem.Presentation)10 DataManager (com.intellij.ide.DataManager)9 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)9 List (java.util.List)9 Consumer (com.intellij.util.Consumer)7 AnAction (com.intellij.openapi.actionSystem.AnAction)6 Transferable (java.awt.datatransfer.Transferable)6