22
22
import org .eclipse .ui .PlatformUI ;
23
23
24
24
public class ProjectExplorerListener implements ISelectionListener {
25
- static IProject projects [] = new IProject [0 ];
25
+ static IProject projects [] = new IProject [0 ];
26
26
27
- // public static IProject getActiveProject() {
28
- // return project;
29
- // }
27
+ // public static IProject getActiveProject() {
28
+ // return project;
29
+ // }
30
30
31
- public static IProject [] getSelectedProjects () {
32
- return projects ;
33
- }
31
+ public static IProject [] getSelectedProjects () {
32
+ return projects ;
33
+ }
34
34
35
- public static void registerListener () {
36
- IWorkbench wb = PlatformUI .getWorkbench ();
37
- IWorkbenchWindow awbw = wb .getActiveWorkbenchWindow ();
38
- ISelectionService ss = awbw .getSelectionService ();
35
+ public static void registerListener () {
36
+ IWorkbench wb = PlatformUI .getWorkbench ();
37
+ IWorkbenchWindow awbw = wb .getActiveWorkbenchWindow ();
38
+ ISelectionService ss = awbw .getSelectionService ();
39
39
40
- ProjectExplorerListener selectionListener = new ProjectExplorerListener ();
41
- ss .addPostSelectionListener (IPageLayout .ID_PROJECT_EXPLORER , selectionListener );
40
+ ProjectExplorerListener selectionListener = new ProjectExplorerListener ();
41
+ ss .addPostSelectionListener (IPageLayout .ID_PROJECT_EXPLORER , selectionListener );
42
42
43
- }
43
+ }
44
44
45
- @ Override
46
- public void selectionChanged (IWorkbenchPart part , ISelection newSelection ) {
47
- if (newSelection instanceof IStructuredSelection ) {
45
+ @ Override
46
+ public void selectionChanged (IWorkbenchPart part , ISelection newSelection ) {
47
+ if (!newSelection .isEmpty ()) {
48
+ if (newSelection instanceof IStructuredSelection ) {
48
49
49
- List <IProject > allSelectedprojects = new ArrayList <>();
50
- for (Object element : ((IStructuredSelection ) newSelection ).toList ()) {
51
- if (element instanceof IAdaptable ) {
52
- @ SuppressWarnings ("cast" ) // this is needed for the oracle
53
- // sdk as it needs the cast and
54
- // otherwise I have a warning
55
- IResource resource = (IResource ) ((IAdaptable ) element ).getAdapter (IResource .class );
56
- if (resource != null ) {
57
- allSelectedprojects .add (resource .getProject ());
58
- }
50
+ List <IProject > allSelectedprojects = new ArrayList <>();
51
+ for (Object element : ((IStructuredSelection ) newSelection ).toList ()) {
52
+ if (element instanceof IAdaptable ) {
53
+ @ SuppressWarnings ("cast" ) // this is needed for the
54
+ // oracle
55
+ // sdk as it needs the cast and
56
+ // otherwise I have a warning
57
+ IResource resource = (IResource ) ((IAdaptable ) element ).getAdapter (IResource .class );
58
+ if (resource != null ) {
59
+ allSelectedprojects .add (resource .getProject ());
60
+ }
59
61
62
+ }
63
+ }
64
+ if (allSelectedprojects .size () > 0 ) {
65
+ projects = new IProject [allSelectedprojects .size ()];
66
+ allSelectedprojects .toArray (projects );
67
+ }
68
+ return ;
69
+ }
60
70
}
61
- }
62
- projects = new IProject [allSelectedprojects .size ()];
63
- allSelectedprojects .toArray (projects );
64
- return ;
65
71
}
66
72
67
- }
68
-
69
73
}
0 commit comments