/*
 * @(#)CustomDatePickerUI.java
 *
 * Copyright (c) 2003-2004 Stand By Soft, Ltd. All rights reserved.
 *
 * This software is the proprietary information of Stand By Soft, Ltd.  
 * Use is subject to license terms.
 */
package com.standbysoft.demo.date.plaf;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComponent;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ComponentUI;

import com.standbysoft.component.date.swing.plaf.basic.BasicDatePickerUI;
import com.standbysoft.component.util.swing.JComboBoxExt;

/**
 <p>A UI delegate for <code>JDatePicker</code> that overrides the method that
 * creates the combo box to register a focus listener.</p>
 
 <p>In order for all <code>JDatePicker</code> components to use such a custom UI,
 * one must register it in the system like this:</p>
 
 <pre>
 *         UIManager.put("DatePickerUI", CustomDatePickerUI.class.getName());
 </pre>
 
 <p>Whenever a new <code>JDatePicker</code> is created it will use the specified UI delegate.</p>
 */
public class CustomDatePickerUI extends BasicDatePickerUI {
    /**
     * Factory method that creates the actual UI delegate.
     */
    public static ComponentUI createUI(JComponent c) {
        return new CustomDatePickerUI();
    }
    
    /**
     * Overrides the method that creates the combo box in order to register a
     * focus listener for it.
     */
    protected JComboBoxExt createComboBox() {
        JComboBoxExt combo = super.createComboBox();
        
        FocusListener fl = new FocusHandler();
        combo.addFocusListener(fl);
        combo.getEditor().getEditorComponent().addFocusListener(fl);
        
        return combo;
    }
    
    /**
     * A focus listener for the combo box used by <code>JDatePicker</code> that
     * sets a green border when the component has focus and a red one when the
     * component loses it.
     */
    class FocusHandler implements FocusListener {
        public void focusGained(FocusEvent e) {
            datePicker.setBorder(new LineBorder(Color.green));
        }

        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                datePicker.setBorder(new LineBorder(Color.red));
            }
        }
    }
}