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
26package com.sun.java.swing;
27
28import sun.awt.AppContext;
29import sun.awt.SunToolkit;
30
31import java.util.Collections;
32import java.util.Map;
33import java.util.WeakHashMap;
34import java.applet.Applet;
35import java.awt.Component;
36import java.awt.Container;
37import java.awt.Window;
38import javax.swing.JComponent;
39import 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 */
53public 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    @SuppressWarnings("deprecation")
92    public static void setVsyncRequested(Container rootContainer,
93                                         boolean isRequested) {
94        assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);
95        if (isRequested) {
96            vsyncedMap.put(rootContainer, Boolean.TRUE);
97        } else {
98            vsyncedMap.remove(rootContainer);
99        }
100    }
101
102    /**
103     * Checks if vsync painting is requested for {@code rootContainer}
104     *
105     * @param rootContainer topmost container. Should be either Window or Applet
106     * @return {@code true} if vsync painting is requested for {@code rootContainer}
107     */
108    @SuppressWarnings("deprecation")
109    public static boolean isVsyncRequested(Container rootContainer) {
110        assert (rootContainer instanceof Applet) || (rootContainer instanceof Window);
111        return Boolean.TRUE == vsyncedMap.get(rootContainer);
112    }
113
114    /**
115     * Returns delegate {@code RepaintManager} for {@code component} hierarchy.
116     */
117    public static RepaintManager getDelegateRepaintManager(Component
118                                                            component) {
119        RepaintManager delegate = null;
120        if (Boolean.TRUE == SunToolkit.targetToAppContext(component)
121                                      .get(DELEGATE_REPAINT_MANAGER_KEY)) {
122            while (delegate == null && component != null) {
123                while (component != null
124                         && ! (component instanceof JComponent)) {
125                    component = component.getParent();
126                }
127                if (component != null) {
128                    delegate = (RepaintManager)
129                        ((JComponent) component)
130                          .getClientProperty(DELEGATE_REPAINT_MANAGER_KEY);
131                    component = component.getParent();
132                }
133
134            }
135        }
136        return delegate;
137    }
138}
139