Add functionality for inserting content into clipboard & add functionality to start/stop listening

MYC-Refactor
Hammy 3 years ago
parent 58d98b7ae6
commit 82d23458be

@ -2,6 +2,7 @@ package me.goudham;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Image; import java.awt.Image;
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;
@ -11,7 +12,10 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import me.goudham.domain.MyClipboardContent; import me.goudham.domain.MyClipboardContent;
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.sleep;
import static me.goudham.ClipboardUtils.getFileContent; import static me.goudham.ClipboardUtils.getFileContent;
import static me.goudham.ClipboardUtils.getImageContent; import static me.goudham.ClipboardUtils.getImageContent;
import static me.goudham.ClipboardUtils.getStringContent; import static me.goudham.ClipboardUtils.getStringContent;
@ -19,9 +23,15 @@ 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 MacClipboardListener extends ClipboardListener { class MacClipboardListener extends ClipboardListener implements Runnable {
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
MyClipboardContent<?>[] myClipboardContents;
private boolean listening = false;
MacClipboardListener() {} MacClipboardListener() {
Transferable oldClipboardContents = clipboard.getContents(null);
myClipboardContents = new MyClipboardContent[] { ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
}
/** /**
* Checks if {@link String} is within the clipboard and changed * Checks if {@link String} is within the clipboard and changed
@ -30,18 +40,20 @@ class MacClipboardListener extends ClipboardListener {
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkText(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkText(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (isTextMonitored()) {
if (TEXT.isAvailable(clipboard) && !FILELIST.isAvailable(clipboard)) { if (TEXT.isAvailable(clipboard) && !FILELIST.isAvailable(clipboard)) {
String newStringContent = getStringContent(newClipboardContents); String newStringContent = getStringContent(newClipboardContents);
if (newStringContent == null) return; if (newStringContent == null) return;
if (isTextMonitored()) {
Object oldContent = myClipboardContents[0].getOldContent(); Object oldContent = myClipboardContents[0].getOldContent();
if (!newStringContent.equals(oldContent)) { if (!newStringContent.equals(oldContent)) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldContent); OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldContent);
getEventManager().notifyTextEvent(oldClipboardContent, newStringContent); getEventManager().notifyTextEvent(oldClipboardContent, newStringContent);
myClipboardContents[0].setOldContent(newStringContent);
} }
} }
myClipboardContents[0].setOldContent(newStringContent);
myClipboardContents[0].setOldDimensionContent(newStringContent);
} }
} }
@ -52,19 +64,20 @@ class MacClipboardListener extends ClipboardListener {
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (isImageMonitored()) {
if (IMAGE.isAvailable(clipboard)) { if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImageContent = getImageContent(newClipboardContents); BufferedImage bufferedImageContent = getImageContent(newClipboardContents);
if (bufferedImageContent == null) return; if (bufferedImageContent == null) return;
Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight()); Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight());
if (isImageMonitored()) {
if (!newDimensionContent.equals(myClipboardContents[0].getOldDimensionContent())) { if (!newDimensionContent.equals(myClipboardContents[0].getOldDimensionContent())) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent());
getEventManager().notifyImageEvent(oldClipboardContent, bufferedImageContent); getEventManager().notifyImageEvent(oldClipboardContent, bufferedImageContent);
myClipboardContents[0].setOldContent(bufferedImageContent);
myClipboardContents[0].setOldDimensionContent(newDimensionContent);
} }
} }
myClipboardContents[0].setOldContent(bufferedImageContent);
myClipboardContents[0].setOldDimensionContent(newDimensionContent);
} }
} }
@ -76,76 +89,112 @@ class MacClipboardListener extends ClipboardListener {
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkFiles(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkFiles(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (isFileMonitored()) {
if (FILELIST.isAvailable(clipboard)) { if (FILELIST.isAvailable(clipboard)) {
List<File> fileListContent = getFileContent(newClipboardContents); List<File> fileListContent = getFileContent(newClipboardContents);
if (fileListContent == null) return; if (fileListContent == null) return;
if (isFileMonitored()) {
if (!fileListContent.equals(myClipboardContents[0].getOldContent())) { if (!fileListContent.equals(myClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent());
getEventManager().notifyFilesEvent(oldClipboardContent, fileListContent); getEventManager().notifyFilesEvent(oldClipboardContent, fileListContent);
myClipboardContents[0].setOldContent(fileListContent);
} }
} }
myClipboardContents[0].setOldContent(fileListContent);
myClipboardContents[0].setOldContent(fileListContent);
} }
} }
@Override
void startListening() {
if (!listening) {
listening = true;
execute();
}
}
/**
* Main entry point for {@link MacClipboardListener}
* <p>Retrieves thread from {@link Executors#newSingleThreadScheduledExecutor()} and executes code on a fixed delay</p>
*/
@Override @Override
void execute() { void stopListening() {
Transferable oldClipboardContents = clipboard.getContents(null); if (listening) {
final MyClipboardContent<?>[] myClipboardContents = new MyClipboardContent[] { ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) }; scheduledExecutorService.shutdown();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); try {
executor.scheduleAtFixedRate(() -> { sleep(200);
Transferable newClipboardContents = clipboard.getContents(null); } catch (InterruptedException ie) {
checkText(newClipboardContents, myClipboardContents); ie.printStackTrace();
checkImages(newClipboardContents, myClipboardContents);
checkFiles(newClipboardContents, myClipboardContents);
}, 0, 350, TimeUnit.MILLISECONDS);
} }
@Override listening = false;
void insert(String stringComponent) { }
}
@Override
void insert(String stringContent) {
insertAndNotify(stringContent);
} }
@Override @Override
void insert(Image imageContent) { void insert(Image imageContent) {
insertAndNotify(imageContent);
} }
@Override @Override
void insert(List<File> fileContent) { void insert(List<File> fileContent) {
insertAndNotify(fileContent);
} }
@Override @Override
void insertAndNotify(String stringContent) { void insertAndNotify(String stringContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
clipboard.setContents(new StringSelection(stringContent), null);
} }
@Override @Override
void insertAndNotify(Image imageContent) { void insertAndNotify(Image imageContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
clipboard.setContents(new TransferableImage(imageContent), null);
} }
@Override @Override
void insertAndNotify(List<File> fileContent) { void insertAndNotify(List<File> fileContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
clipboard.setContents(new TransferableFileList(fileContent), null);
} }
@Override @Override
void startListening() { public void run() {
try {
Transferable newClipboardContents = clipboard.getContents(null);
checkText(newClipboardContents, myClipboardContents);
checkImages(newClipboardContents, myClipboardContents);
checkFiles(newClipboardContents, myClipboardContents);
} catch (IllegalStateException ise) {
ise.printStackTrace();
}
} }
/**
* Main entry point for {@link MacClipboardListener}
* <p>Retrieves thread from {@link Executors#newSingleThreadScheduledExecutor()} and executes code on a fixed delay</p>
*/
@Override @Override
void stopListening() { void execute() {
scheduledExecutorService.scheduleAtFixedRate(this, 0, 200, TimeUnit.MILLISECONDS);
} }
} }

Loading…
Cancel
Save