Path: blob/master/src/java.desktop/share/classes/com/sun/java/swing/SwingUtilities3.java
41161 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.java.swing;2627import sun.awt.AppContext;28import sun.awt.SunToolkit;2930import java.util.Collections;31import java.util.Map;32import java.util.WeakHashMap;33import java.applet.Applet;34import java.awt.Component;35import java.awt.Container;36import java.awt.Window;37import javax.swing.JComponent;38import javax.swing.RepaintManager;3940/**41* A collection of utility methods for Swing.42* <p>43* <b>WARNING:</b> While this class is public, it should not be treated as44* public API and its API may change in incompatable ways between dot dot45* releases and even patch releases. You should not rely on this class even46* existing.47*48* This is a second part of sun.swing.SwingUtilities2. It is required49* to provide services for JavaFX applets.50*51*/52public class SwingUtilities3 {53/**54* The {@code clientProperty} key for delegate {@code RepaintManager}55*/56private static final Object DELEGATE_REPAINT_MANAGER_KEY =57new StringBuilder("DelegateRepaintManagerKey");5859/**60* Registers delegate RepaintManager for {@code JComponent}.61*/62public static void setDelegateRepaintManager(JComponent component,63RepaintManager repaintManager) {64/* setting up flag in AppContext to speed up lookups in case65* there are no delegate RepaintManagers used.66*/67AppContext.getAppContext().put(DELEGATE_REPAINT_MANAGER_KEY,68Boolean.TRUE);6970component.putClientProperty(DELEGATE_REPAINT_MANAGER_KEY,71repaintManager);72}7374private static final Map<Container, Boolean> vsyncedMap =75Collections.synchronizedMap(new WeakHashMap<Container, Boolean>());7677/**78* Sets vsyncRequested state for the {@code rootContainer}. If79* {@code isRequested} is {@code true} then vsynced80* {@code BufferStrategy} is enabled for this {@code rootContainer}.81*82* Note: requesting vsynced painting does not guarantee one. The outcome83* depends on current RepaintManager's RepaintManager.PaintManager84* and on the capabilities of the graphics hardware/software and what not.85*86* @param rootContainer topmost container. Should be either {@code Window}87* or {@code Applet}88* @param isRequested the value to set vsyncRequested state to89*/90@SuppressWarnings("removal")91public static void setVsyncRequested(Container rootContainer,92boolean isRequested) {93assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);94if (isRequested) {95vsyncedMap.put(rootContainer, Boolean.TRUE);96} else {97vsyncedMap.remove(rootContainer);98}99}100101/**102* Checks if vsync painting is requested for {@code rootContainer}103*104* @param rootContainer topmost container. Should be either Window or Applet105* @return {@code true} if vsync painting is requested for {@code rootContainer}106*/107@SuppressWarnings("removal")108public static boolean isVsyncRequested(Container rootContainer) {109assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);110return Boolean.TRUE == vsyncedMap.get(rootContainer);111}112113/**114* Returns delegate {@code RepaintManager} for {@code component} hierarchy.115*/116public static RepaintManager getDelegateRepaintManager(Component117component) {118RepaintManager delegate = null;119if (Boolean.TRUE == SunToolkit.targetToAppContext(component)120.get(DELEGATE_REPAINT_MANAGER_KEY)) {121while (delegate == null && component != null) {122while (component != null123&& ! (component instanceof JComponent)) {124component = component.getParent();125}126if (component != null) {127delegate = (RepaintManager)128((JComponent) component)129.getClientProperty(DELEGATE_REPAINT_MANAGER_KEY);130component = component.getParent();131}132133}134}135return delegate;136}137}138139140