Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26ab4d8

Browse files
committed
Allow pasting text into SwingFileWidget
This restores the ability to paste text into the JTextField of SwingFileWidget. Before this change, the custom TransferHandler did not accept String values. Now we try to get a File value first, and fall back to getting the String value of the current transfer operation. I also tried to restore the full default Copy/Cut/Paste functionality by calling super.importData(support), but couldn't get it to work. So let's stay with some minimal Paste functionality at least.
1 parent d9447c6 commit 26ab4d8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingFileWidget.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ public static List<File> getFiles(
288288
}
289289
}
290290

291+
/**
292+
* Gets the String content of the current transfer support
293+
*
294+
* @param support
295+
* The paste (or drag and drop) operation from which text should
296+
* be extracted.
297+
*
298+
* @return The pasted (or dropped) text
299+
*/
300+
public static String getText(final TransferHandler.TransferSupport support) {
301+
try {
302+
return (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
303+
} catch (UnsupportedFlavorException | IOException exc) {
304+
return "";
305+
}
306+
307+
}
308+
291309
/**
292310
* Filters the given list of files according to the specified
293311
* {@link FileFilter}.
@@ -348,7 +366,15 @@ public boolean canImport(final TransferHandler.TransferSupport support) {
348366
@Override
349367
public boolean importData(TransferHandler.TransferSupport support) {
350368
final List<File> files = getFiles(support);
351-
if (files == null || files.size() != 1) return false;
369+
if (files == null) {
370+
String text = getText(support);
371+
if (text.equals(""))
372+
return false;
373+
// TODO check if text matches filter/style
374+
((JTextField) support.getComponent()).setText(text);
375+
return true;
376+
}
377+
if (files.size() != 1) return false;
352378

353379
final File file = files.get(0);
354380
((JTextField) support.getComponent()).setText(file.getAbsolutePath());

0 commit comments

Comments
 (0)