Refactor listener class to copy images and strings (wip)

osx
Hammy 3 years ago
parent 208701479c
commit 27841266ed

@ -1,50 +1,64 @@
package me.goudham;
import me.goudham.listener.ClipboardEvent;
import me.goudham.model.MyClipboardContent;
import org.jetbrains.annotations.NotNull;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
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.io.IOException;
public class ClipboardListener extends Thread implements ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private @NotNull ClipboardEvent clipboardEvent;
public interface EntryListener {
void onCopy(String data);
public ClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private EntryListener entryListener;
@Override
public void lostOwnership(Clipboard c, Transferable t) {
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try {
sleep(200);
} catch (InterruptedException ignored) { }
Transferable contents = c.getContents(currentThread());
processContents(contents);
regainOwnership(c, contents);
Transferable newClipboardContents = oldClipboard.getContents(currentThread());
processContents(oldClipboard, newClipboardContents);
regainOwnership(oldClipboard, newClipboardContents);
}
public void processContents(Transferable t) {
try {
String what = (String) (t.getTransferData(DataFlavor.stringFlavor));
public void processContents(Clipboard oldClipboard, Transferable newClipboardContents) {
MyClipboardContent<?> clipboardContent = null;
if (entryListener != null) {
entryListener.onCopy(what);
}
try {
if (oldClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
clipboardContent = new MyClipboardContent<>(stringContent);
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
clipboardContent = new MyClipboardContent<>(imageContent);
}
} catch (UnsupportedFlavorException | IOException ignored) { }
clipboardEvent.onCopy(clipboardContent);
}
public void regainOwnership(Clipboard c, Transferable t) {
c.setContents(t, this);
public void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) {
clipboard.setContents(newClipboardContents, this);
}
public void run() {
Transferable transferable = clipboard.getContents(currentThread());
processContents(transferable);
regainOwnership(clipboard, transferable);
Transferable currentClipboardContents = clipboard.getContents(currentThread());
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
}
public void setEntryListener(EntryListener entryListener) {
this.entryListener = entryListener;
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
}
Loading…
Cancel
Save