/*
 * @(#)PDFTextFieldFocusHandler.java  1.0.1  2007-08-13
 *
 * Copyright (c) 2004-2007 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.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;
/**
 * PDFTextFieldFocusHandler. Selects all text of a JTextComponent, if
 * the user used a keyboard focus traversal key, to transfer the focus on the
 * JTextComponent.
 *
 * @author Werner Randelshofer
 * @version 1.0.1 2007-08-13 Clear lastKeyboardTraversingComponent in keyboard
 * manager after having gained focus.
 * <br>1.0 2007-08-06 reated.
 */
public class PDFTextFieldFocusHandler implements FocusListener {
    private static PDFTextFieldFocusHandler instance;
    
    public static PDFTextFieldFocusHandler getInstance() {
        if (instance == null) {
            instance = new PDFTextFieldFocusHandler();
        }
        return instance;
    }
    
    /**
     * Prevent instance creation.
     */
    private PDFTextFieldFocusHandler() {
    }
    
    public void focusGained(FocusEvent event) {
        PDFUtilities.repaintBorder((JComponent) event.getComponent());
        
        final JTextComponent tc = (JTextComponent) event.getSource();
        if (tc.isEditable() && tc.isEnabled()) {
            
            String uiProperty;
            if (tc instanceof JPasswordField) {
                uiProperty = "PasswordField.autoSelect";
            } else if (tc instanceof JFormattedTextField) {
                uiProperty = "FormattedTextField.autoSelect";
            } else {
                uiProperty = "TextField.autoSelect";
            }
            
            if (tc.getClientProperty("Quaqua.TextComponent.autoSelect") == Boolean.TRUE ||
                    tc.getClientProperty("Quaqua.TextComponent.autoSelect") == null &&
                    PDFLookAndFeel.getInstance().getBoolean(uiProperty)
                    ) {
                if (KeyboardFocusManager.getCurrentKeyboardFocusManager() instanceof PDFKeyboardFocusManager) {
                    PDFKeyboardFocusManager kfm = (PDFKeyboardFocusManager) KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    if (event.getOppositeComponent() == kfm.getLastKeyboardTraversingComponent()) {
                        tc.selectAll();
                    }
                }
            }
        }
        if (KeyboardFocusManager.getCurrentKeyboardFocusManager() instanceof PDFKeyboardFocusManager) {
            PDFKeyboardFocusManager kfm = (PDFKeyboardFocusManager) KeyboardFocusManager.getCurrentKeyboardFocusManager();
            kfm.setLastKeyboardTraversingComponent(null);
        }
    }
    
    public void focusLost(FocusEvent event) {
        PDFUtilities.repaintBorder((JComponent) event.getComponent());
    }
}

