Rename MyClipboardContent.java to GenericClipboardContent.java

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

@ -81,28 +81,28 @@ class ClipboardUtils {
} }
/** /**
* Store contents from the given {@link Transferable} into {@link MyClipboardContent} * Store contents from the given {@link Transferable} into {@link GenericClipboardContent}
* *
* @param contents The {@link Transferable} which holds the clipboard contents * @param contents The {@link Transferable} which holds the clipboard contents
* @return {@link MyClipboardContent} containing clipboard contents * @return {@link GenericClipboardContent} containing clipboard contents
*/ */
MyClipboardContent<?> getClipboardContents(Transferable contents) { GenericClipboardContent<?> getClipboardContents(Transferable contents) {
MyClipboardContent<?> myClipboardContent = new MyClipboardContent<>(); GenericClipboardContent<?> genericClipboardContent = new GenericClipboardContent<>();
try { try {
if (contents.isDataFlavorSupported(TEXT.getDataFlavor())) { if (contents.isDataFlavorSupported(TEXT.getDataFlavor())) {
myClipboardContent.setOldContent(contents.getTransferData(TEXT.getDataFlavor())); genericClipboardContent.setOldContent(contents.getTransferData(TEXT.getDataFlavor()));
} else if (contents.isDataFlavorSupported(IMAGE.getDataFlavor())) { } else if (contents.isDataFlavorSupported(IMAGE.getDataFlavor())) {
BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor())); BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor()));
myClipboardContent.setOldContent(new MyBufferedImage(bufferedImage)); genericClipboardContent.setOldContent(new MyBufferedImage(bufferedImage));
} else if (contents.isDataFlavorSupported(FILE.getDataFlavor())) { } else if (contents.isDataFlavorSupported(FILE.getDataFlavor())) {
myClipboardContent.setOldContent(contents.getTransferData(FILE.getDataFlavor())); genericClipboardContent.setOldContent(contents.getTransferData(FILE.getDataFlavor()));
} }
} catch (UnsupportedFlavorException | IOException exp) { } catch (UnsupportedFlavorException | IOException exp) {
logger.error("Exception Thrown When Retrieving Clipboard Contents", exp); logger.error("Exception Thrown When Retrieving Clipboard Contents", exp);
} }
return myClipboardContent; return genericClipboardContent;
} }
/** /**

@ -2,14 +2,14 @@ package me.goudham;
import java.util.Objects; import java.util.Objects;
class MyClipboardContent<T> { class GenericClipboardContent<T> {
private T oldContent; private T oldContent;
MyClipboardContent() { GenericClipboardContent() {
} }
MyClipboardContent(Object oldContent) { GenericClipboardContent(Object oldContent) {
this.oldContent = (T) oldContent; this.oldContent = (T) oldContent;
} }
@ -25,7 +25,7 @@ class MyClipboardContent<T> {
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; GenericClipboardContent<?> that = (GenericClipboardContent<?>) o;
return Objects.equals(oldContent, that.oldContent); return Objects.equals(oldContent, that.oldContent);
} }

@ -17,7 +17,7 @@ import static me.goudham.Contents.TEXT;
class MacClipboardListener extends ClipboardListener implements Runnable { class MacClipboardListener extends ClipboardListener implements Runnable {
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
MyClipboardContent<?>[] myClipboardContents; GenericClipboardContent<?>[] genericClipboardContents;
private boolean listening = false; private boolean listening = false;
MacClipboardListener() { MacClipboardListener() {
@ -28,22 +28,22 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
* Checks if {@link String} is within the clipboard and changed * Checks if {@link String} is within the clipboard and changed
* *
* @param newClipboardContents {@link Transferable} containing new clipboard contents * @param newClipboardContents {@link Transferable} containing new clipboard contents
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param genericClipboardContents {@link GenericClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkText(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkText(Transferable newClipboardContents, GenericClipboardContent<?>[] genericClipboardContents) {
if (TEXT.isAvailable(clipboard) && !FILE.isAvailable(clipboard)) { if (TEXT.isAvailable(clipboard) && !FILE.isAvailable(clipboard)) {
String newStringContent = clipboardUtils.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 = genericClipboardContents[0].getOldContent();
if (!newStringContent.equals(oldContent)) { if (!newStringContent.equals(oldContent)) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldContent); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(oldContent);
eventManager.notifyTextEvent(oldClipboardContent, newStringContent); eventManager.notifyTextEvent(oldClipboardContent, newStringContent);
} }
} }
myClipboardContents[0].setOldContent(newStringContent); genericClipboardContents[0].setOldContent(newStringContent);
} }
} }
@ -51,21 +51,21 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
* Checks if {@link java.awt.Image} is within the clipboard and changed * Checks if {@link java.awt.Image} is within the clipboard and changed
* *
* @param newClipboardContents {@link Transferable} containing new clipboard contents * @param newClipboardContents {@link Transferable} containing new clipboard contents
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param genericClipboardContents {@link GenericClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkImages(Transferable newClipboardContents, GenericClipboardContent<?>[] genericClipboardContents) {
if (IMAGE.isAvailable(clipboard)) { if (IMAGE.isAvailable(clipboard)) {
MyBufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents); MyBufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents);
if (bufferedImageContent.getBufferedImage() == null) return; if (bufferedImageContent.getBufferedImage() == null) return;
if (isImageMonitored()) { if (isImageMonitored()) {
if (!bufferedImageContent.equals(myClipboardContents[0].getOldContent())) { if (!bufferedImageContent.equals(genericClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyImageEvent(oldClipboardContent, bufferedImageContent.getBufferedImage()); eventManager.notifyImageEvent(oldClipboardContent, bufferedImageContent.getBufferedImage());
} }
} }
myClipboardContents[0].setOldContent(bufferedImageContent); genericClipboardContents[0].setOldContent(bufferedImageContent);
} }
} }
@ -74,21 +74,21 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
* Checks if {@link java.util.List} of {@link java.io.File} is within the clipboard and changed * Checks if {@link java.util.List} of {@link java.io.File} is within the clipboard and changed
* *
* @param newClipboardContents {@link Transferable} containing new clipboard contents * @param newClipboardContents {@link Transferable} containing new clipboard contents
* @param myClipboardContents {@link MyClipboardContent[]} of Unknown {@link Class} containing previous contents * @param genericClipboardContents {@link GenericClipboardContent[]} of Unknown {@link Class} containing previous contents
*/ */
void checkFiles(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) { void checkFiles(Transferable newClipboardContents, GenericClipboardContent<?>[] genericClipboardContents) {
if (FILE.isAvailable(clipboard)) { if (FILE.isAvailable(clipboard)) {
List<File> fileListContent = clipboardUtils.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(genericClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(genericClipboardContents[0].getOldContent());
eventManager.notifyFilesEvent(oldClipboardContent, fileListContent); eventManager.notifyFilesEvent(oldClipboardContent, fileListContent);
} }
} }
myClipboardContents[0].setOldContent(fileListContent); genericClipboardContents[0].setOldContent(fileListContent);
} }
} }
@ -168,9 +168,9 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
public void run() { public void run() {
try { try {
Transferable newClipboardContents = clipboard.getContents(null); Transferable newClipboardContents = clipboard.getContents(null);
checkText(newClipboardContents, myClipboardContents); checkText(newClipboardContents, genericClipboardContents);
checkImages(newClipboardContents, myClipboardContents); checkImages(newClipboardContents, genericClipboardContents);
checkFiles(newClipboardContents, myClipboardContents); checkFiles(newClipboardContents, genericClipboardContents);
} catch (IllegalStateException ise) { } catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise); logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
} }
@ -183,7 +183,7 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
@Override @Override
void execute() { void execute() {
Transferable oldClipboardContents = clipboard.getContents(null); Transferable oldClipboardContents = clipboard.getContents(null);
myClipboardContents = new MyClipboardContent[] { clipboardUtils.getClipboardContents(oldClipboardContents) }; genericClipboardContents = new GenericClipboardContent[] { clipboardUtils.getClipboardContents(oldClipboardContents) };
scheduledExecutorService.scheduleAtFixedRate(this, 0, 200, TimeUnit.MILLISECONDS); scheduledExecutorService.scheduleAtFixedRate(this, 0, 200, TimeUnit.MILLISECONDS);
} }
} }

