Below is a snippet of code which will demonstrate Files Drag and Drop in SWT application.
It is an extension of Drag and drop code available at http://www.eclipse.org/swt/snippets/
/*
* example snippet: embed Swing/AWT in SWT
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
/*
* A sample demonstration of file drag and drop
*/
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.TreePath;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.awt.SWT_AWT;
public class DragnDropSWT extends ByteArrayTransfer implements Runnable {
boolean returnFlag = false;
private int[] ids;
private String[] names;
Display display = null;
Shell shell = null;
public static void main(String[] args) {
new DragnDropSWT().run();
}
protected String[] getTypeNames() {
return names;
}
protected int[] getTypeIds() {
return ids;
}
public void run() {
display = new Display();
shell = new Shell(display);
shell.setText("SWT File Drag and Drop Example");
Listener exitListener = new Listener() {
public void handleEvent(Event e) {
MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
dialog.setText("Question");
dialog.setMessage("Exit?");
if (e.type == SWT.Close) e.doit = false;
if (dialog.open() != SWT.OK) return;
shell.dispose();
}
};
Listener aboutListener = new Listener() {
public void handleEvent(Event e) {
final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
s.setText("About");
GridLayout layout = new GridLayout(1, false);
layout.verticalSpacing = 20;
layout.marginHeight = layout.marginWidth = 10;
s.setLayout(layout);
Label label = new Label(s, SWT.NONE);
label.setText("SWT and AWT Example.");
Button button = new Button(s, SWT.PUSH);
button.setText("OK");
GridData data = new GridData();
data.horizontalAlignment = GridData.CENTER;
button.setLayoutData(data);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
s.dispose();
}
});
s.pack();
Rectangle parentBounds = shell.getBounds();
Rectangle bounds = s.getBounds();
int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
s.setLocation(x, y);
s.open();
while (!s.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
};
shell.addListener(SWT.Close, exitListener);
Menu mb = new Menu(shell, SWT.BAR);
MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
fileItem.setText("&File");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(fileMenu);
MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
exitItem.setText("&Exit\tCtrl+X");
exitItem.setAccelerator(SWT.CONTROL + 'X');
exitItem.addListener(SWT.Selection, exitListener);
MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
aboutItem.setText("&About\tCtrl+A");
aboutItem.setAccelerator(SWT.CONTROL + 'A');
aboutItem.addListener(SWT.Selection, aboutListener);
shell.setMenuBar(mb);
RGB color = shell.getBackground().getRGB();
final Composite comp = new Composite(shell, SWT.NONE);
final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
Sash sash = new Sash(comp, SWT.VERTICAL);
Composite tableComp = new Composite(comp, SWT.EMBEDDED);
Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
Composite statusComp = new Composite(shell, SWT.EMBEDDED);
sash.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if (e.detail == SWT.DRAG) return;
GridData data = (GridData)fileTree.getLayoutData();
Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
data.widthHint = e.x - trim.width;
comp.layout();
}
});
File[] roots = File.listRoots();
for (int i = 0; i <>
File file = roots[i];
TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
treeItem.setText(file.getAbsolutePath());
treeItem.setData(file);
new TreeItem(treeItem, SWT.NONE);
}
fileTree.addListener(SWT.Expand, new Listener() {
public void handleEvent(Event e) {
TreeItem item = (TreeItem)e.item;
if (item == null) return;
if (item.getItemCount() == 1) {
TreeItem firstItem = item.getItems()[0];
if (firstItem.getData() != null) return;
firstItem.dispose();
} else {
return;
}
File root = (File)item.getData();
File[] files = root.listFiles();
if (files == null) return;
for (int i = 0; i <>
File file = files[i];
//if (file.isDirectory()) {
TreeItem treeItem = new TreeItem(item, SWT.NONE);
treeItem.setText(file.getName());
treeItem.setData(file);
if (file.isDirectory()){
new TreeItem(treeItem, SWT.NONE);
}
//}
}
}
});
fileTree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
TreeItem item = (TreeItem)e.item;
if (item == null) return;
final File root1 = (File)item.getData();
final File root = new File("c:\\temp");
System.out.println("Root " + item.getData());
EventQueue.invokeLater(new Runnable() {
public void run() {
//statusLabel.setText(root1.getAbsolutePath());
//locationText.setText(root1.getAbsolutePath());
}
});
}
});
Transfer[] types = new Transfer[] { FileTransfer.getInstance() };
int operations = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_MOVE;
final DragSource dragSource = new DragSource(fileTree, DND.DROP_COPY | DND.DROP_MOVE);
dragSource.setTransfer(types);
final TreeItem[] dragSourceItem = new TreeItem[1];
final File dragFiles = new File("C:\\temp\\test");
dragSource.addDragListener(new DragSourceListener() {
TreeItem[] dndSelection = null;
String[] sourceNames = null;
public void dragStart(DragSourceEvent event) {
dndSelection = fileTree.getSelection();
sourceNames = null;
event.doit = dndSelection.length > 0;
}
public void dragFinished(DragSourceEvent event) {
dndSelection = null;
sourceNames = null;
}
public void dragSetData(DragSourceEvent event) {
if (dndSelection == null || dndSelection.length == 0)
return;
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
sourceNames = new String[dndSelection.length];
for (int i = 0; i <>
File file = (File) dndSelection[i].getData();
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});
DropTarget target = new DropTarget(fileTree, operations);
target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
target.addDropListener(new DropTargetAdapter() {
public void dragOver(DropTargetEvent event) {
System.out.println("Dargged Over");
}
public void drop(DropTargetEvent event) {
System.out.println("DROOPED Over" + event.item.getData());
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else if ((event.operations & DND.DROP_LINK) != 0) {
event.detail = DND.DROP_LINK;
} else if ((event.operations & DND.DROP_MOVE) != 0) {
event.detail = DND.DROP_MOVE;
}
}
String fileList[] = null;
FileTransfer ft = FileTransfer.getInstance();
if (ft.isSupportedType(event.currentDataType)) {
fileList = (String[])event.data;
}
for (int i = 0; i <>
File file = new File(fileList[i]);
TreeItem item = (TreeItem)event.item;
TreeItem treeItem = new TreeItem(item, SWT.NONE);
File f2 = new File(event.item.getData()+ "\\" + file.getName());
treeItem.setText(f2.getName());
treeItem.setData(f2);
try {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f2));
int size = 4096;
byte[] buffer = new byte[size];
int len;
while ((len = is.read(buffer, 0, size)) > 0) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void dragEnter(DropTargetEvent event)
{
System.out.println("DROOPED Enter");
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else if ((event.operations & DND.DROP_LINK) != 0) {
event.detail = DND.DROP_LINK;
} else if ((event.operations & DND.DROP_MOVE) != 0) {
event.detail = DND.DROP_MOVE;
}
}
}
});
GridLayout layout = new GridLayout(1, false);
layout.marginWidth = layout.marginHeight = 0;
layout.horizontalSpacing = layout.verticalSpacing = 1;
shell.setLayout(layout);
GridData data;
//
layout = new GridLayout(1, false);
layout.marginWidth = layout.marginHeight = 0;
layout.horizontalSpacing = layout.verticalSpacing = 1;
shell.setLayout(layout);
comp.setLayout(layout);
data = new GridData(GridData.FILL_VERTICAL);
data.widthHint = 200;
data.heightHint = 300;
fileTree.setLayoutData(data);
data = new GridData(GridData.FILL_VERTICAL);
sash.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
tableComp.setLayoutData(data);
shell.open();
shell.setSize(250,500);
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
Monday, March 9, 2009
Subscribe to:
Post Comments (Atom)
doesn't compile
ReplyDeletePoorly formatted as well..
ReplyDelete