/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet.components;

import javax.swing.*;
import javax.swing.border.*;

import java.awt.Component;
import java.beans.*;
import java.awt.*;

public class FileListCellRenderer extends javax.swing.JLabel 
    implements javax.swing.ListCellRenderer
    {
    protected static Border noFocusBorder = null;

    public FileListCellRenderer()
        {
	    super();
	    if ( noFocusBorder == null )
       	    noFocusBorder = new EmptyBorder(1, 1, 1, 1);
	    setOpaque( true );
	    setBorder( noFocusBorder );
        }
    
    public Component getListCellRendererComponent( 
        JList list, Object value, 
        int index, boolean isSelected, boolean cellHasFocus )
        {
        setComponentOrientation(list.getComponentOrientation());
        
        Color fore, back, altback;
	    if ( isSelected )
	        {
	        back = list.getSelectionBackground();
	        fore = list.getSelectionForeground();
            // dull down the background color if the control
            // lacks focus
            if ( !list.hasFocus() )
                {
    	        altback = list.getBackground();
                back = new Color( (back.getRed()+altback.getRed())/2 , 
                                (back.getGreen()+altback.getGreen())/2 , 
                                (back.getBlue() +altback.getBlue())/2   );
                }
	        }
	    else
	        {
	        back = list.getBackground();
	        fore = list.getForeground();
	        }

	    setBackground( back );
	    setForeground( fore );

        setText((value == null) ? "" : value.toString());
        if ( value instanceof Iconic )
            {
            Iconic iconic = (Iconic)value;
            setIcon( iconic.getIcon() );
            }

	    setEnabled(list.isEnabled());
	    setFont(list.getFont());
	    setBorder( 
	        cellHasFocus ? 
	        UIManager.getBorder( "List.focusCellHighlightBorder" ):
	        noFocusBorder );

	    return this;
        }
    }
