From d4bec1eee489d9de481e09406406f742f0db94ce Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 26 Jul 2021 02:34:08 +0100 Subject: [PATCH] Remove all clipboard handling logic --- .../me/goudham/listener/ClipboardEvent.java | 8 -- .../goudham/listener/ClipboardListener.java | 20 ----- .../listener/MacClipboardListener.java | 58 -------------- .../WindowsOrUnixClipboardListener.java | 75 ------------------- src/main/java/me/goudham/model/Contents.java | 37 --------- .../me/goudham/model/MyClipboardContent.java | 45 ----------- .../me/goudham/model/TransferableImage.java | 43 ----------- .../java/me/goudham/util/ClipboardUtils.java | 44 ----------- 8 files changed, 330 deletions(-) delete mode 100644 src/main/java/me/goudham/listener/ClipboardEvent.java delete mode 100644 src/main/java/me/goudham/listener/ClipboardListener.java delete mode 100644 src/main/java/me/goudham/listener/MacClipboardListener.java delete mode 100644 src/main/java/me/goudham/listener/WindowsOrUnixClipboardListener.java delete mode 100644 src/main/java/me/goudham/model/Contents.java delete mode 100644 src/main/java/me/goudham/model/MyClipboardContent.java delete mode 100644 src/main/java/me/goudham/model/TransferableImage.java delete mode 100644 src/main/java/me/goudham/util/ClipboardUtils.java diff --git a/src/main/java/me/goudham/listener/ClipboardEvent.java b/src/main/java/me/goudham/listener/ClipboardEvent.java deleted file mode 100644 index 02ac7ad..0000000 --- a/src/main/java/me/goudham/listener/ClipboardEvent.java +++ /dev/null @@ -1,8 +0,0 @@ -package me.goudham.listener; - -import java.awt.image.BufferedImage; - -public interface ClipboardEvent { - void onCopyString(String stringContent); - void onCopyImage(BufferedImage imageContent); -} diff --git a/src/main/java/me/goudham/listener/ClipboardListener.java b/src/main/java/me/goudham/listener/ClipboardListener.java deleted file mode 100644 index e68851c..0000000 --- a/src/main/java/me/goudham/listener/ClipboardListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package me.goudham.listener; - -import java.util.ArrayList; -import java.util.List; - -public abstract class ClipboardListener { - List 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(); -} diff --git a/src/main/java/me/goudham/listener/MacClipboardListener.java b/src/main/java/me/goudham/listener/MacClipboardListener.java deleted file mode 100644 index 5628f3a..0000000 --- a/src/main/java/me/goudham/listener/MacClipboardListener.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/me/goudham/listener/WindowsOrUnixClipboardListener.java b/src/main/java/me/goudham/listener/WindowsOrUnixClipboardListener.java deleted file mode 100644 index 125ce43..0000000 --- a/src/main/java/me/goudham/listener/WindowsOrUnixClipboardListener.java +++ /dev/null @@ -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(); - } -} \ No newline at end of file diff --git a/src/main/java/me/goudham/model/Contents.java b/src/main/java/me/goudham/model/Contents.java deleted file mode 100644 index 1b0ee05..0000000 --- a/src/main/java/me/goudham/model/Contents.java +++ /dev/null @@ -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); -} diff --git a/src/main/java/me/goudham/model/MyClipboardContent.java b/src/main/java/me/goudham/model/MyClipboardContent.java deleted file mode 100644 index 6c47a94..0000000 --- a/src/main/java/me/goudham/model/MyClipboardContent.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.goudham.model; - -import java.util.Objects; - -public class MyClipboardContent { - 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); - } -} diff --git a/src/main/java/me/goudham/model/TransferableImage.java b/src/main/java/me/goudham/model/TransferableImage.java deleted file mode 100644 index 4e78f4a..0000000 --- a/src/main/java/me/goudham/model/TransferableImage.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/me/goudham/util/ClipboardUtils.java b/src/main/java/me/goudham/util/ClipboardUtils.java deleted file mode 100644 index 4348a1b..0000000 --- a/src/main/java/me/goudham/util/ClipboardUtils.java +++ /dev/null @@ -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; - } -}