Activate find / replace for jface TextViewer eclipse worbench action

I created an eclipse plugin with the TextViewer interface to display a text document, but the standard search / replace will remain grayed out.

+4
source share
2 answers

I assume that you are using TextViewer in the view, not in the editor. In this case:

  • Your view, which uses TextViewer, should "adapt" to org.eclipse.jface.text.IFindReplaceTarget , i.e. its getAdapter() should return the target from the viewer.
  • You need to explicitly register a handler for the org.eclipse.ui.edit.findReplace command (which can be org.eclipse.ui.texteditorFindReplaceAction ). Check out the Platform Command Framework to get started.
+2
source

I used Martia Kayarik pointers to answer this question. I have work with the following code, which, however, uses the internal string identifier from TextEditor. However, here everything goes.

  • getAdapter() in the view should be implemented as follows ( viewer is an instance of TextViewer )

     public Object getAdapter(Class adapter) { if (IFindReplaceTarget.class.equals(adapter)) { if (viewer != null) { return viewer.getFindReplaceTarget(); } } return super.getAdapter(adapter); } 
  • In the createPartControl() your view, add this code:

     FindReplaceAction findAction= new FindReplaceAction(ResourceBundle.getBundle("org.eclipse.ui.texteditor.ConstructedTextEditorMessages"), null, this); IHandlerService handlerService= (IHandlerService) getSite().getService(IHandlerService.class); IHandler handler= new AbstractHandler() { public Object execute(ExecutionEvent event) throws ExecutionException { if (viewer != null && viewer.getDocument() != null) findAction.run(); return null; } }; handlerService.activateHandler("org.eclipse.ui.edit.findReplace", handler); 
  • XML is not required.

+2
source

All Articles