Java Code Examples for java.util.EventListener

The following examples show how to use java.util.EventListener. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Removes a listener that is equal to the given one from this aggregate.
 * <code>equals()</code> method is used to compare listeners.
 *
 * @param listener the listener to be removed
 *
 * @return <code>true</code> if this aggregate contained the specified
 *         <code>listener</code>; <code>false</code> otherwise
 *
 * @throws ClassCastException if <code>listener</code> is not
 *         an instatce of <code>listenerClass</code> specified
 *         in the constructor
 */
public synchronized boolean remove(EventListener listener) {
    Class<?> listenerClass = getListenerClass();

    if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
        throw new ClassCastException("listener " + listener + " is not " +
                "an instance of listener class " + listenerClass);
    }

    for (int i = 0; i < listenerList.length; i++) {
        if (listenerList[i].equals(listener)) {
            EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
                                                                     listenerList.length - 1);
            System.arraycopy(listenerList, 0, tmp, 0, i);
            System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
            listenerList = tmp;

            return true;
        }
    }

    return false;
}
 
Example 2
Source Project: openstock   Source File: MarkerTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Checks that an XYPlot deregisters listeners when clearing markers.
 */
@Test
public void testListenersWithXYPlot() {
    XYPlot plot = new XYPlot();
    ValueMarker marker1 = new ValueMarker(1.0);
    ValueMarker marker2 = new ValueMarker(2.0);
    plot.addDomainMarker(marker1);
    plot.addRangeMarker(marker2);
    EventListener[] listeners1 = marker1.getListeners(
            MarkerChangeListener.class);
    assertTrue(Arrays.asList(listeners1).contains(plot));
    EventListener[] listeners2 = marker1.getListeners(
            MarkerChangeListener.class);
    assertTrue(Arrays.asList(listeners2).contains(plot));
    plot.clearDomainMarkers();
    plot.clearRangeMarkers();
    listeners1 = marker1.getListeners(MarkerChangeListener.class);
    assertFalse(Arrays.asList(listeners1).contains(plot));
    listeners2 = marker1.getListeners(MarkerChangeListener.class);
    assertFalse(Arrays.asList(listeners2).contains(plot));
}
 
Example 3
Source Project: quarkus-http   Source File: ServletContextImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void addListener(final Class<? extends EventListener> listenerClass) {
    ensureNotInitialized();
    ensureNotProgramaticListener();
    if (ApplicationListeners.listenerState() != NO_LISTENER
            && ServletContextListener.class.isAssignableFrom(listenerClass)) {
        throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
    }
    InstanceFactory<? extends EventListener> factory = null;
    try {
        factory = deploymentInfo.getClassIntrospecter().createInstanceFactory(listenerClass);
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    final ListenerInfo listener = new ListenerInfo(listenerClass, factory);
    deploymentInfo.addListener(listener);
    deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
}
 
Example 4
Source Project: Tomcat8-Source-Read   Source File: ApplicationContextFacade.java    License: MIT License 5 votes vote down vote up
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
    if (SecurityUtil.isPackageProtectionEnabled()) {
        doPrivileged("addListener",
                new Class[]{Class.class},
                new Object[]{listenerClass});
    } else {
        context.addListener(listenerClass);
    }
}
 
Example 5
Source Project: Tomcat8-Source-Read   Source File: ApplicationContext.java    License: MIT License 5 votes vote down vote up
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
    EventListener listener;
    try {
        listener = createListener(listenerClass);
    } catch (ServletException e) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.init",
                listenerClass.getName()), e);
    }
    addListener(listener);
}
 
Example 6
Source Project: Tomcat8-Source-Read   Source File: ApplicationContext.java    License: MIT License 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionIdListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener ||
            (t instanceof ServletContextListener && newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example 7
private static int getListenerCount(EventListener l, Class<?> listenerType) {
    if (l instanceof AWTEventMulticaster) {
        AWTEventMulticaster mc = (AWTEventMulticaster)l;
        return getListenerCount(mc.a, listenerType) +
         getListenerCount(mc.b, listenerType);
    }
    else {
        // Only count listeners of correct type
        return listenerType.isInstance(l) ? 1 : 0;
    }
}
 
Example 8
Source Project: kogito-runtimes   Source File: AbstractEventSupport.java    License: Apache License 2.0 5 votes vote down vote up
public void clear() {
    for (EventListener listener : listeners) {
        if (listener instanceof Closeable) {
            ((Closeable) listener).close();
        }
    }
    this.listeners.clear();
}
 
Example 9
/**
 * Adds the listener to this aggregate.
 *
 * @param listener the listener to be added
 *
 * @throws ClassCastException if <code>listener</code> is not
 *         an instatce of <code>listenerClass</code> specified
 *         in the constructor
 */
public synchronized void add(EventListener listener) {
    Class<?> listenerClass = getListenerClass();

    if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
        throw new ClassCastException("listener " + listener + " is not " +
                "an instance of listener class " + listenerClass);
    }

    EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
    System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
    tmp[listenerList.length] = listener;
    listenerList = tmp;
}
 
