Reputation: 351
Sample code:
public class Test {
private static Listener listener = e -> {
String type = null ;
switch ( e.type ) {
case SWT.MouseDown -> type = "MouseDown" ;
case SWT.Activate -> type = "Activate" ;
case SWT.Deactivate -> type = "Deactivate" ;
}
System.out.println( type + " - " + e ) ;
} ;
public static void main(String[] args) {
Shell shell1 = new Shell() ;
shell1.setText( "1" ) ;
shell1.setSize( 300, 300 ) ;
shell1.setLayout( new FillLayout() ) ;
Composite composite1 = new Composite( shell1, SWT.NONE ) ;
Shell shell2 = new Shell() ;
shell2.setText( "2" ) ;
shell2.setSize( 300, 300 ) ;
shell2.setLayout( new FillLayout() ) ;
Composite composite2 = new Composite( shell2, SWT.NONE ) ;
addListeners( shell1 ) ;
addListeners( composite1 ) ;
addListeners( shell2 ) ;
addListeners( composite2 ) ;
// shell2.addListener( SWT.Deactivate, e -> shell2.close() ) ;
shell1.open() ;
shell2.open() ;
Display display = shell1.getDisplay() ;
while ( !shell1.isDisposed() ) {
if ( !display.readAndDispatch() ) {
display.sleep() ;
}
}
display.dispose() ;
}
private static void addListeners( Control control ) {
control.addListener( SWT.MouseDown, listener ) ;
control.addListener( SWT.Activate, listener ) ;
control.addListener( SWT.Deactivate, listener ) ;
}
}
When shell2
is active and the mouse clicks on composite1
, the event flow is as follows:
Activate - Event {type=26 Composite {} [layout=null] time=483235453 data=null x=0 y=0 width=0 height=0 detail=3}
Deactivate - Event {type=27 Shell {2} [layout=FillLayout {type=SWT.HORIZONTAL}] time=483235453 data=null x=0 y=0 width=0 height=0 detail=0}
Deactivate - Event {type=27 Composite {} [layout=null] time=483235453 data=null x=0 y=0 width=0 height=0 detail=0}
Activate - Event {type=26 Shell {1} [layout=FillLayout {type=SWT.HORIZONTAL}] time=483235453 data=null x=0 y=0 width=0 height=0 detail=3}
MouseDown - Event {type=3 Composite {} [layout=null] time=483236000 data=null x=187 y=115 width=0 height=0 detail=0}
However, closing it when shell2
deactivate,
shell2.addListener( SWT.Deactivate, e -> shell2.close() ) ;
results in the following event flow for the same mouse action:
Activate - Event {type=26 Composite {} [layout=null] time=483714125 data=null x=0 y=0 width=0 height=0 detail=3}
Deactivate - Event {type=27 Shell {*Disposed*} [layout=null] time=483714125 data=null x=0 y=0 width=0 height=0 detail=0}
Why are all the events after shell2 Deactivate gone?
What should I do if I still want to get the events after that?
Upvotes: 0
Views: 58