Skip to content

Commit 7fb0c09

Browse files
committed
Add missing protected methods to Widget class
In SWT the SWTEventListener is marked for deletion. To keep compatibility with the latest Eclipse Platform copy from SWT some protected methods in Widget class: - removeListener( int eventType, EventListener listener ) - addTypedListener( EventListener listener, int... eventTypes ) - removeTypedListener( int eventType, EventListener listener ) - getTypedListeners( int eventType, Class<L> listenerType ) Additionally, TypedListener and EventTable classes now use EventListener instead of SWTEventListener. See: eclipse-platform/eclipse.platform.swt@2ce8542 Issue: #342
1 parent 4437045 commit 7fb0c09

5 files changed

Lines changed: 177 additions & 10 deletions

File tree

bundles/org.eclipse.rap.rwt/.settings/.api_filters

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@
4343
</filter>
4444
</resource>
4545
<resource path="src/org/eclipse/swt/widgets/TypedListener.java" type="org.eclipse.swt.widgets.TypedListener">
46-
<filter comment="See bug 334028" id="643842064">
46+
<filter comment="Deprecation and marked for removal of SWTEventListener" id="338792546">
4747
<message_arguments>
48-
<message_argument value="SWTEventListener"/>
49-
<message_argument value="TypedListener"/>
48+
<message_argument value="org.eclipse.swt.widgets.TypedListener"/>
5049
<message_argument value="getEventListener()"/>
5150
</message_arguments>
5251
</filter>
52+
<filter comment="Deprecation and marked for removal of SWTEventListener" id="388161617">
53+
<message_arguments>
54+
<message_argument value="org.eclipse.swt.widgets.TypedListener"/>
55+
<message_argument value="eventListener"/>
56+
</message_arguments>
57+
</filter>
5358
<filter comment="See bug 334028" id="643850349">
5459
<message_arguments>
5560
<message_argument value="SWTEventListener"/>

bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/EventTable.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,6 +10,8 @@
1010
*******************************************************************************/
1111
package org.eclipse.swt.widgets;
1212

13+
import java.util.EventListener;
14+
1315
import org.eclipse.rap.rwt.scripting.ClientListener;
1416
import org.eclipse.swt.SWT;
1517
import org.eclipse.swt.internal.SWTEventListener;
@@ -170,7 +172,7 @@ public void unhook( int eventType, Listener listener ) {
170172
}
171173
}
172174

173-
public void unhook( int eventType, SWTEventListener listener ) {
175+
public void unhook( int eventType, EventListener listener ) {
174176
if( types == null ) {
175177
return;
176178
}

bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/TypedListener.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,8 @@
1111
package org.eclipse.swt.widgets;
1212

1313

14+
import java.util.EventListener;
15+
1416
import org.eclipse.swt.SWT;
1517
import org.eclipse.swt.events.ArmEvent;
1618
import org.eclipse.swt.events.ArmListener;
@@ -68,7 +70,7 @@ public class TypedListener implements Listener {
6870
/**
6971
* The receiver's event listener
7072
*/
71-
protected SWTEventListener eventListener;
73+
protected EventListener eventListener;
7274

7375
/**
7476
* Constructs a new instance of this class for the given event listener.
@@ -85,6 +87,24 @@ public TypedListener (SWTEventListener listener) {
8587
eventListener = listener;
8688
}
8789

90+
/**
91+
* Constructs a new instance of this class for the given event listener.
92+
* <p>
93+
* <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT
94+
* public API. It is marked public only so that it can be shared
95+
* within the packages provided by SWT. It should never be
96+
* referenced from application code.
97+
* </p>
98+
*
99+
* @param listener the event listener to store in the receiver
100+
*
101+
* @noreference This method is not intended to be referenced by clients.
102+
*/
103+
104+
public TypedListener (EventListener listener) {
105+
eventListener = listener;
106+
}
107+
88108
/**
89109
* Returns the receiver's event listener.
90110
* <p>
@@ -95,8 +115,9 @@ public TypedListener (SWTEventListener listener) {
95115
* </p>
96116
*
97117
* @return the receiver's event listener
118+
* @since 4.5
98119
*/
99-
public SWTEventListener getEventListener () {
120+
public EventListener getEventListener () {
100121
return eventListener;
101122
}
102123

bundles/org.eclipse.rap.rwt/src/org/eclipse/swt/widgets/Widget.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import static org.eclipse.rap.rwt.internal.scripting.ClientListenerUtil.clientListenerAdded;
1515
import static org.eclipse.rap.rwt.internal.scripting.ClientListenerUtil.clientListenerRemoved;
1616

17+
import java.util.Arrays;
18+
import java.util.EventListener;
19+
import java.util.stream.Stream;
20+
1721
import org.eclipse.rap.rwt.Adaptable;
1822
import org.eclipse.rap.rwt.RWT;
1923
import org.eclipse.rap.rwt.internal.application.ApplicationContextImpl;
@@ -28,6 +32,7 @@
2832
import org.eclipse.swt.SWT;
2933
import org.eclipse.swt.SWTException;
3034
import org.eclipse.swt.events.DisposeListener;
35+
import org.eclipse.swt.events.SelectionListener;
3136
import org.eclipse.swt.internal.SWTEventListener;
3237
import org.eclipse.swt.internal.SerializableCompatibility;
3338
import org.eclipse.swt.internal.events.EventList;
@@ -642,6 +647,37 @@ public Listener[] getListeners( int eventType ) {
642647
return eventTable == null ? EMPTY_LISTENERS : eventTable.getListeners( eventType );
643648
}
644649

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+
645681
/**
646682
* Removes the listener from the collection of listeners who will
647683
* be notified when an event of the given type occurs.
@@ -670,6 +706,107 @@ public Listener[] getListeners( int eventType ) {
670706
* @since 2.0
671707
*/
672708
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 ) {
673810
checkWidget();
674811
if( listener == null ) {
675812
error( SWT.ERROR_NULL_ARGUMENT );

tests/org.eclipse.rap.rwt.test/src/org/eclipse/swt/widgets/TypedListener_Test.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012 EclipseSource and others.
2+
* Copyright (c) 2012, 2025 EclipseSource and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -21,6 +21,8 @@
2121
import static org.mockito.Mockito.verify;
2222
import static org.mockito.Mockito.verifyNoInteractions;
2323

24+
import java.util.EventListener;
25+
2426
import org.eclipse.swt.SWT;
2527
import org.eclipse.swt.events.ArmEvent;
2628
import org.eclipse.swt.events.ArmListener;
@@ -73,7 +75,7 @@ public void testGetEventListener() {
7375
SWTEventListener listener = mock( SWTEventListener.class );
7476
TypedListener typedListener = new TypedListener( listener );
7577

76-
SWTEventListener eventListener = typedListener.getEventListener();
78+
EventListener eventListener = typedListener.getEventListener();
7779

7880
assertSame( listener, eventListener );
7981
}

0 commit comments

Comments
 (0)