1 /* 2 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt; 27 28 import java.awt.*; 29 30 import sun.util.logging.PlatformLogger; 31 32 public abstract class SunGraphicsCallback { 33 public static final int HEAVYWEIGHTS = 0x1; 34 public static final int LIGHTWEIGHTS = 0x2; 35 public static final int TWO_PASSES = 0x4; 36 37 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.SunGraphicsCallback"); 38 39 public abstract void run(Component comp, Graphics cg); 40 41 protected void constrainGraphics(Graphics g, Rectangle bounds) { 42 if (g instanceof ConstrainableGraphics) { 43 ((ConstrainableGraphics)g).constrain(bounds.x, bounds.y, bounds.width, bounds.height); 44 } else { 45 g.translate(bounds.x, bounds.y); 46 } 47 g.clipRect(0, 0, bounds.width, bounds.height); 48 } 49 50 @SuppressWarnings("deprecation") 51 public final void runOneComponent(Component comp, Rectangle bounds, 52 Graphics g, Shape clip, 53 int weightFlags) { 54 if (comp == null || comp.getPeer() == null || !comp.isVisible()) { 55 return; 56 } 57 boolean lightweight = comp.isLightweight(); 58 if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0) || 59 (!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) { 60 return; 61 } 62 63 if (bounds == null) { 64 bounds = comp.getBounds(); 65 } 66 67 if (clip == null || clip.intersects(bounds)) { 68 Graphics cg = g.create(); 69 try { 70 constrainGraphics(cg, bounds); 71 cg.setFont(comp.getFont()); 72 cg.setColor(comp.getForeground()); 73 if (cg instanceof Graphics2D) { 74 ((Graphics2D)cg).setBackground(comp.getBackground()); 75 } else if (cg instanceof Graphics2Delegate) { 76 ((Graphics2Delegate)cg).setBackground( 77 comp.getBackground()); 78 } 79 run(comp, cg); 80 } finally { 81 cg.dispose(); 82 } 83 } 84 } 85 86 public final void runComponents(Component[] comps, Graphics g, 87 int weightFlags) { 88 int ncomponents = comps.length; 89 Shape clip = g.getClip(); 90 91 if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) { 92 Rectangle newrect = clip.getBounds(); 93 log.finer("x = " + newrect.x + ", y = " + newrect.y + 94 ", width = " + newrect.width + 95 ", height = " + newrect.height); 96 } 97 98 // A seriously sad hack-- 99 // Lightweight components always paint behind peered components, 100 // even if they are at the top of the Z order. We emulate this 101 // behavior by making two printing passes: the first for lightweights; 102 // the second for heavyweights. 103 // 104 // ToDo(dpm): Either build a list of heavyweights during the 105 // lightweight pass, or redesign the components array to keep 106 // lightweights and heavyweights separate. 107 if ((weightFlags & TWO_PASSES) != 0) { 108 for (int i = ncomponents - 1; i >= 0; i--) { 109 runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS); 110 } 111 for (int i = ncomponents - 1; i >= 0; i--) { 112 runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS); 113 } 114 } else { 115 for (int i = ncomponents - 1; i >= 0; i--) { 116 runOneComponent(comps[i], null, g, clip, weightFlags); 117 } 118 } 119 } 120 121 public static final class PaintHeavyweightComponentsCallback 122 extends SunGraphicsCallback 123 { 124 private static PaintHeavyweightComponentsCallback instance = 125 new PaintHeavyweightComponentsCallback(); 126 127 private PaintHeavyweightComponentsCallback() {} 128 public void run(Component comp, Graphics cg) { 129 if (!comp.isLightweight()) { 130 comp.paintAll(cg); 131 } else if (comp instanceof Container) { 132 runComponents(((Container)comp).getComponents(), cg, 133 LIGHTWEIGHTS | HEAVYWEIGHTS); 134 } 135 } 136 public static PaintHeavyweightComponentsCallback getInstance() { 137 return instance; 138 } 139 } 140 public static final class PrintHeavyweightComponentsCallback 141 extends SunGraphicsCallback 142 { 143 private static PrintHeavyweightComponentsCallback instance = 144 new PrintHeavyweightComponentsCallback(); 145 146 private PrintHeavyweightComponentsCallback() {} 147 public void run(Component comp, Graphics cg) { 148 if (!comp.isLightweight()) { 149 comp.printAll(cg); 150 } else if (comp instanceof Container) { 151 runComponents(((Container)comp).getComponents(), cg, 152 LIGHTWEIGHTS | HEAVYWEIGHTS); 153 } 154 } 155 public static PrintHeavyweightComponentsCallback getInstance() { 156 return instance; 157 } 158 } 159 }