Catalin Adam
Catalin Adam

Reputation: 51

How can I launch an Eclipse command from Java?

How do I launch a find and replace command without the user clicking edit->find replace or ctrl+F? I need to do this with a plug-in written in Java.

Upvotes: 2

Views: 185

Answers (1)

Rich Seller
Rich Seller

Reputation: 84088

The action that launches the dialog is FindInFileActionDelegate (it has a few sister types for different scopes), this is found in the org.eclipse.search plugin.

The delegates all inherit from a common parent called RetrieverAction. The RetrieverAction's run() method shows the dialog and runs the query. You can take the relevant processing from this method. You may need to register as an ISelectionListener to track the active selection.

public void run() {
    IWorkbenchPage page= getWorkbenchPage();
    if (page == null) {
        return;
    }
    TextSearchQueryProvider provider= TextSearchQueryProvider.getPreferred();
    String searchForString= getSearchForString(page);
    if (searchForString.length() == 0) {
        MessageDialog.openInformation(getShell(), SearchMessages.RetrieverAction_dialog_title, SearchMessages.RetrieverAction_empty_selection);
        return;
    }
    try {
        ISearchQuery query= createQuery(provider, searchForString);
        if (query != null) {
            NewSearchUI.runQueryInBackground(query);
        }
    } catch (OperationCanceledException ex) {
        // action cancelled
    } catch (CoreException e) {
        ErrorDialog.openError(getShell(), SearchMessages.RetrieverAction_error_title, SearchMessages.RetrieverAction_error_message, e.getStatus());
    }
}

Upvotes: 1

Related Questions