Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaBorder.java
41154 views
/*1* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;2829import javax.swing.*;30import javax.swing.border.Border;31import javax.swing.plaf.UIResource;32import javax.swing.text.JTextComponent;3334import apple.laf.*;35import apple.laf.JRSUIConstants.*;3637import com.apple.laf.AquaUtilControlSize.*;3839public abstract class AquaBorder implements Border, UIResource {40protected final AquaPainter<? extends JRSUIState> painter;41protected final SizeDescriptor sizeDescriptor;42protected SizeVariant sizeVariant;4344protected AquaBorder(final SizeDescriptor sizeDescriptor) {45this.sizeDescriptor = sizeDescriptor;46this.sizeVariant = sizeDescriptor.get(Size.REGULAR);47this.painter = createPainter();48}4950protected AquaPainter<? extends JRSUIState> createPainter() {51final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());52painter.state.set(AlignmentVertical.CENTER);53painter.state.set(AlignmentHorizontal.CENTER);54return painter;55}5657protected AquaBorder(final AquaBorder other) {58this.sizeDescriptor = other.sizeDescriptor;59this.sizeVariant = other.sizeVariant;60this.painter = AquaPainter.create(other.painter.state.derive());61painter.state.set(AlignmentVertical.CENTER);62painter.state.set(AlignmentHorizontal.CENTER);63}6465protected void setSize(final Size size) {66sizeVariant = sizeDescriptor.get(size);67painter.state.set(size);68}6970@Override71public Insets getBorderInsets(final Component c) {72return (Insets) sizeVariant.margins.clone();73}7475protected AquaBorder deriveBorderForSize(final Size size) {76try {77final Class<? extends AquaBorder> clazz = getClass();78final AquaBorder border = clazz.getConstructor(new Class<?>[] { clazz }).newInstance(new Object[] { this });79border.setSize(size);80return border;81} catch (final Throwable e) {82return null;83}84}8586public static void repaintBorder(final JComponent c) {87JComponent borderedComponent = c;88Border border = c.getBorder();89if (border == null) {90// See if it's inside a JScrollpane or something91final Container p = c.getParent();92if (p instanceof JViewport) {93borderedComponent = (JComponent)p.getParent();94if (borderedComponent != null) border = borderedComponent.getBorder();95}96}9798// If we really don't have a border, then bail99// It might be a compound border with a ThemeBorder inside100// The check for that case is tricky, so we just go ahead and repaint any border101if (border == null || borderedComponent == null) return;102103final int width = borderedComponent.getWidth();104final int height = borderedComponent.getHeight();105final Insets i = borderedComponent.getInsets();106107borderedComponent.repaint(0, 0, width, i.top); // Top edge108borderedComponent.repaint(0, 0, i.left, height); // Left edge109borderedComponent.repaint(0, height - i.bottom, width, i.bottom); // Bottom edge110borderedComponent.repaint(width - i.right, 0, i.right, height); // Right edge111}112113// The JScrollPane doesn't let us know if its viewport view has focus114protected boolean isFocused(final Component c) {115// Being really paranoid in case this Component isn't a Swing component116Component focusable = c;117118if (c instanceof JScrollPane) {119final JViewport vp = ((JScrollPane)c).getViewport();120if (vp != null) {121focusable = vp.getView();122// Lists, Tables & Trees get focus rings, TextAreas don't (JBuilder puts TextField border on TextAreas)123if (focusable instanceof JTextComponent) return false;124}125} else if (focusable instanceof JTextComponent) {126// non-editable text areas don't draw the focus ring127if (!((javax.swing.text.JTextComponent)focusable).isEditable()) return false;128}129130return (focusable != null && focusable instanceof JComponent && ((JComponent)focusable).hasFocus());131}132133@Override134public boolean isBorderOpaque() { return false; }135136@Override137public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int w, final int h) {138painter.paint(g, c, x, y, w, h);139}140141static class Default extends AquaBorder {142Default() { super(new SizeDescriptor(new SizeVariant())); }143}144}145146147