How to get an open file's project in Eclipse

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get an open file's project in Eclipse

Ming Cheng
I selected a tab in Eclipse Edit window.

my code to get the selected open file project like this:

public static void getCurrentProject() {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow  window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

                if (window != null) {
                    ISelection iselection = window.getSelectionService().getSelection();
                    IStructuredSelection selection = (IStructuredSelection) iselection;
                    if (selection == null) {
                        log.error("null selection");
                        return;
                    }

                    IWorkbenchPage activePage = window.getActivePage();

                    IEditorPart activeEditor = activePage.getActiveEditor();

                    if (activeEditor != null) {
                       IEditorInput input = activeEditor.getEditorInput();

                       project = input.getAdapter(IProject.class);
                       if (project == null) {
                          IResource resource = input.getAdapter(IResource.class);
                          if (resource != null) {
                             project = resource.getProject();
                          }
                       }
                    } else {
                        log.error("null activeEditor");
                    }
                } else {
                    log.error("null window");
                }
            }
        });
    }

and window variable is not null but selection  is null.  Is my understanding wrong?

project  is a class static member variable.

Thanks.

_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/cdt-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get an open file's project in Eclipse

Alexander Fedorov
Remove the block regarding selection, you don't need it as far as I can see.
/*
                    ISelection iselection = window.getSelectionService().getSelection();
                    IStructuredSelection selection = (IStructuredSelection) iselection;
                    if (selection == null) {
                        log.error("null selection");
                        return;
                    }
*/
Regards,
AF

20.02.2020 11:56, Ming Cheng пишет:
I selected a tab in Eclipse Edit window.

my code to get the selected open file project like this:

public static void getCurrentProject() {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow  window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

                if (window != null) {
                    ISelection iselection = window.getSelectionService().getSelection();
                    IStructuredSelection selection = (IStructuredSelection) iselection;
                    if (selection == null) {
                        log.error("null selection");
                        return;
                    }

                    IWorkbenchPage activePage = window.getActivePage();

                    IEditorPart activeEditor = activePage.getActiveEditor();

                    if (activeEditor != null) {
                       IEditorInput input = activeEditor.getEditorInput();

                       project = input.getAdapter(IProject.class);
                       if (project == null) {
                          IResource resource = input.getAdapter(IResource.class);
                          if (resource != null) {
                             project = resource.getProject();
                          }
                       }
                    } else {
                        log.error("null activeEditor");
                    }
                } else {
                    log.error("null window");
                }
            }
        });
    }

and window variable is not null but selection  is null.  Is my understanding wrong?

project  is a class static member variable.

Thanks.

_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/cdt-dev


_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/cdt-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get an open file's project in Eclipse

Ming Cheng
Hi Alexander

thanks! it works now.


From: [hidden email] <[hidden email]> on behalf of Alexander Fedorov <[hidden email]>
Sent: Thursday, February 20, 2020 9:00 AM
To: [hidden email] <[hidden email]>
Subject: Re: [cdt-dev] How to get an open file's project in Eclipse
 
Remove the block regarding selection, you don't need it as far as I can see.
/*
                    ISelection iselection = window.getSelectionService().getSelection();
                    IStructuredSelection selection = (IStructuredSelection) iselection;
                    if (selection == null) {
                        log.error("null selection");
                        return;
                    }
*/
Regards,
AF

20.02.2020 11:56, Ming Cheng пишет:
I selected a tab in Eclipse Edit window.

my code to get the selected open file project like this:

public static void getCurrentProject() {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow  window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

                if (window != null) {
                    ISelection iselection = window.getSelectionService().getSelection();
                    IStructuredSelection selection = (IStructuredSelection) iselection;
                    if (selection == null) {
                        log.error("null selection");
                        return;
                    }

                    IWorkbenchPage activePage = window.getActivePage();

                    IEditorPart activeEditor = activePage.getActiveEditor();

                    if (activeEditor != null) {
                       IEditorInput input = activeEditor.getEditorInput();

                       project = input.getAdapter(IProject.class);
                       if (project == null) {
                          IResource resource = input.getAdapter(IResource.class);
                          if (resource != null) {
                             project = resource.getProject();
                          }
                       }
                    } else {
                        log.error("null activeEditor");
                    }
                } else {
                    log.error("null window");
                }
            }
        });
    }

and window variable is not null but selection  is null.  Is my understanding wrong?

project  is a class static member variable.

Thanks.

_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/cdt-dev


_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/cdt-dev