diff --git a/src/main/java/org/github/otymko/phoenixbsl/core/CustomTextTransfer.java b/src/main/java/org/github/otymko/phoenixbsl/core/CustomTextTransfer.java new file mode 100644 index 0000000..d8f3def --- /dev/null +++ b/src/main/java/org/github/otymko/phoenixbsl/core/CustomTextTransfer.java @@ -0,0 +1,42 @@ +package org.github.otymko.phoenixbsl.core; + +import lombok.extern.slf4j.Slf4j; + +import java.awt.*; +import java.awt.datatransfer.*; +import java.io.IOException; + +@Slf4j +public class CustomTextTransfer implements ClipboardOwner { + + private StringSelection stringSelection; + + @Override + public void lostOwnership(Clipboard clipboard, Transferable contents) { + // TODO Auto-generated method stub + } + + public void setClipboardContents(String content) { + stringSelection = new StringSelection(content); + var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + clipboard.setContents(stringSelection, this); + } + + public String getClipboardContents() { + String content = ""; + var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + var contents = clipboard.getContents(null); + var hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor); + if (hasTransferableText) { + try { + content = (String) contents.getTransferData(DataFlavor.stringFlavor); + } catch (UnsupportedFlavorException ex) { + LOGGER.error(ex.getMessage()); + } catch (IOException ex) { + LOGGER.error(ex.getMessage()); + } + } + return content; + } + +} diff --git a/src/main/java/org/github/otymko/phoenixbsl/core/PhoenixAPI.java b/src/main/java/org/github/otymko/phoenixbsl/core/PhoenixAPI.java index 67533c8..723d484 100644 --- a/src/main/java/org/github/otymko/phoenixbsl/core/PhoenixAPI.java +++ b/src/main/java/org/github/otymko/phoenixbsl/core/PhoenixAPI.java @@ -17,6 +17,7 @@ public class PhoenixAPI { private static final String FUN_SYMBOL = "☻"; // 9787 private static final CustomRobot robot = new CustomRobot(); + private static final CustomTextTransfer textTransfer = new CustomTextTransfer(); private static boolean isWindowsForm1SByClassName(String classNameForm) { return classNameForm.contains("V8") || classNameForm.contains("SWT_Window"); @@ -93,35 +94,16 @@ public static String getTextAll() { // Взаимодействие с буфером обмена public static void setTextInClipboard(String text) { - Toolkit.getDefaultToolkit() - .getSystemClipboard() - .setContents( - new StringSelection(text), - null - ); + textTransfer.setClipboardContents(text); } private static void clearClipboard() { LOGGER.debug("clearClipboard"); - var stringSelection = new StringSelection(""); - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); // FIXME: падает буфер обмена + textTransfer.setClipboardContents(""); } private static String getFromClipboard() { - var result = ""; - try { - result = getDataClipboard(); - } catch (IOException | UnsupportedFlavorException e) { - LOGGER.error("Не удалось получить из буфера обмена", e); - } - return result; - } - - private static String getDataClipboard() throws IOException, UnsupportedFlavorException { - PhoenixApp.getInstance().sleepCurrentThread(200); - var result = (String) Toolkit.getDefaultToolkit() - .getSystemClipboard().getData(DataFlavor.stringFlavor); - return result; + return textTransfer.getClipboardContents(); } public static int getProcessId() {