alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.pde.internal.ui.util;

import java.util.HashSet;
import java.util.Locale;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

/**
 * FileExtensionsFilter
 *
 */
public class FileExtensionsFilter extends ViewerFilter {

	private HashSet fExtensions;

	/**
	 * 
	 */
	public FileExtensionsFilter() {
		fExtensions = new HashSet();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		// Select based on type
		if (element instanceof IFile) {
			// Files (IFile)
			return processFile((IFile) element);
		} else if (element instanceof IContainer) {
			// Projects (IProject), Folders (IFolder)
			return processContainer((IContainer) element, viewer, parentElement);
		}
		return false;
	}

	/**
	 * @param element
	 * @return
	 */
	private boolean processContainer(IContainer container, Viewer viewer, Object parentElement) {
		// Skip closed projects
		if ((container instanceof IProject) && (((IProject) container).isOpen() == false)) {
			return false;
		}
		// Process the container's members
		try {
			IResource[] resources = container.members();
			for (int i = 0; i < resources.length; i++) {
				if (select(viewer, parentElement, resources[i])) {
					return true;
				}
			}
		} catch (CoreException e) {
			// Ignore
		}
		return false;
	}

	/**
	 * @param element
	 */
	private boolean processFile(IFile file) {
		// Get the file's name (including extension)
		String fileName = file.getName().toLowerCase(Locale.ENGLISH);
		// Get the index of the last '.'
		int lastDotIndex = fileName.lastIndexOf('.');
		int lastIndex = fileName.length() - 1;
		// Validate index
		if (lastDotIndex < 0) {
			return false;
		} else if (lastDotIndex >= lastIndex) {
			return false;
		}
		// Get the file's extension (remove dot)
		String extension = fileName.substring(lastDotIndex + 1);
		// Check to see if the extension should be filtered
		return fExtensions.contains(extension);
	}

	/**
	 * @param extension
	 */
	public void addFileExtension(String extension) {
		fExtensions.add(extension);
	}

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.