|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package me.goudham;
|
|
|
|
|
|
|
|
|
|
import java.awt.Dimension;
|
|
|
|
|
import java.awt.Image;
|
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
|
@ -7,10 +8,19 @@ import java.awt.datatransfer.ClipboardOwner;
|
|
|
|
|
import java.awt.datatransfer.DataFlavor;
|
|
|
|
|
import java.awt.datatransfer.Transferable;
|
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import me.goudham.listener.ClipboardEvent;
|
|
|
|
|
import me.goudham.model.MyClipboardContent;
|
|
|
|
|
import me.goudham.util.ClipboardUtils;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import static me.goudham.util.Contents.IMAGE;
|
|
|
|
|
import static me.goudham.util.Contents.STRING;
|
|
|
|
|
|
|
|
|
|
public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
|
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
|
private @NotNull ClipboardEvent clipboardEvent;
|
|
|
|
@ -36,7 +46,7 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
|
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
|
|
|
|
|
clipboardEvent.onCopyString(stringContent);
|
|
|
|
|
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
|
|
|
|
|
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
|
|
|
|
|
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
|
|
|
|
|
clipboardEvent.onCopyImage(imageContent);
|
|
|
|
|
}
|
|
|
|
|
} catch (UnsupportedFlavorException | IOException ignored) { }
|
|
|
|
@ -56,9 +66,33 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
Transferable currentClipboardContents = clipboard.getContents(currentThread());
|
|
|
|
|
processContents(clipboard, currentClipboardContents);
|
|
|
|
|
regainOwnership(clipboard, currentClipboardContents);
|
|
|
|
|
Transferable oldClipboardContents = clipboard.getContents(null);
|
|
|
|
|
final MyClipboardContent<?>[] myOldClipboardContentsArray = 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());
|
|
|
|
|
}
|
|
|
|
|
} 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())) {
|
|
|
|
|
clipboardEvent.onCopyImage(bufferedImage);
|
|
|
|
|
myOldClipboardContentsArray[0].setContent(myNewClipboardContents.getContent());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (UnsupportedFlavorException | IOException exp) {
|
|
|
|
|
exp.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}, 0, 350, TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {
|
|
|
|
|