Reduce common code in listeners

release
Hammy 3 years ago
parent 606b1d3423
commit 981a48fc6c

@ -1,15 +1,11 @@
package me.goudham;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import me.goudham.listener.ClipboardEventListener;
import me.goudham.domain.MyClipboardContent;
import static me.goudham.domain.Contents.IMAGE;
@ -28,33 +24,25 @@ class MacClipboardListener extends ClipboardListener {
executor.scheduleAtFixedRate(() -> {
Transferable newClipboardContents = clipboard.getContents(null);
try {
if (isTextMonitored()) {
if (STRING.isAvailable(clipboard)) {
String newContent = (String) newClipboardContents.getTransferData(STRING.getDataFlavor());
if (!newContent.equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEventListener clipboardEventListener : eventsListener) {
clipboardEventListener.onCopyString(newContent);
}
myClipboardContents[0].setOldContent(newContent);
String newStringContent = getStringContent(newClipboardContents);
if (!newStringContent.equals(myClipboardContents[0].getOldContent())) {
notifyStringEvent(newStringContent);
myClipboardContents[0].setOldContent(newStringContent);
}
}
}
if (isImagesMonitored()) {
if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(IMAGE.getDataFlavor()));
Dimension newContent = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
if (!newContent.equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEventListener clipboardEventListener : eventsListener) {
clipboardEventListener.onCopyImage(bufferedImage);
}
myClipboardContents[0].setOldContent(newContent);
}
BufferedImage bufferedImageContent = getImageContent(newClipboardContents);
Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight());
if (!newDimensionContent.equals(myClipboardContents[0].getOldContent())) {
notifyImageEvent(bufferedImageContent);
myClipboardContents[0].setOldContent(newDimensionContent);
}
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}, 0, 350, TimeUnit.MILLISECONDS);
}

@ -1,16 +1,11 @@
package me.goudham;
import java.awt.Image;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import me.goudham.listener.ClipboardEventListener;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
@ -34,19 +29,18 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
}
public void processContents(Clipboard oldClipboard, Transferable newClipboardContents) {
try {
if (isTextMonitored()) {
if (STRING.isAvailable(oldClipboard)) {
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
for (ClipboardEventListener clipboardEventListener : eventsListener) {
clipboardEventListener.onCopyString(stringContent);
String stringContent = getStringContent(newClipboardContents);
notifyStringEvent(stringContent);
}
} else if (IMAGE.isAvailable(oldClipboard)) {
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
for (ClipboardEventListener clipboardEventListener : eventsListener) {
clipboardEventListener.onCopyImage(imageContent);
}
if (isImagesMonitored()) {
if (IMAGE.isAvailable(oldClipboard)) {
BufferedImage bufferedImage = getImageContent(newClipboardContents);
notifyImageEvent(bufferedImage);
}
} catch (UnsupportedFlavorException | IOException ignored) {
}
}

Loading…
Cancel
Save