Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/SunGraphicsCallback.java
38827 views
/*1* Copyright (c) 1999, 2013, 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 sun.awt;2627import java.awt.*;2829import sun.util.logging.PlatformLogger;3031public abstract class SunGraphicsCallback {32public static final int HEAVYWEIGHTS = 0x1;33public static final int LIGHTWEIGHTS = 0x2;34public static final int TWO_PASSES = 0x4;3536private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.SunGraphicsCallback");3738public abstract void run(Component comp, Graphics cg);3940protected void constrainGraphics(Graphics g, Rectangle bounds) {41if (g instanceof ConstrainableGraphics) {42((ConstrainableGraphics)g).constrain(bounds.x, bounds.y, bounds.width, bounds.height);43} else {44g.translate(bounds.x, bounds.y);45}46g.clipRect(0, 0, bounds.width, bounds.height);47}4849@SuppressWarnings("deprecation")50public final void runOneComponent(Component comp, Rectangle bounds,51Graphics g, Shape clip,52int weightFlags) {53if (comp == null || comp.getPeer() == null || !comp.isVisible()) {54return;55}56boolean lightweight = comp.isLightweight();57if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0) ||58(!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) {59return;60}6162if (bounds == null) {63bounds = comp.getBounds();64}6566if (clip == null || clip.intersects(bounds)) {67Graphics cg = g.create();68try {69constrainGraphics(cg, bounds);70cg.setFont(comp.getFont());71cg.setColor(comp.getForeground());72if (cg instanceof Graphics2D) {73((Graphics2D)cg).setBackground(comp.getBackground());74} else if (cg instanceof Graphics2Delegate) {75((Graphics2Delegate)cg).setBackground(76comp.getBackground());77}78run(comp, cg);79} finally {80cg.dispose();81}82}83}8485public final void runComponents(Component[] comps, Graphics g,86int weightFlags) {87int ncomponents = comps.length;88Shape clip = g.getClip();8990if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) {91Rectangle newrect = clip.getBounds();92log.finer("x = " + newrect.x + ", y = " + newrect.y +93", width = " + newrect.width +94", height = " + newrect.height);95}9697// A seriously sad hack--98// Lightweight components always paint behind peered components,99// even if they are at the top of the Z order. We emulate this100// behavior by making two printing passes: the first for lightweights;101// the second for heavyweights.102//103// ToDo(dpm): Either build a list of heavyweights during the104// lightweight pass, or redesign the components array to keep105// lightweights and heavyweights separate.106if ((weightFlags & TWO_PASSES) != 0) {107for (int i = ncomponents - 1; i >= 0; i--) {108runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS);109}110for (int i = ncomponents - 1; i >= 0; i--) {111runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS);112}113} else {114for (int i = ncomponents - 1; i >= 0; i--) {115runOneComponent(comps[i], null, g, clip, weightFlags);116}117}118}119120public static final class PaintHeavyweightComponentsCallback121extends SunGraphicsCallback122{123private static PaintHeavyweightComponentsCallback instance =124new PaintHeavyweightComponentsCallback();125126private PaintHeavyweightComponentsCallback() {}127public void run(Component comp, Graphics cg) {128if (!comp.isLightweight()) {129comp.paintAll(cg);130} else if (comp instanceof Container) {131runComponents(((Container)comp).getComponents(), cg,132LIGHTWEIGHTS | HEAVYWEIGHTS);133}134}135public static PaintHeavyweightComponentsCallback getInstance() {136return instance;137}138}139public static final class PrintHeavyweightComponentsCallback140extends SunGraphicsCallback141{142private static PrintHeavyweightComponentsCallback instance =143new PrintHeavyweightComponentsCallback();144145private PrintHeavyweightComponentsCallback() {}146public void run(Component comp, Graphics cg) {147if (!comp.isLightweight()) {148comp.printAll(cg);149} else if (comp instanceof Container) {150runComponents(((Container)comp).getComponents(), cg,151LIGHTWEIGHTS | HEAVYWEIGHTS);152}153}154public static PrintHeavyweightComponentsCallback getInstance() {155return instance;156}157}158}159160161