1 /*
2 * Copyright (c) 1999, 2018, 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 public final void runOneComponent(Component comp, Rectangle bounds,
51 Graphics g, Shape clip,
52 int weightFlags) {
53 if (comp == null || !comp.isDisplayable() || !comp.isVisible()) {
54 return;
55 }
56 boolean lightweight = comp.isLightweight();
57 if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0) ||
58 (!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) {
59 return;
60 }
61
62 if (bounds == null) {
63 bounds = comp.getBounds();
64 }
65
66 if (clip == null || clip.intersects(bounds)) {
67 Graphics cg = g.create();
68 try {
69 constrainGraphics(cg, bounds);
70 cg.setFont(comp.getFont());
71 cg.setColor(comp.getForeground());
72 if (cg instanceof Graphics2D) {
73 ((Graphics2D)cg).setBackground(comp.getBackground());
74 }
75 run(comp, cg);
76 } finally {
77 cg.dispose();
78 }
79 }
80 }
81
82 public final void runComponents(Component[] comps, Graphics g,
83 int weightFlags) {
84 int ncomponents = comps.length;
85 Shape clip = g.getClip();
86
87 if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) {
88 Rectangle newrect = clip.getBounds();
89 log.finer("x = " + newrect.x + ", y = " + newrect.y +
90 ", width = " + newrect.width +
91 ", height = " + newrect.height);
92 }
93
94 // A seriously sad hack--
95 // Lightweight components always paint behind peered components,
96 // even if they are at the top of the Z order. We emulate this
97 // behavior by making two printing passes: the first for lightweights;
98 // the second for heavyweights.
99 //
100 // ToDo(dpm): Either build a list of heavyweights during the
101 // lightweight pass, or redesign the components array to keep
102 // lightweights and heavyweights separate.
103 if ((weightFlags & TWO_PASSES) != 0) {
104 for (int i = ncomponents - 1; i >= 0; i--) {
105 runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS);
106 }
107 for (int i = ncomponents - 1; i >= 0; i--) {
108 runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS);
109 }
110 } else {
111 for (int i = ncomponents - 1; i >= 0; i--) {
112 runOneComponent(comps[i], null, g, clip, weightFlags);
113 }
114 }
115 }
116
117 public static final class PaintHeavyweightComponentsCallback
118 extends SunGraphicsCallback
119 {
120 private static PaintHeavyweightComponentsCallback instance =
121 new PaintHeavyweightComponentsCallback();
122
123 private PaintHeavyweightComponentsCallback() {}
124 public void run(Component comp, Graphics cg) {
125 if (!comp.isLightweight()) {
126 comp.paintAll(cg);
127 } else if (comp instanceof Container) {
128 runComponents(((Container)comp).getComponents(), cg,
129 LIGHTWEIGHTS | HEAVYWEIGHTS);
130 }
131 }
132 public static PaintHeavyweightComponentsCallback getInstance() {
133 return instance;
134 }
135 }
136 public static final class PrintHeavyweightComponentsCallback
137 extends SunGraphicsCallback
138 {
139 private static PrintHeavyweightComponentsCallback instance =
140 new PrintHeavyweightComponentsCallback();
141
142 private PrintHeavyweightComponentsCallback() {}
143 public void run(Component comp, Graphics cg) {
144 if (!comp.isLightweight()) {
145 comp.printAll(cg);
146 } else if (comp instanceof Container) {
147 runComponents(((Container)comp).getComponents(), cg,
148 LIGHTWEIGHTS | HEAVYWEIGHTS);
149 }
150 }
151 public static PrintHeavyweightComponentsCallback getInstance() {
152 return instance;
153 }
154 }
155 }
--- EOF ---