From e45b4b99590b15070bc97870655652af8ee023c7 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 2 Aug 2021 01:34:11 +0100 Subject: [PATCH] Add ability to stop/start listening to clipboard changes & add support for inserting contents into the clipboard --- src/main/java/me/goudham/MyClipboard.java | 39 ++++++++- .../WindowsOrUnixClipboardListener.java | 83 ++++++++++++++++--- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/goudham/MyClipboard.java b/src/main/java/me/goudham/MyClipboard.java index dbe3724..f71bd73 100644 --- a/src/main/java/me/goudham/MyClipboard.java +++ b/src/main/java/me/goudham/MyClipboard.java @@ -1,7 +1,10 @@ package me.goudham; -import me.goudham.exception.UnsupportedSystemException; +import java.awt.Image; +import java.io.File; +import java.util.List; import me.goudham.event.ClipboardEvent; +import me.goudham.exception.UnsupportedSystemException; import org.apache.commons.lang3.SystemUtils; import org.jetbrains.annotations.NotNull; @@ -20,7 +23,6 @@ public class MyClipboard { */ private MyClipboard(@NotNull ClipboardListener clipboardListener) { this.clipboardListener = clipboardListener; - this.clipboardListener.execute(); } /** @@ -44,6 +46,38 @@ public class MyClipboard { return new MyClipboard(clipboardListener); } + public void startListening() { + clipboardListener.startListening(); + } + + public void stopListening() { + clipboardListener.stopListening(); + } + + public void insert(String stringContent) { + clipboardListener.insert(stringContent); + } + + public void insert(Image imageContent) { + clipboardListener.insert(imageContent); + } + + public void insert(List fileContent) { + clipboardListener.insert(fileContent); + } + + public void insertAndNotify(String stringContent) { + clipboardListener.insertAndNotify(stringContent); + } + + public void insertAndNotify(Image imageContent) { + clipboardListener.insertAndNotify(imageContent); + } + + public void insertAndNotify(List fileContent) { + clipboardListener.insertAndNotify(fileContent); + } + /** * Adds a {@link ClipboardEvent} to the underlying {@link ClipboardListener} * @@ -64,7 +98,6 @@ public class MyClipboard { clipboardListener.getEventManager().removeEventListener(clipboardEvent); } - public void toggleTextMonitored() { clipboardListener.toggleTextMonitored(); } diff --git a/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java b/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java index 215b36c..6970d84 100644 --- a/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java +++ b/src/main/java/me/goudham/WindowsOrUnixClipboardListener.java @@ -1,5 +1,6 @@ package me.goudham; +import java.awt.Image; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.StringSelection; @@ -10,15 +11,21 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import me.goudham.domain.OldClipboardContent; +import me.goudham.domain.TransferableFileList; +import me.goudham.domain.TransferableImage; import static java.lang.Thread.currentThread; import static java.lang.Thread.sleep; +import static me.goudham.ClipboardUtils.getFileContent; +import static me.goudham.ClipboardUtils.getImageContent; +import static me.goudham.ClipboardUtils.getStringContent; import static me.goudham.domain.Contents.FILELIST; import static me.goudham.domain.Contents.IMAGE; import static me.goudham.domain.Contents.TEXT; class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnable, ClipboardOwner { private ExecutorService executorService = Executors.newSingleThreadExecutor(); + private boolean listening = false; WindowsOrUnixClipboardListener() {} @@ -66,27 +73,83 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab } void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) { - clipboard.setContents(newClipboardContents, this); - } + try { + clipboard.setContents(newClipboardContents, this); + } catch (Throwable ignored) { - void setStringContent(String stringContent) { - clipboard.setContents(new StringSelection(stringContent), this); + } } @Override void startListening() { - execute(); + if (!listening) { + listening = true; + execute(); + } } @Override void stopListening() { - executorService.shutdown(); - executorService = Executors.newSingleThreadExecutor(); + if (listening) { + executorService.shutdown(); + executorService = Executors.newSingleThreadExecutor(); + + try { + sleep(200); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + + listening = false; + } + } + + @Override + void insert(String stringContent) { try { - sleep(50); - } catch (InterruptedException e) { - e.printStackTrace(); + sleep(200); + } catch (InterruptedException ie) { + ie.printStackTrace(); } + clipboard.setContents(new StringSelection(stringContent), this); + } + + @Override + void insert(Image imageContent) { + try { + sleep(200); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + clipboard.setContents(new TransferableImage(imageContent), this); + } + + @Override + void insert(List fileContent) { + try { + sleep(200); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + clipboard.setContents(new TransferableFileList(fileContent), this); + } + + @Override + void insertAndNotify(String stringContent) { + insert(stringContent); + run(); + } + + @Override + void insertAndNotify(Image imageContent) { + insert(imageContent); + run(); + } + + @Override + void insertAndNotify(List fileContent) { + insert(fileContent); + run(); } @Override