Move utility methods to ClipboardUtils.java & Add functionality to insert contents into the clipboard

MYC-Refactor
Hammy 3 years ago
parent 07a33f71b4
commit ecb63e41a2

@ -3,17 +3,9 @@ package me.goudham;
import java.awt.Image; import java.awt.Image;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard; 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.File;
import java.io.IOException;
import java.util.List; 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 { abstract class ClipboardListener {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private EventManager eventManager = new EventManager(); private EventManager eventManager = new EventManager();
@ -22,58 +14,28 @@ abstract class ClipboardListener {
private boolean fileListMonitored = true; 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} * @see MacClipboardListener
* @return {@link String} representation of {@code clipboardContents} * @see WindowsOrUnixClipboardListener
*/ */
String getStringContent(Transferable clipboardContents) { abstract void execute();
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 startListening();
* 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;
}
List<File> getFileContent(Transferable clipboardContents) { abstract void stopListening();
List<File> fileList = null;
try { abstract void insert(String stringContent);
if (clipboardContents.isDataFlavorSupported(FILELIST.getDataFlavor())) {
fileList = (List<File>) clipboardContents.getTransferData(FILELIST.getDataFlavor());
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
return fileList; abstract void insert(Image imageContent);
}
abstract void insert(List<File> fileContent);
abstract void insertAndNotify(String stringContent);
abstract void insertAndNotify(Image imageContent);
abstract void insertAndNotify(List<File> fileContent);
void toggleTextMonitored() { void toggleTextMonitored() {
this.textMonitored = !textMonitored; this.textMonitored = !textMonitored;
@ -114,12 +76,4 @@ abstract class ClipboardListener {
void setEventManager(EventManager eventManager) { void setEventManager(EventManager eventManager) {
this.eventManager = eventManager; this.eventManager = eventManager;
} }
/**
* Main entry point of execution for both {@link MacClipboardListener} and {@link WindowsOrUnixClipboardListener}
*
* @see MacClipboardListener
* @see WindowsOrUnixClipboardListener
*/
abstract void execute();
} }

Loading…
Cancel
Save