Example 10
Source Project: quarkus-http   Source File: ListenerInfo.java    License: Apache License 2.0 5 votes vote down vote up
public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory, boolean programatic) {
    this.listenerClass = listenerClass;
    this.instanceFactory = instanceFactory;
    this.programatic = programatic;
    if(!ApplicationListeners.isListenerClass(listenerClass)) {
        throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(listenerClass);
    }
}
 
Example 11
Source Project: quarkus-http   Source File: ListenerInfo.java    License: Apache License 2.0 5 votes vote down vote up
public ListenerInfo(final Class<? extends EventListener> listenerClass, boolean programatic) {
    this.listenerClass = listenerClass;
    this.programatic = programatic;

    try {
        final Constructor<EventListener> ctor = (Constructor<EventListener>) listenerClass.getDeclaredConstructor();
        ctor.setAccessible(true);
        this.instanceFactory = new ConstructorInstanceFactory<>(ctor);
    } catch (NoSuchMethodException e) {
        throw UndertowServletMessages.MESSAGES.componentMustHaveDefaultConstructor("Listener", listenerClass);
    }
}
 
Example 12
Source Project: quarkus-http   Source File: ServletContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(final T t) {
    ensureNotInitialized();
    ensureNotProgramaticListener();
    if (ApplicationListeners.listenerState() != NO_LISTENER
            && ServletContextListener.class.isAssignableFrom(t.getClass())) {
        throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
    }
    ListenerInfo listener = new ListenerInfo(t.getClass(), new ImmediateInstanceFactory<EventListener>(t));
    deploymentInfo.addListener(listener);
    deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
}
 
Example 13
Source Project: quarkus-http   Source File: ServletContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> T createListener(final Class<T> clazz) throws ServletException {
    ensureNotProgramaticListener();
    if (!ApplicationListeners.isListenerClass(clazz)) {
        throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(clazz);
    }
    try {
        return deploymentInfo.getClassIntrospecter().createInstanceFactory(clazz).createInstance().getInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(clazz.getName(), e);
    }
}
 
Example 14
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
    if (l == oldl || l == null) {
        return null;
    } else if (l instanceof TopLevelWindowMulticaster) {
        return ((TopLevelWindowMulticaster)l).remove(oldl);
    } else {
        return l;           // it's not here
    }
}
 
Example 15
private static int populateListenerArray(EventListener[] a, EventListener l, int index) {
    if (l instanceof AWTEventMulticaster) {
        AWTEventMulticaster mc = (AWTEventMulticaster)l;
        int lhs = populateListenerArray(a, mc.a, index);
        return populateListenerArray(a, mc.b, lhs);
    }
    else if (a.getClass().getComponentType().isInstance(l)) {
        a[index] = l;
        return index + 1;
    }
    // Skip nulls, instances of wrong class
    else {
        return index;
    }
}
 
Example 16
protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {
  if (l == null) {
      return;
  }
  else if (l instanceof AWTEventMulticaster) {
      ((AWTEventMulticaster)l).saveInternal(s, k);
  }
  else if (l instanceof Serializable) {
       s.writeObject(k);
       s.writeObject(l);
  }
}
 
Example 17
/**
 * Removes a listener from this multicaster and returns the
 * resulting multicast listener.
 * @param oldl the listener to be removed
 */
protected EventListener remove(EventListener oldl) {
    if (oldl == a)  return b;
    if (oldl == b)  return a;
    EventListener a2 = removeInternal(a, oldl);
    EventListener b2 = removeInternal(b, oldl);
    if (a2 == a && b2 == b) {
        return this;        // it's not here
    }
    return addInternal(a2, b2);
}
 
