Skip to content

Commit

Permalink
Merge pull request #80 from otymko/feature/issue61_2
Browse files Browse the repository at this point in the history
Исправление #61 подход 2
  • Loading branch information
otymko authored Jan 28, 2020
2 parents 3cd9b6b + 5fa96de commit a181e56
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
26 changes: 4 additions & 22 deletions src/main/java/org/github/otymko/phoenixbsl/core/PhoenixAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a181e56

Please sign in to comment.