[英]SWT Drag to Explorer (Windows) or 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) {
// 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 + "'");
}
}
以下是一些存在相同問題的其他人,但到目前為止似乎還沒有解決方案: 從SWT 拖到 Desktop , ..want目標路徑作為String
唯一的方法是創建一個臨時文件,然后使用FileTransfer。 無論如何,我懷疑那是您必須在本機代碼中執行的操作。 我看看是否有足夠的時間來繪制樣本...
您不會從中獲取文件位置,而是自己編寫文件。 拖動到桌面意味着一個FileTransfer(您可以檢查dragSetData支持哪種傳輸類型)。
這意味着SWT在DragSourceEvent.data中期望文件路徑的String []。 如果您在dragSetData方法中進行設置,則SWT會將這些文件復制到放置目標-例如,桌面。
@Override
public void dragSetData(DragSourceEvent e) {
System.out.println("--dragSetData--");
System.out.println("Is supported: " + FileTransfer.getInstance().isSupportedType(e.dataType));
FileTransfer f = FileTransfer.getInstance();
String[] filePaths = {"C:\\CamelOut\\4.xml" } ;
e.data = filePaths;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.