|
|
@ -7,19 +7,22 @@ import java.awt.datatransfer.Transferable;
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import me.goudham.listener.ClipboardEventListener;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static me.goudham.domain.Contents.IMAGE;
|
|
|
|
import static me.goudham.domain.Contents.IMAGE;
|
|
|
|
import static me.goudham.domain.Contents.STRING;
|
|
|
|
import static me.goudham.domain.Contents.STRING;
|
|
|
|
|
|
|
|
|
|
|
|
abstract class ClipboardListener {
|
|
|
|
abstract class ClipboardListener {
|
|
|
|
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
List<ClipboardEventListener> eventsListener = new ArrayList<>();
|
|
|
|
private EventManager eventManager = new EventManager();
|
|
|
|
private boolean imagesMonitored = true;
|
|
|
|
private boolean imagesMonitored = true;
|
|
|
|
private boolean textMonitored = true;
|
|
|
|
private boolean textMonitored = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Try to unmarshal {@link Transferable} into {@link String}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param clipboardContents The {@link Transferable} to be converted into {@link String}
|
|
|
|
|
|
|
|
* @return {@link String} representation of {@code clipboardContents}
|
|
|
|
|
|
|
|
*/
|
|
|
|
String getStringContent(Transferable clipboardContents) {
|
|
|
|
String getStringContent(Transferable clipboardContents) {
|
|
|
|
String newContent = null;
|
|
|
|
String newContent = null;
|
|
|
|
|
|
|
|
|
|
|
@ -32,6 +35,12 @@ abstract class ClipboardListener {
|
|
|
|
return newContent;
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Try to unmarshal {@link Transferable} into {@link BufferedImage}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param clipboardContents The {@link Transferable} to be converted into {@link BufferedImage}
|
|
|
|
|
|
|
|
* @return {@link BufferedImage} representation of {@code clipboardContents}
|
|
|
|
|
|
|
|
*/
|
|
|
|
BufferedImage getImageContent(Transferable clipboardContents) {
|
|
|
|
BufferedImage getImageContent(Transferable clipboardContents) {
|
|
|
|
BufferedImage bufferedImage = null;
|
|
|
|
BufferedImage bufferedImage = null;
|
|
|
|
|
|
|
|
|
|
|
@ -44,28 +53,6 @@ abstract class ClipboardListener {
|
|
|
|
return bufferedImage;
|
|
|
|
return bufferedImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void notifyStringEvent(String stringContent) {
|
|
|
|
|
|
|
|
for (ClipboardEventListener clipboardEventListener : eventsListener) {
|
|
|
|
|
|
|
|
clipboardEventListener.onCopyString(stringContent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void notifyImageEvent(BufferedImage imageContent) {
|
|
|
|
|
|
|
|
for (ClipboardEventListener clipboardEventListener : eventsListener) {
|
|
|
|
|
|
|
|
clipboardEventListener.onCopyImage(imageContent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void addEventListener(ClipboardEventListener clipboardEventListener) {
|
|
|
|
|
|
|
|
if (!eventsListener.contains(clipboardEventListener)) {
|
|
|
|
|
|
|
|
eventsListener.add(clipboardEventListener);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void removeEventListener(ClipboardEventListener clipboardEventListener) {
|
|
|
|
|
|
|
|
eventsListener.remove(clipboardEventListener);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void toggleTextMonitored() {
|
|
|
|
void toggleTextMonitored() {
|
|
|
|
this.textMonitored = !textMonitored;
|
|
|
|
this.textMonitored = !textMonitored;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -90,5 +77,19 @@ abstract class ClipboardListener {
|
|
|
|
this.textMonitored = textMonitored;
|
|
|
|
this.textMonitored = textMonitored;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventManager getEventManager() {
|
|
|
|
|
|
|
|
return eventManager;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setEventManager(EventManager eventManager) {
|
|
|
|
|
|
|
|
this.eventManager = eventManager;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Main entry point of execution for both {@link MacClipboardListener} and {@link WindowsOrUnixClipboardListener}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @see MacClipboardListener
|
|
|
|
|
|
|
|
* @see WindowsOrUnixClipboardListener
|
|
|
|
|
|
|
|
*/
|
|
|
|
abstract void execute();
|
|
|
|
abstract void execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|