/*
 * @(#)PDFButtonBorder.java  1.0  31 March 2005
 *
 * Copyright (c) 2004 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */

package ch.randelshofer.pdf.plaf;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import java.beans.*;
/**
 * PDFButtonBorder.
 *
 * @author  Werner Randelshofer
 * @version 1.0  31 March 2005  Created.
 */
public class PDFButtonListener extends BasicButtonListener {
    transient long lastPressedTimestamp = -1;
    transient boolean shouldDiscardRelease = false;
    
    /** Creates a new instance. */
    public PDFButtonListener(AbstractButton button) {
        super(button);
    }
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (prop.equals("Frame.active")) {
            ((AbstractButton)e.getSource()).repaint();
        }
        super.propertyChange(e);
    }
    public void mousePressed(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e) ) {
            AbstractButton b = (AbstractButton) e.getSource();
            
            if(b.contains(e.getX(), e.getY())) {
                long multiClickThreshhold = b.getMultiClickThreshhold();
                long lastTime = lastPressedTimestamp;
                long currentTime = lastPressedTimestamp = e.getWhen();
                if (lastTime != -1 && currentTime - lastTime < multiClickThreshhold) {
                    shouldDiscardRelease = true;
                    return;
                }
                
                ButtonModel model = b.getModel();
                if (!model.isEnabled()) {
                    // Disabled buttons ignore all input...
                    return;
                }
                if (!model.isArmed()) {
                    // button not armed, should be
                    model.setArmed(true);
                }
                model.setPressed(true);
                if(!b.hasFocus() && b.isRequestFocusEnabled()) {
                    b.requestFocus();
                }
            }
        }
    };
    
    public void mouseReleased(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            // Support for multiClickThreshhold
            if (shouldDiscardRelease) {
                shouldDiscardRelease = false;
                return;
            }
            AbstractButton b = (AbstractButton) e.getSource();
            ButtonModel model = b.getModel();
            model.setPressed(false);
            model.setArmed(false);
        }
    };
    
    public void mouseEntered(MouseEvent e) {
        AbstractButton b = (AbstractButton) e.getSource();
        ButtonModel model = b.getModel();
        if(b.isRolloverEnabled()) {
            model.setRollover(true);
        }
        if (model.isPressed())
            model.setArmed(true);
    };
    
    public void mouseExited(MouseEvent e) {
        AbstractButton b = (AbstractButton) e.getSource();
        ButtonModel model = b.getModel();
        if(b.isRolloverEnabled()) {
            model.setRollover(false);
        }
        model.setArmed(false);
    };
    public void focusGained(FocusEvent e) {
        AbstractButton b = (AbstractButton) e.getSource();
        if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
            JRootPane root = b.getRootPane();
            if (root != null) {
                PDFButtonUI ui = (PDFButtonUI)PDFUtilities.getUIOfType(
                        ((AbstractButton)b).getUI(), PDFButtonUI.class);
                if (ui != null &&
                        UIManager.get(ui.getPropertyPrefix()+"defaultButtonFollowsFocus") != Boolean.FALSE) {
                    root.putClientProperty("temporaryDefaultButton", b);
                    root.setDefaultButton((JButton)b);
                    root.putClientProperty("temporaryDefaultButton", null);
                }
            }
        }
        b.repaint();
    }
    
    @Override
    public void focusLost(FocusEvent e) {
        AbstractButton b = (AbstractButton) e.getSource();
        JRootPane root = b.getRootPane();
        if (root != null) {
            JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton");
            if (b != initialDefault) {
                PDFButtonUI ui = (PDFButtonUI)PDFUtilities.getUIOfType(
                        ((AbstractButton)b).getUI(), PDFButtonUI.class);
                if (ui != null &&
                        UIManager.get(ui.getPropertyPrefix()+"defaultButtonFollowsFocus") != Boolean.FALSE) {
                    root.setDefaultButton(initialDefault);
                }
            }
        }
        
        b.getModel().setArmed(false);
        
        b.repaint();
    }
}
