From 5fa96de34885bf1a6b2234def945fcffffe97f2a Mon Sep 17 00:00:00 2001 From: otymko Date: Tue, 28 Jan 2020 07:56:46 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D1=85=D0=BE=D0=B4=202=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D1=8B=20=D1=81=20=D0=B1=D1=83=D1=84=D0=B5=D1=80=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=BE=D0=B1=D0=BC=D0=B5=D0=BD=D1=8B=20=D0=B8=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D1=81=D0=BB=D1=83=D1=88?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=BD=D0=B0=D0=B6=D0=B0=D1=82?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phoenixbsl/core/CustomTextTransfer.java | 42 +++++++++++++++++++ .../otymko/phoenixbsl/core/PhoenixAPI.java | 26 ++---------- 2 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/github/otymko/phoenixbsl/core/CustomTextTransfer.java 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() {