|
14 | 14 | import static org.eclipse.rap.rwt.internal.scripting.ClientListenerUtil.clientListenerAdded; |
15 | 15 | import static org.eclipse.rap.rwt.internal.scripting.ClientListenerUtil.clientListenerRemoved; |
16 | 16 |
|
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.EventListener; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
17 | 21 | import org.eclipse.rap.rwt.Adaptable; |
18 | 22 | import org.eclipse.rap.rwt.RWT; |
19 | 23 | import org.eclipse.rap.rwt.internal.application.ApplicationContextImpl; |
|
28 | 32 | import org.eclipse.swt.SWT; |
29 | 33 | import org.eclipse.swt.SWTException; |
30 | 34 | import org.eclipse.swt.events.DisposeListener; |
| 35 | +import org.eclipse.swt.events.SelectionListener; |
31 | 36 | import org.eclipse.swt.internal.SWTEventListener; |
32 | 37 | import org.eclipse.swt.internal.SerializableCompatibility; |
33 | 38 | import org.eclipse.swt.internal.events.EventList; |
@@ -642,6 +647,37 @@ public Listener[] getListeners( int eventType ) { |
642 | 647 | return eventTable == null ? EMPTY_LISTENERS : eventTable.getListeners( eventType ); |
643 | 648 | } |
644 | 649 |
|
| 650 | + /** |
| 651 | + * Returns the typed listeners who will be notified when an event of the given type occurs. |
| 652 | + * The event type is one of the event constants defined in class {@link SWT} |
| 653 | + * and the specified listener-type must correspond to that event. |
| 654 | + * If for example the {@code eventType} is {@link SWT#Selection}, |
| 655 | + * the listeners type should be {@link SelectionListener}. |
| 656 | + * |
| 657 | + * @param eventType the type of event to listen for |
| 658 | + * @return a stream of typed listeners that will be notified when the event occurs |
| 659 | + * |
| 660 | + * @exception SWTException <ul> |
| 661 | + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 662 | + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| 663 | + * </ul> |
| 664 | + * |
| 665 | + * @see #addTypedListener(EventListener, int...) |
| 666 | + * @see #removeTypedListener(int, EventListener) |
| 667 | + * @see #notifyListeners |
| 668 | + * |
| 669 | + * @since 4.5 |
| 670 | + */ |
| 671 | + public <L extends EventListener> Stream<L> getTypedListeners( int eventType, |
| 672 | + Class<L> listenerType ) |
| 673 | + { |
| 674 | + return Arrays.stream( getListeners( eventType ) ) |
| 675 | + .filter( TypedListener.class::isInstance ) |
| 676 | + .map( l -> ( ( TypedListener )l ).eventListener ) |
| 677 | + .filter( listenerType::isInstance ) |
| 678 | + .map( listenerType::cast ); |
| 679 | + } |
| 680 | + |
645 | 681 | /** |
646 | 682 | * Removes the listener from the collection of listeners who will |
647 | 683 | * be notified when an event of the given type occurs. |
@@ -670,6 +706,107 @@ public Listener[] getListeners( int eventType ) { |
670 | 706 | * @since 2.0 |
671 | 707 | */ |
672 | 708 | protected void removeListener( int eventType, SWTEventListener listener ) { |
| 709 | + removeTypedListener( eventType, listener ); |
| 710 | + } |
| 711 | + |
| 712 | + /** |
| 713 | + * Removes the listener from the collection of listeners who will |
| 714 | + * be notified when an event of the given type occurs. |
| 715 | + * <p> |
| 716 | + * <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT |
| 717 | + * public API. It is marked public only so that it can be shared |
| 718 | + * within the packages provided by SWT. It should never be |
| 719 | + * referenced from application code. |
| 720 | + * </p> |
| 721 | + * |
| 722 | + * @param eventType the type of event to listen for |
| 723 | + * @param listener the listener which should no longer be notified |
| 724 | + * |
| 725 | + * @exception IllegalArgumentException <ul> |
| 726 | + * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
| 727 | + * </ul> |
| 728 | + * @exception SWTException <ul> |
| 729 | + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 730 | + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| 731 | + * </ul> |
| 732 | + * |
| 733 | + * @see Listener |
| 734 | + * @see #addListener |
| 735 | + * |
| 736 | + * @noreference This method is not intended to be referenced by clients. |
| 737 | + * @nooverride This method is not intended to be re-implemented or extended by clients. |
| 738 | + * @since 4.5 |
| 739 | + */ |
| 740 | + protected void removeListener( int eventType, EventListener listener ) { |
| 741 | + removeTypedListener( eventType, listener ); |
| 742 | + } |
| 743 | + |
| 744 | + /** |
| 745 | + * Adds the {@link EventListener typed listener} to the collection of listeners |
| 746 | + * who will be notified when an event of the given types occurs. |
| 747 | + * When the event does occur in the widget, the listener is notified |
| 748 | + * by calling the type's handling methods. |
| 749 | + * The event type is one of the event constants defined in class {@link SWT} |
| 750 | + * and must correspond to the listeners type. |
| 751 | + * If for example a {@link SelectionListener} is passed the {@code eventTypes} |
| 752 | + * can be {@link SWT#Selection} or {@link SWT#DefaultSelection}. |
| 753 | + * |
| 754 | + * @param listener the listener which should be notified when the event occurs |
| 755 | + * @param eventTypes the types of event to listen for |
| 756 | + * |
| 757 | + * @exception IllegalArgumentException <ul> |
| 758 | + * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
| 759 | + * </ul> |
| 760 | + * @exception SWTException <ul> |
| 761 | + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 762 | + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| 763 | + * </ul> |
| 764 | + * |
| 765 | + * @see #getTypedListeners(int, Class) |
| 766 | + * @see #removeTypedListener(int, EventListener) |
| 767 | + * @see #notifyListeners |
| 768 | + * @since 4.5 |
| 769 | + */ |
| 770 | + protected void addTypedListener( EventListener listener, int... eventTypes ) { |
| 771 | + checkWidget(); |
| 772 | + if( listener == null ) { |
| 773 | + SWT.error( SWT.ERROR_NULL_ARGUMENT ); |
| 774 | + } |
| 775 | + TypedListener typedListener = new TypedListener( listener ); |
| 776 | + for( int eventType : eventTypes ) { |
| 777 | + addListener( eventType, typedListener ); |
| 778 | + } |
| 779 | + } |
| 780 | + |
| 781 | + /** |
| 782 | + * Removes the listener from the collection of listeners who will |
| 783 | + * be notified when an event of the given type occurs. |
| 784 | + * <p> |
| 785 | + * <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT |
| 786 | + * public API. It is marked public only so that it can be shared |
| 787 | + * within the packages provided by SWT. It should never be |
| 788 | + * referenced from application code. |
| 789 | + * </p> |
| 790 | + * |
| 791 | + * @param eventType the type of event to listen for |
| 792 | + * @param listener the listener which should no longer be notified |
| 793 | + * |
| 794 | + * @exception IllegalArgumentException <ul> |
| 795 | + * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
| 796 | + * </ul> |
| 797 | + * @exception SWTException <ul> |
| 798 | + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 799 | + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| 800 | + * </ul> |
| 801 | + * |
| 802 | + * @see Listener |
| 803 | + * @see #addListener |
| 804 | + * |
| 805 | + * @noreference This method is not intended to be referenced by clients. |
| 806 | + * @nooverride This method is not intended to be re-implemented or extended by clients. |
| 807 | + * @since 4.5 |
| 808 | + */ |
| 809 | + protected void removeTypedListener( int eventType, EventListener listener ) { |
673 | 810 | checkWidget(); |
674 | 811 | if( listener == null ) { |
675 | 812 | error( SWT.ERROR_NULL_ARGUMENT ); |
|
0 commit comments