|
|
@ -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) {
|
|
|
|