1616
1717package io .spring .javaformat .eclipse .gradle ;
1818
19+ import java .util .Collection ;
20+ import java .util .Collections ;
21+ import java .util .LinkedHashSet ;
22+ import java .util .Set ;
23+
1924import org .eclipse .buildship .core .internal .CorePlugin ;
2025import org .eclipse .buildship .core .internal .event .Event ;
2126import org .eclipse .buildship .core .internal .event .EventListener ;
27+ import org .eclipse .buildship .core .internal .util .collections .AdapterFunction ;
2228import org .eclipse .buildship .core .internal .workspace .GradleNatureAddedEvent ;
2329import org .eclipse .buildship .core .internal .workspace .ProjectCreatedEvent ;
2430import org .eclipse .core .commands .Command ;
2531import org .eclipse .core .commands .ExecutionEvent ;
2632import org .eclipse .core .commands .ExecutionException ;
2733import org .eclipse .core .commands .IExecutionListener ;
2834import 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 ;
2941import org .eclipse .ui .PlatformUI ;
3042import org .eclipse .ui .commands .ICommandService ;
43+ import org .eclipse .ui .handlers .HandlerUtil ;
44+ import org .eclipse .ui .part .FileEditorInput ;
3145
3246/**
3347 * Listeners used to trigger the {@link RefreshProjectsSettingsJob}.
@@ -59,21 +73,55 @@ private static class CommandListener implements IExecutionListener {
5973
6074 private static final String COMMAND_NAME = "org.eclipse.buildship.ui.commands.refreshproject" ; //$NON-NLS-1$
6175
76+ private ThreadLocal <ExecutionEvent > event = new ThreadLocal <ExecutionEvent >();
77+
6278 @ Override
63- public void notHandled (String commandId , NotHandledException exception ) {
79+ public void preExecute (String commandId , ExecutionEvent event ) {
80+ this .event .set (event );
6481 }
6582
6683 @ 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 ;
68117 }
69118
70119 @ Override
71- public void postExecuteSuccess (String commandId , Object returnValue ) {
72- new RefreshProjectsSettingsJob ().schedule ();
120+ public void postExecuteFailure (String commandId , ExecutionException exception ) {
73121 }
74122
75123 @ Override
76- public void preExecute (String commandId , ExecutionEvent event ) {
124+ public void notHandled (String commandId , NotHandledException exception ) {
77125 }
78126
79127 static void attach () {
@@ -89,7 +137,7 @@ static void attach() {
89137 }
90138
91139 /**
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.
93141 */
94142 private static class ProjectListener implements EventListener {
95143
0 commit comments