From f15d371604f65d3ef7435ec8b89c48d84996d653 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 22 Jul 2021 22:50:55 +0100 Subject: [PATCH] Update MyClipboardContent.java to store old & new content --- .../me/goudham/model/MyClipboardContent.java | 31 +++++--- .../java/me/goudham/util/ClipboardUtils.java | 8 +- .../java/me/goudham/view/ClipboardView.java | 75 +++++++++---------- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/main/java/me/goudham/model/MyClipboardContent.java b/src/main/java/me/goudham/model/MyClipboardContent.java index 412c68e..6c47a94 100644 --- a/src/main/java/me/goudham/model/MyClipboardContent.java +++ b/src/main/java/me/goudham/model/MyClipboardContent.java @@ -2,35 +2,44 @@ package me.goudham.model; import java.util.Objects; -public class MyClipboardContent { - private T content; +public class MyClipboardContent { + private T oldContent; + private U newContent; public MyClipboardContent() { } - public MyClipboardContent(T content) { - this.content = content; + public MyClipboardContent(T oldContent) { + this.oldContent = oldContent; } - public void setContent(Object content) { - this.content = (T) content; + public void setOldContent(Object oldContent) { + this.oldContent = (T) oldContent; } - public T getContent() { - return content; + 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(content, that.content); + MyClipboardContent that = (MyClipboardContent) o; + return Objects.equals(oldContent, that.oldContent) && Objects.equals(newContent, that.newContent); } @Override public int hashCode() { - return Objects.hash(content); + return Objects.hash(oldContent, newContent); } } diff --git a/src/main/java/me/goudham/util/ClipboardUtils.java b/src/main/java/me/goudham/util/ClipboardUtils.java index 2a55a62..4348a1b 100644 --- a/src/main/java/me/goudham/util/ClipboardUtils.java +++ b/src/main/java/me/goudham/util/ClipboardUtils.java @@ -15,15 +15,15 @@ import static me.goudham.model.Contents.STRING; public class ClipboardUtils { - public static MyClipboardContent getClipboardContents(Transferable contents, Clipboard clipboard) { - MyClipboardContent myClipboardContent = new MyClipboardContent<>(""); + public static MyClipboardContent getClipboardContents(Transferable contents, Clipboard clipboard) { + MyClipboardContent myClipboardContent = new MyClipboardContent<>(); try { if (STRING.isAvailable(clipboard)) { - myClipboardContent.setContent(contents.getTransferData(STRING.getDataFlavor())); + myClipboardContent.setOldContent(contents.getTransferData(STRING.getDataFlavor())); } else if (IMAGE.isAvailable(clipboard)) { BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor())); - myClipboardContent.setContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); + myClipboardContent.setOldContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); } } catch (UnsupportedFlavorException | IOException exp) { exp.printStackTrace(); diff --git a/src/main/java/me/goudham/view/ClipboardView.java b/src/main/java/me/goudham/view/ClipboardView.java index 2181354..bb8c571 100644 --- a/src/main/java/me/goudham/view/ClipboardView.java +++ b/src/main/java/me/goudham/view/ClipboardView.java @@ -12,15 +12,16 @@ import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import me.goudham.listener.ClipboardEvent; +import me.goudham.listener.ClipboardListener; import me.goudham.listener.MacClipboardListener; import me.goudham.listener.WindowsOrUnixClipboardListener; import org.apache.commons.lang3.SystemUtils; -public class ClipboardView implements ClipboardEvent { +public class ClipboardView { private JPanel clipboard; private JButton copySelectedTextButton; private JList clipboardContentList; - private final DefaultListModel listModel; + private final DefaultListModel listModel = new DefaultListModel<>(); private JLabel title; private JPanel textButtonPanel; private JButton clearAllHistoryButton; @@ -35,29 +36,28 @@ public class ClipboardView implements ClipboardEvent { private boolean toggle = true; private BufferedImage storedImageContent; + private ClipboardListener clipboardListener; public ClipboardView() { - imageScrollPane.setBorder(BorderFactory.createEmptyBorder()); clipboardContentScrollPane.setBorder(BorderFactory.createEmptyBorder()); - textButtonPanel.setBorder(BorderFactory.createEmptyBorder()); - listModel = new DefaultListModel<>(); + imageScrollPane.getVerticalScrollBar().setUnitIncrement(35); + imageScrollPane.getHorizontalScrollBar().setUnitIncrement(35); + clipboardContentScrollPane.getVerticalScrollBar().setUnitIncrement(200); + clipboardContentScrollPane.getHorizontalScrollBar().setUnitIncrement(200); clipboardContentList.setModel(listModel); toggleImageButton.addActionListener(actionEvent -> { - if (toggle) { - imageLabel.setIcon(null); -// anotherImagePanel.setPreferredSize(null); -// anotherImagePanel.setVisible(false); -// imageIconLabel.setMaximumSize(new Dimension(0, 0)); -// anotherImagePanel.setPreferredSize(new Dimension(0, 0)); - toggle = false; - } else { -// anotherImagePanel.setPreferredSize(new Dimension(300, 300)); -// anotherImagePanel.setVisible(true); - imageLabel.setIcon(new ImageIcon(storedImageContent)); - toggle = true; + if (storedImageContent != null) { + if (toggle) { + toggleImageButton.setText("Show Image"); + imageLabel.setIcon(null); + toggle = false; + } else { + imageLabel.setIcon(new ImageIcon(storedImageContent)); + toggleImageButton.setText("Hide Image"); + toggle = true; + } } -// anotherImagePanel.revalidate(); }); // final java.awt.datatransfer.clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); @@ -102,14 +102,28 @@ public class ClipboardView implements ClipboardEvent { } public void createAndShowGUI() { - me.goudham.listener.ClipboardListener clipboardListener = null; if (isMac()) { - clipboardListener = new MacClipboardListener(this); + clipboardListener = new MacClipboardListener(); } else if (isUnix() || isWindows()) { - clipboardListener = new WindowsOrUnixClipboardListener(this); + clipboardListener = new WindowsOrUnixClipboardListener(); } clipboardListener.execute(); + clipboardListener.addEventListener(new ClipboardEvent() { + @Override + public void onCopyString(String stringContent) { + listModel.add(0, stringContent); + } + + @Override + public void onCopyImage(BufferedImage imageContent) { + storedImageContent = imageContent; + imageLabel.setIcon(new ImageIcon(imageContent)); + toggleImageButton.setText("Hide Image"); + toggle = true; + } + }); + JFrame jFrame = new JFrame(); jFrame.setTitle("My Clipboard History"); jFrame.setContentPane(clipboard); @@ -122,25 +136,6 @@ public class ClipboardView implements ClipboardEvent { jFrame.setLocationRelativeTo(null); } - @Override - public void onCopyString(String stringContent) { - listModel.add(0, stringContent); - } - - @Override - public void onCopyImage(BufferedImage imageContent) { - storedImageContent = imageContent; -// anotherImagePanel.setMinimumSize(new Dimension(300, 300)); - imageLabel.setIcon(new ImageIcon(imageContent)); - toggle = true; -// -// if (imageContent.getWidth() > 1000 || imageContent.getHeight() > 1000) { -// imageIconLabel.setIcon(new ImageIcon(new ImageIcon(imageContent).getImage().getScaledInstance(1000, 600, Image.SCALE_SMOOTH))); -// } else { -// imageIconLabel.setIcon(new ImageIcon(imageContent)); -// } - } - private boolean isMac() { return SystemUtils.IS_OS_MAC; }