430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
430N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/Apackage sun.java2d.pipe.hw;
430N/A
430N/Aimport java.util.Collections;
430N/Aimport java.util.HashMap;
430N/Aimport java.util.Iterator;
430N/Aimport java.util.Map;
430N/Aimport java.util.Set;
430N/A
430N/A/**
430N/A * This class is used to notify listeners about accelerated device's
430N/A * events such as device reset or dispose that are about to occur.
430N/A */
430N/Apublic class AccelDeviceEventNotifier {
430N/A
430N/A private static AccelDeviceEventNotifier theInstance;
430N/A
430N/A /**
430N/A * A device is about to be reset. The listeners have to release all
430N/A * resources associated with the device which are required for the device
430N/A * to be reset.
430N/A */
430N/A public static final int DEVICE_RESET = 0;
430N/A
430N/A /**
430N/A * A device is about to be disposed. The listeners have to release all
430N/A * resources associated with the device.
430N/A */
430N/A public static final int DEVICE_DISPOSED = 1;
430N/A
430N/A private final Map<AccelDeviceEventListener, Integer> listeners;
430N/A
430N/A private AccelDeviceEventNotifier() {
430N/A listeners = Collections.synchronizedMap(
430N/A new HashMap<AccelDeviceEventListener, Integer>(1));
430N/A }
430N/A
430N/A /**
430N/A * Returns a singleton of AccelDeviceEventNotifier if it exists. If the
430N/A * passed boolean is false and singleton doesn't exist yet, null is
430N/A * returned. If the passed boolean is {@code true} and singleton doesn't
430N/A * exist it will be created and returned.
430N/A *
430N/A * @param create whether to create a singleton instance if doesn't yet
430N/A * exist
430N/A * @return a singleton instance or null
430N/A */
430N/A private static synchronized
430N/A AccelDeviceEventNotifier getInstance(boolean create)
430N/A {
430N/A if (theInstance == null && create) {
430N/A theInstance = new AccelDeviceEventNotifier();
430N/A }
430N/A return theInstance;
430N/A }
430N/A
430N/A /**
430N/A * Called to indicate that a device event had occured.
430N/A * If a singleton exists, the listeners (those associated with
430N/A * the device) will be notified.
430N/A *
430N/A * @param screen a screen number of the device which is a source of
430N/A * the event
430N/A * @param eventType a type of the event
430N/A * @see #DEVICE_DISPOSED
430N/A * @see #DEVICE_RESET
430N/A */
430N/A public static final void eventOccured(int screen, int eventType) {
430N/A AccelDeviceEventNotifier notifier = getInstance(false);
430N/A if (notifier != null) {
430N/A notifier.notifyListeners(eventType, screen);
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * Adds the listener associated with a device on particular screen.
430N/A *
430N/A * Note: the listener must be removed as otherwise it will forever
430N/A * be referenced by the notifier.
430N/A *
430N/A * @param l the listener
430N/A * @param screen the screen number indicating which device the listener is
430N/A * interested in.
430N/A */
430N/A public static final void addListener(AccelDeviceEventListener l,int screen){
430N/A getInstance(true).add(l, screen);
430N/A }
430N/A
430N/A /**
430N/A * Removes the listener.
430N/A *
430N/A * @param l the listener
430N/A */
430N/A public static final void removeListener(AccelDeviceEventListener l) {
430N/A getInstance(true).remove(l);
430N/A }
430N/A
430N/A private final void add(AccelDeviceEventListener theListener, int screen) {
430N/A listeners.put(theListener, screen);
430N/A }
430N/A private final void remove(AccelDeviceEventListener theListener) {
430N/A listeners.remove(theListener);
430N/A }
430N/A
430N/A /**
430N/A * Notifies the listeners associated with the screen's device about the
430N/A * event.
430N/A *
430N/A * Implementation note: the current list of listeners is first duplicated
430N/A * which allows the listeners to remove themselves during the iteration.
430N/A *
430N/A * @param screen a screen number with which the device which is a source of
430N/A * the event is associated with
430N/A * @param eventType a type of the event
430N/A * @see #DEVICE_DISPOSED
430N/A * @see #DEVICE_RESET
430N/A */
430N/A private final void notifyListeners(int deviceEventType, int screen) {
430N/A HashMap<AccelDeviceEventListener, Integer> listClone;
430N/A Set<AccelDeviceEventListener> cloneSet;
430N/A
430N/A synchronized(listeners) {
430N/A listClone =
430N/A new HashMap<AccelDeviceEventListener, Integer>(listeners);
430N/A }
430N/A
430N/A cloneSet = listClone.keySet();
430N/A Iterator<AccelDeviceEventListener> itr = cloneSet.iterator();
430N/A while (itr.hasNext()) {
430N/A AccelDeviceEventListener current = itr.next();
430N/A Integer i = listClone.get(current);
430N/A // only notify listeners which are interested in this device
430N/A if (i != null && i.intValue() != screen) {
430N/A continue;
430N/A }
430N/A if (deviceEventType == DEVICE_RESET) {
430N/A current.onDeviceReset();
430N/A } else if (deviceEventType == DEVICE_DISPOSED) {
430N/A current.onDeviceDispose();
430N/A }
430N/A }
430N/A }
430N/A}