Merge With osx branch

main
sgoudham 3 years ago
commit 42a288d304
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

3
.gitignore vendored

@ -23,4 +23,5 @@
hs_err_pid*
# User
.idea
.idea
target

@ -1,34 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.goudham</groupId>
<artifactId>MyClipboard</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MyClipboard</name>
<groupId>me.goudham</groupId>
<artifactId>MyClipboard</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MyClipboard</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

@ -1,5 +1,6 @@
package me.goudham;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
@ -7,10 +8,19 @@ import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import me.goudham.listener.ClipboardEvent;
import me.goudham.model.MyClipboardContent;
import me.goudham.util.ClipboardUtils;
import org.jetbrains.annotations.NotNull;
import static me.goudham.util.Contents.IMAGE;
import static me.goudham.util.Contents.STRING;
public class ClipboardListener extends Thread implements ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
private @NotNull ClipboardEvent clipboardEvent;
@ -36,7 +46,7 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
String stringContent = (String) newClipboardContents.getTransferData(DataFlavor.stringFlavor);
clipboardEvent.onCopyString(stringContent);
} else if (oldClipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
BufferedImage imageContent = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor));
clipboardEvent.onCopyImage(imageContent);
}
} catch (UnsupportedFlavorException | IOException ignored) { }
@ -56,9 +66,33 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
}
public void run() {
Transferable currentClipboardContents = clipboard.getContents(currentThread());
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
Transferable oldClipboardContents = clipboard.getContents(null);
final MyClipboardContent<?>[] myOldClipboardContentsArray = new MyClipboardContent[]{ ClipboardUtils.getClipboardContents(oldClipboardContents, clipboard) };
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
Transferable newClipboardContents = clipboard.getContents(null);
MyClipboardContent<?> myNewClipboardContents = new MyClipboardContent<>("");
try {
if (STRING.isAvailable(clipboard)) {
myNewClipboardContents.setContent(newClipboardContents.getTransferData(STRING.getDataFlavor()));
if (!myNewClipboardContents.getContent().equals(myOldClipboardContentsArray[0].getContent())) {
clipboardEvent.onCopyString((String) myNewClipboardContents.getContent());
myOldClipboardContentsArray[0].setContent(myNewClipboardContents.getContent());
}
} else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = ClipboardUtils.convertToBufferedImage((Image) newClipboardContents.getTransferData(IMAGE.getDataFlavor()));
myNewClipboardContents.setContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
if (!myNewClipboardContents.getContent().equals(myOldClipboardContentsArray[0].getContent())) {
clipboardEvent.onCopyImage(bufferedImage);
myOldClipboardContentsArray[0].setContent(myNewClipboardContents.getContent());
}
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}, 0, 350, TimeUnit.MILLISECONDS);
}
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {

@ -1,8 +1,8 @@
package me.goudham.listener;
import java.awt.Image;
import java.awt.image.BufferedImage;
public interface ClipboardEvent {
void onCopyString(String stringContent);
void onCopyImage(Image imageContent);
void onCopyString(String stringContent);
void onCopyImage(BufferedImage imageContent);
}

@ -1,23 +1,32 @@
package me.goudham.model;
import java.awt.Image;
import java.util.Objects;
public class MyClipboardContent<T> {
private T content;
private T content;
public MyClipboardContent(T content) {
this.content = content;
}
public MyClipboardContent(T content) {
this.content = content;
}
public void setContent(Object content) {
this.content = (T) content;
}
public void setContent(Object content) {
this.content = (T) content;
}
public T getContent() {
return content;
}
public T getContent() {
return content;
}
public Image getImage() {
return (Image) content;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyClipboardContent<?> that = (MyClipboardContent<?>) o;
return Objects.equals(content, that.content);
}
@Override
public int hashCode() {
return Objects.hash(content);
}
}

@ -0,0 +1,44 @@
package me.goudham.util;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import me.goudham.model.MyClipboardContent;
import static me.goudham.util.Contents.IMAGE;
import static me.goudham.util.Contents.STRING;
public class ClipboardUtils {
public static MyClipboardContent<?> getClipboardContents(Transferable contents, Clipboard clipboard) {
MyClipboardContent<?> myClipboardContent = new MyClipboardContent<>("");
try {
if (STRING.isAvailable(clipboard)) {
myClipboardContent.setContent(contents.getTransferData(STRING.getDataFlavor()));
} else if (IMAGE.isAvailable(clipboard)) {
BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor()));
myClipboardContent.setContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
return myClipboardContent;
}
public static BufferedImage convertToBufferedImage(Image image) {
BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = newImage.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
}

@ -0,0 +1,37 @@
package me.goudham.util;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
public enum Contents {
STRING(DataFlavor.stringFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
}
},
IMAGE(DataFlavor.imageFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor);
}
},
FILELIST(DataFlavor.javaFileListFlavor) {
@Override
public boolean isAvailable(Clipboard clipboard) {
return clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor);
}
};
private final DataFlavor dataFlavor;
Contents(DataFlavor dataFlavor) {
this.dataFlavor = dataFlavor;
}
public DataFlavor getDataFlavor() {
return dataFlavor;
}
public abstract boolean isAvailable(Clipboard clipboard);
}

@ -2,6 +2,7 @@ package me.goudham.view;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
@ -99,7 +100,11 @@ public class ClipboardView implements ClipboardEvent {
}
@Override
public void onCopyImage(Image imageContent) {
imageIconLabel.setIcon(new ImageIcon(new ImageIcon(imageContent).getImage().getScaledInstance(500, 250, Image.SCALE_SMOOTH)));
public void onCopyImage(BufferedImage imageContent) {
if (imageContent.getWidth() > 1000 || imageContent.getHeight() > 500) {
imageIconLabel.setIcon(new ImageIcon(new ImageIcon(imageContent).getImage().getScaledInstance(1000, 300, Image.SCALE_SMOOTH)));
} else {
imageIconLabel.setIcon(new ImageIcon(imageContent));
}
}
}

Loading…
Cancel
Save