Skip to content

Commit fd59b1a

Browse files
committed
Do not call addListener in constructors of CCombo and StyledText #2733
The method is not final, which may cause issues if a subclass overrides it because it ends up being called upon instantiation, when the object is not fully initialized Since these 2 classes are not in the same package as other subclasses of Widget, I had to resort to expanding the functionality in TypedListener. Fixes #2733
1 parent 9e2bdb5 commit fd59b1a

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public CCombo (Composite parent, int style) {
173173
}
174174

175175
initAccessible();
176-
addListener(SWT.ZoomChanged, this::handleDPIChange);
176+
addTypedListener((ZoomChangedListener) this::handleDPIChange, SWT.ZoomChanged);
177177
}
178178
static int checkStyle (int style) {
179179
int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.LEAD | SWT.CENTER | SWT.TRAIL;

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public StyledText(Composite parent, int style) {
758758
initializeAccessible();
759759
setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this));
760760
if (IS_MAC) setData(STYLEDTEXT_KEY);
761-
addListener(SWT.ZoomChanged, this::handleDPIChange);
761+
addTypedListener((ZoomChangedListener) this::handleDPIChange, SWT.ZoomChanged);
762762
}
763763
/**
764764
* Adds an extended modify listener. An ExtendedModify event is sent by the
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Vector Informatik GmbH - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.events;
15+
16+
import java.util.*;
17+
18+
import org.eclipse.swt.*;
19+
import org.eclipse.swt.widgets.*;
20+
21+
/**
22+
* Interface for typed listeners that react to events of type {@linkplain SWT#ZoomChanged}.
23+
*
24+
* @implNote This API is currently only implemented on Windows and GTK.
25+
* SWT doesn't send zoom events events on Cocoa.
26+
* </p>
27+
* @since 3.132
28+
*
29+
*/
30+
@FunctionalInterface
31+
public interface ZoomChangedListener extends EventListener {
32+
void zoomChanged(Event e);
33+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/TypedListener.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class TypedListener implements Listener {
4848
/**
4949
* The receiver's event listener
5050
*/
51+
@Deprecated
5152
protected EventListener eventListener;
5253

5354
/**
@@ -81,7 +82,7 @@ public TypedListener (SWTEventListener listener) {
8182
*
8283
* @noreference This method is not intended to be referenced by clients.
8384
*/
84-
85+
@Deprecated
8586
public TypedListener (EventListener listener) {
8687
eventListener = listener;
8788
}
@@ -99,6 +100,7 @@ public TypedListener (EventListener listener) {
99100
*
100101
* @noreference This method is not intended to be referenced by clients.
101102
*/
103+
@Deprecated
102104
public EventListener getEventListener () {
103105
// At the moment all typed listeners implement SWTEventListener but that interface is intended to be removed in the future and then they will only implement EventListener.
104106
// This method should not be called for typed listeners listeners that only implement EventListener.
@@ -120,6 +122,7 @@ public EventListener getEventListener () {
120122
* @noreference This method is not intended to be referenced by clients.
121123
*/
122124
@Override
125+
@Deprecated
123126
public void handleEvent (Event e) {
124127
switch (e.type) {
125128
case SWT.Activate: {
@@ -313,6 +316,10 @@ public void handleEvent (Event e) {
313316
e.doit = event.doit;
314317
break;
315318
}
319+
case SWT.ZoomChanged: {
320+
((ZoomChangedListener) eventListener).zoomChanged(e);
321+
break;
322+
}
316323
}
317324
}
318325

0 commit comments

Comments
 (0)