2222
2323import java .util .ArrayList ;
2424import java .util .Arrays ;
25+ import java .util .HashSet ;
2526import java .util .List ;
27+ import java .util .Set ;
2628
29+ import org .eclipse .core .commands .AbstractHandler ;
30+ import org .eclipse .core .commands .ExecutionEvent ;
31+ import org .eclipse .core .commands .ExecutionException ;
2732import org .eclipse .core .resources .IContainer ;
2833import org .eclipse .core .resources .IFile ;
2934import org .eclipse .core .resources .IResource ;
3035import org .eclipse .core .runtime .CoreException ;
36+ import org .eclipse .core .runtime .IAdaptable ;
3137import org .eclipse .jface .action .IAction ;
3238import org .eclipse .jface .viewers .ISelection ;
3339import org .eclipse .jface .viewers .IStructuredSelection ;
3440import org .eclipse .ui .IObjectActionDelegate ;
3541import org .eclipse .ui .IWorkbenchPart ;
42+ import org .eclipse .ui .handlers .HandlerUtil ;
3643
3744import net .sf .eclipsecs .core .jobs .RunCheckstyleOnFilesJob ;
3845import net .sf .eclipsecs .ui .CheckstyleUIPlugin ;
3946
4047/**
41- * Action to diable Checkstyle on one ore more projects.
48+ * Action to run Checkstyle on one ore more projects.
4249 *
4350 * @author Lars Ködderitzsch
4451 */
45- public class CheckSelectedFilesAction implements IObjectActionDelegate {
52+ public class CheckSelectedFilesAction extends AbstractHandler implements IObjectActionDelegate {
4653
4754 private IWorkbenchPart mPart ;
4855
@@ -62,13 +69,26 @@ public void selectionChanged(IAction action, ISelection selection) {
6269 }
6370
6471 @ Override
65- @ SuppressWarnings ("unchecked" )
6672 public void run (IAction action ) {
73+ checkSelection (mSelection );
74+ }
6775
76+ private void checkSelection (IStructuredSelection selection ) {
77+ Set <IResource > resources = new HashSet <>();
78+ for (Object object : selection .toList ()) {
79+ if (object instanceof IAdaptable adaptable ) {
80+ var resource = adaptable .getAdapter (IResource .class );
81+ if (resource != null ) {
82+ resources .add (resource );
83+ }
84+ }
85+ }
6886 List <IFile > filesToCheck = new ArrayList <>();
69-
7087 try {
71- addFileResources (mSelection .toList (), filesToCheck );
88+ addFileResources (List .copyOf (resources ), filesToCheck );
89+ if (filesToCheck .isEmpty ()) {
90+ return ;
91+ }
7292
7393 RunCheckstyleOnFilesJob job = new RunCheckstyleOnFilesJob (filesToCheck );
7494 job .setRule (job );
@@ -79,18 +99,16 @@ public void run(IAction action) {
7999 }
80100
81101 /**
82- * Recursively add all files contained in the given resource collection to the
83- * second list.
102+ * Recursively add all files contained in the given resource collection to the second list.
84103 *
85104 * @param resources
86105 * list of resource
87106 * @param files
88107 * the list of files
89108 * @throws CoreException
90- * en unexpected exception
109+ * an unexpected exception
91110 */
92111 private void addFileResources (List <IResource > resources , List <IFile > files ) throws CoreException {
93-
94112 for (IResource resource : resources ) {
95113
96114 if (!resource .isAccessible ()) {
@@ -104,4 +122,13 @@ private void addFileResources(List<IResource> resources, List<IFile> files) thro
104122 }
105123 }
106124 }
125+
126+ @ Override
127+ public Object execute (ExecutionEvent event ) throws ExecutionException {
128+ var selection = HandlerUtil .getCurrentSelection (event );
129+ if (selection instanceof IStructuredSelection structuredSelection ) {
130+ checkSelection (structuredSelection );
131+ }
132+ return null ;
133+ }
107134}
0 commit comments