/**
 * $id:  $
 */
package org.jvnet.lookout.ui.core.tree.filter.detail;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.ui.core.tree.SearchResultDetail;

public class PropertyFilter extends ViewerFilter {

	private List<SearchResultProperty> visibleProperties;
	
	public PropertyFilter() {
		super();		
		visibleProperties = new ArrayList<SearchResultProperty>(2);
		
		synchWithUserPreferences();
	}
	
	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		
		if(element instanceof SearchResultDetail) {			
			return checkIfDetailIsVisible((SearchResultDetail)element);
		} else {
			//Should not filter anything else... 
			return true;
		}
	}
	
	public void setPropertyVisible(SearchResultProperty property, boolean visible) {
		if(visible && !visibleProperties.contains(property)) {
			visibleProperties.add(property);
		} else if(!visible && visibleProperties.contains(property)) {
			visibleProperties.remove(property);
		}
	}

	private boolean checkIfDetailIsVisible(SearchResultDetail detail) {		
		return visibleProperties.contains(detail.getProperty());		
	}
	
	public void synchWithUserPreferences() {
		//CXB Should probably read from preferences
		setPropertyVisible(SearchResultProperty.SNIPPET, true);
		setPropertyVisible(SearchResultProperty.URL, true);					
	}

}
