IDEADEV-39794 NPE: ConfigurationContext.getProject
[fedora-idea.git] / lang-impl / src / com / intellij / execution / actions / ConfigurationContext.java
blob4853751922469156423fc543b9886946887bc025
1 package com.intellij.execution.actions;
3 import com.intellij.execution.*;
4 import com.intellij.execution.configurations.ConfigurationType;
5 import com.intellij.execution.configurations.RuntimeConfiguration;
6 import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
7 import com.intellij.execution.junit.RuntimeConfigurationProducer;
8 import com.intellij.ide.DataManager;
9 import com.intellij.openapi.actionSystem.DataConstants;
10 import com.intellij.openapi.actionSystem.DataContext;
11 import com.intellij.openapi.actionSystem.LangDataKeys;
12 import com.intellij.openapi.actionSystem.PlatformDataKeys;
13 import com.intellij.openapi.actionSystem.ex.DataConstantsEx;
14 import com.intellij.openapi.diagnostic.Logger;
15 import com.intellij.openapi.editor.Editor;
16 import com.intellij.openapi.module.Module;
17 import com.intellij.openapi.project.Project;
18 import com.intellij.openapi.util.Comparing;
19 import com.intellij.openapi.vfs.VirtualFile;
20 import com.intellij.psi.PsiDocumentManager;
21 import com.intellij.psi.PsiElement;
22 import com.intellij.psi.PsiFile;
23 import com.intellij.psi.PsiManager;
24 import org.jetbrains.annotations.Nullable;
26 import java.awt.*;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class ConfigurationContext {
31 private static final Logger LOG = Logger.getInstance("#com.intellij.execution.actions.ConfigurationContext");
32 private final Location<PsiElement> myLocation;
33 private RunnerAndConfigurationSettingsImpl myConfiguration;
34 private final Module myModule;
35 private final RuntimeConfiguration myRuntimeConfiguration;
36 private final Component myContextComponent;
38 public ConfigurationContext(final DataContext dataContext) {
39 myRuntimeConfiguration = (RuntimeConfiguration)dataContext.getData(DataConstantsEx.RUNTIME_CONFIGURATION);
40 myContextComponent = (Component)dataContext.getData(DataConstants.CONTEXT_COMPONENT);
41 myModule = LangDataKeys.MODULE.getData(dataContext);
42 final Object location = dataContext.getData(Location.LOCATION);
43 if (location != null) {
44 myLocation = (Location<PsiElement>)location;
45 return;
47 final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
48 if (project == null) {
49 myLocation = null;
50 return;
52 final PsiElement element = getSelectedPsiElement(dataContext, project);
53 if (element == null) {
54 myLocation = null;
55 return;
57 myLocation = new PsiLocation<PsiElement>(project, element);
60 public RunnerAndConfigurationSettingsImpl getConfiguration() {
61 if (myConfiguration == null) createConfiguration();
62 return myConfiguration;
65 private void createConfiguration() {
66 LOG.assertTrue(myConfiguration == null);
67 final Location location = getLocation();
68 myConfiguration = location != null ?
69 PreferedProducerFind.createConfiguration(location, this) :
70 null;
73 @Nullable
74 public RunnerAndConfigurationSettings getConfiguration(final RuntimeConfigurationProducer producer) {
75 myConfiguration = producer.getConfiguration();
76 return myConfiguration;
79 Location getLocation() {
80 return myLocation;
83 @Nullable
84 public RunnerAndConfigurationSettingsImpl findExisting() {
85 if (myLocation == null) {
86 return null;
89 final List<ConfigurationType> types = new ArrayList<ConfigurationType>();
90 if (myRuntimeConfiguration != null) {
91 types.add(myRuntimeConfiguration.getType());
93 else {
94 final List<RuntimeConfigurationProducer> producers = PreferedProducerFind.findPreferedProducers(myLocation, this);
95 if (producers == null) return null;
96 for (RuntimeConfigurationProducer producer : producers) {
97 types.add(producer.createProducer(myLocation, this).getConfigurationType());
100 for (ConfigurationType type : types) {
101 if (!(type instanceof LocatableConfigurationType)) continue;
102 final LocatableConfigurationType factoryLocatable = (LocatableConfigurationType)type;
103 final RunnerAndConfigurationSettingsImpl[] configurations = getRunManager().getConfigurationSettings(type);
104 for (final RunnerAndConfigurationSettingsImpl existingConfiguration : configurations) {
105 if (factoryLocatable.isConfigurationByLocation(existingConfiguration.getConfiguration(), myLocation)) {
106 return existingConfiguration;
110 return null;
113 @Nullable
114 private static PsiElement getSelectedPsiElement(final DataContext dataContext, final Project project) {
115 PsiElement element = null;
116 final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
117 if (editor != null){
118 final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
119 if (psiFile != null) {
120 element = psiFile.findElementAt(editor.getCaretModel().getOffset());
123 if (element == null) {
124 element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
126 if (element == null) {
127 final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(dataContext);
128 if (file != null) {
129 element = PsiManager.getInstance(project).findFile(file);
132 return element;
135 public RunManagerEx getRunManager() {
136 return RunManagerEx.getInstanceEx(getProject());
139 public Project getProject() { return myLocation.getProject(); }
141 public Module getModule() {
142 return myModule;
145 public DataContext getDataContext() {
146 return DataManager.getInstance().getDataContext(myContextComponent);
149 @Nullable
150 public RuntimeConfiguration getOriginalConfiguration(final ConfigurationType type) {
151 return myRuntimeConfiguration != null && Comparing.strEqual(type.getId(), myRuntimeConfiguration.getType().getId()) ? myRuntimeConfiguration : null;