Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drag Image Across Shells
Drag Image Across Shells [message #637522] Sat, 06 November 2010 02:27
Bill Masek is currently offline Bill MasekFriend
Messages: 8
Registered: July 2009
Junior Member
This is a multi-part message in MIME format.
--------------040201080801030300010603
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I would like to have drag image that crosses from one SWT to another SWT
shell in the same Display. I've included a modified version of a
snippet that demonstrates what I want. Note: I do not expect to see the
image if I am not over a SWT shell.

Thanks
Bill Masek
781-572-8697
billmasek@comcast.net

--------------040201080801030300010603
Content-Type: text/plain;
name="Snippet257.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="Snippet257.java"

/*********************************************************** ********************
* Copyright (c) 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
************************************************************ *******************/
package bill.test;

/*
* Drag text selection within a StyledText widget
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.3
*/
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet257 {

static String string1 = "A drag source is the provider of data in a Drag and Drop data transfer as well as "+
"the originator of the Drag and Drop operation. The data provided by the drag source "+
"may be transferred to another location in the same widget, to a different widget "+
"within the same application, or to a different application altogether. For example, "+
"you can drag text from your application and drop it on an email application, or you "+
"could drag an item in a tree and drop it below a different node in the same tree.";

static String DRAG_START_DATA = "DRAG_START_DATA";

public static void main (String [] args) {
final Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
shell.setSize(100, 300);
int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
final StyledText text = new StyledText(shell, style);
text.setText(string1);
Shell shell2 = new Shell (display);
shell2.setLayout(new FillLayout());
shell2.setSize(100, 300);
final StyledText text2 = new StyledText(shell2, style);
text2.setText("this is stuff here");
final DragSource source = new DragSource(text, DND.DROP_COPY | DND.DROP_MOVE);
source.setDragSourceEffect(new DragSourceEffect(text) {
public void dragStart(DragSourceEvent event) {
event.image = display.getSystemImage(SWT.ICON_WARNING);
}
});
source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
source.addDragListener(new DragSourceAdapter() {
Point selection;
public void dragStart(DragSourceEvent event) {
selection = text.getSelection();
event.doit = selection.x != selection.y;
text.setData(DRAG_START_DATA, selection);
}
public void dragSetData(DragSourceEvent e) {
e.data = text.getText(selection.x, selection.y-1);
}
public void dragFinished(DragSourceEvent event) {
if (event.detail == DND.DROP_MOVE) {
Point newSelection= text.getSelection();
int length = selection.y - selection.x;
int delta = 0;
if (newSelection.x < selection.x)
delta = length;
text.replaceTextRange(selection.x + delta, length, "");
}
selection = null;
text.setData(DRAG_START_DATA, null);
}
});

DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
target.setTransfer(new Transfer[] {TextTransfer.getInstance()});
target.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if (text.getData(DRAG_START_DATA) == null)
event.detail = DND.DROP_COPY;
else
event.detail = DND.DROP_MOVE;
}
}
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if (text.getData(DRAG_START_DATA) == null)
event.detail = DND.DROP_COPY;
else
event.detail = DND.DROP_MOVE;
}
}
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT;
}
public void drop(DropTargetEvent event) {
if (event.detail != DND.DROP_NONE) {
Point selection = (Point) text.getData(DRAG_START_DATA);
int insertPos = text.getCaretOffset();
if (event.detail == DND.DROP_MOVE && selection != null && selection.x <= insertPos && insertPos <= selection.y
|| event.detail == DND.DROP_COPY && selection != null && selection.x < insertPos && insertPos < selection.y) {
text.setSelection(selection);
event.detail = DND.DROP_COPY; // prevent source from deleting selection
} else {
String string = (String)event.data;
text.insert(string);
if (selection != null)
text.setSelectionRange(insertPos, string.length());
}
}
}
});
DropTarget target2 = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
target2.setTransfer(new Transfer[] {TextTransfer.getInstance()});
target2.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if (text.getData(DRAG_START_DATA) == null)
event.detail = DND.DROP_COPY;
else
event.detail = DND.DROP_MOVE;
}
}
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if (text.getData(DRAG_START_DATA) == null)
event.detail = DND.DROP_COPY;
else
event.detail = DND.DROP_MOVE;
}
}
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT;
}
public void drop(DropTargetEvent event) {
if (event.detail != DND.DROP_NONE) {
Point selection = (Point) text2.getData(DRAG_START_DATA);
int insertPos = text.getCaretOffset();
if (event.detail == DND.DROP_MOVE && selection != null && selection.x <= insertPos && insertPos <= selection.y
|| event.detail == DND.DROP_COPY && selection != null && selection.x < insertPos && insertPos < selection.y) {
text2.setSelection(selection);
event.detail = DND.DROP_COPY; // prevent source from deleting selection
} else {
String string = (String)event.data;
text2.insert(string);
if (selection != null)
text2.setSelectionRange(insertPos, string.length());
}
}
}
});
shell.open ();
shell2.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

}

--------------040201080801030300010603--
Previous Topic:Extra row and column in a SWT Table
Next Topic:Check button and other controls do not show border when focused
Goto Forum:
  


Current Time: Thu Aug 22 04:36:37 GMT 2019

Powered by FUDForum. Page generated in 0.05048 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top