Skip to content
This repository has been archived by the owner on Jun 26, 2018. It is now read-only.

Commit

Permalink
try to fix #1781 dispose drag source
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Dec 12, 2013
1 parent 2f5c366 commit 5e906b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.URLTransfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

Expand Down Expand Up @@ -77,6 +80,14 @@ public DnDAdapter(IGLCanvas canvas, Iterable<IGLMouseListener> mouseListeners) {

public void init() {
canvas.asComposite().addKeyListener(this);
canvas.asComposite().getParent().addDisposeListener(new DisposeListener() {

@Override
public void widgetDisposed(DisposeEvent e) {
freeSource(true);
freeTarget(true);
}
});
}

/**
Expand All @@ -85,24 +96,28 @@ public void init() {
private void ensureTarget() {
if (this.target != null)
return;
target = new DropTarget(canvas.asComposite(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
target = new DropTarget(getComposite(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
target.setTransfer(new Transfer[] { CaleydoTransfer.getInstance(), FileTransfer.getInstance(),
URLTransfer.getInstance(), TextTransfer.getInstance() });
target.addDropListener(this);
}

private Composite getComposite() {
return canvas.asComposite();
}

/**
* lazy dipose of target
*/
private void freeTarget() {
if (targetListeners.isEmpty() && target != null) {
private void freeTarget(boolean force) {
if ((targetListeners.isEmpty() || force) && target != null) {
target.dispose();
target = null;
}
}

private void freeSource() {
if (sourceListeners.isEmpty() && source != null) {
private void freeSource(boolean force) {
if ((sourceListeners.isEmpty() || force) && source != null) {
source.dispose();
source = null;
}
Expand All @@ -111,7 +126,7 @@ private void freeSource() {
private void ensureSource() {
if (this.source != null)
return;
source = new DragSource(canvas.asComposite(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
source = new DragSource(getComposite(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
source.setTransfer(new Transfer[] { CaleydoTransfer.getInstance(), FileTransfer.getInstance(),
URLTransfer.getInstance(), TextTransfer.getInstance() });
source.addDragListener(this);
Expand All @@ -127,6 +142,8 @@ public void run() {
});
}



public void addDropListener(DropTargetListener l) {
targetListeners.add(l);
display().asyncExec(new Runnable() {
Expand All @@ -145,7 +162,7 @@ public boolean removeDragListener(DragSourceListener l) {
display().syncExec(new Runnable() {
@Override
public void run() {
freeSource();
freeSource(false);
}
});
return r;
Expand All @@ -157,7 +174,7 @@ public boolean removeDropListener(DropTargetListener l) {
display().syncExec(new Runnable() {
@Override
public void run() {
freeTarget();
freeTarget(false);
}
});
return r;
Expand Down Expand Up @@ -220,7 +237,7 @@ public void run() {
private Point getPoint(DropTargetEvent event) {
if (event.x == 0 && event.y == 0 && prev != null)
return prev;
Point p = canvas.asComposite().toControl(event.x, event.y);
Point p = getComposite().toControl(event.x, event.y);
prev = p;
return p;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class SWTGLCanvasFactory extends ASWTBasedCanvasFactory {

@Override
public SWTGLCanvas create(GLCapabilitiesImmutable caps, Composite parent) {
// parent = new Composite(parent, SWT.NO_BACKGROUND);
// parent.setLayout(new FillLayout());
GLCanvas canvas = new GLCanvas(parent, SWT.NO_BACKGROUND, caps, null, null);
return new SWTGLCanvas(canvas);
}
Expand Down

0 comments on commit 5e906b0

Please sign in to comment.