@ -127,13 +127,13 @@ class ClipboardUtilsTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("provideArgumentsForRetrievingClipboardContents") @MethodSource("provideArgumentsForRetrievingClipboardContents")
void successfullyRetrieveOldClipboardContents(MyClipboardContent<?> expectedMyClipboardContent, Object expectedContent, DataFlavor dataFlavor) throws IOException, UnsupportedFlavorException { void successfullyRetrieveOldClipboardContents(GenericClipboardContent<?> expectedGenericClipboardContent, Object expectedContent, DataFlavor dataFlavor) throws IOException, UnsupportedFlavorException {
when(transferableMock.isDataFlavorSupported(dataFlavor)).thenReturn(true); when(transferableMock.isDataFlavorSupported(dataFlavor)).thenReturn(true);
when(transferableMock.getTransferData(dataFlavor)).thenReturn(expectedContent); when(transferableMock.getTransferData(dataFlavor)).thenReturn(expectedContent);
MyClipboardContent<?> actualMyClipboardContent = sut.getClipboardContents(transferableMock); GenericClipboardContent<?> actualGenericClipboardContent = sut.getClipboardContents(transferableMock);
assertThat(actualMyClipboardContent.getOldContent(), is(expectedMyClipboardContent.getOldContent())); assertThat(actualGenericClipboardContent.getOldContent(), is(expectedGenericClipboardContent.getOldContent()));
verifyNoInteractions(logger); verifyNoInteractions(logger);
} }
@ -195,9 +195,9 @@ class ClipboardUtilsTest {
List<File> files = List.of(new File("testFile")); List<File> files = List.of(new File("testFile"));
return Stream.of( return Stream.of(
Arguments.of(new MyClipboardContent<>(string), string, TEXT.getDataFlavor()), Arguments.of(new GenericClipboardContent<>(string), string, TEXT.getDataFlavor()),
Arguments.of(new MyClipboardContent<>(myBufferedImage), bufferedImage, IMAGE.getDataFlavor()), Arguments.of(new GenericClipboardContent<>(myBufferedImage), bufferedImage, IMAGE.getDataFlavor()),
Arguments.of(new MyClipboardContent<>(files), files, FILE.getDataFlavor()) Arguments.of(new GenericClipboardContent<>(files), files, FILE.getDataFlavor())
); );
} }
} }
Loading…
Cancel
Save