How to get the Path of current selected file in Eclipse?

10,944

Solution 1

You get the current selection as mentioned by @Danail Nachev. See http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html for information on working with the selection service.

Once you have the selection, the most common pattern is:

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        Object obj = ssel.getFirstElement();
        IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
                IFile.class);
        if (file == null) {
            if (obj instanceof IAdaptable) {
                file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
            }
        }
        if (file != null) {
            // do something
        }
    }

Edit:

Usually you get an InputStream from the IFile and process it that way. Using some FileSystemProviders or EFS implementations, there might not be a local path to the file.

PW

Solution 2

You can retrieve the current workbench selection using two methods with the following code:

  1. through the Workbench SelectionService
  2. getViewSite().getSelectionProvider().getSelection()

getViewSite().getWorkbenchWindow().getSelectionService()

You can find more information in this article.

Using the global workbench selection is the better approach, because it enables your view to get selection from everywhere, which is something the user might expect (I at least do). Also, almost all of the views in Eclipse (and I don't know exceptions to this rule) are using this approach.

If you absolutely must to tie your view to another view, then you can get all IWorkbenchPage iterate over them and search for the view by its ID and when you find the view, you call get its SelectionProvider to get the selection.

Only by reading this explanation, makes my hair go straight up. Considering that there might be multiple instances of the same view, you can end up with a selection from a random view. I'm not sure whether the user will make sense of how things work, unless you have some clear rules, which exactly view you need. It is up to you.

`

Share:
10,944
Abhishek Choudhary
Author by

Abhishek Choudhary

Programming Language Scala, Python, Java Used – Clojure, R-programming Distributed Data & Streaming Technologies Apache Spark, Hadoop, Apache Kafka, Apache Ignite, Apache Flink, Koalas, Apache Druid, Pinot, Apache Pulsar Distributed Databases & Technologies Cassandra, HBase, RocksDB, Elasticsearch, Neo4J Distributed Analytics Presto, Metabase, Apache Preset, Databricks Data Discovery/Catalog & Metadata Management Amundsen, Marquez, Apache Atlas, Metacat Orchestration & Workflow Technologies Docker, Terraform, Kubernetes, Alluxio, Apache Airflow, Prefect, Dagster, Argo, Kubeflow Machine Learning Spark ML, Pytorch, Tensorflow, Scipy, Scikit-Learn, H20 MLOPs & Infrastructure MLFlow, ZenML, DVC, LakeFS, Feast, Hopsworks github - https://github.com/abhishek-ch

Updated on June 04, 2022

Comments

  • Abhishek Choudhary
    Abhishek Choudhary almost 2 years

    I want to get the path of current selected file in Eclipse workspace but my project is a simple view plug-in project.

    I just want to display the name/path of the file selected as soon as user opens the view.