package SesameGUI; import java.awt.dnd.*; import java.awt.datatransfer.*; import java.awt.Point; import java.io.*; import javax.swing.tree.*; import javax.swing.JTree; public class DragDropTree extends JTree { JFrameSesame parent=null; private DragSource dragSource; private DropTarget dropTarget; private DefaultTreeModel model; private TreeGestureListener gestureListener; private TreeSourceListener sourceListener; private TreeTargetListener targetListener; private DefaultMutableTreeNode dragNode = null; private DragDropTree tree = this; private class TreeGestureListener implements DragGestureListener { public void dragGestureRecognized(DragGestureEvent e) { Point nodeLocation = e.getDragOrigin(); DefaultMutableTreeNode treeNode = getTreeNode(nodeLocation); if (treeNode != null) { dragNode = treeNode; // Start the drag. dragSource.startDrag(e, DragSource.DefaultCopyDrop, new StringSelection(treeNode.toString()), sourceListener); } } } public DragDropTree(DefaultTreeModel model1) { super(model1); model = model1; gestureListener = new TreeGestureListener(); sourceListener = new TreeSourceListener(); targetListener = new TreeTargetListener(); // Set up the tree to be a drop target. dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, targetListener, true); // Set up the tree to be a drag source. dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, gestureListener); //{{REGISTER_LISTENERS SymTreeSelection lSymTreeSelection = new SymTreeSelection(); this.addTreeSelectionListener(lSymTreeSelection); //}} } private class TreeSourceListener implements DragSourceListener { public void dragDropEnd(DragSourceDropEvent e) { // Do Nothing. } public void dragEnter(DragSourceDragEvent e) { // Do Nothing. } public void dragExit(DragSourceEvent e) { // Do Nothing. } public void dragOver(DragSourceDragEvent e) { // Do Nothing. } public void dropActionChanged(DragSourceDragEvent e) { // Do Nothing. } } private class TreeTargetListener implements DropTargetListener { public void drop(DropTargetDropEvent e) { try { Point dropLocation = e.getLocation(); DefaultMutableTreeNode treeNode = getTreeNode(dropLocation); DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable data = e.getTransferable(); if (treeNode != null && e.isDataFlavorSupported(stringFlavor)) { String text = (String) data.getTransferData(stringFlavor); e.acceptDrop(DnDConstants.ACTION_MOVE); // Add the new node to the current node. if ( treeNode.getParent()!=null ) { int ind = treeNode.getParent().getIndex(treeNode); if (!treeNode.getAllowsChildren()) treeNode = (DefaultMutableTreeNode)treeNode.getParent(); else ind = 0; SesameObject obj = (SesameObject)dragNode.getUserObject(); if (obj.getType()!=0 && obj.getType()!=3 && treeNode.getAllowsChildren()) { //model.insertNodeInto(dragNode, treeNode, treeNode.getChildCount()); model.insertNodeInto(dragNode, treeNode, ind); model.reload(); } } e.dropComplete(true); TreeNode[] nodes = model.getPathToRoot(dragNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } else { e.rejectDrop(); } } catch(IOException ioe) { ioe.printStackTrace(); } catch(UnsupportedFlavorException ufe) { ufe.printStackTrace(); } } public void dragEnter(DropTargetDragEvent e) { if (isDragOk(e) == false) { e.rejectDrag(); return; } e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } public void dragExit(DropTargetEvent e) { // Do Nothing. } public void dragOver(DropTargetDragEvent e) { Point dragLocation = e.getLocation(); TreePath treePath = getPathForLocation(dragLocation.x, dragLocation.y); if (isDragOk(e) == false || treePath == null) { e.rejectDrag(); return; } // Make the node active. setSelectionPath(treePath); e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } public void dropActionChanged(DropTargetDragEvent e) { if (isDragOk(e) == false) { e.rejectDrag(); return; } e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } private boolean isDragOk(DropTargetDragEvent e) { // Return true for now. return(true); } } /** Meathod: DragDropTree Description: Meathod to initialize the drag and drop tree. Parameter I/O Type Description --------- --- ---- ----------- root I TreeNode The data model for the tree. */ public DragDropTree(TreeNode root) { super(root); gestureListener = new TreeGestureListener(); sourceListener = new TreeSourceListener(); targetListener = new TreeTargetListener(); // Set up the tree to be a drop target. dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, targetListener, true); // Set up the tree to be a drag source. dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, gestureListener); } private DefaultMutableTreeNode getTreeNode(Point location) { TreePath treePath = getPathForLocation(location.x, location.y); if (treePath != null) { return((DefaultMutableTreeNode) treePath.getLastPathComponent()); } else { return(null); } } class SymTreeSelection implements javax.swing.event.TreeSelectionListener { public void valueChanged(javax.swing.event.TreeSelectionEvent event) { Object object = event.getSource(); if (object == DragDropTree.this) DragDropTree_valueChanged(event); } } void setParent(JFrameSesame frame) { parent = frame; } void DragDropTree_valueChanged(javax.swing.event.TreeSelectionEvent event) { // change selected node if( parent==null) return; parent.previewPane.reset(); parent.ACTIVE_OBJECT_PLOTED=null; TreePath nodePath = getSelectionPath(); if(nodePath!=null) { DefaultMutableTreeNode SelectedNode = (DefaultMutableTreeNode)nodePath.getLastPathComponent(); parent.JEditNumberWin.setVisible(false); parent.JEditLengthWin.setVisible(false); parent.JEditSite.setVisible(false); parent.JEditOutFile.setVisible(false); parent.ProcessTimeLabel.setVisible(false); if( SelectedNode.getLevel()==0 ) return; SesameObject obj = (SesameObject)SelectedNode.getUserObject(); if( obj.getType()==3 ) { return; // unasigned file } parent.JEditOutFile.setVisible(true); parent.JEditOutFile.setText(obj.getOutputFile()); parent.ProcessTimeLabel.setVisible(true); parent.ProcessTimeLabel.setText(obj.getProcessTime()); if( obj.getType()==0 ) { parent.JEditSite.setVisible(true); parent.JEditSite.setText(obj.getName()); return; } parent.JEditNumberWin.setVisible(true); parent.JEditLengthWin.setVisible(true); parent.JEditNumberWin.setText(""+obj.getNumberWindows()); parent.JEditLengthWin.setText(""+obj.getWindowsLength()); } } }