Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] How to get an open file's project in Eclipse
  • From: Ming Cheng <chengm349@xxxxxxxxxxx>
  • Date: Thu, 20 Feb 2020 08:56:34 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AW+NlvXBeIw9ei9m3sVf3+CVZowiRR5m/K2i/zvEGVU=; b=VTfGwVgoax+QfGpdaUCLfC2OpJC3VYDIU/540kKMCmjfToqrjqd1FA2bEfutrg+YaKBsVNRLtpcBKe7c6eI2+HT0rsY7dq8R9DnJelIk9CxtKEmtDC0Dgf4wqMz8H2z3wsT8Hq9y+nS9S8y9ctGc5B6F8PiS4ZWIdnjIp+bT+VxJjia1uLvFBtzBvAct/JvXUfoIhmQO6PCpPzIT/kkplaINeRX5oasxcUfxQh2ZKcpzqwwds+jdazeJ6e8tKGU3mL8e1WPxIZ7/yGkss1yBffV8S76trszvb5PetHuW+VeiaLHvbs3/X9tOs2bZKjvhDcrFxyvft60FnJ72mOq4kA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fLcuZUzb3wqyY2bTueiX+oqH6xbynxh0XkDwL7Rk1m4VYntnyi+LRV7JTPrKu4deX1PEUF6Mo0bK31RPwkSdHv4vYkL/NWbqanCE6h5r0UAy0yKG/MHT7t3esusjrLAIzCfdWgZ54mdjTks4sh6fj3kI0U+q0shc3IQ0vZgunUMG4nHm9OWAgyYxa3eTuX2d2CXrtnR50/Uzlva4RqmybTJLPeiFeUEOVWnlev3PL2sM1+5OsU1EPbLdYX22ogUib7B15rOVCBoBJUH6wdvXg6c/y0hzSuIza59IiyzVZIz8J7Y0/dXZodr0KNHZNonHEIXfZiN144ErNBSRUsVOsw==
  • Delivered-to: cdt-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/cdt-dev>
  • List-help: <mailto:cdt-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/cdt-dev>, <mailto:cdt-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/cdt-dev>, <mailto:cdt-dev-request@eclipse.org?subject=unsubscribe>
  • Thread-index: AQHV58r3aa+aNQh9zUq4S+cZ0M2GHw==
  • Thread-topic: How to get an open file's project in Eclipse

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.

Back to the top