/*
 * CatalogTreeCellRenderer.java
 *
 * Copyright (c) 2003, Oracle Corporation. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 */
package samples.javaclient.analyzer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.plaf.basic.BasicGraphicsUtils;

import oracle.bali.ewt.graphics.ImageUtils;

import oracle.dss.util.persistence.PersistableConstants;

/**
 * Customized tree cell renderer for displaying objects in the
 * BI Beans Catalog.
 */
public class CatalogTreeCellRenderer extends DefaultTreeCellRenderer
{
    /**
     * Constructor.
     */
    public CatalogTreeCellRenderer()
    {
        super();
        setHorizontalAlignment(JLabel.LEFT);
    }

    /**
     * Gets the tree cell renderer.
     * 
     * @param tree The tree whose cell to render.
     * @param value The object that the cell represents.
     * @param expanded <code>true</code> if no child nodes appear in the tree,
     *                 <code>false</code> if child nodes appear.
     * @param leaf <code>true</code> if this node has no child nodes,
     *             <code>false</code> if it does.
     * @param row The row in which the cell appears.
     * @param hasFocus <code>true</code> if the cell is selected,
     *                 <code>false</code> if it is not.
     *                 
     * @returns This renderer.
     */
    public Component getTreeCellRendererComponent(JTree tree,
												  Object value,
												  boolean selected,
												  boolean expanded,
												  boolean leaf,
												  int row,
												  boolean hasFocus)
	{
        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        if (value instanceof CatalogTreeNode)
        {
            CatalogTreeNode _node = (CatalogTreeNode) value;
            setText(_node.getName());
            String _type = _node.getObjectType();

            ImageIcon _icon = getIcon(_type);            
            if (_icon != null)
                setIcon(_icon);
        }

        return this;
    }

    // can customize this method to load different icon based on object type
    /**
     * Gets the GIF file for the specified object type.
     * 
     * @param type The type of object that this renderer is for.
     *             Valid values are:
     *             oracle.dss.util.persistence.PersistableConstants.GRAPH,
     *             oracle.dss.util.persistence.PersistableConstants.CROSSTAB,
     *             and 
     *             oracle.dss.util.persistence.PersistableConstants.SELECTION.
     *             
     * @return The appropriate GIF file.
     */
    private ImageIcon getIcon(String type)
    {
        String _imageFileName = null;
        if (PersistableConstants.GRAPH.equals(type))
            _imageFileName = "graph.gif";
        else if (PersistableConstants.CROSSTAB.equals(type))
            _imageFileName = "crosstab.gif";
        else if (PersistableConstants.SELECTION.equals(type))
            _imageFileName = "favorite.gif";

            
        if (_imageFileName != null)
            return new ImageIcon(ImageUtils.getImageResource(oracle.dss.bicontext.BIContext.class, "gui/images/"+_imageFileName));
        else
            return null;
    }
}