Example 18
private Collection<? extends EventListener> getListeners(ListnerHolder.Type type) {
    if ( listeners.isEmpty()) {
        return Collections.emptySet();
    }
    Collection<EventListener> l = new ArrayList<EventListener>();
    for (ListnerHolder listnerHolder : listeners ) {
        if (listnerHolder.type == type) {
            l.add( listnerHolder.listener );
        }
    }
    return l;
}
 
Example 19
/**
 * Adds the listener to this aggregate.
 *
 * @param listener the listener to be added
 *
 * @throws ClassCastException if <code>listener</code> is not
 *         an instatce of <code>listenerClass</code> specified
 *         in the constructor
 */
public synchronized void add(EventListener listener) {
    Class<?> listenerClass = getListenerClass();

    if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
        throw new ClassCastException("listener " + listener + " is not " +
                "an instance of listener class " + listenerClass);
    }

    EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
    System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
    tmp[listenerList.length] = listener;
    listenerList = tmp;
}
 
Example 20
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
    if (l == oldl || l == null) {
        return null;
    } else if (l instanceof GUIInitializedMulticaster) {
        return ((GUIInitializedMulticaster)l).remove(oldl);
    } else {
        return l;           // it's not here
    }
}
 
Example 21
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
    if (l == oldl || l == null) {
        return null;
    } else if (l instanceof TopLevelWindowMulticaster) {
        return ((TopLevelWindowMulticaster)l).remove(oldl);
    } else {
        return l;           // it's not here
    }
}
 
Example 22
Source Project: java-technology-stack   Source File: MockServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
	throw new UnsupportedOperationException();
}
 
Example 23
Source Project: Tomcat8-Source-Read   Source File: JspCServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public <T extends EventListener> T createListener(Class<T> c)
        throws ServletException {
    return null;
}
 
Example 24
Source Project: Tomcat8-Source-Read   Source File: TesterServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    throw new RuntimeException("Not implemented");
}
 
Example 25
Source Project: Tomcat8-Source-Read   Source File: TesterServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
    throw new RuntimeException("Not implemented");
}
 
Example 26
Source Project: Tomcat8-Source-Read   Source File: TesterServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public <T extends EventListener> T createListener(Class<T> c)
        throws ServletException {
    throw new RuntimeException("Not implemented");
}
 
Example 27
Source Project: openstock   Source File: ChartComposite.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
     * Hook an SWT listener on the canvas where the chart is drawn.
     * The purpose of this method is to allow some degree of customization.
     *
     * @param listener The SWT listener to attach to the canvas.
     */
    public void addSWTListener(EventListener listener) {
        if (listener instanceof ControlListener) {
            this.canvas.addControlListener((ControlListener) listener);
        }
        else if (listener instanceof DisposeListener) {
            this.canvas.addDisposeListener((DisposeListener) listener);
//      }
//      else if (listener instanceof DragDetectListener) {
//          this.canvas.addDragDetectListener((DragDetectListener) listener);
        }
        else if (listener instanceof FocusListener) {
            this.canvas.addFocusListener((FocusListener) listener);
        }
        else if (listener instanceof HelpListener) {
            this.canvas.addHelpListener((HelpListener) listener);
        }
        else if (listener instanceof KeyListener) {
            this.canvas.addKeyListener((KeyListener) listener);
//      }
//      else if (listener instanceof MenuDetectListener) {
//          this.canvas.addMenuDetectListener((MenuDetectListener) listener);
        }
        else if (listener instanceof MouseListener) {
            this.canvas.addMouseListener((MouseListener) listener);
        }
        else if (listener instanceof MouseMoveListener) {
            this.canvas.addMouseMoveListener((MouseMoveListener) listener);
        }
        else if (listener instanceof MouseTrackListener) {
            this.canvas.addMouseTrackListener((MouseTrackListener) listener);
//      } else if (listener instanceof MouseWheelListener) {
//          this.canvas.addMouseWheelListener((MouseWheelListener) listener);
        }
        else if (listener instanceof PaintListener) {
            this.canvas.addPaintListener((PaintListener) listener);
        }
        else if (listener instanceof TraverseListener) {
            this.canvas.addTraverseListener((TraverseListener) listener);
        }
    }
 
Example 28
Source Project: java-technology-stack   Source File: MockServletContext.java    License: MIT License 4 votes vote down vote up
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
	throw new UnsupportedOperationException();
}
 
Example 29
protected TopLevelWindowMulticaster(EventListener a, EventListener b) {
    super(a, b);
}
 
Example 30
@Override
public <T extends EventListener> void addListener(T t) {
	if (t instanceof ServletContextListener) {
		((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
	}
}