Add ability to stop/start listening to clipboard changes & add support for inserting contents into the clipboard

MYC-Refactor
Hammy 3 years ago
parent a59dafd8fa
commit e45b4b9959

@ -1,7 +1,10 @@
package me.goudham; 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.event.ClipboardEvent;
import me.goudham.exception.UnsupportedSystemException;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -20,7 +23,6 @@ public class MyClipboard {
*/ */
private MyClipboard(@NotNull ClipboardListener clipboardListener) { private MyClipboard(@NotNull ClipboardListener clipboardListener) {
this.clipboardListener = clipboardListener; this.clipboardListener = clipboardListener;
this.clipboardListener.execute();
} }
/** /**
@ -44,6 +46,38 @@ public class MyClipboard {
return new MyClipboard(clipboardListener); 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<File> fileContent) {
clipboardListener.insert(fileContent);
}
public void insertAndNotify(String stringContent) {
clipboardListener.insertAndNotify(stringContent);
}
public void insertAndNotify(Image imageContent) {
clipboardListener.insertAndNotify(imageContent);
}
public void insertAndNotify(List<File> fileContent) {
clipboardListener.insertAndNotify(fileContent);
}
/** /**
* Adds a {@link ClipboardEvent} to the underlying {@link ClipboardListener} * Adds a {@link ClipboardEvent} to the underlying {@link ClipboardListener}
* *
@ -64,7 +98,6 @@ public class MyClipboard {
clipboardListener.getEventManager().removeEventListener(clipboardEvent); clipboardListener.getEventManager().removeEventListener(clipboardEvent);
} }
public void toggleTextMonitored() { public void toggleTextMonitored() {
clipboardListener.toggleTextMonitored(); clipboardListener.toggleTextMonitored();
} }

@ -1,5 +1,6 @@
package me.goudham; package me.goudham;
import java.awt.Image;
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.StringSelection;
@ -10,15 +11,21 @@ import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import me.goudham.domain.OldClipboardContent; 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.currentThread;
import static java.lang.Thread.sleep; 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.FILELIST;
import static me.goudham.domain.Contents.IMAGE; 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(); private ExecutorService executorService = Executors.newSingleThreadExecutor();
private boolean listening = false;
WindowsOrUnixClipboardListener() {} WindowsOrUnixClipboardListener() {}
@ -66,27 +73,83 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
} }
void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) { void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) {
try {
clipboard.setContents(newClipboardContents, this); clipboard.setContents(newClipboardContents, this);
} } catch (Throwable ignored) {
void setStringContent(String stringContent) { }
clipboard.setContents(new StringSelection(stringContent), this);
} }
@Override @Override
void startListening() { void startListening() {
if (!listening) {
listening = true;
execute(); execute();
} }
}
@Override @Override
void stopListening() { void stopListening() {
if (listening) {
executorService.shutdown(); executorService.shutdown();
executorService = Executors.newSingleThreadExecutor(); executorService = Executors.newSingleThreadExecutor();
try { try {
sleep(50); sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException ie) {
e.printStackTrace(); ie.printStackTrace();
}
listening = false;
}
} }
@Override
void insert(String stringContent) {
try {
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<File> 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<File> fileContent) {
insert(fileContent);
run();
} }
@Override @Override

Loading…
Cancel
Save