Make ClipboardUtils.java not static

MYC-Refactor
Hammy 3 years ago
parent 48a53c7f31
commit 188894f9d9

@ -13,13 +13,21 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
abstract class ClipboardListener { abstract class ClipboardListener {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Clipboard clipboard;
final Logger logger = LoggerFactory.getLogger(getClass()); Logger logger;
private EventManager eventManager = new EventManager(); EventManager eventManager;
ClipboardUtils clipboardUtils;
private boolean imageMonitored = true; private boolean imageMonitored = true;
private boolean textMonitored = true; private boolean textMonitored = true;
private boolean fileMonitored = true; private boolean fileMonitored = true;
ClipboardListener() {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
logger = LoggerFactory.getLogger(getClass());
eventManager = new EventManager();
clipboardUtils = new ClipboardUtils();
}
/** /**
* Main entry point of execution for the correct {@link ClipboardListener} * Main entry point of execution for the correct {@link ClipboardListener}
* *
@ -75,6 +83,38 @@ abstract class ClipboardListener {
this.fileMonitored = !fileMonitored; this.fileMonitored = !fileMonitored;
} }
Clipboard getClipboard() {
return clipboard;
}
void setClipboard(Clipboard clipboard) {
this.clipboard = clipboard;
}
Logger getLogger() {
return logger;
}
void setLogger(Logger logger) {
this.logger = logger;
}
EventManager getEventManager() {
return eventManager;
}
void setEventManager(EventManager eventManager) {
this.eventManager = eventManager;
}
ClipboardUtils getClipboardUtils() {
return clipboardUtils;
}
void setClipboardUtils(ClipboardUtils clipboardUtils) {
this.clipboardUtils = clipboardUtils;
}
boolean isImageMonitored() { boolean isImageMonitored() {
return imageMonitored; return imageMonitored;
} }
@ -99,14 +139,6 @@ abstract class ClipboardListener {
this.fileMonitored = fileMonitored; this.fileMonitored = fileMonitored;
} }
EventManager getEventManager() {
return eventManager;
}
void setEventManager(EventManager eventManager) {
this.eventManager = eventManager;
}
static class TransferableFileList implements Transferable { static class TransferableFileList implements Transferable {
private final List<File> fileList; private final List<File> fileList;

@ -19,7 +19,9 @@ import static me.goudham.Contents.IMAGE;
import static me.goudham.Contents.TEXT; import static me.goudham.Contents.TEXT;
class ClipboardUtils { class ClipboardUtils {
protected static Logger logger = LoggerFactory.getLogger(ClipboardUtils.class); private static Logger logger = LoggerFactory.getLogger(ClipboardUtils.class);
ClipboardUtils() {}
/** /**
* Try to unmarshal {@link Transferable} into {@link String} * Try to unmarshal {@link Transferable} into {@link String}
@ -27,7 +29,7 @@ class ClipboardUtils {
* @param clipboardContents The {@link Transferable} to be converted into {@link String} * @param clipboardContents The {@link Transferable} to be converted into {@link String}
* @return {@link String} representation of {@code clipboardContents} * @return {@link String} representation of {@code clipboardContents}
*/ */
static String getStringContent(Transferable clipboardContents) { String getStringContent(Transferable clipboardContents) {
String newContent = null; String newContent = null;
try { try {
@ -35,7 +37,6 @@ class ClipboardUtils {
newContent = (String) clipboardContents.getTransferData(TEXT.getDataFlavor()); newContent = (String) clipboardContents.getTransferData(TEXT.getDataFlavor());
} }
} catch (UnsupportedFlavorException | IOException exp) { } catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
logger.info("Exception Thrown When Receiving String Content", exp); logger.info("Exception Thrown When Receiving String Content", exp);
} }
@ -48,15 +49,15 @@ class ClipboardUtils {
* @param clipboardContents The {@link Transferable} to be converted into {@link BufferedImage} * @param clipboardContents The {@link Transferable} to be converted into {@link BufferedImage}
* @return {@link BufferedImage} representation of {@code clipboardContents} * @return {@link BufferedImage} representation of {@code clipboardContents}
*/ */
static BufferedImage getImageContent(Transferable clipboardContents) { BufferedImage getImageContent(Transferable clipboardContents) {
BufferedImage bufferedImage = null; BufferedImage bufferedImage = null;
try { try {
if (clipboardContents.isDataFlavorSupported(IMAGE.getDataFlavor())) { if (clipboardContents.isDataFlavorSupported(IMAGE.getDataFlavor())) {
bufferedImage = ClipboardUtils.convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor())); bufferedImage = convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor()));
} }
} catch (UnsupportedFlavorException | IOException exp) { } catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace(); logger.info("Exception Thrown When Receiving Image Content", exp);
} }
return bufferedImage; return bufferedImage;
@ -68,7 +69,7 @@ class ClipboardUtils {
* @param clipboardContents The {@link Transferable} to be converted into {@link List} of {@link File} * @param clipboardContents The {@link Transferable} to be converted into {@link List} of {@link File}
* @return {@link List} of {@link File} representation of {@code clipboardContents} * @return {@link List} of {@link File} representation of {@code clipboardContents}
*/ */
static List<File> getFileContent(Transferable clipboardContents) { List<File> getFileContent(Transferable clipboardContents) {
List<File> fileList = null; List<File> fileList = null;
try { try {
@ -76,13 +77,13 @@ class ClipboardUtils {
fileList = (List<File>) clipboardContents.getTransferData(FILELIST.getDataFlavor()); fileList = (List<File>) clipboardContents.getTransferData(FILELIST.getDataFlavor());
} }
} catch (UnsupportedFlavorException | IOException exp) { } catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace(); logger.info("Exception Thrown When Receiving File Content", exp);
} }
return fileList; return fileList;
} }
static MyClipboardContent<?> getClipboardContents(Transferable contents, Clipboard clipboard) { MyClipboardContent<?> getClipboardContents(Transferable contents, Clipboard clipboard) {
MyClipboardContent<?> myClipboardContent = new MyClipboardContent<>(); MyClipboardContent<?> myClipboardContent = new MyClipboardContent<>();
try { try {
@ -102,7 +103,7 @@ class ClipboardUtils {
return myClipboardContent; return myClipboardContent;
} }
static OldClipboardContent getOldClipboardContent(Transferable oldContents) { OldClipboardContent getOldClipboardContent(Transferable oldContents) {
OldClipboardContent oldClipboardContent = null; OldClipboardContent oldClipboardContent = null;
try { try {
@ -120,7 +121,7 @@ class ClipboardUtils {
return oldClipboardContent; return oldClipboardContent;
} }
static OldClipboardContent getOldClipboardContent(Object object) { OldClipboardContent getOldClipboardContent(Object object) {
OldClipboardContent oldClipboardContent = null; OldClipboardContent oldClipboardContent = null;
if (object instanceof String) { if (object instanceof String) {
@ -134,7 +135,7 @@ class ClipboardUtils {
return oldClipboardContent; return oldClipboardContent;
} }
static BufferedImage convertToBufferedImage(Image image) { BufferedImage convertToBufferedImage(Image image) {
BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = newImage.createGraphics(); Graphics2D graphics = newImage.createGraphics();
@ -143,4 +144,12 @@ class ClipboardUtils {
return newImage; return newImage;
} }
public static Logger getLogger() {
return logger;
}
public static void setLogger(Logger logger) {
ClipboardUtils.logger = logger;
}
} }

@ -13,9 +13,6 @@ import java.util.concurrent.TimeUnit;
import me.goudham.domain.OldClipboardContent; import me.goudham.domain.OldClipboardContent;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
import static me.goudham.ClipboardUtils.getFileContent;
import static me.goudham.ClipboardUtils.getImageContent;
import static me.goudham.ClipboardUtils.getStringContent;
import static me.goudham.Contents.FILELIST; import static me.goudham.Contents.FILELIST;
import static me.goudham.Contents.IMAGE; import static me.goudham.Contents.IMAGE;
import static me.goudham.Contents.TEXT; import static me.goudham.Contents.TEXT;
@ -26,8 +23,9 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
private boolean listening = false; private boolean listening = false;
MacClipboardListener() { MacClipboardListener() {
super();
Transferable oldClipboardContents = clipboard.getContents(null); Transferable oldClipboardContents = clipboard.getContents(null);
myClipboardContents = new MyClipboardContent[] { ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) }; myClipboardContents = new MyClipboardContent[] { clipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
} }
/** /**
@ -38,13 +36,13 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
*/ */
void checkText(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkText(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (TEXT.isAvailable(clipboard) && !FILELIST.isAvailable(clipboard)) { if (TEXT.isAvailable(clipboard) && !FILELIST.isAvailable(clipboard)) {
String newStringContent = getStringContent(newClipboardContents); String newStringContent = clipboardUtils.getStringContent(newClipboardContents);
if (newStringContent == null) return; if (newStringContent == null) return;
if (isTextMonitored()) { if (isTextMonitored()) {
Object oldContent = myClipboardContents[0].getOldContent(); Object oldContent = myClipboardContents[0].getOldContent();
if (!newStringContent.equals(oldContent)) { if (!newStringContent.equals(oldContent)) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldContent); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldContent);
getEventManager().notifyTextEvent(oldClipboardContent, newStringContent); getEventManager().notifyTextEvent(oldClipboardContent, newStringContent);
} }
} }
@ -61,14 +59,14 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
*/ */
void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (IMAGE.isAvailable(clipboard)) { if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImageContent = getImageContent(newClipboardContents); BufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents);
if (bufferedImageContent == null) return; if (bufferedImageContent == null) return;
Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight()); Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight());
OldImage newImageContent = new OldImage(bufferedImageContent, newDimensionContent); OldImage newImageContent = new OldImage(bufferedImageContent, newDimensionContent);
if (isImageMonitored()) { if (isImageMonitored()) {
if (!newImageContent.equals(myClipboardContents[0].getOldContent())) { if (!newImageContent.equals(myClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent());
getEventManager().notifyImageEvent(oldClipboardContent, bufferedImageContent); getEventManager().notifyImageEvent(oldClipboardContent, bufferedImageContent);
} }
} }
@ -86,12 +84,12 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
*/ */
void checkFiles(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkFiles(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (FILELIST.isAvailable(clipboard)) { if (FILELIST.isAvailable(clipboard)) {
List<File> fileListContent = getFileContent(newClipboardContents); List<File> fileListContent = clipboardUtils.getFileContent(newClipboardContents);
if (fileListContent == null) return; if (fileListContent == null) return;
if (isFileMonitored()) { if (isFileMonitored()) {
if (!fileListContent.equals(myClipboardContents[0].getOldContent())) { if (!fileListContent.equals(myClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent());
getEventManager().notifyFilesEvent(oldClipboardContent, fileListContent); getEventManager().notifyFilesEvent(oldClipboardContent, fileListContent);
} }
} }

@ -15,9 +15,6 @@ import me.goudham.domain.OldClipboardContent;
import static java.lang.Thread.currentThread; import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
import static me.goudham.ClipboardUtils.getFileContent;
import static me.goudham.ClipboardUtils.getImageContent;
import static me.goudham.ClipboardUtils.getStringContent;
import static me.goudham.Contents.FILELIST; import static me.goudham.Contents.FILELIST;
import static me.goudham.Contents.IMAGE; import static me.goudham.Contents.IMAGE;
import static me.goudham.Contents.TEXT; import static me.goudham.Contents.TEXT;
@ -26,7 +23,9 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
private ExecutorService executorService = Executors.newSingleThreadExecutor(); private ExecutorService executorService = Executors.newSingleThreadExecutor();
private boolean listening = false; private boolean listening = false;
WindowsOrUnixClipboardListener() {} WindowsOrUnixClipboardListener() {
super();
}
@Override @Override
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) { public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
@ -47,11 +46,11 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
* @param newClipboardContents The new contents of the clipboard * @param newClipboardContents The new contents of the clipboard
*/ */
void processContents(Clipboard oldClipboard, Transferable oldClipboardContents, Transferable newClipboardContents) { void processContents(Clipboard oldClipboard, Transferable oldClipboardContents, Transferable newClipboardContents) {
OldClipboardContent oldClipboardContent = ClipboardUtils.getOldClipboardContent(oldClipboardContents); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldClipboardContents);
if (isTextMonitored()) { if (isTextMonitored()) {
if (TEXT.isAvailable(oldClipboard) && !FILELIST.isAvailable(oldClipboard)) { if (TEXT.isAvailable(oldClipboard) && !FILELIST.isAvailable(oldClipboard)) {
String stringContent = getStringContent(newClipboardContents); String stringContent = clipboardUtils.getStringContent(newClipboardContents);
if (!stringContent.equals(oldClipboardContent.getOldText())) { if (!stringContent.equals(oldClipboardContent.getOldText())) {
getEventManager().notifyTextEvent(oldClipboardContent, stringContent); getEventManager().notifyTextEvent(oldClipboardContent, stringContent);
} }
@ -60,7 +59,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isImageMonitored()) { if (isImageMonitored()) {
if (IMAGE.isAvailable(oldClipboard)) { if (IMAGE.isAvailable(oldClipboard)) {
BufferedImage bufferedImage = getImageContent(newClipboardContents); BufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents);
BufferedImage oldBufferedImage = oldClipboardContent.getOldImage(); BufferedImage oldBufferedImage = oldClipboardContent.getOldImage();
if (bufferedImage != oldBufferedImage) { if (bufferedImage != oldBufferedImage) {
if (oldBufferedImage != null) { if (oldBufferedImage != null) {
@ -78,7 +77,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isFileMonitored()) { if (isFileMonitored()) {
if (FILELIST.isAvailable(oldClipboard)) { if (FILELIST.isAvailable(oldClipboard)) {
List<File> fileList = getFileContent(newClipboardContents); List<File> fileList = clipboardUtils.getFileContent(newClipboardContents);
if (!fileList.equals(oldClipboardContent.getOldFiles())) { if (!fileList.equals(oldClipboardContent.getOldFiles())) {
getEventManager().notifyFilesEvent(oldClipboardContent, fileList); getEventManager().notifyFilesEvent(oldClipboardContent, fileList);
} }

Loading…
Cancel
Save