1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
package test;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class TreeTransfertHandler extends TransferHandler {
/** Last imported tree node.
*/
private DefaultMutableTreeNode importedNode = null;
/** Creates a new instance.
*/
public TreeTransfertHandler() {
}
/** {@inheritDoc}
* <BR>As of Java 1.4.x and 5.0 this method does not work properly (see bug <A HREF="http://developer.java.sun.com/developer/bugParade/bugs/4816922.html">#4816922</A> at Sun's web site).
*/
@Override public Icon getVisualRepresentation(Transferable t) {
return super.getVisualRepresentation(t);
}
/** {@inheritDoc}
*/
@Override protected Transferable createTransferable(JComponent component) {
String entity = exportEntity(component);
// We could use a better transferable that could handle several dataflavors (text+image+treePath to node+....) at once.
// So far this one is good for this TransferHandler and to ANY (including non-Java) app that can receive the STRING_FLAVOR.
return new StringSelection(entity);
}
/** {@inheritDoc}
*/
@Override protected void exportDone(JComponent c, Transferable data, int action) {
cleanup(c, action == MOVE);
}
/////////////////////////////////
// TransferHelper overloading. //
/////////////////////////////////
/** {@inheritDoc}
*/
@Override public int getSourceActions(JComponent component) {
JTree tree = (JTree) component;
Object lastPathComponent = tree.getSelectionPath().getLastPathComponent();
if (lastPathComponent instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastPathComponent;
Object obj = node.getUserObject();
if (obj instanceof String) {
return COPY_OR_MOVE;
}
}
return NONE;
}
/** {@inheritDoc}
*/
@Override public boolean canImport(JComponent component, DataFlavor[] flavors) {
for (DataFlavor f : flavors) {
// We can only import STRING_FLAVOR (plain text) so far.
// We may add additional data flavors here in the future.
if (DataFlavor.stringFlavor.equals(f)) {
return true;
}
}
return false;
}
/** {@inheritDoc}
*/
@Override public boolean importData(JComponent component, Transferable t) {
if (canImport(component, t.getTransferDataFlavors())) {
try {
// Remember as of now, we can only deal with STRING_FLAVOR.
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
// We cheat, normally we should look into the tree for the proper node.
// We could export the tree path to the node as a custom data flavor.
// We have to test this when doing DnD with 2 different trees.
return importEntity((JTree) component, importedNode);
}
catch (UnsupportedFlavorException ufe) {
System.err.println(ufe);
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
return false;
}
/////////////////////////
// Convenient methods. //
/////////////////////////
/** Exports an entity from a component.
* @param component The source component.
* @return The entity to be exported.
*/
public String exportEntity(JComponent component) {
JTree tree = (JTree) component;
Object lastPathComponent = tree.getSelectionPath().getLastPathComponent();
if (lastPathComponent instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastPathComponent;
Object obj = node.getUserObject();
if (obj instanceof String) {
importedNode = node;
return (String) obj;
}
}
return null;
}
/** Imports a string to a component.
* @param component The target component.
* @param importedNode The node to import.
* @return <code>True</code> if import was successfull, <code>false</code> otherwise.
*/
public boolean importEntity(JComponent component, DefaultMutableTreeNode importedNode) {
JTree tree = (JTree) component;
Object lastPathComponent = tree.getSelectionPath().getLastPathComponent();
if (lastPathComponent instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode) lastPathComponent;
TreeNode rootNode = targetNode.getRoot();
Enumeration ancestorEnumeration = targetNode.pathFromAncestorEnumeration(rootNode);
// Verify we do not try to move the node to one of its sub-nodes.
while (ancestorEnumeration.hasMoreElements()) {
DefaultMutableTreeNode ancestorNode = (DefaultMutableTreeNode) ancestorEnumeration.nextElement();
if (ancestorNode.equals(importedNode)) {
System.err.println("Cannot import " + importedNode + " because " + ancestorNode + " is part of the path from " + targetNode + " to " + rootNode);
return false;
}
}
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
// Create a copy of the node structure.
DefaultMutableTreeNode nodeCopy = duplicateNodeStructure(importedNode);
model.insertNodeInto(nodeCopy, targetNode, 0);
return true;
}
return false;
}
/** Cleanup after export.
* @param component The source component.
* @param remove <CODE>True</CODE> if the selection should be removed from the component; <CODE>false</CODE> otherwise.
*/
protected void cleanup(JComponent component, boolean remove) {
JTree tree = (JTree) component;
// Remove string node from parent node.
if (remove) {
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.removeNodeFromParent(importedNode);
}
// Clean references.
importedNode = null;
}
/** Duplicate a node structure.
* @param sourceNode The source node.
* @return A copy of the given node and all its sub-nodes.
*/
private DefaultMutableTreeNode duplicateNodeStructure(DefaultMutableTreeNode sourceNode) {
if (sourceNode == null) {
return null;
}
DefaultMutableTreeNode result = new DefaultMutableTreeNode(sourceNode.getUserObject());
Enumeration childrenEnumeration = sourceNode.children();
while (childrenEnumeration.hasMoreElements()) {
DefaultMutableTreeNode child = duplicateNodeStructure((DefaultMutableTreeNode) childrenEnumeration.nextElement());
result.add(child);
}
return result;
}
} |
Partager