使用图像(DND)在SWT标签上删除文件

时间:2021-03-05 22:46:20

Problem description:

The user should be able to drag an Image-File from his computer to a RCP Application. The drop-target is a SWT-Label which is generated through the Eclipse FormToolkit. (Eclipse Forms)

用户应该能够将Image-File从他的计算机拖到RCP应用程序。 drop-target是一个SWT-Label,它是通过Eclipse FormToolkit生成的。 (Eclipse Forms)

With the following code, the user is able to drag Image-Files as well as Images from a Browser and drop them on the label (works well). The problem occurs, when the label shows a image:

使用以下代码,用户可以从浏览器拖动图像文件和图像,并将它们放在标签上(效果很好)。当标签显示图像时,会出现问题:

lblImage.setImage()

In my example, I set the image of the label, after the user dropped a file. As a consequence, subsequent drags are no longer registered. (dragEnter method is no longer invoked)

在我的示例中,我在用户删除文件后设置标签的图像。因此,后续拖动不再注册。 (不再调用dragEnter方法)

/** create label **/
Label lblImage = fFormToolkit.createLabel(fForm.getBody(), "");
GridData gd = new GridData();
gd.widthHint = 200;
gd.heightHint = 200;
lblImage.setLayoutData(gd);

/** drag drop support **/
int ops = DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT;
final FileTransfer fTransfer = FileTransfer.getInstance();
final ImageTransfer iTransfer = ImageTransfer.getInstance();
Transfer[] transfers = new Transfer[] { fTransfer, iTransfer };
DropTarget target = new DropTarget(fLblArtWork, ops);
target.setTransfer(transfers);

target.addDropListener(new DropTargetAdapter() {

 @Override
 public void drop(DropTargetEvent event) {
  if (event.data instanceof String[]) {
    String[] filenames = (String[]) event.data;
    if (filenames.length > 0){
       Image i = new Image(Display.getCurrent(), filepath);
       lblImage.setImage(i);
    }
  } else if (event.data instanceof ImageData) {
      Image i = new Image(Display.getCurrent(), data);
      lblImage.setImage(i);
  }
 }

 public void dragEnter(DropTargetEvent event) {
   System.out.println("drag enter");
   event.detail = DND.DROP_COPY;
 }
});

Question: How do I register dragEnter Events on a SWT Label that shows an Image?

问题:如何在显示图像的SWT标签上注册dragEnter事件?

Thanks

3 个解决方案

#1


0  

In your example there were some problems that caused this not to compile for me. After I fixed the issues I was able to drag png files onto the component and each successive drop changed the image correctly.

在你的例子中有一些问题导致我不能为我编译。在我修复了问题后,我能够将png文件拖到组件上,每次连续的拖放都会正确地改变图像。

Here are the changes:

以下是更改:

Original

DropTarget target = new DropTarget(fLblArtWork, ops);

became:

DropTarget target = new DropTarget(lblImage, ops);

Original

Image i = new Image(Display.getCurrent(), filepath);

became:

Image i = new Image(Display.getCurrent(), filenames[0]);

Original

Image i = new Image(Display.getCurrent(), data);

became

Image i = new Image(Display.getCurrent(), (ImageData) event.data);

I also create my label the following way:

我还通过以下方式创建我的标签:

final Label lblImage = new Label(shell, SWT.NONE);

but that shouldn't make a difference.

但这不应该有所作为。

#2


0  

I used SashForm here to set an background image from the local system. AS per your requirement I have done the text and label also but I didn't set. You can set it by the labelobject.setImage(image);

我在这里使用SashForm从本地系统设置背景图像。根据您的要求,我也完成了文本和标签,但我没有设置。你可以通过labelobject.setImage(image)来设置它;

    final SashForm sashForm = new SashForm(composite, SWT.BORDER);
    sashForm.setBounds(136, 10, 413, 237);

    final Label lblHello = new Label(composite, SWT.NONE);
    DragSource dragSource = new DragSource(lblHello, DND.DROP_NONE);

    ImageTransfer imgTrans=ImageTransfer.getInstance();
     FileTransfer fileTrans=FileTransfer.getInstance();
    Transfer[] transfer=new Transfer[] { fileTrans,imgTrans,TextTransfer.getInstance() };
    DropTarget dropTarget = new DropTarget(sashForm, DND.DROP_NONE);
    dropTarget.setTransfer(transfer);
    dragSource.setTransfer(transfer);

    lblHello.setBounds(27, 219, 55, 15);
    lblHello.setText("Hello");


    dragSource.addDragListener(new DragSourceAdapter() {
        @Override
        public void dragStart(DragSourceEvent event) {
            event.doit=true;

        }
    });


    //Drop Event
    dropTarget.addDropListener(new DropTargetAdapter() {
        @Override
        public void drop(DropTargetEvent event) {
            System.out.println(event.detail);
            //String path = System.getProperty("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
            Image image=new Image(display, "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");

            sashForm.setBackgroundImage(image);
        }
    });

#3


0  

Easy Way : Drop File on SWT Label with Image (DND)

简单方法:使用图像(DND)在SWT标签上删除文件

The drop event occurs when the user releases the mouse over the Drop target.

当用户将鼠标放在Drop目标上时,会发生drop事件。

final CLabel lblNewLabel = new CLabel(parent, SWT.BORDER);
lblNewLabel.setBounds(10, 43, 326, 241);
lblNewLabel.setText("Drop Target");

// Allow data to be copied or moved to the drop target
DropTarget dropTarget = new DropTarget(lblNewLabel, DND.DROP_MOVE| DND.DROP_COPY | DND.DROP_DEFAULT);

