package com.apple.laf;
import java.awt.*;
import javax.swing.plaf.*;
import javax.swing.text.JTextComponent;
import apple.laf.JRSUIConstants.*;
import com.apple.laf.AquaUtilControlSize.*;
import com.apple.laf.AquaUtils.*;
public class AquaTextFieldBorder extends AquaBorder {
private static final RecyclableSingleton<AquaTextFieldBorder> instance = new RecyclableSingletonFromDefaultConstructor<AquaTextFieldBorder>(AquaTextFieldBorder.class);
public static AquaTextFieldBorder getTextFieldBorder() {
return instance.get();
}
public AquaTextFieldBorder() {
this(new SizeDescriptor(new SizeVariant().alterMargins(6, 7, 6, 7).alterInsets(3, 3, 3, 3)));
painter.state.set(Widget.FRAME_TEXT_FIELD);
painter.state.set(FrameOnly.YES);
painter.state.set(Size.LARGE);
}
public AquaTextFieldBorder(final SizeDescriptor sizeDescriptor) {
super(sizeDescriptor);
}
public AquaTextFieldBorder(final AquaTextFieldBorder other) {
super(other);
}
protected void setSize(final Size size) {
super.setSize(size);
painter.state.set(Size.LARGE);
}
public void paintBorder(final Component c, final Graphics g, int x, int y, int width, int height) {
if (!(c instanceof JTextComponent)) {
painter.state.set(State.ACTIVE);
painter.state.set(Focused.NO);
painter.paint(g, c, x, y, width, height);
return;
}
final JTextComponent jc = (JTextComponent)c;
final State state = getStateFor(jc);
painter.state.set(state);
painter.state.set(State.ACTIVE == state && jc.hasFocus() ? Focused.YES : Focused.NO);
if (jc.isOpaque()) {
painter.paint(g, c, x, y, width, height);
return;
}
final int shrinkage = getShrinkageFor(jc, height);
final Insets subInsets = getSubInsets(shrinkage);
x += subInsets.left;
y += subInsets.top;
width -= (subInsets.left + subInsets.right);
height -= (subInsets.top + subInsets.bottom);
if (shrinkage > 0) {
final Rectangle clipBounds = g.getClipBounds();
clipBounds.x += shrinkage;
clipBounds.width -= shrinkage * 2;
g.setClip(clipBounds);
}
painter.paint(g, c, x, y, width, height);
}
static int getShrinkageFor(final JTextComponent jc, final int height) {
if (jc == null) return 0;
final TextUI ui = jc.getUI();
if (ui == null) return 0;
final Dimension size = ui.getPreferredSize(jc);
if (size == null) return 0;
final int shrinkage = size.height - height;
return (shrinkage < 0) ? 0 : (shrinkage > 3) ? 3 : shrinkage;
}
protected Insets getSubInsets(final int shrinkage) {
final Insets insets = sizeVariant.insets;
if (shrinkage > 0) {
return new InsetsUIResource(insets.top - shrinkage, insets.left, insets.bottom - shrinkage, insets.right);
}
return insets;
}
public Insets getBorderInsets(final Component c) {
if (!(c instanceof JTextComponent) || c.isOpaque()) return new InsetsUIResource(3, 7, 3, 7);
return new InsetsUIResource(5, 5, 5, 5);
}
protected static State getStateFor(final JTextComponent jc) {
if (!AquaFocusHandler.isActive(jc)) {
return State.INACTIVE;
}
if (!jc.isEnabled()) {
return State.DISABLED;
}
if (!jc.isEditable()) {
return State.DISABLED;
}
return State.ACTIVE;
}
}