/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// 
//  DESCRIPTION
//  
//  Peter Pilgrim
//  Thu Apr 22 01:39:01 BST 1999
//  
//  RCS HEADER ``WaveformListRenderer.java''
//  
//  $Author$
//  $Date$
//  $Source$
//  $Revision$    $State$    $Locker$
// 
//  History
//  ================================================================================ 
//	$Log$
//

package xenon.soundkit.gui;

import java.util.Hashtable;
import java.net.URL;
import java.awt.Component;
import java.awt.Color;
import java.awt.Font;

import javax.swing.*;				// for JTree
import javax.swing.event.*;			// for events
import javax.swing.border.*;			// for TitledBorder

import xenon.soundkit.*;


public class WaveformListCellRenderer extends JLabel
implements ListCellRenderer 
{
    public static final String	DEFAULT_ICON_NAME="generic-wave.gif";
    private static Hashtable	iconCache = new Hashtable();

    private Color		unselectedColor = new Color( 224, 224, 224 );
    private EmptyBorder		emptyBorder = new EmptyBorder( 1,1,1,1 );
    private LineBorder		lineBorder  = new LineBorder( Color.yellow, 1 );
    
    public WaveformListCellRenderer() 
    {
	setOpaque(true);
	
    }

    public Component getListCellRendererComponent(
	JList list,
	Object value,
	int index,
	boolean isSelected,
	boolean cellHasFocus )
    {
	if ( value instanceof Wave ) {
	    Wave wave = (Wave)value;
	    ImageIcon icon = mapWaveToImageIcon( wave );
	    setIcon( icon );
	    setText( wave.getDisplayName() +" wave");
	}
	else if ( value instanceof WaveTuple ) {
	    WaveTuple waveTuple = (WaveTuple)value;
	    ImageIcon icon = mapWaveToImageIcon( waveTuple.getWave() );
	    setIcon( icon );
	    setText( waveTuple.getName() );
	}
	else {
	    setIcon(null);
	    setText(value.toString());
	}
	
	if (cellHasFocus)
	    setBorder( lineBorder );
	else
	    setBorder( emptyBorder );
	setBackground(isSelected ? Color.red : unselectedColor );
	setForeground(isSelected ? Color.white : Color.black );
	return (this);
    }
    
    protected ImageIcon mapWaveToImageIcon( Wave wave )
    {
	ImageIcon icon;
	String image_name="";
	
	if ( wave instanceof WhiteNoiseWave ) 
	    image_name="noise-wave.gif";
	else if ( wave instanceof StaticWave ) {
	    StaticWave static_wave = (StaticWave)wave;
	    int type = static_wave.getWaveformType();
	    switch ( type ) {
	      case StaticWave.Sine:
		  image_name="sine-wave.gif"; break;
	      case StaticWave.Square:
		  image_name="square-wave.gif"; break;
	      case StaticWave.Triangle:
		  image_name="triangle-wave.gif"; break;
	      case StaticWave.RampForward:
		  image_name="ramp-forward-wave.gif"; break;
	      case StaticWave.RampBackward:
		  image_name="ramp-backward-wave.gif"; break;
	      case StaticWave.PeriodicWhiteNoise:
		  image_name="noise-wave.gif"; break;
	    }
	}
	
	icon = getImageIconFromCache( "images/"+image_name );
	if (icon == null)
	    icon = getImageIconFromCache( "images/"+DEFAULT_ICON_NAME );
		
	return (icon);
    }

    protected ImageIcon getImageIconFromCache( String iconName )
    {
	// System.out.println( "getImageIconFromCache("+iconName+")" );

	ImageIcon icon=null;
	icon = (ImageIcon)iconCache.get( iconName );
	if ( icon == null ) {
	    URL url = getClass().getResource( iconName );
	    if ( url != null ) {
		icon = new ImageIcon( url );
		iconCache.put( iconName, icon );
	    }
	    // System.out.println( " url:"+ (url != null ? url.toExternalForm(): "[null]" ) );
	}
	// System.out.println( "*FINAL* icon: "+icon );
	
	return (icon);
    }
    
}

// fini
