5
5
import com .intellij .openapi .editor .Editor ;
6
6
import com .intellij .openapi .project .DumbAwareAction ;
7
7
import com .intellij .openapi .project .Project ;
8
+ import com .intellij .openapi .util .Pair ;
8
9
import com .intellij .psi .PsiElement ;
9
10
import com .intellij .psi .PsiFile ;
10
11
import com .intellij .psi .xml .XmlFile ;
13
14
import fr .adrienbrault .idea .symfony2plugin .Symfony2Icons ;
14
15
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
15
16
import fr .adrienbrault .idea .symfony2plugin .action .ui .SymfonyCreateService ;
17
+ import fr .adrienbrault .idea .symfony2plugin .util .IdeHelper ;
16
18
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
19
+ import org .jetbrains .annotations .NotNull ;
20
+ import org .jetbrains .annotations .Nullable ;
17
21
import org .jetbrains .yaml .psi .YAMLFile ;
18
22
23
+ import java .awt .*;
24
+
19
25
/**
20
26
* @author Daniel Espendiller <daniel@espendiller.net>
21
27
*/
@@ -33,29 +39,15 @@ public void update(AnActionEvent event) {
33
39
return ;
34
40
}
35
41
36
- PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
37
- if (psiFile instanceof PhpFile ) {
38
-
39
- if ("ProjectViewPopup" .equals (event .getPlace ())) {
40
-
41
- if (PhpElementsUtil .getFirstClassFromFile ((PhpFile ) psiFile ) == null ) {
42
- this .setStatus (event , false );
43
- }
44
-
45
- } else {
46
- PsiElement psiElement = event .getData (PlatformDataKeys .PSI_ELEMENT );
47
8000
td>- if (!(psiElement instanceof PhpClass )) {
48
- this .setStatus (event , false );
49
- }
50
- }
51
-
42
+ Pair <PsiFile , PhpClass > pair = findPhpClass (event );
43
+ if (pair == null ) {
52
44
return ;
53
45
}
54
46
55
- if (!(psiFile instanceof YAMLFile ) && !(psiFile instanceof XmlFile )) {
47
+ PsiFile psiFile = pair .getFirst ();
48
+ if (!(psiFile instanceof YAMLFile ) && !(psiFile instanceof XmlFile ) && !(psiFile instanceof PhpFile )) {
56
49
this .setStatus (event , false );
57
50
}
58
-
59
51
}
60
52
61
53
private void setStatus (AnActionEvent event , boolean status ) {
@@ -64,42 +56,51 @@ private void setStatus(AnActionEvent event, boolean status) {
64
56
}
65
57
66
58
public void actionPerformed (AnActionEvent event ) {
67
-
68
59
Project project = event .getProject ();
69
60
if (project == null ) {
70
61
return ;
71
62
}
72
63
73
64
Editor editor = event .getData (PlatformDataKeys .EDITOR );
74
- if (editor == null ) {
65
+
66
+ Component applicationWindow = IdeHelper .getWindowComponentFromProject (event .getProject ());
67
+ if (applicationWindow == null ) {
75
68
return ;
76
69
}
77
70
78
- PsiFile psiFile = event . getData ( PlatformDataKeys . PSI_FILE );
79
- if (!( psiFile instanceof YAMLFile ) && !( psiFile instanceof XmlFile ) && !( psiFile instanceof PhpFile ) ) {
71
+ Pair < PsiFile , PhpClass > pair = findPhpClass ( event );
72
+ if (pair == null ) {
80
73
return ;
81
74
}
82
75
83
- PhpClass phpClass = null ;
84
- if (psiFile instanceof PhpFile ) {
76
+ if (pair .getSecond () == null ) {
77
+ SymfonyCreateService .create (applicationWindow , project , pair .getFirst (), editor );
78
+ return ;
79
+ }
85
80
86
- if ("ProjectViewPopup" .equals (event .getPlace ())) {
87
- phpClass = PhpElementsUtil .getFirstClassFromFile ((PhpFile ) psiFile );
88
- } else {
89
- PsiElement psiElement = event .getData (PlatformDataKeys .PSI_ELEMENT );
90
- if (psiElement instanceof PhpClass ) {
91
- phpClass = (PhpClass ) psiElement ;
92
- }
93
- }
81
+ SymfonyCreateService .create (applicationWindow , project , pair .getFirst (), pair .getSecond (), editor );
82
+ }
94
83
84
+ @ Nullable
85
+ private Pair <PsiFile , PhpClass > findPhpClass (@ NotNull AnActionEvent event ) {
86
+ PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
87
+ if (!(psiFile instanceof YAMLFile ) && !(psiFile instanceof XmlFile ) && !(psiFile instanceof PhpFile )) {
88
+ return null ;
95
89
}
96
90
97
- if (phpClass == null ) {
98
- SymfonyCreateService .create (editor .getComponent (), project , psiFile , editor );
99
- return ;
91
+ if ("ProjectViewPopup" .equals (event .getPlace ())) {
92
+ // menu item
93
+ return Pair .create (psiFile , PhpElementsUtil .getFirstClassFromFile ((PhpFile ) psiFile ));
94
+ } else {
95
+ PsiElement psiElement = event .getData (PlatformDataKeys .PSI_ELEMENT );
96
+ if (psiElement instanceof PhpClass ) {
97
+ // directly got the class
98
+ return Pair .create (psiFile , (PhpClass ) psiElement );
99
+ } else {
100
+ // click inside class
101
+ return Pair .create (psiFile , PhpElementsUtil .getFirstClassFromFile ((PhpFile ) psiFile ));
102
+ }
100
103
}
101
-
102
- SymfonyCreateService .create (editor .getComponent (), project , psiFile , phpClass , editor );
103
104
}
104
105
}
105
106
0 commit comments