Showing the changes to java/pkg/sun/java2d/pipe/hw/AccelDeviceEventNotifier.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,167 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2007, 2008, 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 | |||
| 26 | package sun.java2d.pipe.hw; | ||
| 27 | |||
| 28 | import java.util.Collections; | ||
| 29 | import java.util.HashMap; | ||
| 30 | import java.util.Iterator; | ||
| 31 | import java.util.Map; | ||
| 32 | import java.util.Set; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * This class is used to notify listeners about accelerated device's | ||
| 36 | * events such as device reset or dispose that are about to occur. | ||
| 37 | */ | ||
| 38 | public class AccelDeviceEventNotifier { | ||
| 39 | |||
| 40 | private static AccelDeviceEventNotifier theInstance; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * A device is about to be reset. The listeners have to release all | ||
| 44 | * resources associated with the device which are required for the device | ||
| 45 | * to be reset. | ||
| 46 | */ | ||
| 47 | public static final int DEVICE_RESET = 0; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * A device is about to be disposed. The listeners have to release all | ||
| 51 | * resources associated with the device. | ||
| 52 | */ | ||
| 53 | public static final int DEVICE_DISPOSED = 1; | ||
| 54 | |||
| 55 | private final Map<AccelDeviceEventListener, Integer> listeners; | ||
| 56 | |||
| 57 | private AccelDeviceEventNotifier() { | ||
| 58 | listeners = Collections.synchronizedMap( | ||
| 59 | new HashMap<AccelDeviceEventListener, Integer>(1)); | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Returns a singleton of AccelDeviceEventNotifier if it exists. If the | ||
| 64 | * passed boolean is false and singleton doesn't exist yet, null is | ||
| 65 | * returned. If the passed boolean is {@code true} and singleton doesn't | ||
| 66 | * exist it will be created and returned. | ||
| 67 | * | ||
| 68 | * @param create whether to create a singleton instance if doesn't yet | ||
| 69 | * exist | ||
| 70 | * @return a singleton instance or null | ||
| 71 | */ | ||
| 72 | private static synchronized | ||
| 73 | AccelDeviceEventNotifier getInstance(boolean create) | ||
| 74 | { | ||
| 75 | if (theInstance == null && create) { | ||
| 76 | theInstance = new AccelDeviceEventNotifier(); | ||
| 77 | } | ||
| 78 | return theInstance; | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Called to indicate that a device event had occured. | ||
| 83 | * If a singleton exists, the listeners (those associated with | ||
| 84 | * the device) will be notified. | ||
| 85 | * | ||
| 86 | * @param screen a screen number of the device which is a source of | ||
| 87 | * the event | ||
| 88 | * @param eventType a type of the event | ||
| 89 | * @see #DEVICE_DISPOSED | ||
| 90 | * @see #DEVICE_RESET | ||
| 91 | */ | ||
| 92 | public static final void eventOccured(int screen, int eventType) { | ||
| 93 | AccelDeviceEventNotifier notifier = getInstance(false); | ||
| 94 | if (notifier != null) { | ||
| 95 | notifier.notifyListeners(eventType, screen); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Adds the listener associated with a device on particular screen. | ||
| 101 | * | ||
| 102 | * Note: the listener must be removed as otherwise it will forever | ||
| 103 | * be referenced by the notifier. | ||
| 104 | * | ||
| 105 | * @param l the listener | ||
| 106 | * @param screen the screen number indicating which device the listener is | ||
| 107 | * interested in. | ||
| 108 | */ | ||
| 109 | public static final void addListener(AccelDeviceEventListener l,int screen){ | ||
| 110 | getInstance(true).add(l, screen); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Removes the listener. | ||
| 115 | * | ||
| 116 | * @param l the listener | ||
| 117 | */ | ||
| 118 | public static final void removeListener(AccelDeviceEventListener l) { | ||
| 119 | getInstance(true).remove(l); | ||
| 120 | } | ||
| 121 | |||
| 122 | private final void add(AccelDeviceEventListener theListener, int screen) { | ||
| 123 | listeners.put(theListener, screen); | ||
| 124 | } | ||
| 125 | private final void remove(AccelDeviceEventListener theListener) { | ||
| 126 | listeners.remove(theListener); | ||
| 127 | } | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Notifies the listeners associated with the screen's device about the | ||
| 131 | * event. | ||
| 132 | * | ||
| 133 | * Implementation note: the current list of listeners is first duplicated | ||
| 134 | * which allows the listeners to remove themselves during the iteration. | ||
| 135 | * | ||
| 136 | * @param screen a screen number with which the device which is a source of | ||
| 137 | * the event is associated with | ||
| 138 | * @param eventType a type of the event | ||
| 139 | * @see #DEVICE_DISPOSED | ||
| 140 | * @see #DEVICE_RESET | ||
| 141 | */ | ||
| 142 | private final void notifyListeners(int deviceEventType, int screen) { | ||
| 143 | HashMap<AccelDeviceEventListener, Integer> listClone; | ||
| 144 | Set<AccelDeviceEventListener> cloneSet; | ||
| 145 | |||
| 146 | synchronized(listeners) { | ||
| 147 | listClone = | ||
| 148 | new HashMap<AccelDeviceEventListener, Integer>(listeners); | ||
| 149 | } | ||
| 150 | |||
| 151 | cloneSet = listClone.keySet(); | ||
| 152 | Iterator<AccelDeviceEventListener> itr = cloneSet.iterator(); | ||
| 153 | while (itr.hasNext()) { | ||
| 154 | AccelDeviceEventListener current = itr.next(); | ||
| 155 | Integer i = listClone.get(current); | ||
| 156 | // only notify listeners which are interested in this device | ||
| 157 | if (i != null && i.intValue() != screen) { | ||
| 158 | continue; | ||
| 159 | } | ||
| 160 | if (deviceEventType == DEVICE_RESET) { | ||
| 161 | current.onDeviceReset(); | ||
| 162 | } else if (deviceEventType == DEVICE_DISPOSED) { | ||
| 163 | current.onDeviceDispose(); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||