1 11 package org.eclipse.ui.editors.text; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.OperationCanceledException; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.core.runtime.jobs.Job; 30 31 import org.eclipse.core.filebuffers.FileBuffers; 32 import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner; 33 import org.eclipse.core.filebuffers.manipulation.IFileBufferOperation; 34 35 import org.eclipse.swt.widgets.Shell; 36 37 import org.eclipse.jface.action.Action; 38 import org.eclipse.jface.action.IAction; 39 import org.eclipse.jface.viewers.ISelection; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 42 import org.eclipse.jface.text.ITextSelection; 43 44 import org.eclipse.ui.IEditorInput; 45 import org.eclipse.ui.IEditorPart; 46 import org.eclipse.ui.IWorkbenchPart; 47 import org.eclipse.ui.IWorkbenchWindow; 48 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 49 import org.eclipse.ui.PlatformUI; 50 51 56 public class FileBufferOperationAction extends Action implements IWorkbenchWindowActionDelegate { 57 58 private Set fResources; 59 private IPath fLocation; 60 private IWorkbenchWindow fWindow; 61 protected IFileBufferOperation fFileBufferOperation; 62 63 protected FileBufferOperationAction(IFileBufferOperation fileBufferOperation) { 64 Assert.isNotNull(fileBufferOperation); 65 fFileBufferOperation= fileBufferOperation; 66 } 67 68 71 public void dispose() { 72 fResources= null; 73 fWindow= null; 74 fFileBufferOperation= null; 75 } 76 77 80 public void init(IWorkbenchWindow window) { 81 fWindow= window; 82 } 83 84 87 public void selectionChanged(IAction action, ISelection selection) { 88 89 fResources= new HashSet (); 90 fLocation= null; 91 92 if (selection instanceof IStructuredSelection) { 93 IStructuredSelection structuredSelection= (IStructuredSelection) selection; 94 95 Iterator e= structuredSelection.iterator(); 96 while (e.hasNext()) { 97 Object element= e.next(); 98 if (element instanceof IResource) 99 fResources.add(element); 100 else if (element instanceof IAdaptable) { 101 IAdaptable adaptable= (IAdaptable) element; 102 Object adapter= adaptable.getAdapter(IResource.class); 103 if (adapter instanceof IResource) 104 fResources.add(adapter); 105 } 106 } 107 } 108 109 if (selection instanceof ITextSelection) { 110 IWorkbenchWindow window= getWorkbenchWindow(); 111 if (window != null) { 112 IWorkbenchPart workbenchPart= window.getPartService().getActivePart(); 113 if (workbenchPart instanceof IEditorPart) { 114 IEditorPart editorPart= (IEditorPart) workbenchPart; 115 IEditorInput input= editorPart.getEditorInput(); 116 Object adapter= input.getAdapter(IResource.class); 117 if (adapter instanceof IResource) 118 fResources.add(adapter); 119 else { 120 adapter= input.getAdapter(ILocationProvider.class); 121 if (adapter instanceof ILocationProvider) { 122 ILocationProvider provider= (ILocationProvider) adapter; 123 fLocation= provider.getPath(input); 124 } 125 } 126 } 127 } 128 } 129 130 action.setText(getText()); 131 action.setEnabled(!fResources.isEmpty() || fLocation != null); 132 } 133 134 protected final IWorkbenchWindow getWorkbenchWindow() { 135 if (fWindow == null) 136 fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 137 return fWindow; 138 } 139 140 protected final Shell getShell() { 141 IWorkbenchWindow window= getWorkbenchWindow(); 142 return window == null ? null : window.getShell(); 143 } 144 145 148 public void run(IAction action) { 149 if (fResources != null && !fResources.isEmpty()) { 150 IFile[] files= collectFiles((IResource[]) fResources.toArray(new IResource[fResources.size()])); 151 if (files != null && files.length > 0) 152 doRun(files, null, fFileBufferOperation); 153 } else if (isAcceptableLocation(fLocation)) 154 doRun(null, fLocation, fFileBufferOperation); 155 } 156 157 protected IFile[] collectFiles(IResource[] resources) { 158 Set files= new HashSet (); 159 for (int i= 0; i < resources.length; i++) { 160 IResource resource= resources[i]; 161 if ((IResource.FILE & resource.getType()) > 0) 162 files.add(resource); 163 } 164 return (IFile[]) files.toArray(new IFile[files.size()]); 165 } 166 167 protected final void doRun(final IFile[] files, final IPath location, final IFileBufferOperation fileBufferOperation) { 168 Job job= new Job(fileBufferOperation.getOperationName()) { 169 protected IStatus run(IProgressMonitor monitor) { 170 IStatus status; 171 172 try { 173 174 int ticks= 100; 175 monitor.beginTask(fFileBufferOperation.getOperationName(), ticks); 176 try { 177 IPath[] locations; 178 if (files != null) { 179 ticks -= 30; 180 locations= generateLocations(files, new SubProgressMonitor(monitor, 30)); 181 } else 182 locations= new IPath[] { location }; 183 184 if (locations != null && locations.length > 0) { 185 FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell()); 186 runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks)); 187 } 188 status= new Status(IStatus.OK, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); } finally { 190 monitor.done(); 191 } 192 193 } catch (OperationCanceledException e) { 194 status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); } catch (CoreException e) { 196 status= new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "", e); } 198 return status; 199 } 200 }; 201 202 job.setUser(true); 203 job.schedule(); 204 } 205 206 protected final IPath[] generateLocations(IFile[] files, IProgressMonitor progressMonitor) { 207 progressMonitor.beginTask(TextEditorMessages.FileBufferOperationAction_collectionFiles_label, files.length); 208 try { 209 Set locations= new HashSet (); 210 for (int i= 0; i < files.length; i++) { 211 IPath fullPath= files[i].getFullPath(); 212 if (isAcceptableLocation(fullPath)) 213 locations.add(fullPath); 214 progressMonitor.worked(1); 215 } 216 return (IPath[]) locations.toArray(new IPath[locations.size()]); 217 218 } finally { 219 progressMonitor.done(); 220 } 221 } 222 223 protected boolean isAcceptableLocation(IPath location) { 224 return true; 225 } 226 } 227 | Popular Tags |