jdk/src/share/classes/sun/awt/SunDisplayChanger.java
author ewendeli
Sun, 03 Feb 2013 23:25:38 +0100
changeset 16100 379f48d34516
parent 11264 54f2f4c6bd30
child 18178 ee71c923891d
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     2
 * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
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.IllegalComponentStateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    31
import java.util.HashMap;
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    32
import java.util.HashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.WeakHashMap;
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
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class is used to aid in keeping track of DisplayChangedListeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * notifying them when a display change has taken place. DisplayChangedListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * are notified when the display's bit depth is changed, or when a top-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * window has been dragged onto another screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * It is safe for a DisplayChangedListener to be added while the list is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * iterated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The displayChanged() call is propagated after some occurrence (either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * due to user action or some other application) causes the display mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * (e.g., depth or resolution) to change.  All heavyweight components need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * to know when this happens because they need to create new surfaceData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * objects based on the new depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * displayChanged() is also called on Windows when they are moved from one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * screen to another on a system equipped with multiple displays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class SunDisplayChanger {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    58
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    59
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.multiscreen.SunDisplayChanger");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    61
    // Create a new synchronized map with initial capacity of one listener.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // It is asserted that the most common case is to have one GraphicsDevice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // and one top-level Window.
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    64
    private Map<DisplayChangedListener, Void> listeners =
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    65
        Collections.synchronizedMap(new WeakHashMap<DisplayChangedListener, Void>(1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public SunDisplayChanger() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Add a DisplayChangeListener to this SunDisplayChanger so that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * notified when the display is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void add(DisplayChangedListener theListener) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    74
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (theListener == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    76
                log.fine("Assertion (theListener != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    79
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    80
            log.finer("Adding listener: " + theListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        listeners.put(theListener, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Remove the given DisplayChangeListener from this SunDisplayChanger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void remove(DisplayChangedListener theListener) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    89
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (theListener == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    91
                log.fine("Assertion (theListener != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    94
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    95
            log.finer("Removing listener: " + theListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        listeners.remove(theListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Notify our list of DisplayChangedListeners that a display change has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * taken place by calling their displayChanged() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void notifyListeners() {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   105
        if (log.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   106
            log.finest("notifyListeners");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // This method is implemented by making a clone of the set of listeners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // and then iterating over the clone.  This is because during the course
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // of responding to a display change, it may be appropriate for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // DisplayChangedListener to add or remove itself from a SunDisplayChanger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // If the set itself were iterated over, rather than a clone, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // trivial to get a ConcurrentModificationException by having a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // DisplayChangedListener remove itself from its list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // Because all display change handling is done on the event thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // synchronization provides no protection against modifying the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // list while in the middle of iterating over it.  -bchristi 7/10/2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   119
        Set<DisplayChangedListener> cloneSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized(listeners) {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   122
            cloneSet = new HashSet<DisplayChangedListener>(listeners.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   125
        Iterator<DisplayChangedListener> itr = cloneSet.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        while (itr.hasNext()) {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   127
            DisplayChangedListener current = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   129
                if (log.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   130
                    log.finest("displayChanged for listener: " + current);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                current.displayChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } catch (IllegalComponentStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // This DisplayChangeListener is no longer valid.  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // likely, a top-level window was dispose()d, but its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // Java objects have not yet been garbage collected.  In any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // case, we no longer need to track this listener, though we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                // do need to remove it from the original list, not the clone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                listeners.remove(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Notify our list of DisplayChangedListeners that a palette change has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * taken place by calling their paletteChanged() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void notifyPaletteChanged() {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   149
        if (log.isLoggable(PlatformLogger.FINEST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            log.finest("notifyPaletteChanged");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // This method is implemented by making a clone of the set of listeners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // and then iterating over the clone.  This is because during the course
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // of responding to a display change, it may be appropriate for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // DisplayChangedListener to add or remove itself from a SunDisplayChanger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // If the set itself were iterated over, rather than a clone, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // trivial to get a ConcurrentModificationException by having a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // DisplayChangedListener remove itself from its list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Because all display change handling is done on the event thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // synchronization provides no protection against modifying the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // list while in the middle of iterating over it.  -bchristi 7/10/2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   163
        Set<DisplayChangedListener> cloneSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        synchronized (listeners) {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   166
            cloneSet = new HashSet<DisplayChangedListener>(listeners.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   168
        Iterator<DisplayChangedListener> itr = cloneSet.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        while (itr.hasNext()) {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   170
            DisplayChangedListener current = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            try {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   172
                if (log.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   173
                    log.finest("paletteChanged for listener: " + current);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                current.paletteChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } catch (IllegalComponentStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // This DisplayChangeListener is no longer valid.  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                // likely, a top-level window was dispose()d, but its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // Java objects have not yet been garbage collected.  In any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // case, we no longer need to track this listener, though we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // do need to remove it from the original list, not the clone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                listeners.remove(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}