From 23d677beca6fde9d7bb3a0b697eb8967b12be070 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 22 Jul 2021 23:11:15 +0100 Subject: [PATCH] Delete redundant code --- .../java/me/goudham/ClipboardHistory.java | 110 ------------------ .../java/me/goudham/view/ClipboardView.java | 52 +++------ 2 files changed, 13 insertions(+), 149 deletions(-) delete mode 100644 src/main/java/me/goudham/ClipboardHistory.java diff --git a/src/main/java/me/goudham/ClipboardHistory.java b/src/main/java/me/goudham/ClipboardHistory.java deleted file mode 100644 index cb6d2f7..0000000 --- a/src/main/java/me/goudham/ClipboardHistory.java +++ /dev/null @@ -1,110 +0,0 @@ -//package me.goudham; -// -//import java.awt.BorderLayout; -//import java.awt.Dimension; -//import java.awt.GridLayout; -//import java.awt.Toolkit; -//import java.awt.datatransfer.Clipboard; -//import java.awt.datatransfer.StringSelection; -//import java.awt.event.ActionEvent; -//import java.awt.event.ActionListener; -// -//import javax.swing.BorderFactory; -//import javax.swing.BoxLayout; -//import javax.swing.DefaultListModel; -//import javax.swing.JButton; -//import javax.swing.JFrame; -//import javax.swing.JList; -//import javax.swing.JPanel; -//import javax.swing.JScrollPane; -//import javax.swing.JSplitPane; -//import javax.swing.ListSelectionModel; -//import javax.swing.SwingUtilities; -// -//// We are going to build the UI of our Clipboard History software -//// We will use Swing API -//public class ClipboardHistory extends JPanel implements ClipboardListener.EntryListener { -// -// // for the list of entries copied in clipboard -// private final JList list; -// private final DefaultListModel listModel; -// private ListSelectionModel listSelectionModel; -// -// public ClipboardHistory() { -// super(new BorderLayout()); -// listModel = new DefaultListModel<>(); -// list = new JList<>(listModel); -// listSelectionModel = list.getSelectionModel(); -// listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); -// -// // we create a JScrollPane to embed our control pane -// JScrollPane listPane = new JScrollPane(list); -// JPanel controlPane = new JPanel(); -// -// // we add a button to let users copy old entries to the clipboard -// final JButton button = new JButton("Copy"); -// button.addActionListener(e -> { -// String value = list.getSelectedValue(); -// int index = list.getSelectedIndex(); -// // remove selected index to avoid duplicate in our list ... -// listModel.remove(index); -// // copy to clipboard -// copyToClipboard(value); -// }); -// -// // we add the button -// controlPane.add(button); -// -// JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); -// add(splitPane, BorderLayout.CENTER); -// -// JPanel topHalf = new JPanel(); -// topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS)); -// JPanel listContainer = new JPanel(new GridLayout(1, 1)); -// listContainer.setBorder(BorderFactory.createTitledBorder("Entries")); -// listContainer.add(listPane); -// -// topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); -// topHalf.add(listContainer); -// topHalf.setMinimumSize(new Dimension(100, 50)); -// topHalf.setPreferredSize(new Dimension(100, 250)); -// splitPane.add(topHalf); -// -// JPanel bottomHalf = new JPanel(new BorderLayout()); -// bottomHalf.add(controlPane, BorderLayout.CENTER); -// bottomHalf.setPreferredSize(new Dimension(450, 30)); -// splitPane.add(bottomHalf); -// } -// -// public void copyToClipboard(String value) { -// Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); -// StringSelection data = new StringSelection(value); -// clipboard.setContents(data, data); -// } -// -// public void createAndShowGUI() { -// // We create a top JFrame -// JFrame frame = new JFrame("Clipboard History"); -// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// -// setOpaque(true); -// frame.setContentPane(this); -// frame.pack(); -// frame.setVisible(true); // we display on the screen -// -// // we connect the Clipboard Listener to our UI -// ClipboardListener listener = new ClipboardListener(this); -// listener.start(); -// } -// -// @Override -// public void onCopy(String data) { -// // we add new entry on the top of our list -// listModel.add(0, data); -// } -// -// public static void main(String[] args) { -// SwingUtilities.invokeLater(() -> new ClipboardHistory().createAndShowGUI()); -// } -// -//} diff --git a/src/main/java/me/goudham/view/ClipboardView.java b/src/main/java/me/goudham/view/ClipboardView.java index bb8c571..063d374 100644 --- a/src/main/java/me/goudham/view/ClipboardView.java +++ b/src/main/java/me/goudham/view/ClipboardView.java @@ -1,6 +1,9 @@ package me.goudham.view; import java.awt.Dimension; +import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; import java.awt.image.BufferedImage; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; @@ -11,6 +14,7 @@ import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; +import javax.swing.ListSelectionModel; import me.goudham.listener.ClipboardEvent; import me.goudham.listener.ClipboardListener; import me.goudham.listener.MacClipboardListener; @@ -22,6 +26,7 @@ public class ClipboardView { private JButton copySelectedTextButton; private JList clipboardContentList; private final DefaultListModel listModel = new DefaultListModel<>(); + private ListSelectionModel listSelectionModel = clipboardContentList.getSelectionModel(); private JLabel title; private JPanel textButtonPanel; private JButton clearAllHistoryButton; @@ -45,6 +50,7 @@ public class ClipboardView { clipboardContentScrollPane.getVerticalScrollBar().setUnitIncrement(200); clipboardContentScrollPane.getHorizontalScrollBar().setUnitIncrement(200); clipboardContentList.setModel(listModel); + listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); toggleImageButton.addActionListener(actionEvent -> { if (storedImageContent != null) { @@ -60,45 +66,13 @@ public class ClipboardView { } }); -// final java.awt.datatransfer.clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); -// final MyClipboardContent previousContent = new MyClipboardContent<>(""); -// final int[] count = {0}; -// -// systemClipboard.addFlavorListener(e -> { -// MyClipboardContent clipboardContent = null; -// try { -// if (systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) { -// String stringContent = (String) systemClipboard.getData(DataFlavor.stringFlavor); -// clipboardContent = new MyClipboardContent<>(stringContent); -// } else if (systemClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) { -// Image imageContent = (Image) systemClipboard.getData(DataFlavor.imageFlavor); -// clipboardContent = new MyClipboardContent<>(imageContent); -// } -// } catch (UnsupportedFlavorException | IOException unsupportedFlavorException) { -// unsupportedFlavorException.printStackTrace(); -// } -// -// if (previousContent.getContent().equals(clipboardContent.getContent())) { -// count[0]++; -// } -// -// if (!previousContent.getContent().equals(clipboardContent.getContent()) || count[0] > 1) { -// systemClipboard.setContents(new TransferableImage((Image) clipboardContent.getContent()), null); -//// systemClipboard.setContents(new StringSelection((String) clipboardContent.getContent()), null); -// System.out.println("The clipboard contains: " + clipboardContent.getContent()); -// -// DefaultListModel demoList = new DefaultListModel<>(); -// int size = clipboardContentList.getModel().getSize(); -// demoList.addElement("0. " + clipboardContent.getContent()); -// for (int i = 0; i < size; i++) { -// demoList.addElement((i + 1) + ". " + clipboardContentList.getModel().getElementAt(i)); -// } -// clipboardContentList.setModel(demoList); -// -// previousContent.setContent(clipboardContent.getContent()); -// count[0] = 0; -// } -// }); + copySelectedTextButton.addActionListener(actionEvent -> { + String selectedValue = clipboardContentList.getSelectedValue(); + int selectedIndex = clipboardContentList.getSelectedIndex(); + listModel.remove(selectedIndex); + Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + systemClipboard.setContents(new StringSelection(selectedValue), null); + }); } public void createAndShowGUI() {