/**
 * Created on Jun 18, 2005
 * $id:  $
 */
package org.jvnet.lookout.ui.core.tree.sort;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.jvnet.lookout.ui.core.tree.SearchResultDetail;
import org.jvnet.lookout.ui.core.tree.SearchResultItem;

/**
 * <code>SearchResultsSorter</code> sorts the items based on the types 
 * defined in the SearchResultItem.Type enumeration.
 * 
 * 
 * <p>
 * @author Cleo Barretto
 *
 */
//We do not want to change the sorting of the elements as this is one of the points of using a search engine
public class SearchResultsSorter extends ViewerSorter {

    private SearchResultItemSorter searchResultItemSorter;
    private SearchResultDetailSorter searchResultDetailSorter;
        
    public int category(Object element) {        
        if(element instanceof SearchResultItem) {
            return searchResultItemSorter.category((SearchResultItem)element);
        } else if (element instanceof SearchResultDetail) {
            return searchResultDetailSorter.category((SearchResultDetail)element);
        } 
        
        //no sorting...
        return 0;
    }
    
    public int compare(Viewer viewer, Object object1, Object object2) {
        
        if((object1 instanceof SearchResultItem) &&
                (object2 instanceof SearchResultItem)) {
            
            return searchResultItemSorter.compare(
                    viewer, 
                    (SearchResultItem)object1, 
                    (SearchResultItem)object2);            
        } else if ((object1 instanceof SearchResultDetail) &&
                (object2 instanceof SearchResultDetail)) {
            
            return searchResultDetailSorter.compare(
                    viewer, 
                    (SearchResultDetail)object1, 
                    (SearchResultDetail)object2);                    
        } 
        
        //no sorting...
        return 0;
    }

    public SearchResultDetailSorter getSearchResultDetailSorter() {
        return this.searchResultDetailSorter;
    }

    public void setSearchResultDetailSorter(
            SearchResultDetailSorter searchResultDetailSorter) {
        this.searchResultDetailSorter = searchResultDetailSorter;
    }

    public SearchResultItemSorter getSearchResultItemSorter() {
        return this.searchResultItemSorter;
    }

    public void setSearchResultItemSorter(
            SearchResultItemSorter searchResultItemSorter) {
        this.searchResultItemSorter = searchResultItemSorter;
    }    
    
}