Add listeners for different operating systems with different implementations

main
sgoudham 3 years ago
parent 64f49484e8
commit 6a383c102b
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -0,0 +1,5 @@
package me.goudham.listener;
public interface ClipboardListener {
void execute();
}

@ -0,0 +1,59 @@
package me.goudham.listener;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
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.model.MyClipboardContent;
import me.goudham.util.ClipboardUtils;
import org.jetbrains.annotations.NotNull;
import static me.goudham.model.Contents.IMAGE;
import static me.goudham.model.Contents.STRING;
public class MacClipboardListener implements ClipboardListener {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private final @NotNull ClipboardEvent clipboardEvent;
public MacClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
@Override
public void execute() {
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);
}
}

@ -0,0 +1,67 @@
package me.goudham.listener;
import java.awt.Image;
import java.awt.Toolkit;
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.awt.image.BufferedImage;
import java.io.IOException;
import me.goudham.util.ClipboardUtils;
import org.jetbrains.annotations.NotNull;
public class WindowsOrUnixClipboardListener extends Thread implements ClipboardListener, ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private final @NotNull ClipboardEvent clipboardEvent;
public WindowsOrUnixClipboardListener(@NotNull ClipboardEvent clipboardEvent) {
this.clipboardEvent = clipboardEvent;
}
@Override
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try {
sleep(200);
} catch (InterruptedException ignored) {
}
Transferable newClipboardContents = oldClipboard.getContents(currentThread());
processContents(oldClipboard, newClipboardContents);
regainOwnership(oldClipboard, newClipboardContents);
}
public void processContents(Clipboard oldClipboard, Transferable newClipboardContents) {
try {
if (oldClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
clipboardEvent.onCopyString(stringContent);
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
clipboardEvent.onCopyImage(imageContent);
}
} catch (UnsupportedFlavorException | IOException ignored) {
}
}
public void regainOwnership(Clipboard clipboard, Transferable newClipboardContents) {
try {
clipboard.setContents(newClipboardContents, this);
} catch (IllegalStateException ise) {
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
regainOwnership(clipboard, newClipboardContents);
}
}
@Override
public void execute() {
Transferable currentClipboardContents = clipboard.getContents(null);
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
}
}
Loading…
Cancel
Save