author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 3938 | ef327bd847c0 |
child 11264 | 54f2f4c6bd30 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.awt; |
|
27 |
||
28 |
import java.awt.IllegalComponentStateException; |
|
29 |
import java.util.Collections; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Set; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.WeakHashMap; |
|
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 | 37 |
|
38 |
/** |
|
39 |
* This class is used to aid in keeping track of DisplayChangedListeners and |
|
40 |
* notifying them when a display change has taken place. DisplayChangedListeners |
|
41 |
* are notified when the display's bit depth is changed, or when a top-level |
|
42 |
* window has been dragged onto another screen. |
|
43 |
* |
|
44 |
* It is safe for a DisplayChangedListener to be added while the list is being |
|
45 |
* iterated. |
|
46 |
* |
|
47 |
* The displayChanged() call is propagated after some occurrence (either |
|
48 |
* due to user action or some other application) causes the display mode |
|
49 |
* (e.g., depth or resolution) to change. All heavyweight components need |
|
50 |
* to know when this happens because they need to create new surfaceData |
|
51 |
* objects based on the new depth. |
|
52 |
* |
|
53 |
* displayChanged() is also called on Windows when they are moved from one |
|
54 |
* screen to another on a system equipped with multiple displays. |
|
55 |
*/ |
|
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 | 58 |
|
59 |
// Create a new synchronizedMap with initial capacity of one listener. |
|
60 |
// It is asserted that the most common case is to have one GraphicsDevice |
|
61 |
// and one top-level Window. |
|
62 |
private Map listeners = Collections.synchronizedMap(new WeakHashMap(1)); |
|
63 |
||
64 |
public SunDisplayChanger() {} |
|
65 |
||
66 |
/* |
|
67 |
* Add a DisplayChangeListener to this SunDisplayChanger so that it is |
|
68 |
* notified when the display is changed. |
|
69 |
*/ |
|
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 | 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 | 74 |
} |
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 | 78 |
} |
79 |
listeners.put(theListener, null); |
|
80 |
} |
|
81 |
||
82 |
/* |
|
83 |
* Remove the given DisplayChangeListener from this SunDisplayChanger. |
|
84 |
*/ |
|
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 | 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 | 89 |
} |
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 | 93 |
} |
94 |
listeners.remove(theListener); |
|
95 |
} |
|
96 |
||
97 |
/* |
|
98 |
* Notify our list of DisplayChangedListeners that a display change has |
|
99 |
* taken place by calling their displayChanged() methods. |
|
100 |
*/ |
|
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 | 104 |
} |
105 |
// This method is implemented by making a clone of the set of listeners, |
|
106 |
// and then iterating over the clone. This is because during the course |
|
107 |
// of responding to a display change, it may be appropriate for a |
|
108 |
// DisplayChangedListener to add or remove itself from a SunDisplayChanger. |
|
109 |
// If the set itself were iterated over, rather than a clone, it is |
|
110 |
// trivial to get a ConcurrentModificationException by having a |
|
111 |
// DisplayChangedListener remove itself from its list. |
|
112 |
// Because all display change handling is done on the event thread, |
|
113 |
// synchronization provides no protection against modifying the listener |
|
114 |
// list while in the middle of iterating over it. -bchristi 7/10/2001 |
|
115 |
||
116 |
HashMap listClone; |
|
117 |
Set cloneSet; |
|
118 |
||
119 |
synchronized(listeners) { |
|
120 |
listClone = new HashMap(listeners); |
|
121 |
} |
|
122 |
||
123 |
cloneSet = listClone.keySet(); |
|
124 |
Iterator itr = cloneSet.iterator(); |
|
125 |
while (itr.hasNext()) { |
|
126 |
DisplayChangedListener current = |
|
127 |
(DisplayChangedListener) itr.next(); |
|
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 | 131 |
} |
132 |
current.displayChanged(); |
|
133 |
} catch (IllegalComponentStateException e) { |
|
134 |
// This DisplayChangeListener is no longer valid. Most |
|
135 |
// likely, a top-level window was dispose()d, but its |
|
136 |
// Java objects have not yet been garbage collected. In any |
|
137 |
// case, we no longer need to track this listener, though we |
|
138 |
// do need to remove it from the original list, not the clone. |
|
139 |
listeners.remove(current); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/* |
|
145 |
* Notify our list of DisplayChangedListeners that a palette change has |
|
146 |
* taken place by calling their paletteChanged() methods. |
|
147 |
*/ |
|
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 | 150 |
log.finest("notifyPaletteChanged"); |
151 |
} |
|
152 |
// This method is implemented by making a clone of the set of listeners, |
|
153 |
// and then iterating over the clone. This is because during the course |
|
154 |
// of responding to a display change, it may be appropriate for a |
|
155 |
// DisplayChangedListener to add or remove itself from a SunDisplayChanger. |
|
156 |
// If the set itself were iterated over, rather than a clone, it is |
|
157 |
// trivial to get a ConcurrentModificationException by having a |
|
158 |
// DisplayChangedListener remove itself from its list. |
|
159 |
// Because all display change handling is done on the event thread, |
|
160 |
// synchronization provides no protection against modifying the listener |
|
161 |
// list while in the middle of iterating over it. -bchristi 7/10/2001 |
|
162 |
||
163 |
HashMap listClone; |
|
164 |
Set cloneSet; |
|
165 |
||
166 |
synchronized (listeners) { |
|
167 |
listClone = new HashMap(listeners); |
|
168 |
} |
|
169 |
cloneSet = listClone.keySet(); |
|
170 |
Iterator itr = cloneSet.iterator(); |
|
171 |
while (itr.hasNext()) { |
|
172 |
DisplayChangedListener current = |
|
173 |
(DisplayChangedListener) itr.next(); |
|
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 | 177 |
} |
178 |
current.paletteChanged(); |
|
179 |
} catch (IllegalComponentStateException e) { |
|
180 |
// This DisplayChangeListener is no longer valid. Most |
|
181 |
// likely, a top-level window was dispose()d, but its |
|
182 |
// Java objects have not yet been garbage collected. In any |
|
183 |
// case, we no longer need to track this listener, though we |
|
184 |
// do need to remove it from the original list, not the clone. |
|
185 |
listeners.remove(current); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
} |