Use MyBufferedImage instead of OldImage.java

MYC-Refactor
Hammy 3 years ago
parent a8233b3575
commit c0088d9614

@ -1,6 +1,5 @@
package me.goudham; package me.goudham;
import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image; import java.awt.Image;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -41,23 +40,24 @@ class ClipboardUtils {
} }
/** /**
* Try to unmarshal {@link Transferable} into {@link BufferedImage} * Try to unmarshal {@link Transferable} into {@link MyBufferedImage#getBufferedImage()}
* *
* @param clipboardContents The {@link Transferable} to be converted into {@link BufferedImage} * @param clipboardContents The {@link Transferable} to be converted into {@link MyBufferedImage}
* @return {@link BufferedImage} representation of {@code clipboardContents} * @return {@link MyBufferedImage} representation of {@code clipboardContents}
*/ */
BufferedImage getImageContent(Transferable clipboardContents) { MyBufferedImage getImageContent(Transferable clipboardContents) {
BufferedImage bufferedImage = null; MyBufferedImage myBufferedImage = null;
try { try {
if (clipboardContents.isDataFlavorSupported(IMAGE.getDataFlavor())) { if (clipboardContents.isDataFlavorSupported(IMAGE.getDataFlavor())) {
bufferedImage = convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor())); BufferedImage bufferedImage = convertToBufferedImage((Image) clipboardContents.getTransferData(IMAGE.getDataFlavor()));
myBufferedImage = new MyBufferedImage(bufferedImage);
} }
} catch (UnsupportedFlavorException | IOException exp) { } catch (UnsupportedFlavorException | IOException exp) {
logger.error("Exception Thrown When Retrieving Image Content", exp); logger.error("Exception Thrown When Retrieving Image Content", exp);
} }
return bufferedImage; return myBufferedImage;
} }
/** /**
@ -94,8 +94,7 @@ class ClipboardUtils {
myClipboardContent.setOldContent(contents.getTransferData(TEXT.getDataFlavor())); myClipboardContent.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()));
Dimension imageDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()); myClipboardContent.setOldContent(new MyBufferedImage(bufferedImage));
myClipboardContent.setOldContent(new OldImage(bufferedImage, imageDimension));
} else if (contents.isDataFlavorSupported(FILELIST.getDataFlavor())) { } else if (contents.isDataFlavorSupported(FILELIST.getDataFlavor())) {
myClipboardContent.setOldContent(contents.getTransferData(FILELIST.getDataFlavor())); myClipboardContent.setOldContent(contents.getTransferData(FILELIST.getDataFlavor()));
} }
@ -141,8 +140,8 @@ class ClipboardUtils {
if (object instanceof String) { if (object instanceof String) {
oldClipboardContent = new OldClipboardContent((String) object); oldClipboardContent = new OldClipboardContent((String) object);
} else if (object instanceof OldImage) { } else if (object instanceof MyBufferedImage) {
oldClipboardContent = new OldClipboardContent(((OldImage) object).getOldBufferedImage()); oldClipboardContent = new OldClipboardContent(((MyBufferedImage) object).getBufferedImage());
} else if (object instanceof List) { } else if (object instanceof List) {
oldClipboardContent = new OldClipboardContent((List<File>) object); oldClipboardContent = new OldClipboardContent((List<File>) object);
} }

@ -1,10 +1,8 @@
package me.goudham; package me.goudham;
import java.awt.Dimension;
import java.awt.Image; import java.awt.Image;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -57,19 +55,17 @@ 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 = clipboardUtils.getImageContent(newClipboardContents); MyBufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents);
if (bufferedImageContent == null) return; if (bufferedImageContent.getBufferedImage() == null) return;
Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight());
OldImage newImageContent = new OldImage(bufferedImageContent, newDimensionContent);
if (isImageMonitored()) { if (isImageMonitored()) {
if (!newImageContent.equals(myClipboardContents[0].getOldContent())) { if (!bufferedImageContent.equals(myClipboardContents[0].getOldContent())) {
OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent()); OldClipboardContent oldClipboardContent = clipboardUtils.getOldClipboardContent(myClipboardContents[0].getOldContent());
eventManager.notifyImageEvent(oldClipboardContent, bufferedImageContent); eventManager.notifyImageEvent(oldClipboardContent, bufferedImageContent.getBufferedImage());
} }
} }
myClipboardContents[0].setOldContent(newImageContent); myClipboardContents[0].setOldContent(bufferedImageContent);
} }
} }

@ -1,44 +0,0 @@
package me.goudham;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.Objects;
class OldImage {
private BufferedImage oldBufferedImage;
private Dimension oldDimension;
OldImage(BufferedImage oldBufferedImage, Dimension oldDimension) {
this.oldBufferedImage = oldBufferedImage;
this.oldDimension = oldDimension;
}
public BufferedImage getOldBufferedImage() {
return oldBufferedImage;
}
public void setOldBufferedImage(BufferedImage oldBufferedImage) {
this.oldBufferedImage = oldBufferedImage;
}
public Dimension getOldDimension() {
return oldDimension;
}
public void setOldDimension(Dimension oldDimension) {
this.oldDimension = oldDimension;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OldImage oldImage = (OldImage) o;
return Objects.equals(oldBufferedImage, oldImage.oldBufferedImage) || Objects.equals(oldDimension, oldImage.oldDimension);
}
@Override
public int hashCode() {
return Objects.hash(oldBufferedImage, oldDimension);
}
}

@ -1,12 +1,10 @@
package me.goudham; package me.goudham;
import java.awt.Dimension;
import java.awt.Image; import java.awt.Image;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -59,18 +57,10 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isImageMonitored()) { if (isImageMonitored()) {
if (IMAGE.isAvailable(oldClipboard)) { if (IMAGE.isAvailable(oldClipboard)) {
BufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents); MyBufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents);
BufferedImage oldBufferedImage = oldClipboardContent.getOldImage(); MyBufferedImage oldBufferedImage = new MyBufferedImage(oldClipboardContent.getOldImage());
if (bufferedImage != oldBufferedImage) { if (!bufferedImage.equals(oldBufferedImage)) {
if (oldBufferedImage != null) { eventManager.notifyImageEvent(oldClipboardContent, bufferedImage.getBufferedImage());
Dimension imageDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
Dimension oldImageDimension = new Dimension(oldBufferedImage.getWidth(), oldBufferedImage.getHeight());
if (!imageDimension.equals(oldImageDimension)) {
eventManager.notifyImageEvent(oldClipboardContent, bufferedImage);
}
} else {
eventManager.notifyImageEvent(oldClipboardContent, bufferedImage);
}
} }
} }
} }

@ -1,6 +1,5 @@
package me.goudham; package me.goudham;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -75,25 +74,26 @@ class ClipboardUtilsTest {
@Test @Test
void successfullyGetImageContent() { void successfullyGetImageContent() {
BufferedImage expectedImageContent = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); BufferedImage expectedBufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Transferable expectedTransferable = new ClipboardListener.TransferableImage(expectedImageContent); MyBufferedImage expectedImageContent = new MyBufferedImage(expectedBufferedImage);
Transferable expectedTransferable = new ClipboardListener.TransferableImage(expectedBufferedImage);
BufferedImage actualImageContent = sut.getImageContent(expectedTransferable); MyBufferedImage actualImageContent = sut.getImageContent(expectedTransferable);
assertThat(actualImageContent, new BufferedImageMatcher(expectedImageContent)); assertThat(actualImageContent, is(expectedImageContent));
verifyNoInteractions(logger); verifyNoInteractions(logger);
} }
@Test @Test
void failToGetImageContent() throws IOException, UnsupportedFlavorException { void failToGetImageContent() throws IOException, UnsupportedFlavorException {
BufferedImage expectedImageContent = null; MyBufferedImage expectedImageContent = null;
String expectedExceptionMessage = "Exception Thrown When Retrieving Image Content"; String expectedExceptionMessage = "Exception Thrown When Retrieving Image Content";
Throwable expectedException = new UnsupportedFlavorException(IMAGE.getDataFlavor()); Throwable expectedException = new UnsupportedFlavorException(IMAGE.getDataFlavor());
when(transferableMock.isDataFlavorSupported(IMAGE.getDataFlavor())).thenReturn(true); when(transferableMock.isDataFlavorSupported(IMAGE.getDataFlavor())).thenReturn(true);
when(transferableMock.getTransferData(IMAGE.getDataFlavor())).thenThrow(expectedException); when(transferableMock.getTransferData(IMAGE.getDataFlavor())).thenThrow(expectedException);
BufferedImage actualImageContent = sut.getImageContent(transferableMock); MyBufferedImage actualImageContent = sut.getImageContent(transferableMock);
verify(logger, times(1)).error(expectedExceptionMessage, expectedException); verify(logger, times(1)).error(expectedExceptionMessage, expectedException);
assertThat(actualImageContent, is(expectedImageContent)); assertThat(actualImageContent, is(expectedImageContent));
@ -177,13 +177,12 @@ class ClipboardUtilsTest {
static Stream<Arguments> provideArgumentsForOldClipboardContents() { static Stream<Arguments> provideArgumentsForOldClipboardContents() {
String string = "testString"; String string = "testString";
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Dimension dimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()); MyBufferedImage myBufferedImage = new MyBufferedImage(bufferedImage);
OldImage oldImage = new OldImage(bufferedImage, dimension);
List<File> files = List.of(new File("testFile")); List<File> files = List.of(new File("testFile"));
return Stream.of( return Stream.of(
Arguments.of(string, string, null, null), Arguments.of(string, string, null, null),
Arguments.of(oldImage, null, bufferedImage, null), Arguments.of(myBufferedImage, null, bufferedImage, null),
Arguments.of(files, null, null, files) Arguments.of(files, null, null, files)
); );
} }
@ -192,13 +191,12 @@ class ClipboardUtilsTest {
static Stream<Arguments> provideArgumentsForRetrievingClipboardContents() { static Stream<Arguments> provideArgumentsForRetrievingClipboardContents() {
String string = "testString"; String string = "testString";
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Dimension dimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()); MyBufferedImage myBufferedImage = new MyBufferedImage(bufferedImage);
OldImage oldImage = new OldImage(bufferedImage, dimension);
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 MyClipboardContent<>(string), string, TEXT.getDataFlavor()),
Arguments.of(new MyClipboardContent<>(oldImage), bufferedImage, IMAGE.getDataFlavor()), Arguments.of(new MyClipboardContent<>(myBufferedImage), bufferedImage, IMAGE.getDataFlavor()),
Arguments.of(new MyClipboardContent<>(files), files, FILELIST.getDataFlavor()) Arguments.of(new MyClipboardContent<>(files), files, FILELIST.getDataFlavor())
); );
} }

Loading…
Cancel
Save