Implemented default AWT implementation of Sys.getClipboard()

This commit is contained in:
Elias Naur 2004-08-12 15:22:48 +00:00
parent 7f28edb980
commit 483f03a4ec
5 changed files with 32 additions and 8 deletions

View File

@ -266,6 +266,20 @@ public final class Sys {
* Otherwise we return a String, which may be the empty string "".
* @return a String, or null if there is no system clipboard.
*/
public static native String getClipboard();
public static String getClipboard() {
try {
java.awt.datatransfer.Clipboard clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
java.awt.datatransfer.Transferable transferable = clipboard.getContents(null);
if (transferable.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.stringFlavor)) {
return (String)transferable.getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor);
} else {
return null;
}
} catch (Exception e) {
// ignore exception and use native implementation
return nGetClipboard();
}
}
private static native String nGetClipboard();
}

View File

@ -75,8 +75,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_nAlert
JNIEXPORT void JNICALL Java_org_lwjgl_Sys_nOpenURL
(JNIEnv *, jclass, jstring);
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_getClipboard
/*
* Class: org_lwjgl_Sys
* Method: nGetClipboard
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_nGetClipboard
(JNIEnv *, jclass);
#ifdef __cplusplus

View File

@ -167,7 +167,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_nOpenURL
}
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_getClipboard
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_nGetClipboard
(JNIEnv * env, jclass clazz)
{
return NULL;

View File

@ -105,7 +105,10 @@ bool checkXError(JNIEnv *env) {
XSync(getDisplay(), False);
if (async_x_error) {
async_x_error = false;
throwException(env, error_message);
if (env != NULL)
throwException(env, error_message);
else
printfDebug(error_message);
return false;
} else
return true;
@ -132,7 +135,10 @@ Display *incDisplay(JNIEnv *env) {
XSetErrorHandler(errorHandler);
display_connection = XOpenDisplay(NULL);
if (display_connection == NULL) {
throwException(env, "Could not open X display");
if (env != NULL)
throwException(env, "Could not open X display connection");
else
printfDebug("Could not open X display connection\n");
return NULL;
}
warp_atom = XInternAtom(getDisplay(), "ignore_warp_atom", False);

View File

@ -202,7 +202,7 @@ const void * getClipboard(int type)
}
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_getClipboard
JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_nGetClipboard
(JNIEnv * env, jclass clazz)
{
// Check to see if there's text available in the clipboard