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 272803 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/swt/widgets/Display.java (-3 / +67 lines)
Lines 13-18 Link Here
13
13
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStream;
15
import java.io.InputStream;
16
import java.lang.ref.WeakReference;
16
import java.text.MessageFormat;
17
import java.text.MessageFormat;
17
import java.util.*;
18
import java.util.*;
18
import java.util.List;
19
import java.util.List;
Lines 129-135 Link Here
129
  private static final String APP_VERSION
130
  private static final String APP_VERSION
130
    = Display.class.getName() + "#appVersion";
131
    = Display.class.getName() + "#appVersion";
131
132
132
133
  /* Package Name */
133
  /* Package Name */
134
  static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets.";
134
  static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets.";
135
135
Lines 204-211 Link Here
204
204
205
  /* Display Data */
205
  /* Display Data */
206
  private Object data;
206
  private Object data;
207
  private String [] keys;
207
  private String[] keys;
208
  private Object [] values;
208
  private Object[] values;
209
210
  private static WeakReference[] displays = new WeakReference[ 4 ];
209
211
210
  /**
212
  /**
211
   * Constructs a new instance of this class.
213
   * Constructs a new instance of this class.
Lines 233-238 Link Here
233
      SWT.error( SWT.ERROR_NOT_IMPLEMENTED, null, " [multiple displays]" );
235
      SWT.error( SWT.ERROR_NOT_IMPLEMENTED, null, " [multiple displays]" );
234
    }
236
    }
235
    RWTLifeCycle.setSessionDisplay( this );
237
    RWTLifeCycle.setSessionDisplay( this );
238
    register( this);
236
    shells = new ArrayList();
239
    shells = new ArrayList();
237
    readInitialBounds();
240
    readInitialBounds();
238
    readScrollBarSize();
241
    readScrollBarSize();
Lines 690-695 Link Here
690
    // TODO [rh] zero fields
693
    // TODO [rh] zero fields
691
  }
694
  }
692
695
696
  protected void destroy () {
697
    deregister( this );
698
  }
699
693
  private void sendDisposeEvent() {
700
  private void sendDisposeEvent() {
694
    Event event = new Event();
701
    Event event = new Event();
695
    event.display = this;
702
    event.display = this;
Lines 1880-1885 Link Here
1880
    session.setAttribute( APP_VERSION, version );
1887
    session.setAttribute( APP_VERSION, version );
1881
  }
1888
  }
1882
1889
1890
  /**
1891
   * Returns the display which the given thread is the
1892
   * user-interface thread for, or null if the given thread
1893
   * is not a user-interface thread for any display.  Specifying
1894
   * <code>null</code> as the thread will return <code>null</code>
1895
   * for the display.
1896
   *
1897
   * @param thread the user-interface thread
1898
   * @return the display for the given thread
1899
   *
1900
   * @since 1.3
1901
   */
1902
  public static Display findDisplay( final Thread thread ) {
1903
    synchronized( Device.class ) {
1904
      Display result = null;
1905
      for( int i = 0; i < displays.length && result == null; i++ ) {
1906
        WeakReference current = displays[ i ];
1907
        if( current != null ) {
1908
          Display display = ( Display )current.get();
1909
          if( display != null && display.thread == thread ) {
1910
            result = display;
1911
          }
1912
        }
1913
      }
1914
      return result;
1915
    }
1916
  }
1917
1918
  private static void register( final Display display ) {
1919
    synchronized( Device.class ) {
1920
      boolean registered = false;
1921
      for( int i = 0; i < displays.length && !registered; i++ ) {
1922
        if( displays[ i ] == null ) {
1923
          displays[ i ] = new WeakReference( display );
1924
          registered = true;
1925
        }
1926
      }
1927
      if( !registered ) {
1928
        WeakReference[] newDisplays = new WeakReference[ displays.length + 4 ];
1929
        System.arraycopy( displays, 0, newDisplays, 0, displays.length );
1930
        newDisplays[ displays.length ] = new WeakReference( display );
1931
        displays = newDisplays;
1932
      }
1933
    }
1934
  }
1935
1936
  private static void deregister( final Display display ) {
1937
    synchronized( Device.class ) {
1938
      for( int i = 0; i < displays.length; i++ ) {
1939
        WeakReference current = displays[ i ];
1940
        if( current != null && display == current.get() ) {
1941
          displays[ i ] = null;
1942
        }
1943
      }
1944
    }
1945
  }
1946
1883
  /////////////////////
1947
  /////////////////////
1884
  // Consistency checks
1948
  // Consistency checks
1885
1949
(-)src/org/eclipse/swt/widgets/Display_Test.java (-2 / +25 lines)
Lines 20-27 Link Here
20
import org.eclipse.rwt.RWT;
20
import org.eclipse.rwt.RWT;
21
import org.eclipse.rwt.graphics.Graphics;
21
import org.eclipse.rwt.graphics.Graphics;
22
import org.eclipse.rwt.internal.lifecycle.*;
22
import org.eclipse.rwt.internal.lifecycle.*;
23
import org.eclipse.rwt.internal.service.ContextProvider;
23
import org.eclipse.rwt.internal.service.*;
24
import org.eclipse.rwt.internal.service.ServiceContext;
25
import org.eclipse.rwt.lifecycle.*;
24
import org.eclipse.rwt.lifecycle.*;
26
import org.eclipse.rwt.service.ISessionStore;
25
import org.eclipse.rwt.service.ISessionStore;
27
import org.eclipse.swt.*;
26
import org.eclipse.swt.*;
Lines 1122-1127 Link Here
1122
    assertNull( Display.getAppVersion() );
1121
    assertNull( Display.getAppVersion() );
1123
  }
1122
  }
1124
1123
1124
  public void testFindDisplay() {
1125
    Display display = new Display();
1126
    assertSame( display, Display.findDisplay( display.getThread() ) );
1127
    assertNull( Display.findDisplay( null ) );
1128
    display.dispose();
1129
    assertNull( Display.findDisplay( display.getThread() ) );
1130
  }
1131
1132
  public void testFindDisplayFromDifferentSession() throws Exception {
1133
    final Display[] otherDisplay = new Display[ 1 ];
1134
    Thread thread = new Thread( new Runnable() {
1135
      public void run() {
1136
        Fixture.fakeContext();
1137
        ContextProvider.getContext().setStateInfo( new ServiceStateInfo() );
1138
        otherDisplay[ 0 ] = new Display();
1139
      }
1140
    } );
1141
    thread.start();
1142
    thread.join();
1143
    Display display = Display.findDisplay( thread );
1144
    assertNotNull( display );
1145
    assertSame( otherDisplay[ 0 ], display );
1146
  }
1147
1125
  protected void setUp() throws Exception {
1148
  protected void setUp() throws Exception {
1126
    Fixture.setUp();
1149
    Fixture.setUp();
1127
  }
1150
  }

Return to bug 272803