static boolean isLayerEventControllerAdded() { Toolkit toolkit = Toolkit.getDefaultToolkit(); AWTEventListener layerEventController = null; for (AWTEventListener listener : toolkit.getAWTEventListeners()) { if (listener instanceof AWTEventListenerProxy) { listener = ((AWTEventListenerProxy)listener).getListener(); } if ("LayerEventController".equals(listener.getClass().getSimpleName())) { if (layerEventController != null) { throw new RuntimeException("Duplicated LayerEventController"); } layerEventController = listener; } } boolean ret = layerEventController != null; if (ret) { System.out.println("LayerEventController found"); } else { System.out.println("No LayerEventController"); } return ret; }
static boolean layerEventControllerMaskEquals(long mask) { Toolkit toolkit = Toolkit.getDefaultToolkit(); AWTEventListener layerEventController = null; for (AWTEventListener listener : toolkit.getAWTEventListeners(mask)) { if (listener instanceof AWTEventListenerProxy) { listener = ((AWTEventListenerProxy)listener).getListener(); } if ("LayerEventController".equals(listener.getClass().getSimpleName())) { if (layerEventController != null) { throw new RuntimeException("Duplicated LayerEventController"); } layerEventController = listener; } } boolean ret = layerEventController != null; if (ret) { System.out.println("LayerEventController with the correct mask found"); } else { System.out.println("No LayerEventController with the correct mask"); } return ret; }
/** * Removes an AWT event listener from this toolkit. This listener is no * longer informed of any event types it was registered in. * * If a security manager is installed, it is asked first if an * <code>AWTPermission("listenToAllAWTEvents")</code> is allowed. * This may result in a <code>SecurityException</code> beeing thrown. * * It is not recommended to use this kind of notification for normal * applications. It is intended solely for the purpose of debugging and to * support special facilities. * * @param listener the listener to remove * * @throws SecurityException if there is a <code>SecurityManager</code> that * doesn't grant * <code>AWTPermission("listenToAllAWTEvents")</code> * * @since 1.2 * * @see #addAWTEventListener(AWTEventListener, long) * @see #getAWTEventListeners() * @see #getAWTEventListeners(long) */ public void removeAWTEventListener(AWTEventListener listener) { // First we must check the security permissions. SecurityManager s = System.getSecurityManager(); if (s != null) s.checkPermission(new AWTPermission("listenToAllAWTEvents")); // Find the index of the listener. int index = -1; for (int i = 0; i < awtEventListeners.length; ++i) { AWTEventListenerProxy proxy = awtEventListeners[i]; if (proxy.getListener() == listener) { index = i; break; } } // Copy over the arrays and leave out the removed element. if (index != -1) { AWTEventListenerProxy[] newArray = new AWTEventListenerProxy[awtEventListeners.length - 1]; if (index > 0) System.arraycopy(awtEventListeners, 0, newArray, 0, index); if (index < awtEventListeners.length - 1) System.arraycopy(awtEventListeners, index + 1, newArray, index, awtEventListeners.length - index - 1); awtEventListeners = newArray; } }
/** * Dispatches events to listeners registered to this Toolkit. This is called * by {@link Component#dispatchEventImpl(AWTEvent)} in order to dispatch * events globally. * * @param ev the event to dispatch */ void globalDispatchEvent(AWTEvent ev) { // We do not use the accessor methods here because they create new // arrays each time. We must be very efficient, so we access this directly. for (int i = 0; i < awtEventListeners.length; ++i) { AWTEventListenerProxy proxy = awtEventListeners[i]; if ((proxy.getEventMask() & AWTEvent.eventIdToMask(ev.getID())) != 0) proxy.eventDispatched(ev); } }
void removeAWTEventListener(AWTEventListener listener) { if (listener != null) { for (AWTEventListenerProxy proxy : listeners.getUserListeners()) { if (listener == proxy.getListener()) { listeners.removeUserListener(proxy); return; } } } }
AWTEventListener[] getAWTEventListeners() { HashSet<EventListener> listenersSet = new HashSet<EventListener>(); for (AWTEventListenerProxy proxy : listeners.getUserListeners()) { listenersSet.add(proxy.getListener()); } return listenersSet.toArray(new AWTEventListener[listenersSet.size()]); }
AWTEventListener[] getAWTEventListeners(long eventMask) { HashSet<EventListener> listenersSet = new HashSet<EventListener>(); for (AWTEventListenerProxy proxy : listeners.getUserListeners()) { if ((proxy.getEventMask() & eventMask) == eventMask) { listenersSet.add(proxy.getListener()); } } return listenersSet.toArray(new AWTEventListener[listenersSet.size()]); }
void dispatchAWTEvent(AWTEvent event) { AWTEvent.EventDescriptor descriptor = eventTypeLookup.getEventDescriptor(event); if (descriptor == null) { return; } for (AWTEventListenerProxy proxy : listeners.getUserListeners()) { if ((proxy.getEventMask() & descriptor.eventMask) != 0) { proxy.eventDispatched(event); } } }