    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.dnd.DND;
    004import org.eclipse.swt.dnd.DropTarget;
    005import org.eclipse.swt.dnd.DropTargetEvent;
    006import org.eclipse.swt.dnd.DropTargetListener;
    007import org.eclipse.swt.dnd.FileTransfer;
    008import org.eclipse.swt.dnd.TextTransfer;
    009import org.eclipse.swt.dnd.Transfer;
    010import org.eclipse.swt.dnd.TransferData;
    011import org.eclipse.swt.widgets.Control;
    012import org.vishia.gral.base.GralWidget;
    013import org.vishia.gral.ifc.GralUserAction;
    014import org.vishia.util.KeyCode;
    015
    016public class SwtDropListener implements DropTargetListener
    017{
    018  /**A special from org.eclipse.swt.dnd.FileTransfer.
    019   * Depending on droptype in ctor either fileTransfer or {@link #textTransfer} is set. */
    020  private final FileTransfer fileTransfer;
    021
    022  /**A special from org.eclipse.swt.dnd.TextTransfer.
    023   * Depending on droptype in ctor either {@link #fileTransfer} or textTransfer is set. */
    024  private final TextTransfer textTransfer;
    025
    026
    027  SwtDropListener(int dropType, Control control){
    028    DropTarget drop = new DropTarget(control, DND.DROP_COPY);
    029    drop.addDropListener(this);
    030    switch(dropType){
    031      case KeyCode.dropFiles:{
    032        textTransfer = null;
    033        fileTransfer = FileTransfer.getInstance();
    034        Transfer[] transfers = new Transfer[1];
    035        transfers[0]= fileTransfer;
    036        drop.setTransfer(transfers);
    037      } break;
    038      case KeyCode.dropText: {
    039        fileTransfer = null;
    040        textTransfer = TextTransfer.getInstance();
    041        Transfer[] transfers = new Transfer[1];
    042        transfers[0]= textTransfer;
    043        drop.setTransfer(transfers);
    044      } break;
    045      default: throw new IllegalArgumentException("unknown droptype: "+ Integer.toHexString(dropType));
    046    }
    047
    048  }
    049
    050
    051  @Override
    052  public void dragEnter(DropTargetEvent event)
    053  {
    054    TransferData data = event.currentDataType;
    055    if(fileTransfer !=null && fileTransfer.isSupportedType(data)){
    056      event.detail = DND.DROP_COPY;
    057    } else if(textTransfer !=null && textTransfer.isSupportedType(data)){
    058      event.detail = DND.DROP_COPY;
    059    } else {
    060      event.detail = DND.DROP_NONE;
    061    }
    062  }
    063
    064  @Override
    065  public void dragLeave(DropTargetEvent event)
    066  {
    067    // TODO Auto-generated method stub
    068    stop();
    069
    070  }
    071
    072  @Override
    073  public void dragOperationChanged(DropTargetEvent event)
    074  {
    075    // TODO Auto-generated method stub
    076    stop();
    077
    078  }
    079
    080  @Override
    081  public void dragOver(DropTargetEvent event)
    082  {
    083    // TODO Auto-generated method stub
    084    stop();
    085
    086  }
    087
    088  @Override
    089  public void drop(DropTargetEvent event)
    090  { DropTarget drop = (DropTarget)event.getSource();
    091    Control widgetSwt = drop.getControl();
    092    Object oData = widgetSwt.getData();  //the associated text field, should be identical with event.getSource()
    093    if(oData!=null && oData instanceof SwtTextFieldWrapper){
    094      SwtTextFieldWrapper swtgral = (SwtTextFieldWrapper) oData;
    095
    096      GralWidget widgg = swtgral.widgg; //getGralWidget();
    097      GralUserAction action = widgg.getActionDrop();
    098      if(action !=null){
    099        Object gralTransferData;
    100        TransferData data = event.currentDataType;
    101        if(fileTransfer !=null && fileTransfer.isSupportedType(data)) { //a file is droped to a file Transfer field
    102          Object oDropData = fileTransfer.nativeToJava(data);
    103          String[] files = (String[])oDropData;
    104            //call the action to apply the data from drop:
    105          for(int ix = 0; ix < files.length; ++ix){
    106            files[ix] = files[ix].replace('\\', '/');  //at user level: use slash only, on windows too!
    107          }
    108          widgg.setText(files[0]);  //write the file path
    109          action.userActionGui(KeyCode.dropFiles, widgg, (Object[])files);  //Note: 1 file per variable String argument
    110        }
    111        else if(textTransfer !=null && textTransfer.isSupportedType(data)){
    112          Object oDropData = textTransfer.nativeToJava(data);
    113          action.userActionGui(KeyCode.dropText, widgg, oDropData);  //Note: 1 file per variable String argument
    114        }
    115
    116      } else throw new IllegalArgumentException("no action found for drop.");
    117    } else throw new IllegalArgumentException("SwtTextFieldWrapper as getData() of swt.Control expected.");
    118  }
    119
    120  @Override
    121  public void dropAccept(DropTargetEvent event)
    122  {
    123    // TODO Auto-generated method stub
    124    stop();
    125
    126  }
    127
    128
    129
    130  void stop(){}
    131
    132}