Section 9.3. Batching Change Events


9.3. Batching Change Events

Anytime a UI plug-in modifies resources, it should wrap the resource modification code by subclassing org.eclipse.ui.actions.WorkspaceModifyOperation. The primary consequence of using this operation is that events that typically occur as a result of workspace changes (e.g., the firing of resource deltas, performance of autobuilds, etc.) are deferred until the outermost operation has successfully completed. In the Favorites view, if you want to implement a delete operation that removed the resources themselves rather than just the Favorites items that referenced the resources, then it might be implemented as shown below.

The run() method, inherited from WorkspaceModifyOperation and called by an Action or IActionDelegate, first calls the execute() method and then fires a single change event containing all the resources changed by the execute() method.

package com.qualityeclipse.favorites.actions; import ... public class DeleteResourcesOperation    extends WorkspaceModifyOperation {    private final IResource[] resources;    public DeleteResourcesOperation(IResource[] resources) {       this.resources = resources;    }    protected void execute(IProgressMonitor monitor)       throws          CoreException,          InvocationTargetException,          InterruptedException    {       monitor.beginTask("Deleting resources...", resources.length);       for (int i = 0; i < resources.length; i++) {          if (monitor.isCanceled())             break;          resources[i].delete(             true, new SubProgressMonitor(monitor, 1));       }       monitor.done();    } } 


If you are modifying resources in a headless Eclipse environment or in a plug-in that does not rely on any UI plug-ins, the WorkspaceModifyOperation class is not accessible. In this case, use the IWorkspace.run() method to batch change events.

protected void execute(IProgressMonitor monitor)    throws CoreException {    ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {       public void run(IProgressMonitor monitor) throws CoreException        {          monitor.beginTask(             "Deleting resources...", resources.length);          for (int i = 0; i < resources.length; i++) {             resources[i].delete(                true, new SubProgressMonitor(monitor, 1));          }          monitor.done();        }    }, monitor); } 




Eclipse(c) Building Commercial-Quality Plug-ins
Eclipse: Building Commercial-Quality Plug-ins (2nd Edition)
ISBN: 032142672X
EAN: 2147483647
Year: 2004
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net