|
|
@ -4,6 +4,7 @@ import me.goudham.listener.ClipboardEvent;
|
|
|
|
import me.goudham.model.MyClipboardContent;
|
|
|
|
import me.goudham.model.MyClipboardContent;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.awt.HeadlessException;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
@ -12,6 +13,9 @@ import java.awt.datatransfer.DataFlavor;
|
|
|
|
import java.awt.datatransfer.Transferable;
|
|
|
|
import java.awt.datatransfer.Transferable;
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
@ -25,7 +29,8 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
|
|
|
|
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sleep(200);
|
|
|
|
sleep(200);
|
|
|
|
} catch (InterruptedException ignored) { }
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Transferable newClipboardContents = oldClipboard.getContents(currentThread());
|
|
|
|
Transferable newClipboardContents = oldClipboard.getContents(currentThread());
|
|
|
|
processContents(oldClipboard, newClipboardContents);
|
|
|
|
processContents(oldClipboard, newClipboardContents);
|
|
|
@ -43,7 +48,8 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
|
|
|
|
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
|
|
|
|
clipboardContent = new MyClipboardContent<>(imageContent);
|
|
|
|
clipboardContent = new MyClipboardContent<>(imageContent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (UnsupportedFlavorException | IOException ignored) { }
|
|
|
|
} catch (UnsupportedFlavorException | IOException ignored) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clipboardEvent.onCopy(clipboardContent);
|
|
|
|
clipboardEvent.onCopy(clipboardContent);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -53,9 +59,28 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
Transferable currentClipboardContents = clipboard.getContents(currentThread());
|
|
|
|
Transferable currentClipboardContents = clipboard.getContents(null);
|
|
|
|
processContents(clipboard, currentClipboardContents);
|
|
|
|
|
|
|
|
regainOwnership(clipboard, currentClipboardContents);
|
|
|
|
final MyClipboardContent<String>[] recentContent = new MyClipboardContent[]{new MyClipboardContent<>("")};
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
recentContent[0].setContent(currentClipboardContents.getTransferData(DataFlavor.stringFlavor));
|
|
|
|
|
|
|
|
} catch (UnsupportedFlavorException | IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
|
|
|
executor.scheduleAtFixedRate(() -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Transferable newClipboard = clipboard.getContents(null);
|
|
|
|
|
|
|
|
String data = (String) newClipboard.getTransferData(DataFlavor.stringFlavor);
|
|
|
|
|
|
|
|
if (!data.equals(recentContent[0].getContent())) {
|
|
|
|
|
|
|
|
clipboardEvent.onCopy(new MyClipboardContent<>(data));
|
|
|
|
|
|
|
|
recentContent[0].setContent(data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (HeadlessException | IOException | UnsupportedFlavorException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 0, 200, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {
|
|
|
|
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {
|
|
|
|