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

import org.eclipse.swt.graphics.Image;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.core.model.SearchResultType;
import org.jvnet.lookout.ui.core.preference.Preferences;
import org.jvnet.lookout.ui.core.tree.SearchResultDetail;
import org.jvnet.lookout.ui.core.tree.SearchResultItem;
import org.jvnet.lookout.ui.core.tree.label.AbstractTreeLabelProvider;
import org.jvnet.lookout.ui.core.util.Utilities;
import org.jvnet.lookout.ui.google.desktop.Plugin;

public class DesktopLabelProvider extends AbstractTreeLabelProvider {
    
    private Image desktopImage;
    
    public DesktopLabelProvider() {   
    	Plugin plugin = Plugin.getDefault();
        
        desktopImage = Utilities.getImage(
        		Plugin.getResourceString("desktop-result-icon"), plugin);
    }
    
    public Image getImage(SearchResultDetail detail) {

    	Image image = null;
        
    	SearchResultProperty property = (SearchResultProperty)detail.getProperty();
        
        if (SearchResultProperty.URL.equals(property)) {
            
            String groupingMode = getGroupingMode();
            
            if(Preferences.AlphabeticalMode.equals(groupingMode)) {
            	
            	//Use default icon which is an icon that identifie the source 
            	image = desktopImage;
            	
            } else if (
            		Preferences.ResourceTypeMode.equals(groupingMode) ||
            		Preferences.SearchEngineMode.equals(groupingMode)) {
            	
            	image = urlDetailImage;            	
            } 
        }
        return image;
    }
    
    public Image getImage(SearchResultItem item) {
    	
    	String groupingMode = getGroupingMode();
        
    	Image image = null;

    	SearchResultType type 
		= (SearchResultType)item.getPropertyValue(SearchResultProperty.TYPE);
    	
    	if(Preferences.AlphabeticalMode.equals(groupingMode)) {

    		//Use default icon which is the type of resource
    		image = getTypeBasedImage(type);
    		
    	} else if (Preferences.ResourceTypeMode.equals(groupingMode)) {
    		
    		if((item.getParent() instanceof SearchResultItem)) {                	

        		//If it has a parent of its own type, it 
        		//means that it has been grouped, in which case 
        		//we give it the source icon
    			image = desktopImage;
    	    } else {
            	
    	    	//Use default icon which is the type of resource
        		image = getTypeBasedImage(type);
        		
    	    }
    	} else if (Preferences.SearchEngineMode.equals(groupingMode)) {
    		if((item.getParent() instanceof SearchResultItem)) {                	

    			//If it has a parent of its own type, it 
        		//means that it has been grouped, in which case 
        		//we give it the source icon
    			//Use default icon which is the type of resource
        		image = getTypeBasedImage(type);
        		
    	    } else {
    	    	//This is the parent which we want to 
    	    	//display with the source icon
    	    	image = desktopImage;
            }			
    	}
    	return image;
    }    
}
