package com.apple.laf;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.plaf.UIResource;
public class AquaFocusHandler implements FocusListener, PropertyChangeListener {
private boolean wasTemporary = false;
private boolean repaintBorder = false;
protected static final String FRAME_ACTIVE_PROPERTY = "Frame.active";
public void focusGained(final FocusEvent ev) {
if (!wasTemporary || repaintBorder) {
AquaBorder.repaintBorder((JComponent)ev.getSource());
repaintBorder = false;
}
wasTemporary = false;
}
public void focusLost(final FocusEvent ev) {
wasTemporary = ev.isTemporary();
if (!wasTemporary) {
AquaBorder.repaintBorder((JComponent)ev.getSource());
}
}
public void propertyChange(final PropertyChangeEvent ev) {
if (!FRAME_ACTIVE_PROPERTY.equals(ev.getPropertyName())) return;
if (Boolean.TRUE.equals(ev.getNewValue())) {
repaintBorder = true;
} else if (wasTemporary) {
AquaBorder.repaintBorder((JComponent)ev.getSource());
}
}
protected static boolean isActive(final JComponent c) {
if (c == null) return true;
final Object activeObj = c.getClientProperty(AquaFocusHandler.FRAME_ACTIVE_PROPERTY);
if (Boolean.FALSE.equals(activeObj)) return false;
return true;
}
static final PropertyChangeListener REPAINT_LISTENER = new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
final Object source = evt.getSource();
if (source instanceof JComponent) {
((JComponent)source).repaint();
}
}
};
protected static void install(final JComponent c) {
c.addPropertyChangeListener(FRAME_ACTIVE_PROPERTY, REPAINT_LISTENER);
}
protected static void uninstall(final JComponent c) {
c.removePropertyChangeListener(FRAME_ACTIVE_PROPERTY, REPAINT_LISTENER);
}
static void swapSelectionColors(final String prefix, final JTree c, final Object value) {
}
static void swapSelectionColors(final String prefix, final JTable c, final Object value) {
if (!isComponentValid(c)) return;
final Color bg = c.getSelectionBackground();
final Color fg = c.getSelectionForeground();
if (!(bg instanceof UIResource) || !(fg instanceof UIResource)) return;
if (Boolean.FALSE.equals(value)) {
setSelectionColors(c, "Table.selectionInactiveForeground", "Table.selectionInactiveBackground");
return;
}
if (Boolean.TRUE.equals(value)) {
setSelectionColors(c, "Table.selectionForeground", "Table.selectionBackground");
return;
}
}
static void setSelectionColors(final JTable c, final String fgName, final String bgName) {
c.setSelectionForeground(UIManager.getColor(fgName));
c.setSelectionBackground(UIManager.getColor(bgName));
}
static void swapSelectionColors(final String prefix, final JList c, final Object value) {
if (!isComponentValid(c)) return;
final Color bg = c.getSelectionBackground();
final Color fg = c.getSelectionForeground();
if (!(bg instanceof UIResource) || !(fg instanceof UIResource)) return;
if (Boolean.FALSE.equals(value)) {
setSelectionColors(c, "List.selectionInactiveForeground", "List.selectionInactiveBackground");
return;
}
if (Boolean.TRUE.equals(value)) {
setSelectionColors(c, "List.selectionForeground", "List.selectionBackground");
return;
}
}
static void setSelectionColors(final JList c, final String fgName, final String bgName) {
c.setSelectionForeground(UIManager.getColor(fgName));
c.setSelectionBackground(UIManager.getColor(bgName));
}
static boolean isComponentValid(final JComponent c) {
if (c == null) return false;
final Window window = SwingUtilities.getWindowAncestor(c);
if (window == null) return false;
return true;
}
}