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.runtime.IPath; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.core.filebuffers.FileBuffers; 22 import org.eclipse.core.filebuffers.ITextFileBufferManager; 23 import org.eclipse.core.filebuffers.manipulation.ConvertLineDelimitersOperation; 24 25 import org.eclipse.jface.action.Action; 26 import org.eclipse.jface.window.Window; 27 28 import org.eclipse.ui.editors.text.FileBufferOperationAction; 29 30 import org.eclipse.ui.internal.editors.text.SelectResourcesDialog.IFilter; 31 32 33 39 public class ConvertLineDelimitersAction extends FileBufferOperationAction { 40 41 private String fLabel; 42 43 protected ConvertLineDelimitersAction(String lineDelimiter, String label) { 44 super(new ConvertLineDelimitersOperation(lineDelimiter)); 45 setText(constructLabel(label, lineDelimiter, System.getProperty("line.separator"))); fLabel= Action.removeMnemonics(label); 47 } 48 49 private static String constructLabel(String label, String lineDelimiter, String platformLineDelimiter) { 50 if (lineDelimiter.equals(platformLineDelimiter)) 51 return label + TextEditorMessages.ConvertLineDelimitersAction_default_label; 52 return label; 53 } 54 55 58 protected boolean isAcceptableLocation(IPath location) { 59 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 60 return location != null && manager.isTextFileLocation(location, true); 61 } 62 63 66 protected IFile[] collectFiles(IResource[] resources) { 67 IFile[] files= super.collectFiles(resources); 68 files= filterUnacceptableFiles(files); 69 if (containsOnlyFiles(resources)) 70 return files; 71 72 final IFilter filter= new IFilter() { 73 public boolean accept(IResource resource) { 74 return resource != null && isAcceptableLocation(resource.getFullPath()); 75 } 76 }; 77 78 SelectResourcesDialog dialog= new SelectResourcesDialog(getShell(), NLSUtility.format(TextEditorMessages.ConvertLineDelimitersAction_dialog_title, fLabel), TextEditorMessages.ConvertLineDelimitersAction_dialog_description, filter); 79 dialog.setInput(resources); 80 int result= dialog.open(); 81 if (Window.OK == result) { 82 IResource[] selectedResources= dialog.getSelectedResources(); 83 return super.collectFiles(selectedResources); 84 } 85 return null; 86 } 87 88 96 private boolean containsOnlyFiles(IResource[] resources) { 97 for (int i= 0; i < resources.length; i++) { 98 IResource resource= resources[i]; 99 if ((IResource.FILE & resource.getType()) == 0) 100 return false; 101 } 102 return true; 103 } 104 105 112 private IFile[] filterUnacceptableFiles(IFile[] files) { 113 Set filtered= new HashSet (); 114 for (int i= 0; i < files.length; i++) { 115 IFile file= files[i]; 116 if (isAcceptableLocation(file.getFullPath())) 117 filtered.add(file); 118 } 119 return (IFile[]) filtered.toArray(new IFile[filtered.size()]); 120 } 121 122 } 123 | Popular Tags |