Add event listeners to notify classes of clipboard events

main
sgoudham 3 years ago
parent c559176c30
commit f9e934d10c
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -1,5 +1,20 @@
package me.goudham.listener;
public interface ClipboardListener {
void execute();
import java.util.ArrayList;
import java.util.List;
public abstract class ClipboardListener {
List<ClipboardEvent> eventsListener = new ArrayList<>();
public abstract void execute();
public void addEventListener(ClipboardEvent clipboardEvent) {
if (!eventsListener.contains(clipboardEvent)) {
eventsListener.add(clipboardEvent);
}
}
public void removeEventListener(ClipboardEvent clipboardEvent) {
eventsListener.remove(clipboardEvent);
}
}

@ -13,42 +13,41 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import me.goudham.model.MyClipboardContent;
import me.goudham.util.ClipboardUtils;
import org.jetbrains.annotations.NotNull;
import static me.goudham.model.Contents.IMAGE;
import static me.goudham.model.Contents.STRING;
public class MacClipboardListener implements ClipboardListener {
public class MacClipboardListener extends ClipboardListener {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private final @NotNull ClipboardEvent clipboardEvent;
public MacClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
public MacClipboardListener() { }
@Override
public void execute() {
Transferable oldClipboardContents = clipboard.getContents(null);
final MyClipboardContent<?>[] myOldClipboardContentsArray = new MyClipboardContent[]{ ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
final MyClipboardContent<?, ?>[] myClipboardContents = new MyClipboardContent[]{ ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
Transferable newClipboardContents = clipboard.getContents(null);
MyClipboardContent<?> myNewClipboardContents = new MyClipboardContent<>("");
try {
if (STRING.isAvailable(clipboard)) {
myNewClipboardContents.setContent(newClipboardContents.getTransferData(STRING.getDataFlavor()));
if (!myNewClipboardContents.getContent().equals(myOldClipboardContentsArray[0].getContent())) {
clipboardEvent.onCopyString((String) myNewClipboardContents.getContent());
myOldClipboardContentsArray[0].setContent(myNewClipboardContents.getContent());
myClipboardContents[0].setNewContent(newClipboardContents.getTransferData(STRING.getDataFlavor()));
if (!myClipboardContents[0].getNewContent().equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyString((String) myClipboardContents[0].getNewContent());
}
myClipboardContents[0].setOldContent(myClipboardContents[0].getNewContent());
}
} else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(IMAGE.getDataFlavor()));
myNewClipboardContents.setContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
if (!myNewClipboardContents.getContent().equals(myOldClipboardContentsArray[0].getContent())) {
myClipboardContents[0].setNewContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
if (!myClipboardContents[0].getNewContent().equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyImage(bufferedImage);
myOldClipboardContentsArray[0].setContent(myNewClipboardContents.getContent());
}
myClipboardContents[0].setOldContent(myClipboardContents[0].getNewContent());
}
}
} catch (UnsupportedFlavorException | IOException exp) {

@ -10,15 +10,14 @@ import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import me.goudham.util.ClipboardUtils;
import org.jetbrains.annotations.NotNull;
public class WindowsOrUnixClipboardListener extends Thread implements ClipboardListener, ClipboardOwner {
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
public class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnable, ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private final @NotNull ClipboardEvent clipboardEvent;
public WindowsOrUnixClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
public WindowsOrUnixClipboardListener() { }
@Override
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
@ -36,11 +35,15 @@ public class WindowsOrUnixClipboardListener extends Thread implements ClipboardL
try {
if (oldClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyString(stringContent);
}
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyImage(imageContent);
}
}
} catch (UnsupportedFlavorException | IOException ignored) {
}
}
@ -59,9 +62,14 @@ public class WindowsOrUnixClipboardListener extends Thread implements ClipboardL
}
@Override
public void execute() {
public void run() {
Transferable currentClipboardContents = clipboard.getContents(null);
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
}
@Override
public void execute() {
run();
}
}
Loading…
Cancel
Save