4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/A
4632N/Aimport javax.swing.plaf.*;
4632N/Aimport javax.swing.text.JTextComponent;
4632N/A
4632N/Aimport apple.laf.JRSUIConstants.*;
4632N/A
4632N/Aimport com.apple.laf.AquaUtilControlSize.*;
4632N/Aimport com.apple.laf.AquaUtils.*;
4632N/A
4632N/Apublic class AquaTextFieldBorder extends AquaBorder {
4632N/A protected static final RecyclableSingleton<AquaTextFieldBorder> instance = new RecyclableSingletonFromDefaultConstructor<AquaTextFieldBorder>(AquaTextFieldBorder.class);
4632N/A public static AquaTextFieldBorder getTextFieldBorder() {
4632N/A return instance.get();
4632N/A }
4632N/A
4632N/A public AquaTextFieldBorder() {
4632N/A this(new SizeDescriptor(new SizeVariant().alterMargins(6, 7, 6, 7).alterInsets(3, 3, 3, 3)));
4632N/A painter.state.set(Widget.FRAME_TEXT_FIELD);
4632N/A painter.state.set(FrameOnly.YES);
4632N/A painter.state.set(Size.LARGE);
4632N/A }
4632N/A
4632N/A public AquaTextFieldBorder(final SizeDescriptor sizeDescriptor) {
4632N/A super(sizeDescriptor);
4632N/A }
4632N/A
4632N/A public AquaTextFieldBorder(final AquaTextFieldBorder other) {
4632N/A super(other);
4632N/A }
4632N/A
4632N/A protected void setSize(final Size size) {
4632N/A super.setSize(size);
4632N/A painter.state.set(Size.LARGE);
4632N/A }
4632N/A
4632N/A public void paintBorder(final Component c, final Graphics g, int x, int y, int width, int height) {
4632N/A// g.setColor(Color.MAGENTA);
4632N/A// g.drawRect(x, y, width - 1, height - 1);
4632N/A
4632N/A if (!(c instanceof JTextComponent)) {
4632N/A painter.state.set(State.ACTIVE);
4632N/A painter.state.set(Focused.NO);
4632N/A painter.paint(g, c, x, y, width, height);
4632N/A return;
4632N/A }
4632N/A
4632N/A final JTextComponent jc = (JTextComponent)c;
4632N/A final State state = getStateFor(jc);
4632N/A painter.state.set(state);
4632N/A painter.state.set(State.ACTIVE == state && jc.hasFocus() ? Focused.YES : Focused.NO);
4632N/A
4632N/A if (jc.isOpaque()) {
4632N/A painter.paint(g, c, x, y, width, height);
4632N/A return;
4632N/A }
4632N/A
4632N/A final int shrinkage = getShrinkageFor(jc, height);
4632N/A final Insets subInsets = getSubInsets(shrinkage);
4632N/A x += subInsets.left;
4632N/A y += subInsets.top;
4632N/A width -= (subInsets.left + subInsets.right);
4632N/A height -= (subInsets.top + subInsets.bottom);
4632N/A
4632N/A if (shrinkage > 0) {
4632N/A final Rectangle clipBounds = g.getClipBounds();
4632N/A clipBounds.x += shrinkage;
4632N/A clipBounds.width -= shrinkage * 2;
4632N/A g.setClip(clipBounds);
4632N/A }
4632N/A
4632N/A painter.paint(g, c, x, y, width, height);
4632N/A// g.setColor(Color.ORANGE);
4632N/A// g.drawRect(x, y, width - 1, height - 1);
4632N/A }
4632N/A
4632N/A static int getShrinkageFor(final JTextComponent jc, final int height) {
4632N/A if (jc == null) return 0;
4632N/A final TextUI ui = jc.getUI();
4632N/A if (ui == null) return 0;
4632N/A final Dimension size = ui.getPreferredSize(jc);
4632N/A if (size == null) return 0;
4632N/A final int shrinkage = size.height - height;
4632N/A return (shrinkage < 0) ? 0 : (shrinkage > 3) ? 3 : shrinkage;
4632N/A }
4632N/A
4632N/A // this determines the rect that we should draw inset to our existing bounds
4632N/A protected Insets getSubInsets(final int shrinkage) {
4632N/A final Insets insets = sizeVariant.insets;
4632N/A
4632N/A if (shrinkage > 0) {
4632N/A return new InsetsUIResource(insets.top - shrinkage, insets.left, insets.bottom - shrinkage, insets.right);
4632N/A }
4632N/A
4632N/A return insets;
4632N/A }
4632N/A
4632N/A public Insets getBorderInsets(final Component c) {
4632N/A if (!(c instanceof JTextComponent) || c.isOpaque()) return new InsetsUIResource(3, 7, 3, 7);
4632N/A return new InsetsUIResource(6, 7, 6, 7);
4632N/A }
4632N/A
4632N/A protected static State getStateFor(final JTextComponent jc) {
4632N/A if (!AquaFocusHandler.isActive(jc)) {
4632N/A return State.INACTIVE;
4632N/A }
4632N/A
4632N/A if (!jc.isEnabled()) {
4632N/A return State.DISABLED;
4632N/A }
4632N/A
4632N/A if (!jc.isEditable()) {
4632N/A return State.DISABLED;
4632N/A }
4632N/A
4632N/A return State.ACTIVE;
4632N/A }
4632N/A}