Remove all clipboard handling logic

main
Hammy 3 years ago
parent be659cb525
commit d4bec1eee4

@ -1,8 +0,0 @@
package me.goudham.listener;
import java.awt.image.BufferedImage;
public interface ClipboardEvent {
void onCopyString(String stringContent);
void onCopyImage(BufferedImage imageContent);
}

@ -1,20 +0,0 @@
package me.goudham.listener;
import java.util.ArrayList;
import java.util.List;
public abstract class ClipboardListener {
List<ClipboardEvent> eventsListener = new ArrayList<>();
public void addEventListener(ClipboardEvent clipboardEvent) {
if (!eventsListener.contains(clipboardEvent)) {
eventsListener.add(clipboardEvent);
}
}
public void removeEventListener(ClipboardEvent clipboardEvent) {
eventsListener.remove(clipboardEvent);
}
public abstract void execute();
}

@ -1,58 +0,0 @@
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 static me.goudham.model.Contents.IMAGE;
import static me.goudham.model.Contents.STRING;
public class MacClipboardListener extends ClipboardListener {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public MacClipboardListener() { }
@Override
public void execute() {
Transferable oldClipboardContents = clipboard.getContents(null);
final MyClipboardContent<?, ?>[] myClipboardContents = new MyClipboardContent[]{ ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
Transferable newClipboardContents = clipboard.getContents(null);
try {
if (STRING.isAvailable(clipboard)) {
myClipboardContents[0].setNewContent(newClipboardContents.getTransferData(STRING.getDataFlavor()));
if (!myClipboardContents[0].getNewContent().equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyString((String) myClipboardContents[0].getNewContent());
}
myClipboardContents[0].setOldContent(myClipboardContents[0].getNewContent());
}
} else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(IMAGE.getDataFlavor()));
myClipboardContents[0].setNewContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
if (!myClipboardContents[0].getNewContent().equals(myClipboardContents[0].getOldContent())) {
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyImage(bufferedImage);
}
myClipboardContents[0].setOldContent(myClipboardContents[0].getNewContent());
}
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}, 0, 350, TimeUnit.MILLISECONDS);
}
}

@ -1,75 +0,0 @@
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 static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
public class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnable, ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public WindowsOrUnixClipboardListener() { }
@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);
for (ClipboardEvent clipboardEvent : eventsListener) {
clipboardEvent.onCopyString(stringContent);
}
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
for (ClipboardEvent clipboardEvent : eventsListener) {
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 run() {
Transferable currentClipboardContents = clipboard.getContents(null);
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
}
@Override
public void execute() {
run();
}
}

@ -1,37 +0,0 @@
package me.goudham.model;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
public enum Contents {
STRING(DataFlavor.stringFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
}
},
IMAGE(DataFlavor.imageFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor);
}
},
FILELIST(DataFlavor.javaFileListFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor);
}
};
private final DataFlavor dataFlavor;
Contents(DataFlavor dataFlavor) {
this.dataFlavor = dataFlavor;
}
public DataFlavor getDataFlavor() {
return dataFlavor;
}
public abstract boolean isAvailable(Clipboard clipboard);
}

@ -1,45 +0,0 @@
package me.goudham.model;
import java.util.Objects;
public class MyClipboardContent<T, U> {
private T oldContent;
private U newContent;
public MyClipboardContent() {
}
public MyClipboardContent(T oldContent) {
this.oldContent = oldContent;
}
public void setOldContent(Object oldContent) {
this.oldContent = (T) oldContent;
}
public void setNewContent(Object newContent) {
this.newContent = (U) newContent;
}
public T getOldContent() {
return oldContent;
}
public U getNewContent() {
return newContent;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyClipboardContent<?, ?> that = (MyClipboardContent<?, ?>) o;
return Objects.equals(oldContent, that.oldContent) && Objects.equals(newContent, that.newContent);
}
@Override
public int hashCode() {
return Objects.hash(oldContent, newContent);
}
}

@ -1,43 +0,0 @@
package me.goudham.model;
import org.jetbrains.annotations.NotNull;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
public class TransferableImage implements Transferable {
private final Image image;
public TransferableImage(@NotNull Image image) {
this.image = image;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (flavor.equals(DataFlavor.imageFlavor)) {
return image;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
DataFlavor[] flavors = getTransferDataFlavors();
for (DataFlavor dataFlavor : flavors) {
if (flavor.equals(dataFlavor)) {
return true;
}
}
return false;
}
}

@ -1,44 +0,0 @@
package me.goudham.util;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
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 me.goudham.model.MyClipboardContent;
import static me.goudham.model.Contents.IMAGE;
import static me.goudham.model.Contents.STRING;
public class ClipboardUtils {
public static MyClipboardContent<?, ?> getClipboardContents(Transferable contents, Clipboard clipboard) {
MyClipboardContent<?, ?> myClipboardContent = new MyClipboardContent<>();
try {
if (STRING.isAvailable(clipboard)) {
myClipboardContent.setOldContent(contents.getTransferData(STRING.getDataFlavor()));
} else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor()));
myClipboardContent.setOldContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
return myClipboardContent;
}
public static BufferedImage convertToBufferedImage(Image image) {
BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = newImage.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
}
Loading…
Cancel
Save