From 535e35d3e8062afef9b5d7af4fb036db97955bf4 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 27 Jul 2021 02:31:47 +0100 Subject: [PATCH] Add Helper method for retrieving old clipboard contents --- src/main/java/me/goudham/ClipboardUtils.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/me/goudham/ClipboardUtils.java b/src/main/java/me/goudham/ClipboardUtils.java index be9b6da..728e23f 100644 --- a/src/main/java/me/goudham/ClipboardUtils.java +++ b/src/main/java/me/goudham/ClipboardUtils.java @@ -9,6 +9,7 @@ import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.image.BufferedImage; import java.io.IOException; import me.goudham.domain.MyClipboardContent; +import me.goudham.domain.OldClipboardContent; import static me.goudham.domain.Contents.IMAGE; import static me.goudham.domain.Contents.STRING; @@ -32,6 +33,22 @@ class ClipboardUtils { return myClipboardContent; } + static OldClipboardContent getOldClipboardContent(Transferable oldContents) { + OldClipboardContent oldClipboardContent = null; + + try { + if (oldContents.isDataFlavorSupported(STRING.getDataFlavor())) { + oldClipboardContent = new OldClipboardContent((String) oldContents.getTransferData(STRING.getDataFlavor())); + } else if (oldContents.isDataFlavorSupported(IMAGE.getDataFlavor())) { + oldClipboardContent = new OldClipboardContent(convertToBufferedImage((Image) oldContents.getTransferData(IMAGE.getDataFlavor()))); + } + } catch (UnsupportedFlavorException | IOException exp) { + exp.printStackTrace(); + } + + return oldClipboardContent; + } + static BufferedImage convertToBufferedImage(Image image) { BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);