-
Bug
-
Resolution: Fixed
-
P3
-
1.2.2, 1.3.0
-
linux-rc1
-
x86
-
linux
-
Verified
Name: skT88420 Date: 12/10/99
java version "1.2.2"
Classic VM (build 1.2.2-I, green threads, javacomp)
When using the sample code from url:
http://java.about.com/compute/java/library/weekly/aa011299.htm?REDIR_404=yes&
there seems to be a problem. When I run on NT, it seems to run good. When I
run on Linux, the drop operation never seems to occur (even when the mouse
button is released). I have to hit Escape to terminate the drag event.
Previously I ran this testcase on Linux with the Blackdown JDK1.2.2 RC3 code and
I got an occasional hang during the drag/drop (but it didn't happen regularly).
No exceptions appear to be thrown. I have consolidated the multiple java files
from the web site into 1 .java file. Below is the source. Thanks.
________________________________________________________________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.Hashtable;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.IOException;
import java.io.*;
import javax.swing.plaf.TreeUI;
import java.util.*;
import java.util.List;
public class TreeTester {
public static void main (String args[]) {
JFrame f = new JFrame("Tree Dragging Tester");
CustomCellRenderer renderer = new CustomCellRenderer();
DraggableTree tree = new DraggableTree();
tree.setModel(getDefaultTreeModel());
tree.setCellRenderer(renderer);
JScrollPane leftPane = new JScrollPane(tree);
DroppableList list = new DroppableList();
list.setCellRenderer(renderer);
JScrollPane rightPane = new JScrollPane(list);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane,
rightPane);
f.getContentPane().add (splitPane, BorderLayout.CENTER);
f.setSize (400, 300);
f.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible (true);
}
private static TreeModel getDefaultTreeModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Drag Me");
DefaultMutableTreeNode parent;
parent = new DefaultMutableTreeNode("Auctions");
root.add(parent);
parent.add(new DefaultMutableTreeNode(makeNode("eBay",
"http://www.ebay.com")));
parent.add(new DefaultMutableTreeNode(makeNode("EggHead",
"http://www.egghead.com")));
parent.add(new DefaultMutableTreeNode(makeNode("First Auction",
"http://www.firstauction.com")));
parent.add(new DefaultMutableTreeNode(makeNode("uBid",
"http://www.ubid.com")));
parent = new DefaultMutableTreeNode("Search Engines");
root.add(parent);
parent.add(new DefaultMutableTreeNode(makeNode("HotBot",
"http://www.hotbot.com")));
parent.add(new DefaultMutableTreeNode(makeNode("Infoseek",
"http://www.infoseek.com")));
parent.add(new DefaultMutableTreeNode(makeNode("Lycos",
"http://www.lycos.com")));
parent.add(new DefaultMutableTreeNode(makeNode("Yahoo",
"http://www.yahoo.com")));
parent = new DefaultMutableTreeNode("Java");
root.add(parent);
parent.add(new DefaultMutableTreeNode(makeNode("Focus on Java",
"http://java.miningco.com")));
parent.add(new DefaultMutableTreeNode(makeNode("JavaWorld",
"http://www.javaworld.com")));
parent.add(new DefaultMutableTreeNode(makeNode("Sun",
"http://java.sun.com")));
return new DefaultTreeModel(root);
}
private static Hashtable makeNode(String name, String url) {
Hashtable hashtable = new Hashtable();
hashtable.put("name", name);
hashtable.put("url", url);
return hashtable;
}
}
/********************************************************/
class DraggableTree extends JTree implements DragGestureListener {
DragSource dragSource = DragSource.getDefaultDragSource();
final static DragSourceListener dragSourceListener = new
MyDragSourceListener();
static class MyDragSourceListener implements DragSourceListener {
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
System.out.println ("MyDragSourceListener dragDropEnd");
}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
System.out.println ("MyDragSourceListener dragEnter");
}
public void dragExit(DragSourceEvent DragSourceEvent) {
System.out.println ("MyDragSourceListener dragExit");
}
public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
System.out.println ("MyDragSourceListener dragOver");
}
public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
System.out.println ("MyDragSourceListener dropActionChanged");
}
}
public DraggableTree () {
dragSource.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public DraggableTree (TreeModel model) {
super (model);
dragSource.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_COPY_OR_MOVE, this);
}
// DragGestureListener
public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
TreePath path = getSelectionPath();
System.out.println (" dragGestureRecognized");
if (path == null) {
// Nothing selected, nothing to drag
System.out.println ("Nothing selected - beep");
getToolkit().beep();
} else {
DefaultMutableTreeNode selection =
(DefaultMutableTreeNode)path.getLastPathComponent();
TransferableTreeNode node = new TransferableTreeNode(selection);
dragSource.startDrag(dragGestureEvent, DragSource.DefaultCopyDrop, node,
dragSourceListener);
}
}
}
/********************************************************/
class TransferableTreeNode extends DefaultMutableTreeNode implements
Transferable {
final static int TREE = 0;
final static int STRING = 1;
final static int PLAIN_TEXT = 2;
final public static DataFlavor DEFAULT_MUTABLE_TREENODE_FLAVOR =
new DataFlavor(DefaultMutableTreeNode.class, "Default Mutable Tree Node");
static DataFlavor flavors[] = {DEFAULT_MUTABLE_TREENODE_FLAVOR,
DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
// static DataFlavor flavors[] = {DEFAULT_MUTABLE_TREENODE_FLAVOR}; // works
fine
private DefaultMutableTreeNode data;
public TransferableTreeNode(DefaultMutableTreeNode data) {
this.data = data;
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
Object returnObject;
System.out.println ("getTransferData");
if (flavor.equals(flavors[TREE])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data;
} else {
returnObject = userObject;
}
} else if (flavor.equals(flavors[STRING])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data.toString();
} else {
returnObject = userObject.toString();
}
} else if (flavor.equals(flavors[PLAIN_TEXT])) {
Object userObject = data.getUserObject();
String string;
if (userObject == null) {
string = data.toString();
} else {
string = userObject.toString();
}
returnObject = new ByteArrayInputStream(string.getBytes());
} else {
throw new UnsupportedFlavorException(flavor);
}
return returnObject;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean returnValue = false;
System.out.println (" isDataFlavorSuppored");
for (int i=0, n=flavors.length; i<n; i++) {
if (flavor.equals(flavors[i])) {
returnValue = true;
break;
}
}
return returnValue;
}
}
/********************************************************/
class DroppableList extends JList implements DropTargetListener {
DropTarget dropTarget;
public DroppableList() {
dropTarget = new DropTarget (this, this);
setModel(new DefaultListModel());
}
public void dragEnter (DropTargetDragEvent dropTargetDragEvent) {
System.out.println("DroppableList dragEnter");
dropTargetDragEvent.acceptDrag (DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit (DropTargetEvent dropTargetEvent) {
System.out.println("DroppableList dragExit");
}
public void dragOver (DropTargetDragEvent dropTargetDragEvent) {
System.out.println("DroppableList dragOver");
}
public synchronized void drop (DropTargetDropEvent dropTargetDropEvent) {
try {
System.out.println("Drop");
Transferable tr = dropTargetDropEvent.getTransferable();
if
(tr.isDataFlavorSupported(TransferableTreeNode.DEFAULT_MUTABLE_TREENODE_FLAVOR))
{
dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
Object userObject =
tr.getTransferData(TransferableTreeNode.DEFAULT_MUTABLE_TREENODE_FLAVOR);
((DefaultListModel)getModel()).addElement(userObject);
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else if (tr.isDataFlavorSupported (DataFlavor.stringFlavor)) {
dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
String string = (String)tr.getTransferData (DataFlavor.stringFlavor);
((DefaultListModel)getModel()).addElement(string);
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else if (tr.isDataFlavorSupported (DataFlavor.plainTextFlavor)) {
dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
Object stream = tr.getTransferData(DataFlavor.plainTextFlavor);
if (stream instanceof InputStream) {
InputStreamReader isr = new InputStreamReader((InputStream)stream);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).addElement(line);
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else if (stream instanceof Reader) {
BufferedReader reader = new BufferedReader((Reader)stream);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).addElement(line);
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else {
System.err.println ("Unknown type: " + stream.getClass());
dropTargetDropEvent.rejectDrop();
}
} else if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor)) {
dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
List fileList = (List)tr.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext()) {
File file = (File)iterator.next();
Hashtable hashtable = new Hashtable();
hashtable.put("name", file.getName());
hashtable.put("url", file.toURL().toString());
((DefaultListModel)getModel()).addElement(hashtable);
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else {
System.err.println ("Rejected");
dropTargetDropEvent.rejectDrop();
}
} catch (IOException io) {
io.printStackTrace();
dropTargetDropEvent.rejectDrop();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
public void dropActionChanged (DropTargetDragEvent dropTargetDragEvent) {
}
}
/********************************************************/
class CustomCellRenderer implements ListCellRenderer, TreeCellRenderer {
DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
DefaultTreeCellRenderer treeCellRenderer = new DefaultTreeCellRenderer();
public Component getListCellRendererComponent(JList list, Object value, int
index, boolean selected, boolean hasFocus) {
listCellRenderer.getListCellRendererComponent(list, value, index, selected,
hasFocus);
listCellRenderer.setText(getValueString(value));
System.out.println("getListCellRendererComponent");
return listCellRenderer;
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
treeCellRenderer.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row, hasFocus);
System.out.println("getTreeCellRenderer");
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
value = node.getUserObject();
}
treeCellRenderer.setText(getValueString(value));
return treeCellRenderer;
}
private String getValueString(Object value) {
System.out.println("getValueString");
String returnString = "null";
if (value != null) {
if (value instanceof Hashtable) {
Hashtable h = (Hashtable)value;
String name = (String)h.get("name");
String url = (String)h.get("url");
returnString = name + " ==> " + url;
} else {
returnString = "X: " + value.toString();
}
}
return returnString;
}
}
(Review ID: 98831)
======================================================================
- duplicates
-
JDK-4342212 Linux: Tree node is dragged incorrectly
-
- Closed
-