package com.sun.solarisinstall.panels;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.ColorUIResource;

/**
 * The cell renderer for 3-way checkbox nodes.
 */
public class CheckNodeRenderer extends JPanel implements TreeCellRenderer {
	protected TreeLabel label;
	protected ImageIcon selectedCheckBox;
	protected ImageIcon emptyCheckBox;
	protected ImageIcon partialCheckBox;
	private String nodeType;
	
	// Constructor
	public CheckNodeRenderer(Image box1, Image box2, Image box3, String type) {
		setLayout(null);
		add(label = new TreeLabel());
		
		// Set up the icons for the nodes
		selectedCheckBox = new ImageIcon(box1);
		emptyCheckBox = new ImageIcon(box2);
		partialCheckBox = new ImageIcon(box3);
		
		// Set up the right background colors
		label.setForeground(UIManager.getColor("Tree.textForeground"));
                this.nodeType = type;
	}



	// Overridden from TreeCellRenderer
	public Component getTreeCellRendererComponent(JTree tree, Object value,
               boolean isSelected, boolean expanded,
               boolean leaf, int row, boolean hasFocus) {
         
                // Set up the label
                String  stringValue = tree.convertValueToText(value, isSelected,
			expanded, leaf, row, hasFocus);
   		setEnabled(tree.isEnabled());
   		DefaultMutableTreeNode node = null;	
   		// Give us the node for node-specific values
                if (nodeType.equals("GeoNode")) {
   		  node = (GeoNode)value;
                } else  {
                  node = (CDNode)value;
                }
   		
   		// Set up the label
   		if (!node.isRoot()) {
   	  	   label.setFont(tree.getFont());
   		   label.setText(stringValue);
                       if (nodeType.equals("GeoNode")) {
		          label.setSelected(((GeoNode)node).isNodeSelected());
			  label.setPartial(((GeoNode)node).isPartialSelected());
                       } else {
			  label.setSelected(((CDNode)node).isNodeSelected());
			  label.setPartial(((CDNode)node).isPartialSelected());
                       }
    
                       label.setFocus(hasFocus);
			
		       // Initialize all to blank check
		       label.setIcon(emptyCheckBox);
		} else {
		   label.setFont(tree.getFont());
   		   label.setText("");
   		   label.setIcon(null);
   		}
        
		// Return
		return this;
	} // getTreeCellRendererComponent
	
	// From JComponent
	public Dimension getPreferredSize() {
    	Dimension d_label = label.getPreferredSize();
    	return new Dimension(d_label.width, d_label.height);
	} // getPreferredSize()
  
  	// Overridden from Container
	public void doLayout() {
		Dimension d_label = label.getPreferredSize();
		int y_label = 0;
		label.setLocation(0,y_label);    
		label.setBounds(0,y_label,d_label.width,d_label.height + 2);   
	} // doLayout
	
	public void setBackground(Color color) {
		if (color instanceof ColorUIResource)
			color = null;
		super.setBackground(color);
	} // setBackground()
	


	/* ***********************************************
	 * Inner class for the node labels
	 * *********************************************** */
	public class TreeLabel extends JLabel {
		boolean isSelected;
		boolean hasFocus;
		boolean partialSelected;
    
		// Constructor
		public TreeLabel() {
		}
        
		public void setBackground(Color color) {
			if(color instanceof ColorUIResource)
				color = null;
			super.setBackground(color);
		} // setBackground

         
		public void paint(Graphics g) {
			String str;
			if ((str = getText()) != null) {
				if (0 < str.length()) {
					if (isSelected) {
						g.setColor(UIManager.getColor("Tree.textBackground"));
						if (partialSelected) 
							setIcon(partialCheckBox);
						else
							setIcon(selectedCheckBox);
					} else {
						g.setColor(UIManager.getColor("Tree.textBackground"));
						setIcon(emptyCheckBox);
					}
					
					Dimension d = getPreferredSize();
					int imageOffset = 0;
					Icon currentI = getIcon();
					if (currentI != null) {
						imageOffset = currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
					}
				
					g.fillRect(imageOffset, 0, d.width -1 - imageOffset, d.height);
					
					// This outlines the node that has been selected.
					if (hasFocus) {
						// g.setColor(UIManager.getColor("Tree.selectionBorderColor"));
						// g.drawRect(imageOffset, 0, d.width -1 - imageOffset, d.height -1);     
					}
				}
			}
			
			// Paint 
      		super.paint(g);
		} // paint()
  
    public Dimension getPreferredSize() {
			Dimension retDimension = super.getPreferredSize();
			if (retDimension != null) {
				retDimension = new Dimension(retDimension.width + 3,
				retDimension.height);
			}
			return retDimension;
		} // getPreferredSize()
    
		public void setSelected(boolean isSelected) {
			this.isSelected = isSelected;
		} // setSelection
	
		public void setPartial(boolean isPartial) {
			this.partialSelected = isPartial;
		} // setSelection
    
		public void setFocus(boolean hasFocus) {
			this.hasFocus = hasFocus;
		}
  } // TreeLabel
  
} // CheckNodeRenderer

