Ensure that exceptions are caught and clipboard listener is started again

MYC-Refactor
Hammy 3 years ago
parent 05dd51546f
commit 7e0ef573e7

@ -2,6 +2,7 @@ package me.goudham;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
@ -17,33 +18,33 @@ import static me.goudham.domain.Contents.IMAGE;
import static me.goudham.domain.Contents.TEXT; import static me.goudham.domain.Contents.TEXT;
class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnable, ClipboardOwner { class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnable, ClipboardOwner {
private ExecutorService executorService = Executors.newSingleThreadExecutor();
WindowsOrUnixClipboardListener() { } WindowsOrUnixClipboardListener() {}
@Override @Override
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) { public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try { try {
sleep(200); sleep(200);
} catch (InterruptedException ignored) {
}
Transferable newClipboardContents = oldClipboard.getContents(currentThread()); Transferable newClipboardContents = oldClipboard.getContents(currentThread());
processContents(oldClipboard, oldClipboardContents, newClipboardContents); processContents(oldClipboard, oldClipboardContents, newClipboardContents);
regainOwnership(oldClipboard, newClipboardContents); regainOwnership(oldClipboard, newClipboardContents);
} catch (IllegalStateException | InterruptedException err) {
err.printStackTrace();
executorService.submit(this);
}
} }
/** /**
*
*
* @param oldClipboard The clipboard that is no longer owned * @param oldClipboard The clipboard that is no longer owned
* @param oldClipboardContents The old contents of the clipboard * @param oldClipboardContents The old contents of the clipboard
* @param newClipboardContents The new contents of the clipboard * @param newClipboardContents The new contents of the clipboard
*/ */
public void processContents(Clipboard oldClipboard, Transferable oldClipboardContents, Transferable newClipboardContents) { void processContents(Clipboard oldClipboard, Transferable oldClipboardContents, Transferable newClipboardContents) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldClipboardContents); OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldClipboardContents);
if (isTextMonitored()) { if (isTextMonitored()) {
if (TEXT.isAvailable(oldClipboard)) { if (TEXT.isAvailable(oldClipboard) && !FILELIST.isAvailable(oldClipboard)) {
String stringContent = getStringContent(newClipboardContents); String stringContent = getStringContent(newClipboardContents);
getEventManager().notifyTextEvent(oldClipboardContent, stringContent); getEventManager().notifyTextEvent(oldClipboardContent, stringContent);
} }
@ -64,33 +65,48 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
} }
} }
public void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) { void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) {
try {
clipboard.setContents(newClipboardContents, this); clipboard.setContents(newClipboardContents, this);
} catch (IllegalStateException ise) {
try {
sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
} }
regainOwnership(clipboard, newClipboardContents);
void setStringContent(String stringContent) {
clipboard.setContents(new StringSelection(stringContent), this);
}
@Override
void startListening() {
execute();
}
@Override
void stopListening() {
executorService.shutdown();
executorService = Executors.newSingleThreadExecutor();
try {
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
@Override @Override
public void run() { public void run() {
try {
Transferable currentClipboardContents = clipboard.getContents(null); Transferable currentClipboardContents = clipboard.getContents(null);
processContents(clipboard, currentClipboardContents, currentClipboardContents); processContents(clipboard, currentClipboardContents, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents); regainOwnership(clipboard, currentClipboardContents);
} catch (IllegalStateException err) {
err.printStackTrace();
executorService.submit(this);
}
} }
/** /**
* Entry point for {@link WindowsOrUnixClipboardListener} * Entry point for {@link WindowsOrUnixClipboardListener}
* <p>Retrieves a thread from {@link Executors#newCachedThreadPool()} and executes code in the background</p> * <p>Retrieves a thread from {@link Executors#newSingleThreadExecutor()} and executes code in the background</p>
*/ */
@Override @Override
public void execute() { public void execute() {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(this); executorService.submit(this);
} }
} }
Loading…
Cancel
Save