Drag and Drop...


#1

I’m trying to get a JGo application to accept drag+drops from external sources. My intent is to allow certain type of files to be dropped that I extract info from to create my nodes. I also don’t want to break any of the internal DnD stuff.

I’m guessing that I have create a class that extends the JGoView and overrides the onExternalDrop() method but I’m a bit confused as to how JGo handles them…

from the docs:
“… and calls the doDrop method to handle standard drops from other
JGoViews.
If doDrop returns true, it fires the JGoViewEvent.EXTERNAL_OBJECTS_DROPPED
event …”

From this it would seem that I don’t have to extend the JGoView class but just to handle this event in my viewListener(). I can’t seem to get the view to accept any external drops though… I have myView.setDragDropEnabled(true) but it doesn’t seem to do anything.

dWiGhT


#2

Demo1View.java demonstrates handling a drop from a JList, implemented by Demo1List. You just need to override the JGoView.drop and isDropFlavorAcceptable methods.
In your drop method, if the data flavor is string, which might be the data type for filenames (??? but I’m not sure about that!!!), then you can do what you want. Otherwise, call the super method to get the standard behavior for handling drag-and-drops from other JGoViews.
JGoView.drop is only called for external drops (i.e. drag-and-drops from other components/controls).


#3

Thanks for the pointers… for anyone else that will try this, here is what I did:

in my view constructor:

    :            :            :            :
// Set up types that can be accepted
Transfer[] viewTypes = new Transfer[] { JGoTransfer.getInstance(), FileTransfer.getInstance() };

    
    getDropTarget().setTransfer(viewTypes);
    
    // Enable drag and drop
    setDragDropEnabled(true);
    :            :            :            :

in the my extended JGoView class I overrode the following methods:

    :            :            :            :
  // don't allow drops onto a JGoObject,
  // but allow it to be dropped elsewhere in the view
  public int computeAcceptableDrop(DropTargetEvent e) {
      System.err.println("drop type: "+e.currentDataType);
    if (FileTransfer.getInstance().isSupportedType(e.currentDataType)) {
      Point p = toControl(new Point(e.x, e.y));
      convertViewToDoc(p);
      JGoObject obj = pickDocObject(p, true);
      if (obj != null) {
          return DND.DROP_NONE;
      }
      else {
          return DND.DROP_COPY;
      }
    } else {
      return super.computeAcceptableDrop(e);
    }
  }

// handle drops of file type (array of strings)
public void onExternalDrop(DropTargetEvent e) {
JGoDocument doc = getDocument();
doc.startTransaction();
if (FileTransfer.getInstance().isSupportedType(e.currentDataType)) { // handle Filenames
// Extract all the filenames that got dropped on here
String[] s = (String[]) e.data;
for (int i = 0; i < s.length; i++) {
System.err.println( “Filename #” + i + ": " + s );
}
fireUpdate(JGoViewEvent.EXTERNAL_OBJECTS_DROPPED, 0, null);
doc.endTransaction(“dropped text”);
} else if (doDrop(e, null)) { // handle drop of JGoSelection (i.e. JGoTransfer)
fireUpdate(JGoViewEvent.EXTERNAL_OBJECTS_DROPPED, 0, null);
doc.endTransaction(getEditPresentationName(6));
} else {
e.detail = DND.DROP_NONE;
doc.endTransaction(false);
}
}
: : : :

Now when a drop of a file(s) occur the filename(s) get printed to system.err… obviously you want to do more with it at that time. Enjoy!!!

dWiGhT