Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with custom TransferHandler

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Problem with custom TransferHandler

    Hi there.

    I have a JTree and my own custom TreeModel implementation. I now want to add both Drag&Drop, as well as Copy, Cut & Paste support to my JTree.
    I've read, that I need to implement my own TransferHandler because the JTree does not have its own TransferHandler implementation.

    The problem is now, that my TreeModel is also a Set. No item in the model may be at 2 positions at the same time. So if I want to Move an item from one position to another I need to remove the item first and then insert it. I thought that would not be a problem, but apparently for Swing it is.

    As far as I see it the TransferHandler will first call importData (for inserting the moved nodes) and only later call exportDone (for removing the moved nodes). This is, by definition of my model, illegal.
    So I thought maybe I can hack it a little bit and remove the nodes in the importData method if the action is a move action. This works great for Drag&Drop, but does not work at all for Cut operations.

    The problem is, when an object is Pasted (importData is called) I can not find out whether the object was cut or copied. On the other hand, in the exportDone method (called after importData) I can not find out whether the action is a Drag&Drop action or not.

    I am really frustrated by this Design by now. I just can not see a good way to implement this without it looking like the ugliest hack.

    Here is what I currently have for the TransferHandler: (I know, its huge)
    public class TreeModelTransferHandler extends TransferHandler {
    	private static final long serialVersionUID = 1L;
     
    	private final DataFlavor nodeFlavor;
     
    	public TreeModelTransferHandler() {
    		nodeFlavor = new DataFlavor(List.class, List.class.getSimpleName());
    	}
     
    	public boolean canImport(TransferHandler.TransferSupport support) {
    		if (!support.isDataFlavorSupported(nodeFlavor)) {
    			return false;
    		}
    		if (support.isDrop()) {
    			return canImportDrop(support);
    		} else {
    			return canImportPaste(support);
    		}
    	}
     
    	private boolean canImportDrop(TransferHandler.TransferSupport support) {
    		support.setShowDropLocation(true);
     
    		/*
    		 * Can not drop a path on itself or on a descendant of itself.
    		 * The component is always a JTree and its model is always a TreeModel2
    		 */
    		JTree tree = (JTree) support.getComponent();
    		TreeModel2 model = (TreeModel2) tree.getModel();
     
    		JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
    		TreePath dropPath = dl.getPath();
     
    		TreePath[] selectedPaths = tree.getSelectionPaths();
    		for (TreePath selectedPath : selectedPaths) {
    			if (!model.canAddChild(dropPath.getLastPathComponent(), selectedPath.getLastPathComponent())) {
    				return false;
    			}
    			if (selectedPath.isDescendant(dropPath)) {
    				return false;
    			}
    		}
    		return true;
    	}
     
    	private boolean canImportPaste(TransferHandler.TransferSupport support) {
    		/*
    		 * Can only paste nodes if tree has exactly one path selected.
    		 * Otherwise the paste location is not known...
    		 * The component is always a JTree and its model is always a TreeModel2
    		 */
    		JTree tree = (JTree) support.getComponent();
    		TreePath[] selectedPaths = tree.getSelectionPaths();
    		return selectedPaths.length == 1 && selectedPaths[0] != null;
    	}
     
    	public boolean importData(TransferHandler.TransferSupport support) {
    		if(!canImport(support)) {
    			return false;
    		}
    		if (support.isDrop()) {
    			return importDataDrop(support);
    		} else {
    			return importDataPaste(support);
    		}
    	}
     
    	/*
    	 * Import data when a Drag&Drop action is done
    	 */
    	@SuppressWarnings("unchecked")
    	private boolean importDataDrop(TransferHandler.TransferSupport support) {
    		// Extract nodes
    		Transferable trans = support.getTransferable();
    		List<Object> nodeList = Collections.emptyList();
    		try {
    			nodeList = (List<Object>) trans.getTransferData(nodeFlavor);
    		} catch (UnsupportedFlavorException | IOException e) {
    			e.printStackTrace();
    			return false;
    		}
    		Object[] nodes = nodeList.toArray(new Copyable<?>[nodeList.size()]);
     
    		// Component is always a JTree and has always a TreeModel2 as its model
    		JTree tree = (JTree) support.getComponent();
    		TreeModel2 model = (TreeModel2) tree.getModel();
     
    		// Extract drop location and drop index
    		JTree.DropLocation dl = (JTree.DropLocation)support.getDropLocation();
     
    		TreePath destPath = dl.getPath();
    		Object parent = destPath.getLastPathComponent();
    		int index = dl.getChildIndex();
     
    		if (index == -1) {
    			// Drop location is on top of a node
    			index = model.getChildCount(parent);
    		}
     
    		/*
    		 * moveChildren will first remove the nodes from their previous location 
    		 * and then insert the nodes at their new location.
    		 */
    		model.moveChildren(parent, nodes, index++);
     
    		return true;
    	}
     
    	@SuppressWarnings("unchecked")
    	private boolean importDataPaste(TransferHandler.TransferSupport support) {
    		// Extract nodes
    		Transferable trans = support.getTransferable();
    		Object[] nodes;
     
    		/* 
    		 * When the current action is copy we need to create a copy 
    		 * of all the nodes or otherwise a node would be contained 
    		 * in the model twice.
    		 */
    		int action = COPY; // unfortunately, we can not get the action from the support... What a bummer.
    		if ((action & COPY) == COPY) {
    			List<Copyable<?>> nodeList = Collections.emptyList();
    			try {
    				nodeList = (List<Copyable<?>>) trans.getTransferData(nodeFlavor);
    			} catch (UnsupportedFlavorException | IOException e) {
    				e.printStackTrace();
    				return false;
    			}
    			nodes = new Object[nodeList.size()];
    			int i = 0;
    			for (Copyable<?> obj : nodeList) {
    				// In order to be copied nodes must be Copyable
    				nodes[i++] = obj.makeCopy();
    			}
    		} else {
    			List<Object> nodeList = Collections.emptyList();
    			try {
    				nodeList = (List<Object>) trans.getTransferData(nodeFlavor);
    			} catch (UnsupportedFlavorException | IOException e) {
    				e.printStackTrace();
    				return false;
    			}
    			nodes = nodeList.toArray(new Object[nodeList.size()]);
    		}
     
    		// Component is always a JTree and has always a TreeModel2 as its model
    		JTree tree = (JTree) support.getComponent();
    		TreeModel2 model = (TreeModel2) tree.getModel();
     
    		// Extract drop location and drop index
    		// Drop location depends on selection
    		TreePath destPath = tree.getSelectionPath();
    		Object selected = destPath.getLastPathComponent();
     
    		/* 
    		 * If the selected can have children added, then it will become the drop location
    		 * Otherwise the parent of the selected node will become the drop location.
    		 * We know, that the parent of the selected node can have children, otherwise 
    		 * it would not be a parent.
    		 */
    		boolean selectedIsParent = true;
     
    		for (Object node : nodes) {
    			if (!model.canAddChild(selected, node)) {
    				selectedIsParent = false;
    				break;
    			}
    		}
     
    		Object parent;
    		int index;
    		if (selectedIsParent) {
    			parent = selected;
    			index = model.getChildCount(parent); // append at the end
    		} else {
    			parent = model.getParent(selected);
    			// append after the selected node
    			index = model.getIndexOfChild(parent, selected);
    		}
     
    		/* 
    		 * Inserts the new nodes into the model. 
    		 * Nodes must NOT be contained in the model at this point!
    		 */
    		model.insertChildren(parent, nodes, index++);
     
    		return true;
    	}
     
    	@SuppressWarnings("unchecked")
    	protected void exportDone(JComponent c, Transferable data, int action) {
    		if (action != MOVE) {
    			// Copy actions do not need to delete data after the export
    			return;
    		}
     
    		boolean isDragAndDrop = false; // We have no way of knowing... What a bummer.
     
    		// Drag & Drop is handled in the importData method.
    		if (!isDragAndDrop) {
    			// Extract nodes from data
    			List<Object> nodeList = Collections.emptyList();
    			try {
    				nodeList = (List<Object>) data.getTransferData(nodeFlavor);
    			} catch (UnsupportedFlavorException | IOException e) {
    				e.printStackTrace();
    			}
    			Object[] nodes = nodeList.toArray(new Copyable<?>[nodeList.size()]);
     
    			// The component is always a JTree and always has a TreeModel2 as its model
    			JTree tree = (JTree) c;
    			TreeModel2 model = (TreeModel2) tree.getModel();
     
    			// Remove the nodes from the model
    			// This will throw an exception if the nodes are not contained in the model!
    			model.removeChildren(nodes);
    		}
    	}
     
    	protected Transferable createTransferable(JComponent c) {
    		try {
    			// Component is always a JTree
    			JTree tree = (JTree)c;
     
    			// Extract nodes to be transfered => Always the selected nodes
    			TreePath[] paths = tree.getSelectionPaths();
    			if(paths != null) {
    				List<Object> nodeList = new ArrayList<>(); 
    				for (TreePath path : paths) {
    					nodeList.add(path.getLastPathComponent());
    				}
    				return new ObjectTransferable<List<Object>>(nodeList, nodeFlavor);
    			}
    			return null;
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
     
    	public int getSourceActions(JComponent c) {
    		return COPY_OR_MOVE;
    	}
     
    }

    The 2 problematic areas are within the methods "exportDone" and "importDataPaste".
    The problematic lines of code within these methods are marked with a comment.


    Any kind of help would be greatly appreciated, thanks.

    Also crossposted at:
    https://stackoverflow.com/questions/...ransferhandler
    and
    http://www.coderanch.com/t/636184/GU...ransferHandler
    Last edited by Cornix; July 6th, 2014 at 12:13 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Problem with custom TransferHandler

    I made an MCVE for Stack-Overflow, I am going to post it here as well: (But it is huge)
    package pkg;
     
    import java.awt.KeyboardFocusManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
     
    import javax.swing.Action;
    import javax.swing.JComponent;
     
    public class TransferActionListener implements ActionListener, PropertyChangeListener {
    	private JComponent focusOwner = null;
     
    	/*
    	 * This class is taken from the oracle tutorial website for Copy-Cut-Paste support.
    	 * http://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
    	 */
     
    	public static final TransferActionListener INSTANCE = new TransferActionListener();
     
    	private TransferActionListener() {
    		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    		manager.addPropertyChangeListener("permanentFocusOwner", this);
    	}
     
    	public void propertyChange(PropertyChangeEvent e) {
    		Object obj = e.getNewValue();
    		if (obj instanceof JComponent) {
    			focusOwner = (JComponent)obj;
    		} else {
    			focusOwner = null;
    		}
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if (focusOwner == null) {
    			return;
    		}
     
    		String action = (String) e.getActionCommand();
    		Action a = focusOwner.getActionMap().get(action);
    		if (a != null) {
    			a.actionPerformed(new ActionEvent(focusOwner, ActionEvent.ACTION_PERFORMED, null));
    		}
    	}
    }

    package pkg;
     
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.io.IOException;
     
    public class ObjectTransferable<E> implements Transferable {
     
    	/*
    	 * This class can be used to transfer any kind of java class.
    	 * Can only be used within the same JVM.
    	 */
     
    	private final DataFlavor[] flavors;
    	private final E obj;
     
    	public ObjectTransferable(E object) throws ClassNotFoundException {
    		obj = object;
    		flavors = new DataFlavor[] {
    			new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class="+object.getClass().getName())
    		};
    	}
     
    	public ObjectTransferable(E object, DataFlavor flavor) {
    		obj = object;
    		flavors = new DataFlavor[] {
    			flavor
    		};
    	}
     
    	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    		if (!isDataFlavorSupported(flavor)) {
    			throw new UnsupportedFlavorException(flavor);
    		}
    		return obj;
    	}
     
    	public DataFlavor[] getTransferDataFlavors() {
    		return flavors;
    	}
     
    	public boolean isDataFlavorSupported(DataFlavor flavor) {
    		return flavors[0].equals(flavor);
    	}
     
    }

    package pkg;
     
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
     
    public class Node {
     
    	private final String name;
    	private final List<Node> children;
    	private MyModel model;
    	private Node parent;
     
    	public Node(String name) {
    		this.name = name;
    		children = new ArrayList<>();
    		parent = null;
    	}
     
    	protected void setModel(MyModel model) {
    		this.model = model;
    		for (Node child : getChildren()) {
    			child.setModel(model);
    		}
    	}
     
    	protected MyModel getModel() {
    		return model;
    	}
     
    	protected void setParent(Node node) {
    		parent = node;
    	}
     
    	public Node getParent() {
    		return parent;
    	}
     
    	public void addChild(Node child) {
    		addChild(child, getChildren().size());
    	}
     
    	public void addChild(Node child, int index) {
    		if (child.getParent() == this) {
    			throw new IllegalArgumentException("Node '"+child+"' is already a child of '"+this+"'.");
    		}
    		if (child.getParent() != null) {
    			throw new IllegalArgumentException("Node '"+child+"' already has a parent.");
    		}
    		child.setParent(this);
    		child.setModel(getModel());
    		children.add(index, child);
    		fireInsertEvent(child, index);
    	}
     
    	public void removeChild(Node child) {
    		if (child.getParent() != this) {
    			throw new IllegalArgumentException("Node '"+child+"' is not a child of '"+this+"'.");
    		}
    		int index = children.indexOf(child);
    		fireRemoveEvent(child, index);
    		child.setParent(null);
    		child.setModel(null);
    		children.remove(index);
    	}
     
    	public List<Node> getChildren() {
    		return Collections.unmodifiableList(children);
    	}
     
    	protected void fireInsertEvent(Node node, int index) {
    		if (getModel() != null) {
    			getModel().fireInsertEvent(node, index);
    		}
    	}
     
    	protected void fireRemoveEvent(Node node, int index) {
    		if (getModel() != null) {
    			getModel().fireRemoveEvent(node, index);
    		}
    	}
     
    	public String toString() {
    		return name;
    	}
     
    }

    package pkg;
     
    import java.util.ArrayList;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.List;
     
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
     
    public class MyModel implements TreeModel {
     
    	private final List<TreeModelListener> listeners;
    	private Node root;
     
    	public MyModel(Node rootNode) {
    		listeners = new ArrayList<>();
    		root = rootNode;
    		root.setModel(this);
    	}
     
    	public Object getRoot() {
    		return root;
    	}
     
    	/**
    	 * Returns the parent node for the given child.
    	 * Assumes that the child is an object of type Node.
    	 * @param child
    	 * @return
    	 */
    	public Object getParent(Object child) {
    		Node childNode = (Node) child;
    		return childNode.getParent();
    	}
     
    	/**
    	 * Returns the child node at index for the given parent.
    	 * Assumes that the parent is an object of type Node.
    	 * @param child
    	 * @return
    	 */
    	public Object getChild(Object parent, int index) {
    		Node parentNode = (Node) parent;
    		return parentNode.getChildren().get(index);
    	}
     
    	/**
    	 * Returns the number of children the parent has.
    	 * Assumes that the parent is an object of type Node.
    	 * @param child
    	 * @return
    	 */
    	public int getChildCount(Object parent) {
    		Node parentNode = (Node) parent;
    		return parentNode.getChildren().size();
    	}
     
    	/**
    	 * Returns the index of child within the given parent.
    	 * Returns -1 if child is not a child of parent.
    	 * Assumes that the parent is an object of type Node.
    	 * @param child
    	 * @return
    	 */
    	public int getIndexOfChild(Object parent, Object child) {
    		Node parentNode = (Node) parent;
    		return parentNode.getChildren().indexOf(child);
    	}
     
    	/**
    	 * Returns true if the given node does not have any children.
    	 * Assumes that node is an object of type Node.
    	 * @param child
    	 * @return
    	 */
    	public boolean isLeaf(Object node) {
    		Node someNode = (Node) node;
    		return someNode.getChildren().isEmpty();
    	}
     
    	/**
    	 * Removes all nodes, within the iterable, from this model.
    	 * If an object from the iterable is not a Node this method will throw an exception.
    	 * @param nodes
    	 */
    	public void removeNodes(Iterable<Object> nodes) {
    		for (Object obj : nodes) {
    			Node node = (Node) obj;
    			Node parent = node.getParent();
     
    			parent.removeChild(node);
    		}
    	}
     
    	/**
    	 * Adds all nodes, within the iterable, as children to the given parent.
    	 * Starts the insertion at startIndex and counts up by one for each insertion.
    	 * If an object from the iterable is not a Node this method will throw an exception.
    	 * @param parent
    	 * @param startIndex
    	 * @param nodes
    	 */
    	public void insertNodes(Object parent, int startIndex, Iterable<Object> nodes) {
    		Node parentNode = (Node) parent;
    		if (startIndex > parentNode.getChildren().size()) {
    			startIndex = parentNode.getChildren().size();
    		}
    		for (Object obj : nodes) {
    			Node child = (Node) obj;
    			parentNode.addChild(child, startIndex++);
    		}
    	}
     
    	/**
    	 * Not used and not implement.
    	 * Will throw an {@link UnsupportedOperationException} if called.
    	 */
    	public void valueForPathChanged(TreePath path, Object newValue) {
    		// Never being used.
    		throw new UnsupportedOperationException("Not implemented.");
    	}
     
    	public void addTreeModelListener(TreeModelListener l) {
    		listeners.add(l);
    	}
     
    	public void removeTreeModelListener(TreeModelListener l) {
    		listeners.remove(l);
    	}
     
    	/**
    	 * Constructs a TreeModelEvent for the given node and index 
    	 * and calls treeNodesInserted on all registered listeners.
    	 * The node must never be null.
    	 * @param node
    	 * @param index
    	 */
    	protected void fireInsertEvent(Node node, int index) {
    		TreeModelEvent e = makeEvent(node, index);
    		for (TreeModelListener l : listeners) {
    			l.treeNodesInserted(e);
    		}
    	}
     
    	/**
    	 * Constructs a TreeModelEvent for the given node and index 
    	 * and calls treeNodesRemoved on all registered listeners.
    	 * The node must never be null.
    	 * @param node
    	 * @param index
    	 */
    	protected void fireRemoveEvent(Node node, int index) {
    		TreeModelEvent e = makeEvent(node, index);
    		for (TreeModelListener l : listeners) {
    			l.treeNodesRemoved(e);
    		}
    	}
     
    	/**
    	 * Creates a TreeModelEvent for the given node and index.
    	 * The node must never be null.
    	 * @param node
    	 * @param index
    	 * @return
    	 */
    	protected TreeModelEvent makeEvent(Node node, int index) {
    		return new TreeModelEvent(this, makePath(node), asArray(index), asArray(node));
    	}
     
    	/**
    	 * Creates a {@link TreePath} for the given node.
    	 * The last component in the path will be the given node.
    	 * The root of the tree will not be a part of the path.
    	 * @param node
    	 * @return
    	 */
    	protected TreePath makePath(Object node) {
    		if (node == null) {
    			throw new NullPointerException();
    		}
    		Deque<Object> pathAsStack = new LinkedList<>();
    		Object current = node;
    		while (current != null) {
    			pathAsStack.add(current);
    			current = getParent(current);
    		}
    		Object[] pathAsArray = new Object[pathAsStack.size() - 1];
    		int index = 0;
    		while (pathAsStack.size() > 1) {
    			pathAsArray[index++] = pathAsStack.pollLast();
    		}
    		return new TreePath(pathAsArray);
    	}
     
    	/**
    	 * Simple wrapper.
    	 * @param index
    	 * @return
    	 */
    	protected int[] asArray(int index) {
    		return new int[] {index};
    	}
     
    	/**
    	 * Simple wrapper.
    	 * @param index
    	 * @return
    	 */
    	protected Object[] asArray(Object obj) {
    		return new Object[] {obj};
    	}
     
    }

    package pkg;
     
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.JComponent;
    import javax.swing.JTree;
    import javax.swing.TransferHandler;
    import javax.swing.tree.TreePath;
     
    public class JTreeTransferHandler extends TransferHandler {
    	private static final long serialVersionUID = 1L;
     
    	// This flavor will be used for the transfers.
    	private final DataFlavor nodeFlavor;
     
    	public JTreeTransferHandler() {
    		// We always transfer a List of objects.
    		nodeFlavor = new DataFlavor(List.class, List.class.getSimpleName());
    	}
     
    	/*
    	 * Next three methods will handle the canImport functionality.
    	 * canImport determines whether an import can take place or is rejected.
    	 * We will treat this differently for Drag & Drop and Copy-Cut-Paste.
    	 */
     
    	public boolean canImport(TransferHandler.TransferSupport support) {
    		try {
    			// First, check for the right flavor.
    			if (!support.isDataFlavorSupported(nodeFlavor)) {
    				return false;
    			}
    			// Then, handle the special cases.
    			if (support.isDrop()) {
    				return canImportDrop(support);
    			} else {
    				return canImportPaste(support);
    			}
    		} catch (Exception e) {
    			/* 
    			 * We do this because otherwise the exception would be swallowed by swing
    			 * and we wont know what happened.
    			 */
    			e.printStackTrace();
    			throw e;
    		}
    	}
     
    	private boolean canImportDrop(TransferHandler.TransferSupport support) {
    		support.setShowDropLocation(true);
     
    		/*
    		 * Can not drop a path on itself or on a descendant of itself.
    		 * We know, that the component is a JTree.
    		 */
    		JTree tree = (JTree) support.getComponent();
     
    		JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
    		TreePath dropPath = dl.getPath();
     
    		/* 
    		 * If one of the selected paths is supposed to be dropped on 
    		 * itself or a descendant of itself, return false.
    		 */
    		TreePath[] selectedPaths = tree.getSelectionPaths();
    		for (TreePath selectedPath : selectedPaths) {
    			if (selectedPath.isDescendant(dropPath)) {
    				return false;
    			}
    		}
    		// Otherwise, return true.
    		return true;
    	}
     
    	private boolean canImportPaste(TransferHandler.TransferSupport support) {
    		/*
    		 * Can only paste nodes if tree has exactly one path selected.
    		 * Otherwise the paste location is not known...
    		 */
    		JTree tree = (JTree) support.getComponent();
    		TreePath[] selectedPaths = tree.getSelectionPaths();
    		return selectedPaths.length == 1 && selectedPaths[0] != null;
    	}
     
    	/*
    	 * Next three methods will handle the importData functionality.
    	 * importData will insert the data into our model.
    	 * We will treat this differently for Drag & Drop and Copy-Cut-Paste.
    	 */
     
    	public boolean importData(TransferHandler.TransferSupport support) {
    		try {
    			// Check if we can import.
    			if(!canImport(support)) {
    				return false;
    			}
    			// Handle the different situations.
    			if (support.isDrop()) {
    				return importDataDrop(support);
    			} else {
    				return importDataPaste(support);
    			}
    		} catch (Exception e) {
    			/* 
    			 * We do this because otherwise the exception would be swallowed by swing
    			 * and we wont know what happened.
    			 */
    			e.printStackTrace();
    			throw e;
    		}
    	}
     
    	private boolean importDataDrop(TransferHandler.TransferSupport support) {
    		/* 
    		 * When dropped the action is a MOVE command.
    		 * We must first remove the old data, and then insert the new data.
    		 */
    		List<Object> data = extractImportData(support);
     
    		/*
    		 * We know, that the component is always a JTree and the model is always a MyModel.
    		 */
    		JTree tree = (JTree) support.getComponent();
    		MyModel model = (MyModel) tree.getModel();
     
    		// Extract drop location and drop index
    		JTree.DropLocation dl = (JTree.DropLocation)support.getDropLocation();
     
    		TreePath destPath = dl.getPath();
    		Object parent = destPath.getLastPathComponent();
    		int index = dl.getChildIndex();
     
    		if (index == -1) {
    			// Drop location is on top of a node
    			index = model.getChildCount(parent);
    		}
     
    		// First remove data
    		model.removeNodes(data);
    		// Then insert data
    		model.insertNodes(parent, index, data);
     
    		return true;
    	}
     
    	private boolean importDataPaste(TransferHandler.TransferSupport support) {
    		/* 
    		 * This is either a copy & paste or a cut & paste.
    		 * If this was a copy & paste we need to clone the data!
    		 * If this was a cut & paste we can simply insert it.
    		 * 
    		 * Unfortunately, there is no good way to know...
    		 */
     
    		List<Object> data = extractImportData(support);
     
    		// no way to know... what a bummer.
    		int action = MOVE;
    		if ((action & COPY) == COPY) {
    			// When we copy, then clone the list data!
    			// somehow clone the data...
    		}
     
    		/*
    		 * We know, that the component is always a JTree and the model is always a MyModel.
    		 */
    		JTree tree = (JTree) support.getComponent();
    		MyModel model = (MyModel) tree.getModel();
     
    		// Extract drop location and drop index
    		// Drop location depends on selection
    		TreePath destPath = tree.getSelectionPath();
    		Object parent;
    		// Path can be null if nothing is selected.
    		if (destPath == null) {
    			parent = model.getRoot();
    		} else {
    			parent = destPath.getLastPathComponent();
    		}
    		int index = model.getChildCount(parent);
     
    		/* 
    		 * Inserts the new nodes into the model. 
    		 * Nodes must NOT be contained in the model at this point!
    		 */
    		model.insertNodes(parent, index, data);
     
    		return true;
    	}
     
    	/*
    	 * This method handles the removal of data if the action was a Cut.
    	 */
     
    	protected void exportDone(JComponent c, Transferable data, int action) {
    		// Only a move action needs to remove the old data.
    		if (action != MOVE) {
    			return;
    		}
    		/* 
    		 * When this is a drag & drop, do nothing.
    		 * When this was a cut, then remove the old data.
    		 */
     
    		// no way to know... what a bummer.
    		boolean isDragAndDrop = true;
    		if (!isDragAndDrop) {
    			// Extract nodes from data
    			List<Object> nodes = extractImportData(data);
     
    			// The component is always a JTree and always has a TreeModel2 as its model
    			JTree tree = (JTree) c;
    			MyModel model = (MyModel) tree.getModel();
     
    			// Remove the nodes from the model
    			// This will throw an exception if the nodes are not contained in the model!
    			model.removeNodes(nodes);
    		}
    	}
     
    	/*
    	 * Creates our Transferable as a list of all selected paths in the tree.
    	 */
     
    	protected Transferable createTransferable(JComponent c) {
    		try {
    			// Component is always a JTree
    			JTree tree = (JTree) c;
     
    			// Extract nodes to be transfered => Always the selected nodes
    			TreePath[] paths = tree.getSelectionPaths();
    			if(paths != null) {
    				List<Object> nodeList = new ArrayList<>(); 
    				for (TreePath path : paths) {
    					nodeList.add(path.getLastPathComponent());
    				}
    				return new ObjectTransferable<List<Object>>(nodeList, nodeFlavor);
    			}
    			return null;
    		} catch (Exception e) {
    			/* 
    			 * We do this because otherwise the exception would be swallowed by swing
    			 * and we wont know what happened.
    			 */
    			e.printStackTrace();
    			throw e;
    		}
    	}
     
    	public int getSourceActions(JComponent c) {
    		return COPY_OR_MOVE;
    	}
     
    	/*
    	 * Utility methods for extracting data from a transfer.
    	 */
     
    	private List<Object> extractImportData(TransferHandler.TransferSupport support) {
    		return extractImportData(support.getTransferable());
    	}
     
    	@SuppressWarnings("unchecked")
    	private List<Object> extractImportData(Transferable trans) {
    		try {
    			return (List<Object>) trans.getTransferData(nodeFlavor);
    		} catch (Exception e) {
    			// We dont need a checked exception because we wont do anything with it anyways.
    			throw new RuntimeException(e);
    		}
    	}
     
    }

    package pkg;
     
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
     
    import java.awt.BorderLayout;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
     
    import javax.swing.Action;
    import javax.swing.DropMode;
    import javax.swing.JTree;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    import javax.swing.TransferHandler;
    import javax.swing.tree.TreeSelectionModel;
     
    public class App {
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					new App();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * The number of nodes that will be randomly constructed. Must be smaller then NODE_NAMES.length.
    	 */
    	private static final int NODE_COUNT = 10;
    	/**
    	 * An array containing random names that will be used for constructing the tree.
    	 */
    	private static final String[] NODE_NAMES = new String[] {
    		"Albert",	"Annabell",	"Benjamin",	"Bella",		"Cedric",		"Cecile",
    		"David",	"Danielle",	"Emanuel",	"Elisabeth",	"Frederick",	"Felicita",
    		"Georg",	"Giselle",	"Hans",		"Henriette",	"Ismael",		"Irene",
    		"Joshua",	"Joceline",	"Kyle",		"Kaithlin",		"Lyod",			"Lisa",
    		"Michael",	"Michelle",	"Norbert",	"Nele",			"Olaf",			"Ophelia",
    		"Robert",	"Renate",	"Stuart",	"Sabrina",		"Theo",			"Tania",
    		"Ulric",	"Ursula",	"Victor",	"Veronica",		"William",		"Wilma"
    	};
     
    	/*
    	 * If the static final variables have illegal values we will throw an exception at class initialization.
    	 */
    	static {
    		if (NODE_NAMES.length < NODE_COUNT) {
    			throw new RuntimeException("Node count must be no bigger then: "+NODE_NAMES.length);
    		}
    	}
     
    	public App() {
    		// Setup the frame
    		JFrame frmTreeModelTest = new JFrame();
    		frmTreeModelTest.setTitle("JTree Transfer Handler Test");
    		frmTreeModelTest.setSize(600, 480);
    		frmTreeModelTest.setLocationRelativeTo(null);
    		frmTreeModelTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// Scroll panel for the tree
    		JScrollPane scrollPane = new JScrollPane();
    		frmTreeModelTest.getContentPane().add(scrollPane, BorderLayout.CENTER);
     
    		/*
    		 * Construct our initial nodes.
    		 * This will create a random tree which contains all kinds of names.
    		 */
    		Node rootNode = new Node("Root");
    		List<String> possibleNames = new ArrayList<>(Arrays.asList(NODE_NAMES));
    		List<Node> existingNodes = new ArrayList<>();
    		existingNodes.add(rootNode);
    		Random random = new Random();
    		for (int i = 0; i < NODE_COUNT; i++) {
    			int nameID = random.nextInt(possibleNames.size());
    			Node node = new Node(possibleNames.remove(nameID));
     
    			int parentID = random.nextInt(existingNodes.size());
    			Node parent = existingNodes.get(parentID);
     
    			parent.addChild(node);
    			existingNodes.add(node);
    		}
     
    		// The JTree that will be used for this test
    		JTree tree = new JTree();
    		tree.setModel(new MyModel(rootNode));	
    		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    		tree.setTransferHandler(new JTreeTransferHandler());
    		tree.setDragEnabled(true);
    		tree.setDropMode(DropMode.ON_OR_INSERT);
     
    		/*
    		 * This code was taken from the oracle tutorial website for Copy-Cut-Paste support.
    		 * http://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
    		 */
    		tree.getActionMap().put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    		tree.getActionMap().put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    		tree.getActionMap().put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
     
    		scrollPane.setViewportView(tree);
     
    		// Construct the menu bar with CCP functionality.
    		JMenuBar menuBar = new JMenuBar();
    		frmTreeModelTest.setJMenuBar(menuBar);
     
    		JMenu mnEdit = new JMenu("Edit");
    		menuBar.add(mnEdit);
     
    		JMenuItem mntmCopy = new JMenuItem("Copy");
    		mntmCopy.addActionListener(TransferActionListener.INSTANCE);
    		mntmCopy.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
    		mntmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
    		mnEdit.add(mntmCopy);
     
    		JMenuItem mntmCut = new JMenuItem("Cut");
    		mntmCut.addActionListener(TransferActionListener.INSTANCE);
    		mntmCut.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
    		mntmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
    		mnEdit.add(mntmCut);
     
    		JMenuItem mntmPaste = new JMenuItem("Paste");
    		mntmPaste.addActionListener(TransferActionListener.INSTANCE);
    		mntmPaste.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
    		mntmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
    		mnEdit.add(mntmPaste);
     
    		// Show the frame
    		frmTreeModelTest.setVisible(true);
    	}
     
    }


    The drag & drop works, but the Copy and Cut do not work.
    Hopefully somebody can help me with this.

Similar Threads

  1. Replies: 2
    Last Post: May 20th, 2014, 04:05 PM
  2. Problem with arrays and custom objects
    By MonkeyBeast in forum Collections and Generics
    Replies: 1
    Last Post: January 31st, 2012, 12:50 PM
  3. Cannot get TransferHandler to work in Swing
    By pbpersson in forum AWT / Java Swing
    Replies: 2
    Last Post: January 31st, 2011, 09:07 PM
  4. Problem in generation of custom classes at web service client
    By vinod.pandey in forum Member Introductions
    Replies: 0
    Last Post: January 10th, 2011, 06:12 AM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 01:25 AM