From 2aa4cf9b0ddf8bec7df9a2558e22cd4f5c427b85 Mon Sep 17 00:00:00 2001 From: Hammy Date: Thu, 12 Aug 2021 04:31:47 +0100 Subject: [PATCH] Remove 200ms delay when inserting contents & ensure file event is not notified for images --- .../WindowsOrUnixClipboardListener.java | 59 ++++++------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java b/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java index cfdcc1c..53e80fb 100644 --- a/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java +++ b/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java @@ -14,7 +14,6 @@ import me.goudham.domain.MyBufferedImage; import me.goudham.domain.TransferableFile; import me.goudham.domain.TransferableImage; -import static java.lang.Thread.currentThread; import static java.lang.Thread.sleep; import static me.goudham.Contents.FILE; import static me.goudham.Contents.IMAGE; @@ -32,7 +31,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) { try { sleep(200); - Transferable newClipboardContents = oldClipboard.getContents(currentThread()); + Transferable newClipboardContents = oldClipboard.getContents(null); processContents(oldClipboard, oldClipboardContents, newClipboardContents); regainOwnership(oldClipboard, newClipboardContents); } catch (IllegalStateException | InterruptedException exp) { @@ -69,7 +68,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab } if (isFileMonitored()) { - if (FILE.isAvailable(oldClipboard)) { + if (FILE.isAvailable(oldClipboard) && !IMAGE.isAvailable(oldClipboard)) { List fileList = clipboardUtils.getFileContent(newClipboardContents); if (!fileList.equals(clipboardContent.getFiles())) { eventManager.notifyFilesEvent(clipboardContent, fileList); @@ -108,73 +107,49 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab @Override void insert(String stringContent) { - try { - sleep(200); - } catch (InterruptedException ie) { - logger.error("Exception Thrown As Thread Cannot Sleep", ie); - } - - try { - clipboard.setContents(new StringSelection(stringContent), this); - } catch (IllegalStateException ise) { - logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise); - executorService.submit(this); - } + setContents(new StringSelection(stringContent)); } @Override void insert(Image imageContent) { - try { - sleep(200); - } catch (InterruptedException ie) { - logger.error("Exception Thrown As Thread Cannot Sleep", ie); - } - - try { - clipboard.setContents(new TransferableImage(imageContent), this); - } catch (IllegalStateException ise) { - logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise); - executorService.submit(this); - } + setContents(new TransferableImage(imageContent)); } @Override void insert(List fileContent) { - try { - sleep(200); - } catch (InterruptedException ie) { - logger.error("Exception Thrown As Thread Cannot Sleep", ie); - } - - try { - clipboard.setContents(new TransferableFile(fileContent), this); - } catch (IllegalStateException ise) { - logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise); - executorService.submit(this); - } + setContents(new TransferableFile(fileContent)); } @Override void insertAndNotify(String stringContent) { - Transferable currentClipboardContents = clipboard.getContents(this); + Transferable currentClipboardContents = clipboard.getContents(null); insert(stringContent); lostOwnership(clipboard, currentClipboardContents); } @Override void insertAndNotify(Image imageContent) { - Transferable currentClipboardContents = clipboard.getContents(this); + Transferable currentClipboardContents = clipboard.getContents(null); insert(imageContent); lostOwnership(clipboard, currentClipboardContents); } @Override void insertAndNotify(List fileContent) { - Transferable currentClipboardContents = clipboard.getContents(this); + Transferable currentClipboardContents = clipboard.getContents(null); insert(fileContent); lostOwnership(clipboard, currentClipboardContents); } + void setContents(Transferable contents) { + try { + clipboard.setContents(contents, this); + } catch (IllegalStateException ise) { + logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise); + executorService.submit(this); + } + } + @Override public void run() { try {