/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.showdependencies;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import com.togethersoft.openapi.ide.resource.IdeResourceManagerAccess;
import com.togethersoft.openapi.ide.resource.IdeResourceIconType;
import com.togethersoft.openapi.rwi.*;
import com.togethersoft.openapi.sci.*;

/**
 * Tree cell renderer class.
 *
 * @author TogetherSoft
 * @see javax.swing specification
 */
class DependencyTreeRenderer extends DefaultTreeCellRenderer
{
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
        setForeground(Color.black);
        Object foundObject = ((DefaultMutableTreeNode)value).getUserObject();
        if (foundObject instanceof SciClass)
        { // the node stores a class/interface
            SciClass theClass = (SciClass)foundObject;
            if (theClass.isDeleted())
            { //the tree had been generated, and later the user has deleted the class
                setText("This class or interface is deleted.");
                setIcon(null);
            }
            else
            {
                // setting the right icon
                if (theClass.hasProperty(SciProperty.INTERFACE))
                {
                    if (interfaceIcon == null)
                    { // haven't found it yet
                        RwiElement element = RwiModelAccess.getModel().findElement(theClass.getUniqueName());
                        if (element != null)
                        {
                            interfaceIcon = IdeResourceManagerAccess.getResourceManager().getIcon(element,
                                IdeResourceIconType.SMALL);
                        }
                    }
                    setIcon(interfaceIcon);
                }
                else
                {
                    if (classIcon == null)
                    { // haven't found it yet
                        RwiElement element = RwiModelAccess.getModel().findElement(theClass.getUniqueName());
                        if (element != null)
                        {
                            classIcon = IdeResourceManagerAccess.getResourceManager().getIcon(element,
                                IdeResourceIconType.SMALL);
                        }
                    }
                    setIcon(classIcon);
                }
                setText(theClass.getQualifiedName()); //text for this node
            }

            TreePath path = tree.getPathForRow(row);
            String toolTip = null;

            // determining the tooltip
            if (path != null)
            { // row is visible
                int level = path.getPathCount();
                if (level == 2)
                { // the node is the first after the root
                    toolTip = (isVictimsGoFirst()) ? "Dependent class" : "The cause of depencency";
                }
                else if (level == 3)
                {
                    toolTip = (isVictimsGoFirst()) ? "The cause of depencency" : "Dependent class";
                }
            }
            setToolTipText(toolTip);
        }
        else if (foundObject instanceof Dependency)
        { // the node stores the Dependency object
            setIcon(null); // no icon
            if (((Dependency)foundObject).getVictim().isDeleted())
            { //the dependent class has been deleted
                setText("The dependent class is deleted.");
                setToolTipText(null);
            }
            else
            {
                CodeFragment codeFragment = ((Dependency)foundObject).getCodeFragment();
                setText(codeFragment.getVisibleText()); //text is the CodeFragment's text

                // setting the color corresponding to the kind of this CodeFragment
                Color color = null;
                switch (codeFragment.getKind()) {
                    case CodeFragment.REFERENCE_EXPRESSION:
                        color = Color.black;
                        break;
                    case CodeFragment.DECLARATION:
                        color = Color.green.darker();
                        break;
                    case CodeFragment.ATTRIBUTE:
                        color = Color.blue;
                        break;
                    case CodeFragment.OPERATION:
                        color = Color.orange.darker().darker().darker();
                        break;
                    case CodeFragment.INHERITANCE:
                        color = Color.red.darker();
                        break;
                }
                setForeground(color);
                setToolTipText(codeFragment.getComment());
            }
        }
        this.setPreferredSize(myPrototypeLabel.getPreferredSize());
        return this;
    }

    public boolean isVictimsGoFirst()
    {
        return myVictimsGoFirst;
    }

    public void setVictimsGoFirst(boolean victimsGoFirst)
    {
        myVictimsGoFirst = victimsGoFirst;
    }

    private boolean myVictimsGoFirst;
    private JLabel myPrototypeLabel =
    new JLabel("This is text of maximum length which will be visible completely");
    private Icon classIcon;
    private Icon interfaceIcon;

}
