Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@
package org.eclipse.swt.internal.widgets;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.swt.widgets.Display;


public class DisplaysHolder {
private WeakReference<Display>[] displays;

@SuppressWarnings("unchecked")
public final class DisplaysHolder {

private final Map<Thread, WeakReference<Display>> displays;

public DisplaysHolder() {
displays = new WeakReference[ 4 ];
displays = Collections.synchronizedMap( new WeakHashMap<>() );
}
public WeakReference<Display>[] getDisplays() {
return displays;

public void addDisplay( Thread tread, Display display ) {
displays.put( tread, new WeakReference<>( display ) );
}

public void setDisplays( WeakReference<Display>[] displays ) {
this.displays = displays;

public Display getDisplay( Thread thread ) {
WeakReference<Display> wr = displays.get( thread );
if ( wr != null ) {
return wr.get();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.eclipse.rap.rwt.remote.JsonMapping.readRectangle;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -939,17 +938,11 @@ private void detachThread() {
* @return the display for the given thread
*/
public static Display findDisplay( Thread thread ) {
synchronized( Device.class ) {
for( WeakReference<Display> displayRef : getDisplays() ) {
if( displayRef != null ) {
Display display = displayRef.get();
if( display != null && !display.isDisposed() && display.thread == thread ) {
return display;
}
}
}
Display display = getDisplay( thread );
if (display == null || display.thread != thread || display.isDisposed()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the code formatting here: if( display == ... )

return null;
}
return display;
}

/**
Expand Down Expand Up @@ -2217,57 +2210,20 @@ public boolean getTouchEnabled() {
return false;
}

@SuppressWarnings("unchecked")
private void register() {
synchronized( Device.class ) {
boolean registered = false;
WeakReference<Display>[] displays = getDisplays();
for( int i = 0; !registered && i < displays.length; i++ ) {
if( canDisplayRefBeReplaced( displays[ i ] ) ) {
displays[ i ] = new WeakReference<>( this );
registered = true;
}
}
if( !registered ) {
WeakReference<Display>[] newDisplays = new WeakReference[ displays.length + 4 ];
System.arraycopy( displays, 0, newDisplays, 0, displays.length );
newDisplays[ displays.length ] = new WeakReference<>( this );
setDisplays( newDisplays );
}
}
}

private boolean canDisplayRefBeReplaced( WeakReference<Display> displayRef ) {
boolean result = false;
if( displayRef == null ) {
result = true;
} else {
Display display = displayRef.get();
if( display == null || display.thread == thread ) {
result = true;
}
}
return result;
addDisplay( this );
}

private void deregister() {
synchronized( Device.class ) {
WeakReference<Display>[] displays = getDisplays();
for( int i = 0; i < displays.length; i++ ) {
WeakReference<Display> current = displays[ i ];
if( current != null && this == current.get() ) {
displays[ i ] = null;
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to remove the display from the holder here?

}

private static WeakReference<Display>[] getDisplays() {
return ContextProvider.getApplicationContext().getDisplaysHolder().getDisplays();
private static Display getDisplay( Thread thread ) {
return ContextProvider.getApplicationContext().getDisplaysHolder().getDisplay( thread );
}

private void setDisplays( WeakReference<Display>[] displays ) {
getApplicationContext().getDisplaysHolder().setDisplays( displays );
private void addDisplay( Display display ) {
getApplicationContext().getDisplaysHolder().addDisplay( thread, display );
}

/////////////////////
Expand Down