1 /*
   2  * Copyright (c) 2002, 2010, 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 com.sun.java.swing;
  27 
  28 import sun.awt.AppContext;
  29 import sun.awt.SunToolkit;
  30 
  31 import java.util.Collections;
  32 import java.util.Map;
  33 import java.util.WeakHashMap;
  34 import java.applet.Applet;
  35 import java.awt.Component;
  36 import java.awt.Container;
  37 import java.awt.Window;
  38 import javax.swing.JComponent;
  39 import javax.swing.RepaintManager;
  40 
  41 /**
  42  * A collection of utility methods for Swing.
  43  * <p>
  44  * <b>WARNING:</b> While this class is public, it should not be treated as
  45  * public API and its API may change in incompatable ways between dot dot
  46  * releases and even patch releases. You should not rely on this class even
  47  * existing.
  48  *
  49  * This is a second part of sun.swing.SwingUtilities2. It is required
  50  * to provide services for JavaFX applets.
  51  *
  52  */
  53 public class SwingUtilities3 {
  54     /**
  55      * The {@code clientProperty} key for delegate {@code RepaintManager}
  56      */
  57     private static final Object DELEGATE_REPAINT_MANAGER_KEY =
  58         new StringBuilder("DelegateRepaintManagerKey");
  59 
  60     /**
  61       * Registers delegate RepaintManager for {@code JComponent}.
  62       */
  63     public static void setDelegateRepaintManager(JComponent component,
  64                                                 RepaintManager repaintManager) {
  65         /* setting up flag in AppContext to speed up lookups in case
  66          * there are no delegate RepaintManagers used.
  67          */
  68         AppContext.getAppContext().put(DELEGATE_REPAINT_MANAGER_KEY,
  69                                        Boolean.TRUE);
  70 
  71         component.putClientProperty(DELEGATE_REPAINT_MANAGER_KEY,
  72                                     repaintManager);
  73     }
  74 
  75     private static final Map<Container, Boolean> vsyncedMap =
  76         Collections.synchronizedMap(new WeakHashMap<Container, Boolean>());
  77 
  78     /**
  79      * Sets vsyncRequested state for the {@code rootContainer}.  If
  80      * {@code isRequested} is {@code true} then vsynced
  81      * {@code BufferStrategy} is enabled for this {@code rootContainer}.
  82      *
  83      * Note: requesting vsynced painting does not guarantee one. The outcome
  84      * depends on current RepaintManager's RepaintManager.PaintManager
  85      * and on the capabilities of the graphics hardware/software and what not.
  86      *
  87      * @param rootContainer topmost container. Should be either {@code Window}
  88      *  or {@code Applet}
  89      * @param isRequested the value to set vsyncRequested state to
  90      */
  91     public static void setVsyncRequested(Container rootContainer,
  92                                          boolean isRequested) {
  93         assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);
  94         if (isRequested) {
  95             vsyncedMap.put(rootContainer, Boolean.TRUE);
  96         } else {
  97             vsyncedMap.remove(rootContainer);
  98         }
  99     }
 100 
 101     /**
 102      * Checks if vsync painting is requested for {@code rootContainer}
 103      *
 104      * @param rootContainer topmost container. Should be either Window or Applet
 105      * @return {@code true} if vsync painting is requested for {@code rootContainer}
 106      */
 107     public static boolean isVsyncRequested(Container rootContainer) {
 108         assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);
 109         return Boolean.TRUE == vsyncedMap.get(rootContainer);
 110     }
 111 
 112     /**
 113      * Returns delegate {@code RepaintManager} for {@code component} hierarchy.
 114      */
 115     public static RepaintManager getDelegateRepaintManager(Component
 116                                                             component) {
 117         RepaintManager delegate = null;
 118         if (Boolean.TRUE == SunToolkit.targetToAppContext(component)
 119                                       .get(DELEGATE_REPAINT_MANAGER_KEY)) {
 120             while (delegate == null && component != null) {
 121                 while (component != null
 122                          && ! (component instanceof JComponent)) {
 123                     component = component.getParent();
 124                 }
 125                 if (component != null) {
 126                     delegate = (RepaintManager)
 127                         ((JComponent) component)
 128                           .getClientProperty(DELEGATE_REPAINT_MANAGER_KEY);
 129                     component = component.getParent();
 130                 }
 131 
 132             }
 133         }
 134         return delegate;
 135     }
 136 }