/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
public abstract class FolderSelectionViewFilter extends ViewerFilter {
private boolean _resourceOk;
public boolean select(Object element) {
Object parent = null;
if (element instanceof IResource) {
parent = ((IResource) element).getParent();
}
return select(null, parent, element);
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
_resourceOk = false;
if (element instanceof IContainer) {
IContainer container = (IContainer) element;
try {
container.accept(new IResourceVisitor() {
public boolean visit(IResource resource) throws CoreException {
if (!_resourceOk && resource instanceof IContainer) {
IContainer container = (IContainer) resource;
if (isValidLocation(container)) {
_resourceOk = true;
return false;
}
return true;
}
return false;
}
});
} catch (CoreException e) {
Activator.getDefault().logWarning("Error while searching for valid children in resource " + container.getLocation().toString(), e);
}
return _resourceOk;
} else {
return false;
}
}
public abstract boolean isValidLocation(IContainer container);
}