// Receive data in Text or File format
final TextTransfer textTransfer = TextTransfer.getInstance();
final FileTransfer fileTransfer = FileTransfer.getInstance();
Transfer[] types = new Transfer[] {fileTransfer, textTransfer};
dropTarget.setTransfer(types);

// DropTargetEvent
dropTarget.addDropListener(new DropTargetAdapter() {

    @Override
    public void drop(DropTargetEvent event) {

        if (textTransfer.isSupportedType(event.currentDataType)) {
            String text = (String)event.data;
            lblNewLabel.setText(text);
        }
        if (fileTransfer.isSupportedType(event.currentDataType)){

            //clear Label Text
            lblNewLabel.setText("");

            //list out selected file
            String[] files = (String[])event.data;
            for (int i = 0; i < files.length; i++) {

                String[] split = files[i].split("\\.");
                String ext = split[split.length - 1];

                // Set Images format "jpg" and "png"
                if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("png"))
                {
                    lblNewLabel.setImage(SWTResourceManager.getImage(files[i]));    
                }
                else
                {
                    lblNewLabel.setText(files[i]);
                }
            }//end for loop
        }
    }//End drop()
});//End addDropListener

#1


0  

In your example there were some problems that caused this not to compile for me. After I fixed the issues I was able to drag png files onto the component and each successive drop changed the image correctly.

在你的例子中有一些问题导致我不能为我编译。在我修复了问题后,我能够将png文件拖到组件上,每次连续的拖放都会正确地改变图像。

Here are the changes:

以下是更改:

Original

DropTarget target = new DropTarget(fLblArtWork, ops);

became:

DropTarget target = new DropTarget(lblImage, ops);

Original

Image i = new Image(Display.getCurrent(), filepath);

became:

Image i = new Image(Display.getCurrent(), filenames[0]);

Original

Image i = new Image(Display.getCurrent(), data);

became

Image i = new Image(Display.getCurrent(), (ImageData) event.data);

I also create my label the following way:

我还通过以下方式创建我的标签:

final Label lblImage = new Label(shell, SWT.NONE);

but that shouldn't make a difference.

但这不应该有所作为。

#2


0  

I used SashForm here to set an background image from the local system. AS per your requirement I have done the text and label also but I didn't set. You can set it by the labelobject.setImage(image);

我在这里使用SashForm从本地系统设置背景图像。根据您的要求,我也完成了文本和标签,但我没有设置。你可以通过labelobject.setImage(image)来设置它;

    final SashForm sashForm = new SashForm(composite, SWT.BORDER);
    sashForm.setBounds(136, 10, 413, 237);

    final Label lblHello = new Label(composite, SWT.NONE);
    DragSource dragSource = new DragSource(lblHello, DND.DROP_NONE);

    ImageTransfer imgTrans=ImageTransfer.getInstance();
     FileTransfer fileTrans=FileTransfer.getInstance();
    Transfer[] transfer=new Transfer[] { fileTrans,imgTrans,TextTransfer.getInstance() };
    DropTarget dropTarget = new DropTarget(sashForm, DND.DROP_NONE);
    dropTarget.setTransfer(transfer);
    dragSource.setTransfer(transfer);

    lblHello.setBounds(27, 219, 55, 15);
    lblHello.setText("Hello");


    dragSource.addDragListener(new DragSourceAdapter() {
        @Override
        public void dragStart(DragSourceEvent event) {
            event.doit=true;

        }
    });


    //Drop Event
    dropTarget.addDropListener(new DropTargetAdapter() {
        @Override
        public void drop(DropTargetEvent event) {
            System.out.println(event.detail);
            //String path = System.getProperty("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
            Image image=new Image(display, "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");

            sashForm.setBackgroundImage(image);
        }
    });

#3


0  

Easy Way : Drop File on SWT Label with Image (DND)

简单方法:使用图像(DND)在SWT标签上删除文件

The drop event occurs when the user releases the mouse over the Drop target.

当用户将鼠标放在Drop目标上时,会发生drop事件。

final CLabel lblNewLabel = new CLabel(parent, SWT.BORDER);
lblNewLabel.setBounds(10, 43, 326, 241);
lblNewLabel.setText("Drop Target");

// Allow data to be copied or moved to the drop target
DropTarget dropTarget = new DropTarget(lblNewLabel, DND.DROP_MOVE| DND.DROP_COPY | DND.DROP_DEFAULT);

// Receive data in Text or File format
final TextTransfer textTransfer = TextTransfer.getInstance();
final FileTransfer fileTransfer = FileTransfer.getInstance();
Transfer[] types = new Transfer[] {fileTransfer, textTransfer};
dropTarget.setTransfer(types);

// DropTargetEvent
dropTarget.addDropListener(new DropTargetAdapter() {

    @Override
    public void drop(DropTargetEvent event) {

        if (textTransfer.isSupportedType(event.currentDataType)) {
            String text = (String)event.data;
            lblNewLabel.setText(text);
        }
        if (fileTransfer.isSupportedType(event.currentDataType)){

            //clear Label Text
            lblNewLabel.setText("");

            //list out selected file
            String[] files = (String[])event.data;
            for (int i = 0; i < files.length; i++) {

                String[] split = files[i].split("\\.");
                String ext = split[split.length - 1];

                // Set Images format "jpg" and "png"
                if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("png"))
                {
                    lblNewLabel.setImage(SWTResourceManager.getImage(files[i]));    
                }
                else
                {
                    lblNewLabel.setText(files[i]);
                }
            }//end for loop
        }
    }//End drop()
});//End addDropListener