1 package org.hibernate.eclipse.console; 2 3 import java.util.Arrays ; 4 import java.util.List ; 5 6 import org.eclipse.core.resources.IContainer; 7 import org.eclipse.core.resources.IFile; 8 import org.eclipse.core.resources.IFolder; 9 import org.eclipse.core.resources.IResource; 10 import org.eclipse.core.runtime.CoreException; 11 import org.eclipse.core.runtime.IPath; 12 import org.eclipse.jdt.internal.ui.JavaPlugin; 13 import org.eclipse.jface.viewers.Viewer; 14 import org.eclipse.jface.viewers.ViewerFilter; 15 16 23 public class FileFilter extends ViewerFilter { 24 25 private final String [] fileExtensions; 26 27 private List excludedFiles; 28 private boolean recursive ; 29 30 private final boolean allowDirectories; 31 32 38 public FileFilter(String [] fileExtensions, IFile[] excludedFiles, boolean recusive) { 39 this.fileExtensions = fileExtensions; 40 if (excludedFiles != null) { 41 this.excludedFiles= Arrays.asList(excludedFiles); 42 } else { 43 this.excludedFiles= null; 44 } 45 recursive = recusive; 46 allowDirectories = false; 47 } 48 49 public FileFilter(String [] fileExtensions, List excludedFiles, boolean recusive, boolean allowDirectories) { 50 51 this.fileExtensions = fileExtensions; 52 this.excludedFiles= excludedFiles; 53 recursive = recusive; 54 this.allowDirectories = allowDirectories; 55 } 56 57 60 public boolean select(Viewer viewer, Object parent, Object element) { 61 if ((element instanceof IFile)) { 62 if (this.excludedFiles != null && this.excludedFiles.contains(element)) { 63 return false; 64 } 65 return isFileExtension(((IFile)element).getFullPath()); 66 } 67 else if (allowDirectories && element instanceof IFolder) { 68 return true; 69 } else if (element instanceof IContainer) { if (!recursive ) { 71 return true; 72 } 73 try { 74 IResource[] resources= ((IContainer)element).members(); 75 for (int i= 0; i < resources.length; i++) { 76 if (select(viewer, parent, resources[i])) { 78 return true; 79 } 80 } 81 } catch (CoreException e) { 82 JavaPlugin.log(e.getStatus()); 83 } 84 } 85 return false; 86 } 87 88 public boolean isFileExtension(IPath path) { 89 for (int i= 0; i < fileExtensions.length; i++) { 90 if (path.lastSegment().endsWith(fileExtensions[i])) { 91 return true; 92 } 93 } 94 return false; 95 } 96 97 98 99 } 100 | Popular Tags |