From 606b1d342348c478bff79448cb212bf94bc5c983 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 26 Jul 2021 01:34:19 +0100 Subject: [PATCH] Add helper methods --- .../java/me/goudham/ClipboardListener.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/main/java/me/goudham/ClipboardListener.java b/src/main/java/me/goudham/ClipboardListener.java index 70260b0..3ec0a7e 100644 --- a/src/main/java/me/goudham/ClipboardListener.java +++ b/src/main/java/me/goudham/ClipboardListener.java @@ -1,17 +1,61 @@ package me.goudham; +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.ArrayList; import java.util.List; import me.goudham.listener.ClipboardEventListener; +import static me.goudham.domain.Contents.IMAGE; +import static me.goudham.domain.Contents.STRING; + abstract class ClipboardListener { final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); List eventsListener = new ArrayList<>(); private boolean imagesMonitored = true; private boolean textMonitored = true; + String getStringContent(Transferable clipboardContents) { + String newContent = null; + + try { + newContent = (String) clipboardContents.getTransferData(STRING.getDataFlavor()); + } catch (UnsupportedFlavorException | IOException exp) { + exp.printStackTrace(); + } + + return newContent; + } + + BufferedImage getImageContent(Transferable clipboardContents) { + BufferedImage bufferedImage = null; + + try { + bufferedImage = ClipboardUtils.convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor())); + } catch (UnsupportedFlavorException | IOException exp) { + exp.printStackTrace(); + } + + return bufferedImage; + } + + void notifyStringEvent(String stringContent) { + for (ClipboardEventListener clipboardEventListener : eventsListener) { + clipboardEventListener.onCopyString(stringContent); + } + } + + void notifyImageEvent(BufferedImage imageContent) { + for (ClipboardEventListener clipboardEventListener : eventsListener) { + clipboardEventListener.onCopyImage(imageContent); + } + } + void addEventListener(ClipboardEventListener clipboardEventListener) { if (!eventsListener.contains(clipboardEventListener)) { eventsListener.add(clipboardEventListener);