Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4407521

Cursor not modified during Drag and Drop

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 1.3.0
    • Fix Version/s: 1.4.2
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      mantis
    • CPU:
      x86
    • OS:
      windows_nt
    • Verification:
      Verified

      Description



      Name: boT120536 Date: 01/23/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      When initiating a DnD operation within a JTree, the JTree acts as both a
      source and a target. When the JTree rejects the drag, the cursor should change
      to the NoDrop cursor, but does not. If you hit the Shift key, the
      DropTargetListener receives the "dropActionChanged" message and the cursor is
      properly changed to the NoDrop cursor.

      Also, when the drag is rejected by the DropTargetListener, the
      DropSourceListener does NOT receive the "dragExit" message as stated in section
      2.3.3 of the "Drag and Drop Subsystem for JFC" document
      (http://java.sun.com/j2se/1.3/docs/guide/dragndrop/spec/dnd1.doc5.html).

      By the way, I tried this using JDK 1.2.2 on NT4 and encountered the same
      problems.
      //
      // ~~~~~~~~~~~~~~~~~ code sample follows ~~~~~~~~~~~~~~~~~~~~
      //

      /**
      The following test program demonstrates several cursor problems in the DnD
      system.

      Try dragging a "true" node within the tree. The drag is rejected by the
      DropTargetListener, but the cursor never changes to the NoDrop cursor!

      Also, the DragSourceListener only receives the "dragEnd" and "dragDropEnd"
      messages, never the "dragEnter" or "dragOver" messages.

      Lastly, the cursor never changes when you drag over a component (JLabel
      or JButton) with no associated DropTarget. Shouldn't the cursor change
      to the NoDrop cursor?
      */

      import java.awt.*;
      import java.awt.datatransfer.*;
      import java.awt.dnd.*;

      import javax.swing.*;
      import javax.swing.tree.*;


      public class DndTree extends JPanel
      {
         /** scrollpane to display tree */
         protected JScrollPane scrollPane = new JScrollPane();

         /** JTree displaying the server */
         protected JTree serverTree = new JTree();

         /** root node of the tree */
         protected DefaultMutableTreeNode root;

         /** tree model */
         protected DefaultTreeModel model;

         // DND related vars
         protected DragSource myDragSource;
         protected DndSourceHandler dndSource;
         protected DndTargetHandler dndTarget;
         protected DropTarget myDropTarget;

         /**
          * Constructor.
          */
         public DndTree()
         {
            try
            {
               jbInit();
            }
            catch(Exception ex)
            {
               System.out.println(ex);
            }

            root = new DefaultMutableTreeNode("root");

            // give the tree a model using root
            model = new DefaultTreeModel(root);
            serverTree.setModel(model);

            // add some child nodes
            model.insertNodeInto(new DefaultMutableTreeNode(new Boolean(true)), root,
      0);
            model.insertNodeInto(new DefaultMutableTreeNode(new Boolean(false)), root,
      0);
            model.insertNodeInto(new DefaultMutableTreeNode(new Boolean(false)), root,
      0);
            model.insertNodeInto(new DefaultMutableTreeNode(new Boolean(true)), root,
      0);

            // set other tree display properties
            serverTree.putClientProperty("JTree.lineStyle", "Angled");
            serverTree.setShowsRootHandles(true);
                  serverTree.getSelectionModel().setSelectionMode(
                                           TreeSelectionModel.SINGLE_TREE_SELECTION);

            // setup DND for dragging IElements into the Task Editor
            myDragSource = new DragSource();
            dndSource = new DndSourceHandler();
            myDragSource.createDefaultDragGestureRecognizer(serverTree,
                                               DnDConstants.ACTION_MOVE, dndSource);

            // setup DND target to receive drags within tree
            dndTarget = new DndTargetHandler();
            myDropTarget = new DropTarget(serverTree, dndTarget);

         }

         private void jbInit() throws Exception
         {
            this.setLayout(new BorderLayout());
            this.add(new JLabel("just some text", JLabel.CENTER), BorderLayout.NORTH);
            this.add(scrollPane, BorderLayout.CENTER);
            this.add(new JButton("RIGHT"), BorderLayout.EAST);
            this.add(new JButton("LEFT"), BorderLayout.WEST);
            scrollPane.getViewport().add(serverTree, null);

         }

         /**
          * Allows the JTree to act as a drag source
          */
         private class DndSourceHandler implements DragSourceListener,
      DragGestureListener
         {
            /**
             */
            public void dragGestureRecognized(DragGestureEvent dge)
            {
               TreePath path = serverTree.getSelectionPath();
               if (null == path)
                  return;

               DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                                             path.getLastPathComponent();
               Boolean b = (Boolean) node.getUserObject();
               if ( b.booleanValue() == true ) {

                  // create an object to contain our data
                  Transferable t = (Transferable) new StringSelection(b.toString());

                  // start the DnD drag operation
                  myDragSource.startDrag(dge, DragSource.DefaultLinkDrop, t,
                                         DndSourceHandler.this);
                  System.out.println("source starting drag!");
               }
            }
            public void dragEnter(DragSourceDragEvent dsde) {
               System.out.println("source dragEnter: " + dsde.getTargetActions());
            }
            public void dragExit(DragSourceEvent dse) {
               System.out.println("source dragExit: " +
                                  dse.getDragSourceContext().getCursor().toString());
            }
            public void dragDropEnd(DragSourceDropEvent dsde) {
               System.out.println("source dragDropEnd: " + dsde.getDropAction());
            }
            public void dragOver(DragSourceDragEvent dsde) {
               System.out.println("source dragOver: " + dsde.getTargetActions());
            }
            public void dropActionChanged(DragSourceDragEvent dsde) {
               System.out.println("source dropActionChanged: " +
                                  dsde.getTargetActions());
            }

         }

         /**
          * Allows JTree to act as a drop target
          */
         protected class DndTargetHandler implements DropTargetListener {
            public void dragEnter(DropTargetDragEvent dtde) {
               System.out.println("target rejecting drag!");
               dtde.rejectDrag();
               System.out.println("target dragEnter: " + dtde.getDropAction());
            }
            public void dragOver(DropTargetDragEvent dtde) {
            }
            public void drop(DropTargetDropEvent dtde) {
               // reject the drop
               System.out.println("target rejecting drop!");
               dtde.rejectDrop();
               System.out.println("target dragEnter: " + dtde.getDropAction());
            }
            public void dragExit(DropTargetEvent dte) {
               System.out.println("target dragExit: " +
      dte.getDropTargetContext().getComponent().
                                  getCursor().toString());
            }
            public void dropActionChanged(DropTargetDragEvent dtde) {
               System.out.println("target dropActionChanged: " +
                                   dtde.getDropAction());
            }
         }


         //
         //
         // TESTING ONLY !!
         //
         //
         static public void main(String[] argv) {
            // create a frame
            final JFrame frame = new JFrame("DndTree Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);
            frame.setLocation(350, 250);
            frame.setVisible(true);
            // create a dialog based on the frame
            JDialog dialog = new JDialog(frame, "DnD Tree Tester", true);
            dialog.getContentPane().setLayout(new BorderLayout());
            dialog.setSize(new Dimension(400, 350));
            dialog.setLocation(300, 200);
            dialog.setResizable(true);
            // add the tree panel to the dialog
            DndTree panel = new DndTree();
            dialog.getContentPane().add(panel, BorderLayout.CENTER);
            dialog.setVisible(true);
         }

      }
      (Review ID: 112887)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                agerasimsunw Alexander Gerasimov (Inactive)
                Reporter:
                bonealsunw Bret O'neal (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: