Update MyClipboardContent.java to store old & new content

main
sgoudham 3 years ago
parent f9e934d10c
commit f15d371604
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -2,35 +2,44 @@ package me.goudham.model;
import java.util.Objects; import java.util.Objects;
public class MyClipboardContent<T> { public class MyClipboardContent<T, U> {
private T content; private T oldContent;
private U newContent;
public MyClipboardContent() { public MyClipboardContent() {
} }
public MyClipboardContent(T content) { public MyClipboardContent(T oldContent) {
this.content = content; this.oldContent = oldContent;
} }
public void setContent(Object content) { public void setOldContent(Object oldContent) {
this.content = (T) content; this.oldContent = (T) oldContent;
} }
public T getContent() { public void setNewContent(Object newContent) {
return content; this.newContent = (U) newContent;
}
public T getOldContent() {
return oldContent;
}
public U getNewContent() {
return newContent;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
MyClipboardContent<?> that = (MyClipboardContent<?>) o; MyClipboardContent<?, ?> that = (MyClipboardContent<?, ?>) o;
return Objects.equals(content, that.content); return Objects.equals(oldContent, that.oldContent) && Objects.equals(newContent, that.newContent);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(content); return Objects.hash(oldContent, newContent);
} }
} }

@ -15,15 +15,15 @@ import static me.goudham.model.Contents.STRING;
public class ClipboardUtils { public class ClipboardUtils {
public static MyClipboardContent<?> getClipboardContents(Transferable contents, Clipboard clipboard) { public static MyClipboardContent<?, ?> getClipboardContents(Transferable contents, Clipboard clipboard) {
MyClipboardContent<?> myClipboardContent = new MyClipboardContent<>(""); MyClipboardContent<?, ?> myClipboardContent = new MyClipboardContent<>();
try { try {
if (STRING.isAvailable(clipboard)) { if (STRING.isAvailable(clipboard)) {
myClipboardContent.setContent(contents.getTransferData(STRING.getDataFlavor())); myClipboardContent.setOldContent(contents.getTransferData(STRING.getDataFlavor()));
} else if (IMAGE.isAvailable(clipboard)) { } else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor())); 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) { } catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace(); exp.printStackTrace();

@ -12,15 +12,16 @@ import javax.swing.JList;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import me.goudham.listener.ClipboardEvent; import me.goudham.listener.ClipboardEvent;
import me.goudham.listener.ClipboardListener;
import me.goudham.listener.MacClipboardListener; import me.goudham.listener.MacClipboardListener;
import me.goudham.listener.WindowsOrUnixClipboardListener; import me.goudham.listener.WindowsOrUnixClipboardListener;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
public class ClipboardView implements ClipboardEvent { public class ClipboardView {
private JPanel clipboard; private JPanel clipboard;
private JButton copySelectedTextButton; private JButton copySelectedTextButton;
private JList<String> clipboardContentList; private JList<String> clipboardContentList;
private final DefaultListModel<String> listModel; private final DefaultListModel<String> listModel = new DefaultListModel<>();
private JLabel title; private JLabel title;
private JPanel textButtonPanel; private JPanel textButtonPanel;
private JButton clearAllHistoryButton; private JButton clearAllHistoryButton;
@ -35,29 +36,28 @@ public class ClipboardView implements ClipboardEvent {
private boolean toggle = true; private boolean toggle = true;
private BufferedImage storedImageContent; private BufferedImage storedImageContent;
private ClipboardListener clipboardListener;
public ClipboardView() { public ClipboardView() {
imageScrollPane.setBorder(BorderFactory.createEmptyBorder());
clipboardContentScrollPane.setBorder(BorderFactory.createEmptyBorder()); clipboardContentScrollPane.setBorder(BorderFactory.createEmptyBorder());
textButtonPanel.setBorder(BorderFactory.createEmptyBorder()); imageScrollPane.getVerticalScrollBar().setUnitIncrement(35);
listModel = new DefaultListModel<>(); imageScrollPane.getHorizontalScrollBar().setUnitIncrement(35);
clipboardContentScrollPane.getVerticalScrollBar().setUnitIncrement(200);
clipboardContentScrollPane.getHorizontalScrollBar().setUnitIncrement(200);
clipboardContentList.setModel(listModel); clipboardContentList.setModel(listModel);
toggleImageButton.addActionListener(actionEvent -> { toggleImageButton.addActionListener(actionEvent -> {
if (toggle) { if (storedImageContent != null) {
imageLabel.setIcon(null); if (toggle) {
// anotherImagePanel.setPreferredSize(null); toggleImageButton.setText("Show Image");
// anotherImagePanel.setVisible(false); imageLabel.setIcon(null);
// imageIconLabel.setMaximumSize(new Dimension(0, 0)); toggle = false;
// anotherImagePanel.setPreferredSize(new Dimension(0, 0)); } else {
toggle = false; imageLabel.setIcon(new ImageIcon(storedImageContent));
} else { toggleImageButton.setText("Hide Image");
// anotherImagePanel.setPreferredSize(new Dimension(300, 300)); toggle = true;
// anotherImagePanel.setVisible(true); }
imageLabel.setIcon(new ImageIcon(storedImageContent));
toggle = true;
} }
// anotherImagePanel.revalidate();
}); });
// final java.awt.datatransfer.clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); // final java.awt.datatransfer.clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
@ -102,14 +102,28 @@ public class ClipboardView implements ClipboardEvent {
} }
public void createAndShowGUI() { public void createAndShowGUI() {
me.goudham.listener.ClipboardListener clipboardListener = null;
if (isMac()) { if (isMac()) {
clipboardListener = new MacClipboardListener(this); clipboardListener = new MacClipboardListener();
} else if (isUnix() || isWindows()) { } else if (isUnix() || isWindows()) {
clipboardListener = new WindowsOrUnixClipboardListener(this); clipboardListener = new WindowsOrUnixClipboardListener();
} }
clipboardListener.execute(); 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 jFrame = new JFrame();
jFrame.setTitle("My Clipboard History"); jFrame.setTitle("My Clipboard History");
jFrame.setContentPane(clipboard); jFrame.setContentPane(clipboard);
@ -122,25 +136,6 @@ public class ClipboardView implements ClipboardEvent {
jFrame.setLocationRelativeTo(null); 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() { private boolean isMac() {
return SystemUtils.IS_OS_MAC; return SystemUtils.IS_OS_MAC;
} }

Loading…
Cancel
Save