我想在CommandBar(工具栏)中使用Tree和drap中的drog和Drap方法
`我在第
行中有一个NullPointer异常commandBar.setTransferHandler(this);
commandBar.setDropTarget(dropTarget);
这是完整的代码:
Import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import com.jidesoft.action.CommandBar;
public class TreeTransferHandler extends TransferHandler
{
DataFlavor nodesFlavor;
DataFlavor[] flavors = new DataFlavor[1];
DefaultMutableTreeNode[] nodesToRemove;
private JTree tree;
private CommandBar[] commandBars;
public static List<ToolBarContainer> toolbarContainer = new Vector<>();
DropTarget dropTarget = new DropTarget();
public TreeTransferHandler(JTree tree, CommandBar... commandBars)
{
this.tree = tree;
this.commandBars = commandBars;
tree.setDragEnabled(true);
tree.setTransferHandler(this);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
for (CommandBar commandBar : commandBars)
{
commandBar.setTransferHandler(this);
commandBar.setDropTarget(dropTarget);
}
try
{
String mimeType = DataFlavor.javaJVMLocalObjectMimeType +
";class=\"" +
javax.swing.tree.DefaultMutableTreeNode[].class.getName() +
"\"";
nodesFlavor = new DataFlavor(mimeType);
flavors[0] = nodesFlavor;
}
catch (ClassNotFoundException e)
{
System.out.println("ClassNotFound: " + e.getMessage());
}
}
@Override
public boolean canImport(TransferHandler.TransferSupport support)
{
if (!support.isDrop())
{
return false;
}
support.setShowDropLocation(true);
if (!support.isDataFlavorSupported(nodesFlavor))
{
return false;
}
for (CommandBar commandBar : commandBars)
{
if (support.getComponent() == commandBar)
return true;
}
return false;
}
private boolean haveCompleteNode(JTree tree)
{
int[] selRows = tree.getSelectionRows();
TreePath path = tree.getPathForRow(selRows[0]);
DefaultMutableTreeNode first =
(DefaultMutableTreeNode) path.getLastPathComponent();
int childCount = first.getChildCount();
// first has children and no children are selected.
if (childCount > 0 && selRows.length == 1)
return false;
// first may have children.
for (int i = 1; i < selRows.length; i++)
{
path = tree.getPathForRow(selRows[i]);
DefaultMutableTreeNode next =
(DefaultMutableTreeNode) path.getLastPathComponent();
if (first.isNodeChild(next))
{
// Found a child of first.
if (childCount > selRows.length - 1)
{
// Not all children of first are selected.
return false;
}
}
}
return true;
}
@Override
protected Transferable createTransferable(JComponent c)
{
JTree tree = (JTree) c;
TreePath[] paths = tree.getSelectionPaths();
if (paths != null)
{
// Make up a node array of copies for transfer and
// another for/of the nodes that will be removed in
// exportDone after a successful drop.
List<DefaultMutableTreeNode> copies =
new ArrayList<>();
List<DefaultMutableTreeNode> toRemove =
new ArrayList<>();
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) paths[0].getLastPathComponent();
DefaultMutableTreeNode copy = copy(node);
copies.add(copy);
toRemove.add(node);
for (int i = 1; i < paths.length; i++)
{
DefaultMutableTreeNode next =
(DefaultMutableTreeNode) paths[i].getLastPathComponent();
// Do not allow higher level nodes to be added to list.
if (next.getLevel() < node.getLevel())
{
break;
}
else if (next.getLevel() > node.getLevel())
{ // child node
copy.add(copy(next));
// node already contains child
}
else
{ // sibling
copies.add(copy(next));
toRemove.add(next);
}
}
DefaultMutableTreeNode[] nodes =
copies.toArray(new DefaultMutableTreeNode[copies.size()]);
nodesToRemove =
toRemove.toArray(new DefaultMutableTreeNode[toRemove.size()]);
return new NodesTransferable(nodes);
}
return null;
}
/** Defensive copy used in createTransferable. */
private DefaultMutableTreeNode copy(TreeNode node)
{
DefaultMutableTreeNode n = (DefaultMutableTreeNode) node;
return (DefaultMutableTreeNode) n.clone();
}
@Override
protected void exportDone(JComponent source, Transferable data, int action)
{
if ((action & MOVE) == MOVE)
{
JTree tree = (JTree) source;
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
// Remove nodes saved in nodesToRemove in createTransferable.
for (int i = 0; i < nodesToRemove.length; i++)
{
model.removeNodeFromParent(nodesToRemove[i]);
}
}
}
@Override
public int getSourceActions(JComponent c)
{
return COPY_OR_MOVE;
}
@Override
public boolean importData(TransferHandler.TransferSupport support)
{
if (!canImport(support))
{
return false;
}
if (!(support.getComponent() instanceof CommandBar))
return false;
CommandBar destination = (CommandBar) support.getComponent();
//System.out.println(support.getComponent().getName());
// Extract transfer data.
DefaultMutableTreeNode[] nodes = null;
try
{
Transferable t = support.getTransferable();
nodes = (DefaultMutableTreeNode[]) t.getTransferData(nodesFlavor);
// Get drop location info.
for (int i = 0; i < nodes.length; i++)
{
if (nodes != null)
{
Komet3Button but = new Komet3Button(Komet3Function.getToolBarKey(i));
System.out.println(Komet3Function.getToolBarKey(i));
destination.add(but);
}
// cc.set(nodes[i])
}
Vector<Komet3Function> shownFuncs = new Vector<>();
for (int i = 0; i < nodes.length; i++)
{
// shownFuncs.add(list.getModel().getElementAt(i));
}
}
catch (UnsupportedFlavorException ufe)
{
System.out.println("UnsupportedFlavor: " + ufe.getMessage());
}
catch (java.io.IOException ioe)
{
System.out.println("I/O error: " + ioe.getMessage());
}
return true;
}
@Override
public String toString()
{
return getClass().getName();
}
public class NodesTransferable implements Transferable
{
DefaultMutableTreeNode[] nodes;
public NodesTransferable(DefaultMutableTreeNode[] nodes)
{
this.nodes = nodes;
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if (!isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flavor);
return nodes;
}
@Override
public DataFlavor[] getTransferDataFlavors()
{
return flavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return nodesFlavor.equals(flavor);
}
}
}