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

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