1 11 package org.eclipse.ui.editors.text; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.core.commands.AbstractHandler; 21 import org.eclipse.core.commands.ExecutionEvent; 22 import org.eclipse.core.commands.ExecutionException; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.core.runtime.IPath; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.OperationCanceledException; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.core.runtime.SubProgressMonitor; 32 import org.eclipse.core.runtime.jobs.Job; 33 34 import org.eclipse.core.resources.IFile; 35 import org.eclipse.core.resources.IResource; 36 37 import org.eclipse.core.filebuffers.FileBuffers; 38 import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner; 39 import org.eclipse.core.filebuffers.manipulation.IFileBufferOperation; 40 41 import org.eclipse.jface.viewers.ISelection; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 44 import org.eclipse.jface.text.ITextSelection; 45 46 import org.eclipse.ui.IEditorInput; 47 import org.eclipse.ui.IEditorPart; 48 import org.eclipse.ui.IWorkbenchPart; 49 import org.eclipse.ui.IWorkbenchWindow; 50 import org.eclipse.ui.PlatformUI; 51 52 59 public class FileBufferOperationHandler extends AbstractHandler { 60 61 private IFileBufferOperation fFileBufferOperation; 62 private IWorkbenchWindow fWindow; 63 private IResource[] fResources; 64 private IPath fLocation; 65 66 71 public FileBufferOperationHandler(IFileBufferOperation fileBufferOperation) { 72 fFileBufferOperation= fileBufferOperation; 73 } 74 75 82 public void initialize(IResource[] resources, IPath location) { 83 if (resources != null) { 84 fResources= new IResource[resources.length]; 85 System.arraycopy(resources, 0, fResources, 0, resources.length); 86 } else { 87 fResources= null; 88 } 89 fLocation= location; 90 } 91 92 95 protected final void computeSelectedResources() { 96 97 if (fResources != null || fLocation != null) 98 return; 99 100 ISelection selection= getSelection(); 101 if (selection instanceof IStructuredSelection) { 102 IStructuredSelection structuredSelection= (IStructuredSelection) selection; 103 ArrayList resources= new ArrayList (structuredSelection.size()); 104 105 Iterator e= structuredSelection.iterator(); 106 while (e.hasNext()) { 107 Object element= e.next(); 108 if (element instanceof IResource) 109 resources.add(element); 110 else if (element instanceof IAdaptable) { 111 IAdaptable adaptable= (IAdaptable) element; 112 Object adapter= adaptable.getAdapter(IResource.class); 113 if (adapter instanceof IResource) 114 resources.add(adapter); 115 } 116 } 117 118 if (!resources.isEmpty()) 119 fResources= (IResource[]) resources.toArray(new IResource[resources.size()]); 120 121 } else if (selection instanceof ITextSelection) { 122 IWorkbenchWindow window= getWorkbenchWindow(); 123 if (window != null) { 124 IWorkbenchPart workbenchPart= window.getPartService().getActivePart(); 125 if (workbenchPart instanceof IEditorPart) { 126 IEditorPart editorPart= (IEditorPart) workbenchPart; 127 IEditorInput input= editorPart.getEditorInput(); 128 Object adapter= input.getAdapter(IResource.class); 129 if (adapter instanceof IResource) 130 fResources= new IResource[] { (IResource) adapter }; 131 else { 132 adapter= input.getAdapter(ILocationProvider.class); 133 if (adapter instanceof ILocationProvider) { 134 ILocationProvider provider= (ILocationProvider) adapter; 135 fLocation= provider.getPath(input); 136 } 137 } 138 } 139 } 140 } 141 } 142 143 148 protected final ISelection getSelection() { 149 IWorkbenchWindow window= getWorkbenchWindow(); 150 if (window != null) 151 return window.getSelectionService().getSelection(); 152 return null; 153 } 154 155 160 protected final IWorkbenchWindow getWorkbenchWindow() { 161 if (fWindow == null) 162 fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 163 return fWindow; 164 } 165 166 172 protected IFile[] collectFiles(IResource[] resources) { 173 Set files= new HashSet (); 174 for (int i= 0; i < resources.length; i++) { 175 IResource resource= resources[i]; 176 if ((IResource.FILE & resource.getType()) > 0) 177 files.add(resource); 178 } 179 return (IFile[]) files.toArray(new IFile[files.size()]); 180 } 181 182 189 protected final void doRun(final IFile[] files, final IPath location, final IFileBufferOperation fileBufferOperation) { 190 Job job= new Job(fileBufferOperation.getOperationName()) { 191 protected IStatus run(IProgressMonitor monitor) { 192 IStatus status; 193 194 try { 195 196 int ticks= 100; 197 monitor.beginTask(fFileBufferOperation.getOperationName(), ticks); 198 try { 199 IPath[] locations; 200 if (files != null) { 201 ticks -= 30; 202 locations= generateLocations(files, new SubProgressMonitor(monitor, 30)); 203 } else 204 locations= new IPath[] { location }; 205 206 if (locations != null && locations.length > 0) { 207 FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell()); 208 runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks)); 209 } 210 status= new Status(IStatus.OK, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); } finally { 212 monitor.done(); 213 } 214 215 } catch (OperationCanceledException e) { 216 status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); } catch (CoreException e) { 218 status= new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "", e); } 220 return status; 221 } 222 }; 223 224 job.setUser(true); 225 job.schedule(); 226 } 227 228 233 protected final Shell getShell() { 234 IWorkbenchWindow window= getWorkbenchWindow(); 235 return window == null ? null : window.getShell(); 236 } 237 238 245 protected final IPath[] generateLocations(IFile[] files, IProgressMonitor progressMonitor) { 246 progressMonitor.beginTask(TextEditorMessages.FileBufferOperationHandler_collectionFiles_label, files.length); 247 try { 248 Set locations= new HashSet (); 249 for (int i= 0; i < files.length; i++) { 250 IPath fullPath= files[i].getFullPath(); 251 if (isAcceptableLocation(fullPath)) 252 locations.add(fullPath); 253 progressMonitor.worked(1); 254 } 255 return (IPath[]) locations.toArray(new IPath[locations.size()]); 256 257 } finally { 258 progressMonitor.done(); 259 } 260 } 261 262 268 protected boolean isAcceptableLocation(IPath location) { 269 return true; 270 } 271 272 276 public Object execute(ExecutionEvent event) throws ExecutionException { 277 computeSelectedResources(); 278 try { 279 280 if (fResources != null && fResources.length > 0) { 281 IFile[] files= collectFiles(fResources); 282 if (files != null && files.length > 0) 283 doRun(files, null, fFileBufferOperation); 284 } else if (isAcceptableLocation(fLocation)) 285 doRun(null, fLocation, fFileBufferOperation); 286 287 return null; 289 290 } finally { 291 fResources= null; 292 fLocation= null; 293 } 294 } 295 } 296 | Popular Tags |