定义Drop:
package public_project.view.dnd;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.viewers.TreeViewer;import org.eclipse.jface.viewers.Viewer;import org.eclipse.jface.viewers.ViewerDropAdapter;import org.eclipse.swt.dnd.DND;import org.eclipse.swt.dnd.DropTargetEvent;import org.eclipse.swt.dnd.TransferData;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.TreeItem;import public_project.view.model.descriptors.ProjectDetail;import public_project.view.model.descriptors.ProjectGroup;public class ViewDropListener extends ViewerDropAdapter { private TreeViewer treeViewer; public ViewDropListener(Viewer viewer) { super(viewer); this.treeViewer = (TreeViewer) viewer; } @Override public void drop(DropTargetEvent event) { if (event.data == null) { event.detail = DND.DROP_NONE; return; } String [] strData = (String[]) event.data; String text = (String) strData[0]; if (event.item == null) { MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "哈哈!!", "哈哈哈!!!!你想多了。!!!功能太鸡肋,不开放!!"); } else { TreeItem item = (TreeItem) event.item; Object objItem = item.getData(); if(objItem instanceof ProjectGroup){ ProjectGroup group = (ProjectGroup) objItem; ProjectDetail [] details = group.getProjectDetail(); String addProjectName = text.substring(text.lastIndexOf("\\")+1);// genJar(addProjectName);// ftpUpload(); if(details != null && details.length >0){ //判断是否存在 for(ProjectDetail detail : details){ if(addProjectName.equals(detail.getProjectName())){ return; } } ProjectDetail detail1 = new ProjectDetail(); detail1.setProjectName(addProjectName); detail1.setProjectStatus("0"); group.addProjectDetail(detail1); treeViewer.refresh(); }else{ ProjectDetail detail1 = new ProjectDetail(); detail1.setProjectName(addProjectName); detail1.setProjectStatus("0"); group.addProjectDetail(detail1); treeViewer.refresh(); } } } super.drop(event); } @Override public boolean performDrop(Object data) { return false; } @Override public boolean validateDrop(Object target, int operation, TransferData transferType) { return true; }}定义Drag:
package public_project.view.dnd;import org.eclipse.swt.dnd.DragSourceEvent;import org.eclipse.swt.dnd.DragSourceListener;public class ViewDragListener implements DragSourceListener{ @Override public void dragStart(DragSourceEvent event) { System.out.println(); } @Override public void dragSetData(DragSourceEvent event) { System.out.println(); } @Override public void dragFinished(DragSourceEvent event) { System.out.println(); }}调用:
int operations = DND.DROP_COPY | DND.DROP_MOVE; Transfer[] transferTypes = new Transfer[]{FileTransfer.getInstance()}; treeViewer.addDropSupport(operations, transferTypes, new ViewDropListener(treeViewer)); treeViewer.addDragSupport(operations, transferTypes, new ViewDragListener());