Programming eclipse plugin for searching file within a project (basic help needed)

I have to program an Eclipse-Plugin but I have never done this before so I have some questions. The plugin should appear in the context menu when you right click a Java project in the project explorer of Eclipse. It should open a dialog where the user can enter a file name he is looking for within the selected project and then the file gets highlighted (if there is a file with this name). What I managed to do so far is to setup the plugin development project, the extension point for the plugin and the dialog. But now I don't know how to get access to the selected project. Can you tell me how this is done or a link to the corresponding API? Thanks in advance :)

Answers


I assume you have a Handler class for the right-click action in your plugin. The Handler extends the AbstractHandler and overrides the method execute(..).

Then you can do something like this:

public class YourHandler extends AbstractHandler {

private ExecutionEvent event;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

    // First get the tree of the right-clicked project.
    ISelection sel = HandlerUtil.getActiveMenuSelection(event);

    IResource resource = null;
    IProject project = null;

    try {
        IStructuredSelection selection = (IStructuredSelection) sel;

        // Get the first element of the tree (return type Object).
        Object firstElement = selection.getFirstElement();

        // Get the IResource and from this the IProject of the selection.
        if (firstElement instanceof IAdaptable) {
            IResource resource = (IResource) (((IAdaptable) firstElement)
                .getAdapter(IResource.class));

            project = res.getProject();
        }
    } catch (ClassCastException e) {
        // Do nothing.
    }

    // Then you can do something with the project.

    return project;
}

Look also at the Eclipse API for IProject for what you can do: http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IProject.html

For example getting a file from name:

IFile getFile(String name)

Returns a handle to the file with the given name in this project.

Hope this helps.

By the way: if you need some nice tutorials about developing Eclipse plugins, I can recommend this website http://www.vogella.com/eclipse.html

Cheers.


Need Your Help

Durandal Weyland optimizations doesn't handle the require function

javascript requirejs durandal durandal-2.0 weyland

I've found out that weyland optimizer replaces the paths in the javascript files with their real values. But it doesn't replace the paths used in middle of the code using require function.

Differences between NSTableView and NSCollectionView

cocoa cocoa-bindings nstableview nscollectionview

I'm trying to decide which path to take for developing my Snow Leopard app. First, I should preface with the obvious differences: