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()]);
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations