1 /*
   2  * Copyright (c) 1999, 2015, 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                 } else if (cg instanceof Graphics2Delegate) {
  75                     ((Graphics2Delegate)cg).setBackground(
  76                         comp.getBackground());
  77                 }
  78                 run(comp, cg);
  79             } finally {
  80                 cg.dispose();
  81             }
  82         }
  83     }
  84 
  85     public final void runComponents(Component[] comps, Graphics g,
  86                                     int weightFlags) {
  87         int ncomponents = comps.length;
  88         Shape clip = g.getClip();
  89 
  90         if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) {
  91             Rectangle newrect = clip.getBounds();
  92             log.finer("x = " + newrect.x + ", y = " + newrect.y +
  93                       ", width = " + newrect.width +
  94                       ", height = " + newrect.height);
  95         }
  96 
  97         // 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 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
 121         extends SunGraphicsCallback
 122     {
 123         private static PaintHeavyweightComponentsCallback instance =
 124             new PaintHeavyweightComponentsCallback();
 125 
 126         private PaintHeavyweightComponentsCallback() {}
 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         public static PaintHeavyweightComponentsCallback getInstance() {
 136             return instance;
 137         }
 138     }
 139     public static final class PrintHeavyweightComponentsCallback
 140         extends SunGraphicsCallback
 141     {
 142         private static PrintHeavyweightComponentsCallback instance =
 143             new PrintHeavyweightComponentsCallback();
 144 
 145         private PrintHeavyweightComponentsCallback() {}
 146         public void run(Component comp, Graphics cg) {
 147             if (!comp.isLightweight()) {
 148                 comp.printAll(cg);
 149             } else if (comp instanceof Container) {
 150                 runComponents(((Container)comp).getComponents(), cg,
 151                               LIGHTWEIGHTS | HEAVYWEIGHTS);
 152             }
 153         }
 154         public static PrintHeavyweightComponentsCallback getInstance() {
 155             return instance;
 156         }
 157     }
 158 }
--- EOF ---