001: /*
002: * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.*;
029:
030: public abstract class SunGraphicsCallback {
031: public static final int HEAVYWEIGHTS = 0x1;
032: public static final int LIGHTWEIGHTS = 0x2;
033: public static final int TWO_PASSES = 0x4;
034:
035: private static final DebugHelper dbg = DebugHelper
036: .create(SunGraphicsCallback.class);
037:
038: public abstract void run(Component comp, Graphics cg);
039:
040: protected void constrainGraphics(Graphics g, Rectangle bounds) {
041: if (g instanceof ConstrainableGraphics) {
042: ((ConstrainableGraphics) g).constrain(bounds.x, bounds.y,
043: bounds.width, bounds.height);
044: } else {
045: g.translate(bounds.x, bounds.y);
046: }
047: g.clipRect(0, 0, bounds.width, bounds.height);
048: }
049:
050: public final void runOneComponent(Component comp, Rectangle bounds,
051: Graphics g, Shape clip, int weightFlags) {
052: if (comp == null || comp.getPeer() == null || !comp.isVisible()) {
053: return;
054: }
055: boolean lightweight = comp.isLightweight();
056: if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0)
057: || (!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) {
058: return;
059: }
060:
061: if (bounds == null) {
062: bounds = comp.getBounds();
063: }
064:
065: if (clip == null || clip.intersects(bounds)) {
066: Graphics cg = g.create();
067: try {
068: constrainGraphics(cg, bounds);
069: cg.setFont(comp.getFont());
070: cg.setColor(comp.getForeground());
071: if (cg instanceof Graphics2D) {
072: ((Graphics2D) cg).setBackground(comp
073: .getBackground());
074: } else if (cg instanceof Graphics2Delegate) {
075: ((Graphics2Delegate) cg).setBackground(comp
076: .getBackground());
077: }
078: run(comp, cg);
079: } finally {
080: cg.dispose();
081: }
082: }
083: }
084:
085: public final void runComponents(Component[] comps, Graphics g,
086: int weightFlags) {
087: int ncomponents = comps.length;
088: Shape clip = g.getClip();
089:
090: if (dbg.on && clip != null) {
091: Rectangle newrect = clip.getBounds();
092: dbg.println("GraphicsCallback::runComponents : x = "
093: + newrect.x + " y = " + newrect.y + " width = "
094: + newrect.width + " height = " + newrect.height);
095: }
096:
097: // A seriously sad hack--
098: // Lightweight components always paint behind peered components,
099: // even if they are at the top of the Z order. We emulate this
100: // 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 the
104: // lightweight pass, or redesign the components array to keep
105: // lightweights and heavyweights separate.
106: if ((weightFlags & TWO_PASSES) != 0) {
107: for (int i = ncomponents - 1; i >= 0; i--) {
108: runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS);
109: }
110: for (int i = ncomponents - 1; i >= 0; i--) {
111: runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS);
112: }
113: } else {
114: for (int i = ncomponents - 1; i >= 0; i--) {
115: runOneComponent(comps[i], null, g, clip, weightFlags);
116: }
117: }
118: }
119:
120: public static final class PaintHeavyweightComponentsCallback extends
121: SunGraphicsCallback {
122: private static PaintHeavyweightComponentsCallback instance = new PaintHeavyweightComponentsCallback();
123:
124: private PaintHeavyweightComponentsCallback() {
125: }
126:
127: public void run(Component comp, Graphics cg) {
128: if (!comp.isLightweight()) {
129: comp.paintAll(cg);
130: } else if (comp instanceof Container) {
131: runComponents(((Container) comp).getComponents(), cg,
132: LIGHTWEIGHTS | HEAVYWEIGHTS);
133: }
134: }
135:
136: public static PaintHeavyweightComponentsCallback getInstance() {
137: return instance;
138: }
139: }
140:
141: public static final class PrintHeavyweightComponentsCallback extends
142: SunGraphicsCallback {
143: private static PrintHeavyweightComponentsCallback instance = new PrintHeavyweightComponentsCallback();
144:
145: private PrintHeavyweightComponentsCallback() {
146: }
147:
148: public void run(Component comp, Graphics cg) {
149: if (!comp.isLightweight()) {
150: comp.printAll(cg);
151: } else if (comp instanceof Container) {
152: runComponents(((Container) comp).getComponents(), cg,
153: LIGHTWEIGHTS | HEAVYWEIGHTS);
154: }
155: }
156:
157: public static PrintHeavyweightComponentsCallback getInstance() {
158: return instance;
159: }
160: }
161: }
|