Rename OldClipboardContent.java to ClipboardContent.java

pull/2/head
Hammy 3 years ago
parent 4ed59cc691
commit cf598be605

@ -8,7 +8,7 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -106,47 +106,47 @@ class ClipboardUtils {
}
/**
* Store contents from the given {@link Transferable} into {@link OldClipboardContent}
* Store contents from the given {@link Transferable} into {@link ClipboardContent}
*
* @param oldContents The given {@link Transferable} which holds the clipboard contents
* @return {@link OldClipboardContent} containing old clipboard contents
* @return {@link ClipboardContent} containing old clipboard contents
*/
OldClipboardContent getOldClipboardContent(Transferable oldContents) {
OldClipboardContent oldClipboardContent = null;
ClipboardContent getOldClipboardContent(Transferable oldContents) {
ClipboardContent clipboardContent = null;
try {
if (oldContents.isDataFlavorSupported(TEXT.getDataFlavor())) {
oldClipboardContent = new OldClipboardContent((String) oldContents.getTransferData(TEXT.getDataFlavor()));
clipboardContent = new ClipboardContent((String) oldContents.getTransferData(TEXT.getDataFlavor()));
} else if (oldContents.isDataFlavorSupported(IMAGE.getDataFlavor())) {
oldClipboardContent = new OldClipboardContent(convertToBufferedImage((Image) oldContents.getTransferData(IMAGE.getDataFlavor())));
clipboardContent = new ClipboardContent(convertToBufferedImage((Image) oldContents.getTransferData(IMAGE.getDataFlavor())));
} else if (oldContents.isDataFlavorSupported(FILE.getDataFlavor())) {
oldClipboardContent = new OldClipboardContent((List<File>) oldContents.getTransferData(FILE.getDataFlavor()));
clipboardContent = new ClipboardContent((List<File>) oldContents.getTransferData(FILE.getDataFlavor()));
}
} catch (UnsupportedFlavorException | IOException exp) {
logger.error("Exception Thrown When Retrieving Clipboard Contents", exp);
}
return oldClipboardContent;
return clipboardContent;
}
/**
* Store contents from the given {@link Object} into {@link OldClipboardContent}
* Store contents from the given {@link Object} into {@link ClipboardContent}
*
* @param object The given {@link Object} which holds the clipboard contents
* @return {@link OldClipboardContent} containing old clipboard contents
* @return {@link ClipboardContent} containing old clipboard contents
*/
OldClipboardContent getOldClipboardContent(Object object) {
OldClipboardContent oldClipboardContent = null;
ClipboardContent getOldClipboardContent(Object object) {
ClipboardContent clipboardContent = null;
if (object instanceof String) {
oldClipboardContent = new OldClipboardContent((String) object);
clipboardContent = new ClipboardContent((String) object);
} else if (object instanceof MyBufferedImage) {
oldClipboardContent = new OldClipboardContent(((MyBufferedImage) object).getBufferedImage());
clipboardContent = new ClipboardContent(((MyBufferedImage) object).getBufferedImage());
} else if (object instanceof List) {
oldClipboardContent = new OldClipboardContent((List<File>) object);
clipboardContent = new ClipboardContent((List<File>) object);
}
return oldClipboardContent;
return clipboardContent;
}
/**

@ -4,7 +4,7 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
import me.goudham.event.FileEvent;
import me.goudham.event.ImageEvent;
import me.goudham.event.TextEvent;
@ -74,36 +74,36 @@ class EventManager {
/**
* Produces {@link String} change notifications to all consumers listening
*
* @param oldClipboardContent The previous clipboard contents
* @param clipboardContent The previous clipboard contents
* @param stringContent {@link String} to be consumed
*/
void notifyTextEvent(OldClipboardContent oldClipboardContent, String stringContent) {
void notifyTextEvent(ClipboardContent clipboardContent, String stringContent) {
for (TextEvent textEvent : textEventListener) {
textEvent.onCopyText(oldClipboardContent, stringContent);
textEvent.onCopyText(clipboardContent, stringContent);
}
}
/**
* Produces {@link BufferedImage} change notifications to all consumers listening
*
* @param oldClipboardContent The previous clipboard contents
* @param clipboardContent The previous clipboard contents
* @param imageContent {@link BufferedImage} to be consumed
*/
void notifyImageEvent(OldClipboardContent oldClipboardContent, BufferedImage imageContent) {
void notifyImageEvent(ClipboardContent clipboardContent, BufferedImage imageContent) {
for (ImageEvent imageEvent : imageEventListener) {
imageEvent.onCopyImage(oldClipboardContent, imageContent);
imageEvent.onCopyImage(clipboardContent, imageContent);
}
}
/**
* Produces {@link List} of {@link File} change notifications to all consumers listening
*
* @param oldClipboardContent The previous clipboard contents
* @param clipboardContent The previous clipboard contents
* @param fileContent {@link List} of {@link File} to be consumed
*/
void notifyFilesEvent(OldClipboardContent oldClipboardContent, List<File> fileContent) {
void notifyFilesEvent(ClipboardContent clipboardContent, List<File> fileContent) {
for (FileEvent fileEvent : fileEventListener) {
fileEvent.onCopyFiles(oldClipboardContent, fileContent);
fileEvent.onCopyFiles(clipboardContent, fileContent);
}
}
}

@ -8,7 +8,7 @@ import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
import static java.lang.Thread.sleep;
import static me.goudham.Contents.FILE;
@ -38,8 +38,8 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
if (isTextMonitored()) {
Object oldContent = genericClipboardContents[0].getOldContent();
if (!newStringContent.equals(oldContent)) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldContent);
eventManager.notifyTextEvent(oldClipboardContent, newStringContent);
ClipboardContent clipboardContent = clipboardUtils.getOldClipboardContent(oldContent);
eventManager.notifyTextEvent(clipboardContent, newStringContent);
}
}
@ -60,8 +60,8 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
if (isImageMonitored()) {
if (!bufferedImageContent.equals(genericClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyImageEvent(oldClipboardContent, bufferedImageContent.getBufferedImage());
ClipboardContent clipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyImageEvent(clipboardContent, bufferedImageContent.getBufferedImage());
}
}
@ -83,8 +83,8 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
if (isFileMonitored()) {
if (!fileListContent.equals(genericClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyFilesEvent(oldClipboardContent, fileListContent);
ClipboardContent clipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyFilesEvent(clipboardContent, fileListContent);
}
}

@ -9,7 +9,7 @@ import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
@ -44,13 +44,13 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
* @param newClipboardContents The new contents of the clipboard
*/
void processContents(Clipboard oldClipboard, Transferable oldClipboardContents, Transferable newClipboardContents) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldClipboardContents);
ClipboardContent clipboardContent = clipboardUtils.getOldClipboardContent(oldClipboardContents);
if (isTextMonitored()) {
if (TEXT.isAvailable(oldClipboard) && !FILE.isAvailable(oldClipboard)) {
String stringContent = clipboardUtils.getStringContent(newClipboardContents);
if (!stringContent.equals(oldClipboardContent.getOldText())) {
eventManager.notifyTextEvent(oldClipboardContent, stringContent);
if (!stringContent.equals(clipboardContent.getText())) {
eventManager.notifyTextEvent(clipboardContent, stringContent);
}
}
}
@ -58,9 +58,9 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isImageMonitored()) {
if (IMAGE.isAvailable(oldClipboard)) {
MyBufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents);
MyBufferedImage oldBufferedImage = new MyBufferedImage(oldClipboardContent.getOldImage());
MyBufferedImage oldBufferedImage = new MyBufferedImage(clipboardContent.getBufferedImage());
if (!bufferedImage.equals(oldBufferedImage)) {
eventManager.notifyImageEvent(oldClipboardContent, bufferedImage.getBufferedImage());
eventManager.notifyImageEvent(clipboardContent, bufferedImage.getBufferedImage());
}
}
}
@ -68,8 +68,8 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isFileMonitored()) {
if (FILE.isAvailable(oldClipboard)) {
List<File> fileList = clipboardUtils.getFileContent(newClipboardContents);
if (!fileList.equals(oldClipboardContent.getOldFiles())) {
eventManager.notifyFilesEvent(oldClipboardContent, fileList);
if (!fileList.equals(clipboardContent.getFiles())) {
eventManager.notifyFilesEvent(clipboardContent, fileList);
}
}
}

@ -0,0 +1,35 @@
package me.goudham.domain;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
public class ClipboardContent {
private String text;
private BufferedImage bufferedImage;
private List<File> files;
public ClipboardContent(String text) {
this.text = text;
}
public ClipboardContent(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public ClipboardContent(List<File> files) {
this.files = files;
}
public BufferedImage getBufferedImage() {
return bufferedImage;
}
public String getText() {
return text;
}
public List<File> getFiles() {
return files;
}
}

@ -1,35 +0,0 @@
package me.goudham.domain;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
public class OldClipboardContent {
private String oldText;
private BufferedImage oldImage;
private List<File> oldFiles;
public OldClipboardContent(String oldText) {
this.oldText = oldText;
}
public OldClipboardContent(BufferedImage oldImage) {
this.oldImage = oldImage;
}
public OldClipboardContent(List<File> oldFiles) {
this.oldFiles = oldFiles;
}
public BufferedImage getOldImage() {
return oldImage;
}
public String getOldText() {
return oldText;
}
public List<File> getOldFiles() {
return oldFiles;
}
}

@ -2,8 +2,8 @@ package me.goudham.event;
import java.io.File;
import java.util.List;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
public interface FileEvent extends ClipboardEvent {
void onCopyFiles(OldClipboardContent oldContent, List<File> newContent);
void onCopyFiles(ClipboardContent oldContent, List<File> newContent);
}

@ -1,8 +1,8 @@
package me.goudham.event;
import java.awt.image.BufferedImage;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
public interface ImageEvent extends ClipboardEvent {
void onCopyImage(OldClipboardContent oldContent, BufferedImage newContent);
void onCopyImage(ClipboardContent oldContent, BufferedImage newContent);
}

@ -1,7 +1,7 @@
package me.goudham.event;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
public interface TextEvent extends ClipboardEvent {
void onCopyText(OldClipboardContent oldContent, String newContent);
void onCopyText(ClipboardContent oldContent, String newContent);
}

@ -9,7 +9,7 @@ import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
import me.goudham.domain.OldClipboardContent;
import me.goudham.domain.ClipboardContent;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@ -143,22 +143,22 @@ class ClipboardUtilsTest {
when(transferableMock.isDataFlavorSupported(dataFlavor)).thenReturn(true);
when(transferableMock.getTransferData(dataFlavor)).thenReturn(expectedContent);
OldClipboardContent actualOldClipboardContent = sut.getOldClipboardContent(transferableMock);
ClipboardContent actualClipboardContent = sut.getOldClipboardContent(transferableMock);
assertThat(actualOldClipboardContent.getOldText(), is(expectedString));
assertThat(actualOldClipboardContent.getOldFiles(), is(expectedFiles));
assertThat(actualOldClipboardContent.getOldImage(), is(new BufferedImageMatcher(expectedImage)));
assertThat(actualClipboardContent.getText(), is(expectedString));
assertThat(actualClipboardContent.getFiles(), is(expectedFiles));
assertThat(actualClipboardContent.getBufferedImage(), is(new BufferedImageMatcher(expectedImage)));
verifyNoInteractions(logger);
}
@ParameterizedTest
@MethodSource("provideArgumentsForOldClipboardContents")
void successfullyMarshallClipboardContentsIntoOldClipboardContent(Object expectedOldContent, String expectedString, BufferedImage expectedImage, List<File> expectedFiles) {
OldClipboardContent actualOldClipboardContent = sut.getOldClipboardContent(expectedOldContent);
ClipboardContent actualClipboardContent = sut.getOldClipboardContent(expectedOldContent);
assertThat(actualOldClipboardContent.getOldText(), is(expectedString));
assertThat(actualOldClipboardContent.getOldImage(), is(expectedImage));
assertThat(actualOldClipboardContent.getOldFiles(), is(expectedFiles));
assertThat(actualClipboardContent.getText(), is(expectedString));
assertThat(actualClipboardContent.getBufferedImage(), is(expectedImage));
assertThat(actualClipboardContent.getFiles(), is(expectedFiles));
verifyNoInteractions(logger);
}

Loading…
Cancel
Save