Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaFocus.java
41154 views
/*1* Copyright (c) 2011, 2012, 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*/2425//26// AquaFocus.java27// Copyright (c) 2009 Apple Inc. All rights reserved.28//2930package com.apple.laf;3132import java.awt.*;3334import javax.swing.*;3536import sun.java2d.*;37import apple.laf.JRSUIFocus;3839import com.apple.laf.AquaUtils.Painter;4041public class AquaFocus {42interface Drawable {43public void draw(final Graphics2D sg2d);44}4546static boolean paintFocus(final Graphics g, final Drawable drawable) {47// TODO: requires OSXSurfaceData48return false;49/*if (!(g instanceof SunGraphics2D)) return false;50final SunGraphics2D sg2d = (SunGraphics2D)g;5152final SurfaceData surfaceData = sg2d.getSurfaceData();53if (!(surfaceData instanceof OSXSurfaceData)) return false;5455try {56((OSXSurfaceData)surfaceData).performCocoaDrawing(sg2d, new OSXSurfaceData.CGContextDrawable() {57@Override58public void drawIntoCGContext(final long cgContext) {59final JRSUIFocus focus = new JRSUIFocus(cgContext);60focus.beginFocus(JRSUIFocus.RING_BELOW);61drawable.draw(sg2d);62focus.endFocus();63}64});65} finally {66sg2d.dispose();67}68return true;*/69}7071public static Icon createFocusedIcon(final Icon tmpIcon, final Component c, final int slack) {72return new FocusedIcon(tmpIcon, slack);73}7475/* -- disabled until we can get the real JRSUI focus painter working7677static class FocusedIcon implements Icon {78final Icon icon;79final int slack;8081public FocusedIcon(final Icon icon, final int slack) {82this.icon = icon;83this.slack = slack;84}8586@Override87public int getIconHeight() {88return icon.getIconHeight() + slack + slack;89}9091@Override92public int getIconWidth() {93return icon.getIconWidth() + slack + slack;94}9596@Override97public void paintIcon(final Component c, final Graphics g, final int x, final int y) {98final boolean painted = paintFocus(g, new Drawable() {99@Override100public void draw(SunGraphics2D sg2d) {101icon.paintIcon(c, sg2d, x + slack, y + slack);102}103});104if (!painted) {105icon.paintIcon(c, g, x + slack, y + slack);106}107}108}109*/110111static class FocusedIcon extends AquaUtils.ShadowBorder implements Icon {112final Icon icon;113final int slack;114115public FocusedIcon(final Icon icon, final int slack) {116super(117new Painter() {118public void paint(Graphics g, int x, int y, int w, int h) {119Graphics2D imgG = (Graphics2D)g;120imgG.setComposite(AlphaComposite.Src);121imgG.setColor(UIManager.getColor("Focus.color"));122imgG.fillRect(x, y, w - (slack * 2), h - (slack * 2));123imgG.setComposite(AlphaComposite.DstAtop);124icon.paintIcon(null, imgG, x, y);125}126},127new Painter() {128public void paint(Graphics g, int x, int y, int w, int h) {129((Graphics2D)g).setComposite(AlphaComposite.SrcAtop);130icon.paintIcon(null, g, x, y);131}132},133slack, slack, 0.0f, 1.8f, 7134);135this.icon = icon;136this.slack = slack;137}138139@Override140public int getIconHeight() {141return icon.getIconHeight() + slack + slack;142}143144@Override145public int getIconWidth() {146return icon.getIconWidth() + slack + slack;147}148149@Override150public void paintIcon(final Component c, final Graphics g, final int x, final int y) {151paintBorder(c, g, x, y, getIconWidth(), getIconHeight());152icon.paintIcon(c, g, x + slack, y + slack);153}154}155}156157158