SWT拖动到资源管理器(Windows)或Finder(OS X) [英] SWT Drag to Explorer (Windows) or Finder (OS X)

查看:119
本文介绍了SWT拖动到资源管理器(Windows)或Finder(OS X)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SWT应用程序与一大堆图形元素。我希望用户能够将元素拖到桌面/ Windows资源管理器/ OS X Finder中。当它们删除元素时,我需要将它们放入的路径,以便我可以在该位置创建代表元素的文件。



我不认为我可以使用 FileTransfer ,因为没有源文件。有一个源对象可以创建一个文件,但只有一旦它知道放在哪里。



下面是一个简单的例子,我正在尝试实现,有一个带有标签拖动的文本框。如果用户拖动到某个文件夹或文件,我想获取它们拖到的路径。如果他们拖到一个文件,我想用文本框中的任何内容替换该文件的内容。如果他们拖动到一个文件夹,我想创建一个名为TestFile的文件,其中包含文本框中的任何内容。

  import org.eclipse.swt.SWT; 
import org.eclipse.swt.dnd。*;
import org.eclipse.swt.layout。*;
import org.eclipse.swt.widgets。*;

public class DesktopDragExample {

public static void main(String [] args){
//将SWT主循环放在一起
final显示显示= Display.getDefault();
display.syncExec(new Runnable(){
@Override
public void run(){
Shell shell = new Shell(display,SWT.SHELL_TRIM);
initializeGui(shell);

//打开shell
shell.open();

//运行事件循环
while(!shell .isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
} );
}

//创建gui
private static void initializeGui(Composite parent){
GridLayout layout = new GridLayout(2,false);
parent.setLayout(layout);

//指示标签
标签infoLbl =新标签(父,SWT.WRAP);
GridData gd = new GridData();
gd.grabExcessHorizo​​ntalSpace = true;
gd.horizo​​ntalAlignment = SWT.FILL;
gd.horizo​​ntalSpan = 2;
infoLbl.setLayoutData(gd);
infoLbl.setText(
你应该可以拖动到桌面,Windows资源管理器或OS X Finder.\
如果拖动到一个文件,它将用文本框的内容替换该文件的内容。\
如果拖动到一个文件夹,它将创建一个名为TestFile的文件,其内容是文本框中的任何内容。 );

//使文本元素
final text text = new Text(parent,SWT.SINGLE | SWT.BORDER);
gd = new GridData();
gd.grabExcessHorizo​​ntalSpace = true;
gd.horizo​​ntalAlignment = SWT.FILL;
text.setLayoutData(gd);

//使标签元素
Label label = new Label(parent,SWT.NONE);
label.setText(拖我);

//拖动器的监听器
DragSourceListener dragListener = new DragSourceListener(){
@Override
public void dragStart(DragSourceEvent e){
e.detail = DND.DROP_COPY;
}

@Override
public void dragFinished(DragSourceEvent e){
System.out.println( - dragFinished--);
System.out.println(e.data =+ e.data);
}

@Override
public void dragSetData(DragSourceEvent e){
System.out.println( - dragSetData--);
System.out.println(e.data =+ e.data);
}
};


// DragSource
DragSource dragSource = new DragSource(label,DND.DROP_COPY);
dragSource.setTransfer(new Transfer [] {FileTransfer.getInstance()});
dragSource.addDragListener(dragListener);
}

private static void draggedTo(String path,String textBoxContents){
System.out.println(Draged the contents+ textBoxContents +'to'+ path +');
}

}

这里有一些其他人同样的问题,但是至今没有解决方案:
从SWT拖到桌面 ..想要目的地路径为String

解决方案

做它是通过创建一个临时文件,然后使用FileTransfer。我怀疑这是你本来必须做的本地代码。我会看看我有足够的时间来绘制样品...


I've got an SWT application with a bunch of graphical elements. I'd like for the user to be able to drag an element to their Desktop / Windows Explorer / OS X Finder. When they drop the element, I need the path that they dropped it to, so that I can create a file in that location which represents the element.

I don't think I can use a FileTransfer, because there is no source file. There is a source object which can create a file, but only once it knows where to put it.

Inlined below is a simple example of what I'm trying to achieve, there is a text box with a label to drag from. If the user drags to some folder or file, I'd like to get the path that they dragged to. If they dragged to a file, I'd like to replace the contents of that file with whatever is in the text box. If they dragged to a folder, I'd like to create a file called "TestFile" with the contents of whatever is in the text box.

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class DesktopDragExample {

public static void main(String[] args) {
    // put together the SWT main loop
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {
        @Override
        public void run() {
            Shell shell = new Shell(display, SWT.SHELL_TRIM);
            initializeGui(shell);

            //open the shell
            shell.open();

            //run the event loop
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }
    });
}

// create the gui
private static void initializeGui(Composite parent) {
    GridLayout layout = new GridLayout(2, false);
    parent.setLayout(layout);

    // make the instructions label
    Label infoLbl = new Label(parent, SWT.WRAP);
    GridData gd = new GridData();
    gd.grabExcessHorizontalSpace = true;
    gd.horizontalAlignment = SWT.FILL;
    gd.horizontalSpan = 2;
    infoLbl.setLayoutData(gd);
    infoLbl.setText(
            "You should be able to drag to the desktop, Windows Explorer, or OS X Finder.\n" +
            "If you drag to a file, it will replace the contents of that file with the contents of the text box.\n" +
            "If you drag to a folder, it will create a file named 'TestFile' whose contents are whatever is in the text box.");

    // make the text element
    final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
    gd = new GridData();
    gd.grabExcessHorizontalSpace = true;
    gd.horizontalAlignment = SWT.FILL;
    text.setLayoutData(gd);

    // make the label element
    Label label = new Label(parent, SWT.NONE);
    label.setText("Drag me");

    // listener for drags
    DragSourceListener dragListener = new DragSourceListener() {
        @Override
        public void dragStart(DragSourceEvent e) {
            e.detail = DND.DROP_COPY;
        }

        @Override
        public void dragFinished(DragSourceEvent e) {
            System.out.println("--dragFinished--");
            System.out.println("e.data=" + e.data);
        }

        @Override
        public void dragSetData(DragSourceEvent e) {
            System.out.println("--dragSetData--");
            System.out.println("e.data=" + e.data);
        }
    };


    // the DragSource
    DragSource dragSource = new DragSource(label, DND.DROP_COPY);
    dragSource.setTransfer(new Transfer[]{FileTransfer.getInstance()});
    dragSource.addDragListener(dragListener);
}

private static void draggedTo(String path, String textBoxContents) {
    System.out.println("Dragged the contents '" + textBoxContents + "' to '" + path + "'");
}

}

Here are some other people with the same problem, but looks like no solution so far: Drag from SWT to Desktop, ..want destination path as String

解决方案

The only way to do it is by creating a temporary file and then using the FileTransfer. I suspect that's what you'd have to do in native code anyways. I'll see if I have enough time to sketch the sample...

这篇关于SWT拖动到资源管理器(Windows)或Finder(OS X)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