1 11 package org.eclipse.ui.internal.editors.text; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IPath; 19 20 import org.eclipse.core.filebuffers.FileBuffers; 21 import org.eclipse.core.filebuffers.ITextFileBufferManager; 22 import org.eclipse.core.filebuffers.manipulation.RemoveTrailingWhitespaceOperation; 23 24 import org.eclipse.jface.window.Window; 25 26 import org.eclipse.ui.editors.text.FileBufferOperationHandler; 27 28 import org.eclipse.ui.internal.editors.text.SelectResourcesDialog.IFilter; 29 30 31 36 public class RemoveTrailingWhitespaceHandler extends FileBufferOperationHandler { 37 38 public RemoveTrailingWhitespaceHandler() { 39 super(new RemoveTrailingWhitespaceOperation()); 40 } 41 42 45 protected boolean isAcceptableLocation(IPath location) { 46 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 47 return location != null && manager.isTextFileLocation(location, true); 48 } 49 50 53 protected IFile[] collectFiles(IResource[] resources) { 54 IFile[] files= super.collectFiles(resources); 55 files= filterUnacceptableFiles(files); 56 if (containsOnlyFiles(resources)) 57 return files; 58 59 final IFilter filter= new IFilter() { 60 public boolean accept(IResource resource) { 61 return resource != null && isAcceptableLocation(resource.getFullPath()); 62 } 63 }; 64 65 SelectResourcesDialog dialog= new SelectResourcesDialog(getShell(), TextEditorMessages.RemoveTrailingWhitespaceHandler_dialog_title, TextEditorMessages.RemoveTrailingWhitespaceHandler_dialog_description, filter); 66 dialog.setInput(resources); 67 int result= dialog.open(); 68 if (Window.OK == result) { 69 IResource[] selectedResources= dialog.getSelectedResources(); 70 return super.collectFiles(selectedResources); 71 } 72 return null; 73 } 74 75 83 private boolean containsOnlyFiles(IResource[] resources) { 84 for (int i= 0; i < resources.length; i++) { 85 IResource resource= resources[i]; 86 if ((IResource.FILE & resource.getType()) == 0) 87 return false; 88 } 89 return true; 90 } 91 92 99 private IFile[] filterUnacceptableFiles(IFile[] files) { 100 Set filtered= new HashSet (); 101 for (int i= 0; i < files.length; i++) { 102 IFile file= files[i]; 103 if (isAcceptableLocation(file.getFullPath())) 104 filtered.add(file); 105 } 106 return (IFile[]) filtered.toArray(new IFile[filtered.size()]); 107 } 108 109 } 110 | Popular Tags |