How to Call Eclipse F5 Refresh Programmatically af

2019-07-18 00:12发布

问题:

I have been trying to call refresh (F5) programmatically in eclipse and get a NotHandledException, handler not found error. Help would be much appreciated.

In my class: NewPreferencePage extends PreferencePage implements IWorkbenchPreferencePage I have the following code:

@Override        
public void dispose() {

    super.dispose();

    final String COMMAND_ID = "org.eclipse.ui.file.refresh";
    IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);

    try {
        handlerService.executeCommand(COMMAND_ID,null);
    } catch (ExecutionException ex) {
        ex.printStackTrace();
    } catch (NotDefinedException ex) {
        ex.printStackTrace();
    } catch (NotEnabledException ex) {
        ex.printStackTrace();
    } catch (NotHandledException ex) {
        ex.printStackTrace();
    }
}

回答1:

You can use IResource#refreshLocal(), which has (almost) the same effect. The following is an example for a single file, but you can do a project in the same way:

IResource dfile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
dfile.create(blahblahblah, true, new NullProgressMonitor()); // obviously you don't need this, it's just an example
dfile.refreshLocal(IResource.DEPTH_ZERO, null);

See FAQ When should I use refreshLocal? for more information.



回答2:

I was just struggling with a similar problem and just want to share my insights here...

Background: I'm working with a GMF editor and wanted to add a number of nodes and edges programmatically. The "proper" way to do this is to add a number of create-node-and-element-commands, that will create both, the model element and the view, but for quickly prototyping a solution I just added the model elements and let GMF figure out the nodes for itself. That worked fine for the nodes, but edges required to refresh the editor with F5.

I looked into the plugin.xml searching for the F5 keybinding. Calling the command did not work, though, because it checks the selection, and apparently the selection is null when executing my actual command (the one creating the elements). However, what that command is actually doing, after the checks, worked for me (refreshing the diagram and revealing all the nodes and edges for the newly created elements):

EObject modelElement = ((View) ((EditPart) structuredSelection
        .getFirstElement()).getModel()).getElement();
List editPolicies = CanonicalEditPolicy
        .getRegisteredEditPolicies(modelElement);
for (Iterator it = editPolicies.iterator(); it.hasNext();) {
    CanonicalEditPolicy nextEditPolicy = (CanonicalEditPolicy) it.next();
    nextEditPolicy.refresh();
}

Or shorter:

CanonicalEditPolicy.getRegisteredEditPolicies(modelElement)
        .forEach(CanonicalEditPolicy::refresh);

That is, of course, assuming that (a) you are having a GMF editor, and (b) you have access to the model element and/or the edit part, preferably of the diagram itself. You can get those like this:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();