Use MyBufferedImage instead of OldImage.java

MYC-Refactor
Hammy 3 years ago
parent a8233b3575
commit c0088d9614

@ -1,6 +1,5 @@
package me.goudham;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
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}
* @return {@link BufferedImage} representation of {@code clipboardContents}
* @param clipboardContents The {@link Transferable} to be converted into {@link MyBufferedImage}
* @return {@link MyBufferedImage} representation of {@code clipboardContents}
*/
BufferedImage getImageContent(Transferable clipboardContents) {
BufferedImage bufferedImage = null;
MyBufferedImage getImageContent(Transferable clipboardContents) {
MyBufferedImage myBufferedImage = null;
try {
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) {
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()));
} else if (contents.isDataFlavorSupported(IMAGE.getDataFlavor())) {
BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor()));
Dimension imageDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
myClipboardContent.setOldContent(new OldImage(bufferedImage, imageDimension));
myClipboardContent.setOldContent(new MyBufferedImage(bufferedImage));
} else if (contents.isDataFlavorSupported(FILELIST.getDataFlavor())) {
myClipboardContent.setOldContent(contents.getTransferData(FILELIST.getDataFlavor()));
}
@ -141,8 +140,8 @@ class ClipboardUtils {
if (object instanceof String) {
oldClipboardContent = new OldClipboardContent((String) object);
} else if (object instanceof OldImage) {
oldClipboardContent = new OldClipboardContent(((OldImage) object).getOldBufferedImage());
} else if (object instanceof MyBufferedImage) {
oldClipboardContent = new OldClipboardContent(((MyBufferedImage) object).getBufferedImage());
} else if (object instanceof List) {
oldClipboardContent = new OldClipboardContent((List<File>) object);
}

@ -1,10 +1,8 @@
package me.goudham;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.concurrent.Executors;
@ -57,19 +55,17 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
*/
void checkImages(Transferable newClipboardContents, MyClipboardContent<?>[] myClipboardContents) {
if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents);
if (bufferedImageContent == null) return;
Dimension newDimensionContent = new Dimension(bufferedImageContent.getWidth(), bufferedImageContent.getHeight());
OldImage newImageContent = new OldImage(bufferedImageContent, newDimensionContent);
MyBufferedImage bufferedImageContent = clipboardUtils.getImageContent(newClipboardContents);
if (bufferedImageContent.getBufferedImage() == null) return;
if (isImageMonitored()) {
if (!newImageContent.equals(myClipboardContents[0].getOldContent())) {
if (!bufferedImageContent.equals(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;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
@ -59,18 +57,10 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
if (isImageMonitored()) {
if (IMAGE.isAvailable(oldClipboard)) {
BufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents);
BufferedImage oldBufferedImage = oldClipboardContent.getOldImage();
if (bufferedImage != oldBufferedImage) {
if (oldBufferedImage != null) {
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);
}
MyBufferedImage bufferedImage = clipboardUtils.getImageContent(newClipboardContents);
MyBufferedImage oldBufferedImage = new MyBufferedImage(oldClipboardContent.getOldImage());
if (!bufferedImage.equals(oldBufferedImage)) {
eventManager.notifyImageEvent(oldClipboardContent, bufferedImage.getBufferedImage());
}
}
}

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

Loading…
Cancel
Save