-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1934 from usethesource/clipboard
added default clipboard utility (ported from Eclipse environment), which should work on any non-headless JVM environment
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.rascalmpl.library.util; | ||
|
||
import java.awt.datatransfer.DataFlavor; | ||
import java.awt.datatransfer.StringSelection; | ||
import java.awt.datatransfer.UnsupportedFlavorException; | ||
import java.io.IOException; | ||
import org.rascalmpl.debug.IRascalMonitor; | ||
import io.usethesource.vallang.IString; | ||
import io.usethesource.vallang.IValueFactory; | ||
|
||
public class Clipboard { | ||
private final IValueFactory vf; | ||
private final java.awt.datatransfer.Clipboard cp; | ||
private IRascalMonitor monitor; | ||
|
||
public Clipboard(IValueFactory vf, IRascalMonitor monitor) { | ||
this.vf = vf; | ||
this.cp = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); | ||
this.monitor = monitor; | ||
} | ||
|
||
public void copy(IString arg) { | ||
var selection = new StringSelection(arg.getValue()); | ||
cp.setContents(selection, selection); | ||
} | ||
|
||
public IString paste() { | ||
try { | ||
if (cp.isDataFlavorAvailable(DataFlavor.stringFlavor)) { | ||
return vf.string(cp.getData(DataFlavor.stringFlavor).toString()); | ||
} | ||
} | ||
catch (UnsupportedFlavorException | IOException e) { | ||
monitor.warning("Clipboard::paste failed", null); | ||
} | ||
|
||
return vf.string(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
@synopsis{Programmatic access to the native system clipboard (copy and paste), with some handy (de)-escaping features.} | ||
module util::Clipboard | ||
|
||
@javaClass{org.rascalmpl.library.util.Clipboard} | ||
@synopsis{If there is textual content in the current clipboard, then return it as a string.} | ||
@description{ | ||
This will transfer any plaintext content that is currently on the system clipboard, no matter | ||
the source to a string value. | ||
|
||
If the system's clipboard is not accessible (say we are running in a headless environment), | ||
then paste always returns the empty string. | ||
} | ||
@pitfalls{ | ||
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds. | ||
} | ||
@benefits{ | ||
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL. | ||
* paste encodes and escapes all kinds of wild characters automatically. | ||
} | ||
java str paste(); | ||
|
||
@javaClass{org.rascalmpl.library.util.Clipboard} | ||
@synopsis{Load the contents of a string value into the system clipboard.} | ||
@description{ | ||
This will put the contents of the string value in into the system clipboard. | ||
So this is not the string value with quotes and escapes, but the pure string | ||
data. | ||
|
||
If the system's clipboard is not accessible (say we are running in a headless environment), | ||
then copy always has no effect. | ||
} | ||
@pitfalls{ | ||
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds. | ||
} | ||
@benefits{ | ||
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL. | ||
* copy transfers the pure content of the string, no quotes or escapes. | ||
} | ||
java void copy(str content); |