Remove 200ms delay when inserting contents & ensure file event is not notified for images

pull/2/head
Hammy 3 years ago
parent 0de683ad82
commit 2aa4cf9b0d

@ -14,7 +14,6 @@ import me.goudham.domain.MyBufferedImage;
import me.goudham.domain.TransferableFile; import me.goudham.domain.TransferableFile;
import me.goudham.domain.TransferableImage; import me.goudham.domain.TransferableImage;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
import static me.goudham.Contents.FILE; import static me.goudham.Contents.FILE;
import static me.goudham.Contents.IMAGE; import static me.goudham.Contents.IMAGE;
@ -32,7 +31,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) { public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try { try {
sleep(200); sleep(200);
Transferable newClipboardContents = oldClipboard.getContents(currentThread()); Transferable newClipboardContents = oldClipboard.getContents(null);
processContents(oldClipboard, oldClipboardContents, newClipboardContents); processContents(oldClipboard, oldClipboardContents, newClipboardContents);
regainOwnership(oldClipboard, newClipboardContents); regainOwnership(oldClipboard, newClipboardContents);
} catch (IllegalStateException | InterruptedException exp) { } catch (IllegalStateException | InterruptedException exp) {
@ -69,7 +68,7 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
} }
if (isFileMonitored()) { if (isFileMonitored()) {
if (FILE.isAvailable(oldClipboard)) { if (FILE.isAvailable(oldClipboard) && !IMAGE.isAvailable(oldClipboard)) {
List<File> fileList = clipboardUtils.getFileContent(newClipboardContents); List<File> fileList = clipboardUtils.getFileContent(newClipboardContents);
if (!fileList.equals(clipboardContent.getFiles())) { if (!fileList.equals(clipboardContent.getFiles())) {
eventManager.notifyFilesEvent(clipboardContent, fileList); eventManager.notifyFilesEvent(clipboardContent, fileList);
@ -108,73 +107,49 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
@Override @Override
void insert(String stringContent) { void insert(String stringContent) {
try { setContents(new StringSelection(stringContent));
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
try {
clipboard.setContents(new StringSelection(stringContent), this);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
} }
@Override @Override
void insert(Image imageContent) { void insert(Image imageContent) {
try { setContents(new TransferableImage(imageContent));
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
try {
clipboard.setContents(new TransferableImage(imageContent), this);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
} }
@Override @Override
void insert(List<File> fileContent) { void insert(List<File> fileContent) {
try { setContents(new TransferableFile(fileContent));
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
try {
clipboard.setContents(new TransferableFile(fileContent), this);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
} }
@Override @Override
void insertAndNotify(String stringContent) { void insertAndNotify(String stringContent) {
Transferable currentClipboardContents = clipboard.getContents(this); Transferable currentClipboardContents = clipboard.getContents(null);
insert(stringContent); insert(stringContent);
lostOwnership(clipboard, currentClipboardContents); lostOwnership(clipboard, currentClipboardContents);
} }
@Override @Override
void insertAndNotify(Image imageContent) { void insertAndNotify(Image imageContent) {
Transferable currentClipboardContents = clipboard.getContents(this); Transferable currentClipboardContents = clipboard.getContents(null);
insert(imageContent); insert(imageContent);
lostOwnership(clipboard, currentClipboardContents); lostOwnership(clipboard, currentClipboardContents);
} }
@Override @Override
void insertAndNotify(List<File> fileContent) { void insertAndNotify(List<File> fileContent) {
Transferable currentClipboardContents = clipboard.getContents(this); Transferable currentClipboardContents = clipboard.getContents(null);
insert(fileContent); insert(fileContent);
lostOwnership(clipboard, currentClipboardContents); lostOwnership(clipboard, currentClipboardContents);
} }
void setContents(Transferable contents) {
try {
clipboard.setContents(contents, this);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
}
@Override @Override
public void run() { public void run() {
try { try {

Loading…
Cancel
Save