简体   繁体   中英

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 :)

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.

I write some util class to do the job. Hope it help you

    public class SelectionUtil {
    private IWorkbenchWindow window;
    private IWorkbenchPage activePage;
    private TreeSelection treeSelection;
    private TreePath[] treePaths;
    HashMap<Object, Object> selectData;
    private IProject theProject;
    private IResource theResource;
    private IFile theFile;
    private IPackageFragment theFragment;
    private String workspaceName;
    private String projectName;
    private String fileName;
    private String fileNameFile;
    private String fragmentName;
    private TreePath treePath;
    public SelectionUtil(ExecutionEvent event) {
        this.window = HandlerUtil.getActiveWorkbenchWindow(event);
        // Get the active WorkbenchPage
        this.activePage = this.window.getActivePage();

        // Get the Selection from the active WorkbenchPage page
        ISelection selection = this.activePage.getSelection();
        if (selection instanceof ITreeSelection) {
            this.treeSelection = (TreeSelection) selection;
            this.treePaths = treeSelection.getPaths();
            this.treePath = treePaths[0];
            selectData = new ProjectSelectionUtil()
                    .populatePojectData(treePath);
            setData();
        } else {
            String selectionClass = selection.getClass().getSimpleName();
            MessageDialog
                    .openError(
                            this.window.getShell(),
                            "Unexpected Selection Class",
                            String.format(
                                    "Expected a TreeSelection but got a %s instead.\nProcessing Terminated.",
                                    selectionClass));
        }
    }
   public void setData() {
        this.theProject = (IProject) selectData.get("Project");
        this.theResource = (IResource) selectData.get("Resource");
        this.theFragment = (IPackageFragment) selectData.get("Fragment");
        this.workspaceName = this.theResource.getWorkspace().getRoot()
                .getLocation().toOSString();
        this.projectName = this.theProject.getName();
        if (this.theFragment != null)
            this.fragmentName = this.theFragment.getElementName();
        try {
            if (!this.theResource.getName().isEmpty()
                    && this.theResource.getName().length() > 5)
                this.fileName = this.theResource.getName().substring(0,
                        this.theResource.getName().length() - 5);
        } catch (NullPointerException e) {
            System.out
                    .println(" GactusWindowSelectionUtil SetData NullPointerException"
                            + e.getMessage() + e.getLocalizedMessage());
        } catch (StringIndexOutOfBoundsException e) {
            System.out
                    .println(" StringIndexOutOfBoundsException SetData NullPointerException"
                            + e.getMessage() + e.getLocalizedMessage());
        }

    }






    public String toString() {
        ProjectInformation myProject = new ProjectInformation(theProject);
        return "Segment Count " + treePath.getSegmentCount() + " Iproject"
                + myProject.toString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM