[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.gef] Re: GEF as native Drag Source

Hi,

I had the same issue as you. The problem is that GEF only allows one DragSource to be active at a time, so if you have multiple, only when DragSource will be selected. What it happens right now (and what happened to me as well) is that you DragSource acts as the default one. What I made to fix the problem was to only allow my DragSource to act under certain conditions (in my application this happens when you click and drag the mouse AND the SHIFT key is pressed). So when the SHIFT key is not pressed my DragSource does nothing and the editor's internal drag and drop works fine.

To make this happen, I made my DragListener extend the class "org.eclipse.gef.dnd.AbstractTransferDragSourceListener", and, the important place here is the method "dragStart":

public void dragStart(DragSourceEvent event) 
{
  ...

  if (!condition.isValid())
     event.doit = false;

  ...
}

So if certain condition is not valid this listener won't start the drag, and the next one (the normal GEF dnd listener) will handle the job. To detect that the SHIFT key was pressed I just added a couple of lines of code to the keyHandler of the editor, this is the method:

protected KeyHandler getCommonKeyHandler()
{
  if (sharedKeyHandler == null)
  {
    sharedKeyHandler = new KeyHandler()
    {
      public boolean keyPressed(KeyEvent event)
      {
	if (event.keyCode == SWT.SHIFT)
	{
	   ModelListTransfer.getInstance().setIsDragAllowed(true);
	   return true;
        }
	return false;
      }
				
      public boolean keyReleased(KeyEvent event)
      {
	if (event.keyCode == SWT.SHIFT)
	{
	  ModelListTransfer.getInstance().setIsDragAllowed(false);
	  return true;
        }
	return false;
      }
   };
  }
  return sharedKeyHandler;
}

ModeListTransfer (extends org.eclipse.gef.dnd.SimpleObjectTransfer) is just the class I use to transfer the objects between the drag source and the drop target; and I use it as well to store the condition if the drag is allowed or not (so if the SHIFT key is pressed or not).  

I hope this helps to solve you problem :-) !