diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java index dd37798a..81623955 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java @@ -165,7 +165,7 @@ public void updateUiState() { EditableType type = EditableType.fromEntry(referenceEntry); this.renameItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type))); - this.pasteItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type)) && !GuiUtil.getClipboard().equals("")); + this.pasteItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type)) && GuiUtil.hasClipboardText()); this.editJavadocItem.setEnabled(isRenamable && this.gui.isEditable(EditableType.JAVADOC)); this.showInheritanceItem.setEnabled(isClassEntry || isMethodEntry || isConstructorEntry); this.showImplementationsItem.setEnabled(isClassEntry || isMethodEntry); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GuiUtil.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GuiUtil.java index 106859cc..37851fa9 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GuiUtil.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GuiUtil.java @@ -5,7 +5,9 @@ import java.awt.Desktop; import java.awt.Font; import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -87,7 +89,22 @@ public static void copyToClipboard(String text) { Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null); } + public static boolean hasClipboardText() { + Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable contents = systemClipboard.getContents(null); + + if (contents != null) { + return contents.isDataFlavorSupported(DataFlavor.stringFlavor); + } + + return false; + } + public static String getClipboard() { + if (!hasClipboardText()) { + return ""; + } + try { return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException | IOException e) {