Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 340736
Collapse All | Expand All

(-)src/org/eclipse/swt/widgets/Display.java (-5 / +5 lines)
Lines 275-292 Link Here
275
    return focusControl;
275
    return focusControl;
276
  }
276
  }
277
277
278
  private void setFocusControl( final Control focusControl ) {
278
  private void setFocusControl( Control focusControl ) {
279
    if( this.focusControl != focusControl ) {
279
    if( this.focusControl != focusControl ) {
280
      if( this.focusControl != null && !this.focusControl.isInDispose() ) {
280
      if( this.focusControl != null && !this.focusControl.isInDispose() ) {
281
        FocusEvent event
281
        FocusEvent event = new FocusEvent( this.focusControl, FocusEvent.FOCUS_LOST );
282
          = new FocusEvent( this.focusControl, FocusEvent.FOCUS_LOST );
283
        event.processEvent();
282
        event.processEvent();
283
        this.focusControl.getShell().updateDefaultButton( this.focusControl, false );
284
      }
284
      }
285
      this.focusControl = focusControl;
285
      this.focusControl = focusControl;
286
      if( this.focusControl != null ) {
286
      if( this.focusControl != null ) {
287
        FocusEvent event
287
        FocusEvent event = new FocusEvent( this.focusControl, FocusEvent.FOCUS_GAINED );
288
          = new FocusEvent( this.focusControl, FocusEvent.FOCUS_GAINED );
289
        event.processEvent();
288
        event.processEvent();
289
        this.focusControl.getShell().updateDefaultButton( this.focusControl, true );
290
      }
290
      }
291
    }
291
    }
292
  }
292
  }
(-)src/org/eclipse/swt/widgets/Shell.java (+11 lines)
Lines 842-847 Link Here
842
    }
842
    }
843
  }
843
  }
844
844
845
  void updateDefaultButton( Control focusControl, boolean set ) {
846
    if( focusControl instanceof Button && ( focusControl.getStyle() & SWT.PUSH ) != 0 ) {
847
      Button defaultButton = ( Button )focusControl;
848
      if( set ) {
849
        focusControl.getShell().setDefaultButton( defaultButton, false );
850
      } else {
851
        focusControl.getShell().setDefaultButton( null, false );
852
      }
853
    }
854
  }
855
845
  /**
856
  /**
846
   * Returns the receiver's default button if one had
857
   * Returns the receiver's default button if one had
847
   * previously been set, otherwise returns null.
858
   * previously been set, otherwise returns null.
(-)src/org/eclipse/swt/widgets/Shell_Test.java (+32 lines)
Lines 333-338 Link Here
333
    assertNull( shell.getDefaultButton() );
333
    assertNull( shell.getDefaultButton() );
334
  }
334
  }
335
335
336
  public void testSetDefaultButtonOnFocus() {
337
    shell.open();
338
    assertNull( shell.getDefaultButton() );
339
    Button button = new Button( shell, SWT.PUSH );
340
    Combo combo = new Combo( shell, SWT.NONE );
341
    button.setFocus();
342
    assertSame( button, shell.getDefaultButton() );
343
    combo.setFocus();
344
    assertNull( shell.getDefaultButton() );
345
  }
346
347
  public void testSetDefaultButtonOnFocus_EventOrder() {
348
    final ArrayList log = new ArrayList();
349
    shell.open();
350
    assertNull( shell.getDefaultButton() );
351
    Button button = new Button( shell, SWT.PUSH );
352
    Combo combo = new Combo( shell, SWT.NONE );
353
    button.addFocusListener( new FocusListener() {
354
      public void focusGained( FocusEvent e ) {
355
        log.add( shell.getDefaultButton() );
356
      }
357
      public void focusLost( FocusEvent e ) {
358
        log.add( shell.getDefaultButton() );
359
      }
360
    } );
361
    button.setFocus();
362
    combo.setFocus();
363
    assertEquals( 2, log.size() );
364
    assertNull( log.get( 0 ) );
365
    assertSame( button, log.get( 1 ) );
366
  }
367
336
  public void testForceActive() {
368
  public void testForceActive() {
337
    Shell secondShell = new Shell( display );
369
    Shell secondShell = new Shell( display );
338
    shell.open();
370
    shell.open();

Return to bug 340736