M.M
M.M

Reputation: 1373

How to search for a file from Eclipse Plugin?

I want to search for a file from Eclipse plugin. I used the following code but it did not work.

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    String str = JOptionPane.showInputDialog(null,
            "Please enter the search file name");
    for (IProject project : root.getProjects()) {
        IFile file = project.getFile(str);
        if (file.exists()) {
            JOptionPane.showMessageDialog(null, file.getName());
        } else {
            JOptionPane.showMessageDialog(null, "File Not Found! ");
        }
    }

Upvotes: 1

Views: 1159

Answers (1)

Chris Gerken
Chris Gerken

Reputation: 16392

Once you have the file name you can write a recursive method that searches containers for files and more containers. Start with the workspace root. Some things to note:

  • IWorkspaceRoot, IProject and IFolder are all IContainers.
  • IWorkspaceRoot contains IProjects
  • IProjects and IFolders contain IFolders and IFiles
  • IContainers have a members() method that returns a list of the IFiles and IContainers contained directly in that container.
  • IWorkspaceRoot, IProjects, IFolders, IFiles are all IResources and IResource has a method that tells you what kind of resource it is (project, folder, file, etc.)
  • An IFile can tell you its file name, project, project relative path and absolute file name

Upvotes: 3

Related Questions