Tuesday, April 21, 2009

SWT Designer Tool

While designing some SWT application, I came across a designer tool which seems very helpful for creating complex application in quick time.

www.instantiations.com/

They provide a free license version, if you are interested you can also buy from them

Code for implementing the drop functionality in SWT tree

Below is a small snippet for code for implementing the drop functionality in SWT tree

int operations = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_MOVE;
DropTarget target = new DropTarget(tree, operations);
target.setTransfer(new Transfer[] { FileTransfer.getInstance() };);
target.addDropListener(new DropTargetAdapter() {

public void drop(DropTargetEvent event) {
File f = new File (event.item)
TreeItem item = new TreeItem (root, 0);
item.setText ("item");
item.setData (f);
}

File drag functionality for a tree in SWT

Below is a small snippet of implementing file drag functionality for a tree in SWT

int operations = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_MOVE;
DragSource dragSource = new DragSource(tree, operations);
Transfer[] types = new Transfer[] { FileTransfer.getInstance() };
dragSource.setTransfer(types);
dragSource.addDragListener(new DragSourceListener() {

TreeItem[] dndSelection = null;
String[] sourceNames = null;

public void dragStart(DragSourceEvent event) {
dndSelection = ExporttargetTree.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;
for (int i = 0; i <>
File file = (File) dndSelection[i].getData();
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});

File drag functionality for a tree in SWT

Below is a small snippet of impementing filedrag functionality for a tree in SWT

int operations = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_MOVE;
DragSource dragSource = new DragSource(tree, operations);
Transfer[] types = new Transfer[] { FileTransfer.getInstance() };
dragSource.setTransfer(types);
dragSource.addDragListener(new DragSourceListener() {

TreeItem[] dndSelection = null;
String[] sourceNames = null;

public void dragStart(DragSourceEvent event) {
dndSelection = ExporttargetTree.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;
for (int i = 0; i <>
File file = (File) dndSelection[i].getData();
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});

Code for selecting/de-selecting the main checkboxes in the tree SWT

Below is a small snippet of code for selecting/de-selecting the main parent checkboxes in the tree

TreeItem[] itm = tree.getItems();
for(int i =0; i <>
//selecting parent items
itm[i].setChecked(true); //use false for de-selecting
}

Code for selecting/de-selecting the checkboxes in the child items of tree SWT

Below is a small snippet of code for selecting/de-selecting the checkboxes in the child items of tree

TreeItem[] itm = tree.getItems();
for(int i =0; i <>
//getting the child items for the parent items
TreeItem[] childItm = itm[i].getItems();
for(int j = 0; j <>
childItm[j].setChecked(true); //use false for de-selecting
}
}

Code for selecting/de-selecting the checkbox in tree SWT

Below is a small snippet for code for selecting/de-selecting the child checkbox in tree

TreeItem[] itm = tree.getItems();
for(int i =0; i <>
//selecting parent items
itm[i].setChecked(true); //use false for de-selecting
//getting the child items for the parent items
TreeItem[] childItm = itm[i].getItems();
for(int j = 0; j <>
childItm[j].setChecked(true);
}
}