    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.dnd.DND;
    004import org.eclipse.swt.dnd.DragSource;
    005import org.eclipse.swt.dnd.DragSourceAdapter;
    006import org.eclipse.swt.dnd.DragSourceEvent;
    007import org.eclipse.swt.dnd.DragSourceListener;
    008import org.eclipse.swt.dnd.FileTransfer;
    009import org.eclipse.swt.dnd.TextTransfer;
    010import org.eclipse.swt.dnd.Transfer;
    011import org.eclipse.swt.dnd.TransferData;
    012import org.eclipse.swt.widgets.Control;
    013import org.vishia.gral.base.GetGralWidget_ifc;
    014import org.vishia.gral.base.GralWidget;
    015import org.vishia.gral.ifc.GralUserAction;
    016import org.vishia.util.KeyCode;
    017
    018public class SwtDragListener extends DragSourceAdapter
    019{
    020  /**This instance helps to check whether a Transfer can be done. */
    021  private final FileTransfer fileTransfer;
    022
    023  private final TextTransfer textTransfer;
    024
    025  SwtDragListener(int dragType, Control control){
    026    DragSource drag = new DragSource(control, DND.DROP_COPY);
    027    drag.addDragListener(this);
    028    switch(dragType){
    029      case KeyCode.dragFiles:{
    030        fileTransfer = FileTransfer.getInstance();
    031        textTransfer = TextTransfer.getInstance();
    032        Transfer[] transfers = new Transfer[2];
    033        transfers[0]= fileTransfer;
    034        transfers[1]= textTransfer;
    035        drag.setTransfer(transfers);
    036      } break;
    037      case KeyCode.dragText:{
    038        fileTransfer = FileTransfer.getInstance();
    039        textTransfer = TextTransfer.getInstance();
    040        Transfer[] transfers = new Transfer[1];
    041        transfers[0]= textTransfer;
    042        drag.setTransfer(transfers);
    043      } break;
    044      default: throw new IllegalArgumentException("unknown dragtype: "+ Integer.toHexString(dragType));
    045    }
    046
    047  }
    048
    049
    050  @Override
    051  public void dragFinished(DragSourceEvent event)
    052  {
    053    // TODO Auto-generated method stub
    054    stop();
    055    System.out.println("drag finished\n");
    056  }
    057
    058
    059  /**This method is called if the mouse button is released over the target.
    060   * The possible data types of the receiver are known therefore: The receiver may accept
    061   * a text transfer if it is a text editor, or a file transfer if it is a file browser etc.
    062   * The type of transfer can be tested with event.dataType.
    063   * @see org.eclipse.swt.dnd.DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
    064   */
    065  @Override public void dragSetData(DragSourceEvent event)
    066  {
    067    DragSource drag = (DragSource)event.getSource();
    068    Control widgetSwt = drag.getControl();
    069    //the associated text field, should be identical with event.getSource()
    070    GralWidget widgg = GralWidget.ImplAccess.gralWidgetFromImplData(widgetSwt.getData());
    071    if(widgg!=null){
    072      GralUserAction action = widgg.getActionDrag();
    073      if(action !=null){
    074        //call the action to get the data from drag
    075        //ret is a array which references the necessary String[] for answer.
    076        String[][] ret = new String[1][];
    077        boolean bOk = action.userActionGui(KeyCode.dragFiles, widgg, (Object)ret);  //Note: 1 file per variable String argument
    078        if(bOk && ret[0] !=null){
    079          String[] data = ret[0];
    080          TransferData transferData = event.dataType;
    081          if(textTransfer.isSupportedType(transferData)){
    082            //the call of fileTransfer.javaToNative(data, transferData) will be done
    083            event.data = data[0]; //transferData;
    084            event.doit = true;
    085          } else if(fileTransfer.isSupportedType(transferData)){
    086            //the call of fileTransfer.javaToNative(data, transferData) will be done
    087            data[0] = data[0].replace('/', '\\');
    088            event.data = data; //transferData;
    089            event.doit = true;
    090          } else {
    091            //the destination does not support a file transfer.
    092            event.doit = false;
    093          }
    094        } else {
    095          //action will be prevent drag, no data:
    096          event.doit = false;
    097        }
    098      } else throw new IllegalArgumentException("no action found for drop.");
    099    } else throw new IllegalArgumentException("GralWidget as getData() of swt.Control expected.");
    100
    101  }
    102
    103
    104  @Override
    105  public void dragStart(DragSourceEvent event)
    106  {
    107    // TODO Auto-generated method stub
    108    stop();
    109    event.doit = true;
    110  }
    111
    112  void stop(){}
    113
    114}