From ecb63e41a2eaffa3ad01d18419cc60d6ea2e4228 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 2 Aug 2021 01:12:30 +0100 Subject: [PATCH] Move utility methods to ClipboardUtils.java & Add functionality to insert contents into the clipboard --- .../java/me/goudham/ClipboardListener.java | 78 ++++--------------- 1 file changed, 16 insertions(+), 62 deletions(-) diff --git a/src/main/java/me/goudham/ClipboardListener.java b/src/main/java/me/goudham/ClipboardListener.java index 4fba855..43b3131 100644 --- a/src/main/java/me/goudham/ClipboardListener.java +++ b/src/main/java/me/goudham/ClipboardListener.java @@ -3,17 +3,9 @@ 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.File; -import java.io.IOException; import java.util.List; -import static me.goudham.domain.Contents.FILELIST; -import static me.goudham.domain.Contents.IMAGE; -import static me.goudham.domain.Contents.TEXT; - abstract class ClipboardListener { final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); private EventManager eventManager = new EventManager(); @@ -22,58 +14,28 @@ abstract class ClipboardListener { private boolean fileListMonitored = true; /** - * Try to unmarshal {@link Transferable} into {@link String} + * Main entry point of execution for both {@link MacClipboardListener} and {@link WindowsOrUnixClipboardListener} * - * @param clipboardContents The {@link Transferable} to be converted into {@link String} - * @return {@link String} representation of {@code clipboardContents} + * @see MacClipboardListener + * @see WindowsOrUnixClipboardListener */ - String getStringContent(Transferable clipboardContents) { - String newContent = null; - - try { - if (clipboardContents.isDataFlavorSupported(TEXT.getDataFlavor())) { - newContent = (String) clipboardContents.getTransferData(TEXT.getDataFlavor()); - } - } catch (UnsupportedFlavorException | IOException exp) { - exp.printStackTrace(); - } - - return newContent; - } + abstract void execute(); - /** - * Try to unmarshal {@link Transferable} into {@link BufferedImage} - * - * @param clipboardContents The {@link Transferable} to be converted into {@link BufferedImage} - * @return {@link BufferedImage} representation of {@code clipboardContents} - */ - BufferedImage getImageContent(Transferable clipboardContents) { - BufferedImage bufferedImage = null; - - try { - if (clipboardContents.isDataFlavorSupported(IMAGE.getDataFlavor())) { - bufferedImage = ClipboardUtils.convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor())); - } - } catch (UnsupportedFlavorException | IOException exp) { - exp.printStackTrace(); - } - - return bufferedImage; - } + abstract void startListening(); - List getFileContent(Transferable clipboardContents) { - List fileList = null; + abstract void stopListening(); - try { - if (clipboardContents.isDataFlavorSupported(FILELIST.getDataFlavor())) { - fileList = (List) clipboardContents.getTransferData(FILELIST.getDataFlavor()); - } - } catch (UnsupportedFlavorException | IOException exp) { - exp.printStackTrace(); - } + abstract void insert(String stringContent); - return fileList; - } + abstract void insert(Image imageContent); + + abstract void insert(List fileContent); + + abstract void insertAndNotify(String stringContent); + + abstract void insertAndNotify(Image imageContent); + + abstract void insertAndNotify(List fileContent); void toggleTextMonitored() { this.textMonitored = !textMonitored; @@ -114,12 +76,4 @@ abstract class ClipboardListener { void setEventManager(EventManager eventManager) { this.eventManager = eventManager; } - - /** - * Main entry point of execution for both {@link MacClipboardListener} and {@link WindowsOrUnixClipboardListener} - * - * @see MacClipboardListener - * @see WindowsOrUnixClipboardListener - */ - abstract void execute(); }