001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import java.util.HashSet;
013: import java.util.Locale;
014:
015: import org.eclipse.core.resources.IContainer;
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.jface.viewers.ViewerFilter;
022:
023: /**
024: * FileExtensionsFilter
025: *
026: */
027: public class FileExtensionsFilter extends ViewerFilter {
028:
029: private HashSet fExtensions;
030:
031: /**
032: *
033: */
034: public FileExtensionsFilter() {
035: fExtensions = new HashSet();
036: }
037:
038: /* (non-Javadoc)
039: * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
040: */
041: public boolean select(Viewer viewer, Object parentElement,
042: Object element) {
043: // Select based on type
044: if (element instanceof IFile) {
045: // Files (IFile)
046: return processFile((IFile) element);
047: } else if (element instanceof IContainer) {
048: // Projects (IProject), Folders (IFolder)
049: return processContainer((IContainer) element, viewer,
050: parentElement);
051: }
052: return false;
053: }
054:
055: /**
056: * @param element
057: * @return
058: */
059: private boolean processContainer(IContainer container,
060: Viewer viewer, Object parentElement) {
061: // Skip closed projects
062: if ((container instanceof IProject)
063: && (((IProject) container).isOpen() == false)) {
064: return false;
065: }
066: // Process the container's members
067: try {
068: IResource[] resources = container.members();
069: for (int i = 0; i < resources.length; i++) {
070: if (select(viewer, parentElement, resources[i])) {
071: return true;
072: }
073: }
074: } catch (CoreException e) {
075: // Ignore
076: }
077: return false;
078: }
079:
080: /**
081: * @param element
082: */
083: private boolean processFile(IFile file) {
084: // Get the file's name (including extension)
085: String fileName = file.getName().toLowerCase(Locale.ENGLISH);
086: // Get the index of the last '.'
087: int lastDotIndex = fileName.lastIndexOf('.');
088: int lastIndex = fileName.length() - 1;
089: // Validate index
090: if (lastDotIndex < 0) {
091: return false;
092: } else if (lastDotIndex >= lastIndex) {
093: return false;
094: }
095: // Get the file's extension (remove dot)
096: String extension = fileName.substring(lastDotIndex + 1);
097: // Check to see if the extension should be filtered
098: return fExtensions.contains(extension);
099: }
100:
101: /**
102: * @param extension
103: */
104: public void addFileExtension(String extension) {
105: fExtensions.add(extension);
106: }
107:
108: }
|