16
16
17
17
package io .spring .javaformat .eclipse .gradle ;
18
18
19
+ import java .util .Collection ;
20
+ import java .util .Collections ;
21
+ import java .util .LinkedHashSet ;
22
+ import java .util .Set ;
23
+
19
24
import org .eclipse .buildship .core .internal .CorePlugin ;
20
25
import org .eclipse .buildship .core .internal .event .Event ;
21
26
import org .eclipse .buildship .core .internal .event .EventListener ;
27
+ import org .eclipse .buildship .core .internal .util .collections .AdapterFunction ;
22
28
import org .eclipse .buildship .core .internal .workspace .GradleNatureAddedEvent ;
23
29
import org .eclipse .buildship .core .internal .workspace .ProjectCreatedEvent ;
24
30
import org .eclipse .core .commands .Command ;
25
31
import org .eclipse .core .commands .ExecutionEvent ;
26
32
import org .eclipse .core .commands .ExecutionException ;
27
33
import org .eclipse .core .commands .IExecutionListener ;
28
34
import org .eclipse .core .commands .NotHandledException ;
35
+ import org .eclipse .core .resources .IFile ;
36
+ import org .eclipse .core .resources .IProject ;
37
+ import org .eclipse .core .resources .IResource ;
38
+ import org .eclipse .jface .viewers .ISelection ;
39
+ import org .eclipse .jface .viewers .IStructuredSelection ;
40
+ import org .eclipse .ui .IEditorInput ;
29
41
import org .eclipse .ui .PlatformUI ;
30
42
import org .eclipse .ui .commands .ICommandService ;
43
+ import org .eclipse .ui .handlers .HandlerUtil ;
44
+ import org .eclipse .ui .part .FileEditorInput ;
31
45
32
46
/**
33
47
* Listeners used to trigger the {@link RefreshProjectsSettingsJob}.
@@ -59,21 +73,55 @@ private static class CommandListener implements IExecutionListener {
59
73
60
74
private static final String COMMAND_NAME = "org.eclipse.buildship.ui.commands.refreshproject" ; //$NON-NLS-1$
61
75
76
+ private ThreadLocal <ExecutionEvent > event = new ThreadLocal <ExecutionEvent >();
77
+
62
78
@ Override
63
- public void notHandled (String commandId , NotHandledException exception ) {
79
+ public void preExecute (String commandId , ExecutionEvent event ) {
80
+ this .event .set (event );
64
81
}
65
82
66
83
@ Override
67
- public void postExecuteFailure (String commandId , ExecutionException exception ) {
84
+ public void postExecuteSuccess (String commandId , Object returnValue ) {
85
+ Set <IProject > projects = getProjects (this .event .get ());
86
+ this .event .set (null );
87
+ new RefreshProjectsSettingsJob (projects ).schedule ();
88
+ }
89
+
90
+ private Set <IProject > getProjects (ExecutionEvent event ) {
91
+ if (event == null ) {
92
+ return null ;
93
+ }
94
+ ISelection currentSelection = HandlerUtil .getCurrentSelection (event );
95
+ if (currentSelection instanceof IStructuredSelection ) {
96
+ IStructuredSelection selection = (IStructuredSelection ) currentSelection ;
97
+ return collectGradleProjects (selection .toList ());
98
+ }
99
+ IEditorInput editorInput = HandlerUtil .getActiveEditorInput (event );
100
+ if (editorInput instanceof FileEditorInput ) {
101
+ IFile file = ((FileEditorInput ) editorInput ).getFile ();
102
+ return collectGradleProjects (Collections .singleton (file ));
103
+ }
104
+ return null ;
105
+ }
106
+
107
+ private Set <IProject > collectGradleProjects (Collection <?> candidates ) {
108
+ Set <IProject > projects = new LinkedHashSet <>(candidates .size ());
109
+ AdapterFunction <IResource > adapter = AdapterFunction .forType (IResource .class );
110
+ for (Object candidate : candidates ) {
111
+ IResource resource = adapter .apply (candidate );
112
+ if (resource != null ) {
113
+ projects .add (resource .getProject ());
114
+ }
115
+ }
116
+ return projects ;
68
117
}
69
118
70
119
@ Override
71
- public void postExecuteSuccess (String commandId , Object returnValue ) {
72
- new RefreshProjectsSettingsJob ().schedule ();
120
+ public void postExecuteFailure (String commandId , ExecutionException exception ) {
73
121
}
74
122
75
123
@ Override
76
- public void preExecute (String commandId , ExecutionEvent event ) {
124
+ public void notHandled (String commandId , NotHandledException exception ) {
77
125
}
78
126
79
127
static void attach () {
@@ -89,7 +137,7 @@ static void attach() {
89
137
}
90
138
91
139
/**
92
- * Event Listener to triger an update after a project import or gradle nature change.
140
+ * Event Listener to trigger an update after a project import or gradle nature change.
93
141
*/
94
142
private static class ProjectListener implements EventListener {
95
143
0 commit comments