jdk/src/share/classes/sun/awt/SunDisplayChanger.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    36
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is used to aid in keeping track of DisplayChangedListeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * notifying them when a display change has taken place. DisplayChangedListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * are notified when the display's bit depth is changed, or when a top-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * window has been dragged onto another screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * It is safe for a DisplayChangedListener to be added while the list is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * iterated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The displayChanged() call is propagated after some occurrence (either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * due to user action or some other application) causes the display mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * (e.g., depth or resolution) to change.  All heavyweight components need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * to know when this happens because they need to create new surfaceData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * objects based on the new depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * displayChanged() is also called on Windows when they are moved from one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * screen to another on a system equipped with multiple displays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class SunDisplayChanger {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    57
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.multiscreen.SunDisplayChanger");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Create a new synchronizedMap with initial capacity of one listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // It is asserted that the most common case is to have one GraphicsDevice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // and one top-level Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private Map listeners = Collections.synchronizedMap(new WeakHashMap(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public SunDisplayChanger() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Add a DisplayChangeListener to this SunDisplayChanger so that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * notified when the display is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void add(DisplayChangedListener theListener) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    71
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (theListener == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    73
                log.fine("Assertion (theListener != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    76
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    77
            log.finer("Adding listener: " + theListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        listeners.put(theListener, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Remove the given DisplayChangeListener from this SunDisplayChanger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void remove(DisplayChangedListener theListener) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    86
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (theListener == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    88
                log.fine("Assertion (theListener != null) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    91
        if (log.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    92
            log.finer("Removing listener: " + theListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        listeners.remove(theListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Notify our list of DisplayChangedListeners that a display change has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * taken place by calling their displayChanged() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void notifyListeners() {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   102
        if (log.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   103
            log.finest("notifyListeners");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // This method is implemented by making a clone of the set of listeners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // and then iterating over the clone.  This is because during the course
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // of responding to a display change, it may be appropriate for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // DisplayChangedListener to add or remove itself from a SunDisplayChanger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // If the set itself were iterated over, rather than a clone, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // trivial to get a ConcurrentModificationException by having a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // DisplayChangedListener remove itself from its list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // Because all display change handling is done on the event thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // synchronization provides no protection against modifying the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // list while in the middle of iterating over it.  -bchristi 7/10/2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        HashMap listClone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Set cloneSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            listClone = new HashMap(listeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        cloneSet = listClone.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Iterator itr = cloneSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            DisplayChangedListener current =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             (DisplayChangedListener) itr.next();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        HashMap listClone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Set cloneSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            listClone = new HashMap(listeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        cloneSet = listClone.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Iterator itr = cloneSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            DisplayChangedListener current =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
             (DisplayChangedListener) itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   175
                if (log.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   176
                    log.finest("paletteChanged for listener: " + current);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                current.paletteChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } catch (IllegalComponentStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // This DisplayChangeListener is no longer valid.  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // likely, a top-level window was dispose()d, but its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // Java objects have not yet been garbage collected.  In any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // case, we no longer need to track this listener, though we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                // do need to remove it from the original list, not the clone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                listeners.remove(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}