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; 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.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; import java.io.IOException;
public class ClipboardListener extends Thread implements ClipboardOwner { public class ClipboardListener extends Thread implements ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private @NotNull ClipboardEvent clipboardEvent;
public interface EntryListener { public ClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
void onCopy(String data); this.clipboardEvent = clipboardEvent;
} }
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private EntryListener entryListener;
@Override @Override
public void lostOwnership(Clipboard c, Transferable t) { public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try { try {
sleep(200); sleep(200);
} catch (InterruptedException ignored) { } } catch (InterruptedException ignored) { }
Transferable contents = c.getContents(currentThread()); Transferable newClipboardContents = oldClipboard.getContents(currentThread());
processContents(contents); processContents(oldClipboard, newClipboardContents);
regainOwnership(c, contents); regainOwnership(oldClipboard, newClipboardContents);
} }
public void processContents(Transferable t) { public void processContents(Clipboard oldClipboard, Transferable newClipboardContents) {
try { MyClipboardContent<?> clipboardContent = null;
String what = (String) (t.getTransferData(DataFlavor.stringFlavor));
if (entryListener != null) { try {
entryListener.onCopy(what); 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) { } } catch (UnsupportedFlavorException | IOException ignored) { }
clipboardEvent.onCopy(clipboardContent);
} }
public void regainOwnership(Clipboard c, Transferable t) { public void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) {
c.setContents(t, this); clipboard.setContents(newClipboardContents, this);
} }
public void run() { public void run() {
Transferable transferable = clipboard.getContents(currentThread()); Transferable currentClipboardContents = clipboard.getContents(currentThread());
processContents(transferable); processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, transferable); regainOwnership(clipboard, currentClipboardContents);
} }
public void setEntryListener(EntryListener entryListener) { public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {
this.entryListener = entryListener; this.clipboardEvent = clipboardEvent;
} }
} }
Loading…
Cancel
Save