Hi Pat,

Thanks for your reply. But I would like to put across my question in a
different perspective. 

While dragging a node over other nodes, they are expected to have a blue
background and indicate they are valid drop targets. The problem what I am
facing is, not able to highlight/show blue background on nodes which are
lying beneath(valid drop targets) the node being dragged over them. 

A node is expected to be highlighted if and only when the some other node is
being dragged over it. I am able to get the TreePath of the node over which
a node is currently dragged. But I couldn't find
straight-forward/round-about methods to set that node's background to be
highlighted but not selected.

I tried my best to achieve this, but couldn't succeed. So, could you please
help me out to achieve this functionality. 

Thanks a lot.

With regards,
Arun.


-----Original Message-----
From: Patricia Keith [mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 12, 2003 11:15 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED];
[EMAIL PROTECTED]
Subject: Re: URGENT: Need help - Node Highligthing on a JTree while
dragging


Arun,

I've coded something similar under 1.3.1_07-b02 but it probably works much
the 
same way in 1.4. Probably the cursor stuff actually works according to the 
documentation. The following works for 1.3.1:

I don't know how to "ghost" the dragged object, but you'll need to set that 
image as a custom cursor when defining the DragSource by overriding both the

createDragSourceContext method of class DragSource and the
updateCurrentCursor 
method of the DragSourceContext which createDragSourceContext creates.
Typical 
code for cursors looks like this:

  dragSource = new DragSource() {
    public DragSourceContext createDragSourceContext(
        DragSourceContextPeer dscp,
        DragGestureEvent dgl,
        Cursor myCursor,
        Image img,
        Point offset,
        Transferable tr,
        DragSourceListener dsl) {
        
      customCursor = myCursor;
      DragSourceContext dsc = 
          new DragSourceContext(dscp, dgl, myCursor, img, offset, tr, dsl) {
        protected void updateCurrentCursor(int dropOperation,int
targetActions, 
            int status) {
          Cursor cursor = DragSource.DefaultMoveNoDrop; //default cursor
          switch(status) {
            case DragSourceContext.OVER:
              *if valid drop area* cursor = customCursor;
              break;
            case DragSourceContext.ENTER:
              break;
            case DragSourceContext.CHANGED:
              int result = dropOperation & targetActions;
              if (result != DnDConstants.ACTION_NONE) cursor = customCursor;
              break;
            }
            setCursor(cursor);
          }
        };
      return dsc;
    } //end of createDragSourceContext()
  };

where customCursor is a class variable. In your dragGestureRecognized method

you'll need to build the custom cursor based on the object being dragged:

public void dragGestureRecognized(DragGestureEvent event) {
  .
  . 
  .
  // This code creates a custom cursor -- it is optional:
  Cursor cursor = DragSource.DefaultMoveDrop;
  Dimension size = Toolkit.getDefaultToolkit().getBestCursorSize(8,8);
  //if size is (0,0) then this system does not support custom cursors
  if ( (size.width>1) && (size.height>1) ) {
    Image image = *whatever image you need to use*;
    Point hotspot = new Point(size.width/2, size.height/2);
    String name = *whatever name for Java Accessibility*;
    cursor =
Toolkit.getDefaultToolkit().createCustomCursor(image,hotSpot,name);
  } 
  event.startDrag(cursor, (Transferable)dragSelection, this);               
}
  
You'll need to define a cell renderer for the JTree. When a drag is in
progress, 
your dragOver method should determine which node is the potential drop
target 
from its DropTargetDragEvent event:

        Point p = event.getLocation();
        TreePath tp = getClosestPathForLocation(p.x, p.y);
        TreeNode node = tp.getLastPathComponent();
        
Make note of this node as the current potential drop target using a class 
variable, eg.
        dragTarget = node;

Then, your cell renderer should check the value Object passed into it in the

getTreeCellRendererComponent method to see if the potential drop site is a
valid 
target for whatever you're dragging (I call a private method to determine
this 
because some nodes in MyTree are not valid drop targets) and if the cell
being 
rendered matches the dragTarget you noted. If so, change the rendering as 
desired. For example, I change the cell foreground text to red to indicate 
Drop-INTO a particular node but you can do anything that is legal for a
JLabel 
which is what tree nodes actually are in rendering, eg. in pseudo-code:

        if (((TreeNode)value == dragTarget) && 
                ((MyTree)tree).isValidDropPostion()) {
          this.setForeground(Color.red);
        }
        
As I noted, the isValidDropPosition method is of your own design and may not
be 
needed if there are no restrictions on which nodes can be dropped on.

Hope this explains the basics.

Pat Keith
           
=>Delivered-To: [EMAIL PROTECTED]
=>From: Arunachalam Ramanathan <[EMAIL PROTECTED]>
=>To: [EMAIL PROTECTED], [EMAIL PROTECTED]
=>Subject: URGENT: Need help - Node Highligthing on a JTree while dragging
=>Date: Wed, 12 Mar 2003 11:14:41 +0530
=>
=>Hi All,
=>
=>I am having this requirement where I need to develop a similar interface
as
=>that of the Windows Explorer. I am facing problems in the JTree where one
of
=>the requirements say that I need to be able to highlight(select) a node
when
=>it is dragged upon although that might not be the drop target, very
similar
=>to Windows Explorer where while dragging a folder, even before dropping
the
=>folder on a target folder the drag selects all the nodes over which the
drag
=>moves. 
=>
=>Also I need to be able to get a ghost image of the item being dragged,
very
=>similar to that of Windows explorer. For your kind information, I am using
=>Java 1.4.
=>
=>If anybody out there knows how to achieve this Please help.
=>
=>With Thanks and Regards,
=>Arun.
=>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Patricia.A.Keith        And how are you spending your dash?
NASA Glenn Research Center
at Lewis Field            
Cleveland, OH             
Phone: (216) 433-5191               
Fax:   (216) 433-8000
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to