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