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.*;
4632N/Aimport javax.swing.border.Border;
4632N/Aimport javax.swing.plaf.UIResource;
4632N/Aimport javax.swing.text.JTextComponent;
4632N/A
4632N/Aimport apple.laf.*;
4632N/Aimport apple.laf.JRSUIConstants.*;
4632N/A
4632N/Aimport com.apple.laf.AquaUtilControlSize.*;
4632N/A
4632N/Apublic abstract class AquaBorder implements Border, UIResource {
4632N/A protected final AquaPainter<? extends JRSUIState> painter;
4632N/A protected final SizeDescriptor sizeDescriptor;
4632N/A protected SizeVariant sizeVariant;
4632N/A
4632N/A protected AquaBorder(final SizeDescriptor sizeDescriptor) {
4632N/A this.sizeDescriptor = sizeDescriptor;
4632N/A this.sizeVariant = sizeDescriptor.get(Size.REGULAR);
4632N/A this.painter = createPainter();
4632N/A }
4632N/A
4632N/A protected AquaPainter<? extends JRSUIState> createPainter() {
4632N/A final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
4632N/A painter.state.set(AlignmentVertical.CENTER);
4632N/A painter.state.set(AlignmentHorizontal.CENTER);
4632N/A return painter;
4632N/A }
4632N/A
4632N/A protected AquaBorder(final AquaBorder other) {
4632N/A this.sizeDescriptor = other.sizeDescriptor;
4632N/A this.sizeVariant = other.sizeVariant;
4632N/A this.painter = AquaPainter.create(other.painter.state.derive());
4632N/A painter.state.set(AlignmentVertical.CENTER);
4632N/A painter.state.set(AlignmentHorizontal.CENTER);
4632N/A }
4632N/A
4632N/A protected void setSize(final Size size) {
4632N/A sizeVariant = sizeDescriptor.get(size);
4632N/A painter.state.set(size);
4632N/A }
4632N/A
4632N/A public Insets getBorderInsets(final Component c) {
4632N/A return sizeVariant.margins;
4632N/A }
4632N/A
4632N/A protected AquaBorder deriveBorderForSize(final Size size) {
4632N/A try {
4632N/A final Class<? extends AquaBorder> clazz = getClass();
4632N/A final AquaBorder border = clazz.getConstructor(new Class[] { clazz }).newInstance(new Object[] { this });
4632N/A border.setSize(size);
4632N/A return border;
4632N/A } catch (final Throwable e) {
4632N/A return null;
4632N/A }
4632N/A }
4632N/A
4632N/A public static void repaintBorder(final JComponent c) {
4632N/A JComponent borderedComponent = c;
4632N/A Border border = c.getBorder();
4632N/A if (border == null) {
4632N/A // See if it's inside a JScrollpane or something
4632N/A final Container p = c.getParent();
4632N/A if (p instanceof JViewport) {
4632N/A borderedComponent = (JComponent)p.getParent();
4632N/A if (borderedComponent != null) border = borderedComponent.getBorder();
4632N/A }
4632N/A }
4632N/A
4632N/A // If we really don't have a border, then bail
4632N/A // It might be a compound border with a ThemeBorder inside
4632N/A // The check for that case is tricky, so we just go ahead and repaint any border
4632N/A if (border == null || borderedComponent == null) return;
4632N/A
4632N/A final int width = borderedComponent.getWidth();
4632N/A final int height = borderedComponent.getHeight();
4632N/A final Insets i = borderedComponent.getInsets();
4632N/A
4632N/A borderedComponent.repaint(0, 0, width, i.top); // Top edge
4632N/A borderedComponent.repaint(0, 0, i.left, height); // Left edge
4632N/A borderedComponent.repaint(0, height - i.bottom, width, i.bottom); // Bottom edge
4632N/A borderedComponent.repaint(width - i.right, 0, i.right, height); // Right edge
4632N/A }
4632N/A
4632N/A // The JScrollPane doesn't let us know if its viewport view has focus
4632N/A protected boolean isFocused(final Component c) {
4632N/A // Being really paranoid in case this Component isn't a Swing component
4632N/A Component focusable = c;
4632N/A
4632N/A if (c instanceof JScrollPane) {
4632N/A final JViewport vp = ((JScrollPane)c).getViewport();
4632N/A if (vp != null) {
4632N/A focusable = vp.getView();
4632N/A // Lists, Tables & Trees get focus rings, TextAreas don't (JBuilder puts TextField border on TextAreas)
4632N/A if (focusable instanceof JTextComponent) return false;
4632N/A }
4632N/A } else if (focusable instanceof JTextComponent) {
4632N/A // non-editable text areas don't draw the focus ring
4632N/A if (!((javax.swing.text.JTextComponent)focusable).isEditable()) return false;
4632N/A }
4632N/A
4632N/A return (focusable != null && focusable instanceof JComponent && ((JComponent)focusable).hasFocus());
4632N/A }
4632N/A
4632N/A public boolean isBorderOpaque() { return false; }
4632N/A
4632N/A public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int w, final int h) {
4632N/A painter.paint(g, c, x, y, w, h);
4632N/A }
4632N/A
4632N/A static class Default extends AquaBorder {
4632N/A Default() { super(new SizeDescriptor(new SizeVariant())); }
4632N/A }
4632N/A}