From 9b5141471a42be0a105e27eb97ca8f972c958ca1 Mon Sep 17 00:00:00 2001 From: Yanis48 Date: Fri, 2 Apr 2021 21:43:58 +0200 Subject: Copy method/field descriptor on click --- .../cuchaz/enigma/gui/panels/IdentifierPanel.java | 23 ++++++++++++++++++++-- .../main/java/cuchaz/enigma/gui/util/GuiUtil.java | 19 ++++++++++++++++++ enigma/src/main/resources/lang/en_us.json | 2 ++ enigma/src/main/resources/lang/fr_fr.json | 4 ++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/IdentifierPanel.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/IdentifierPanel.java index 255fe81..a491467 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/IdentifierPanel.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/IdentifierPanel.java @@ -5,6 +5,8 @@ import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ItemEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.util.function.Consumer; import javax.swing.BorderFactory; @@ -97,7 +99,7 @@ public class IdentifierPanel { FieldEntry fe = (FieldEntry) deobfEntry; this.nameField = th.addRenameTextField(I18n.translate("info_panel.identifier.field"), fe.getName()); th.addStringRow(I18n.translate("info_panel.identifier.class"), fe.getParent().getFullName()); - th.addStringRow(I18n.translate("info_panel.identifier.type_descriptor"), fe.getDesc().toString()); + th.addCopiableStringRow(I18n.translate("info_panel.identifier.type_descriptor"), fe.getDesc().toString()); th.addModifierRow(I18n.translate("info_panel.identifier.modifier"), this::onModifierChanged); } else if (deobfEntry instanceof MethodEntry) { MethodEntry me = (MethodEntry) deobfEntry; @@ -107,7 +109,7 @@ public class IdentifierPanel { this.nameField = th.addRenameTextField(I18n.translate("info_panel.identifier.method"), me.getName()); th.addStringRow(I18n.translate("info_panel.identifier.class"), me.getParent().getFullName()); } - th.addStringRow(I18n.translate("info_panel.identifier.method_descriptor"), me.getDesc().toString()); + th.addCopiableStringRow(I18n.translate("info_panel.identifier.method_descriptor"), me.getDesc().toString()); th.addModifierRow(I18n.translate("info_panel.identifier.modifier"), this::onModifierChanged); } else if (deobfEntry instanceof LocalVariableEntry) { LocalVariableEntry lve = (LocalVariableEntry) deobfEntry; @@ -207,6 +209,19 @@ public class IdentifierPanel { this.row += 1; } + public void addCopiableRow(JLabel c1, JLabel c2) { + c2.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1) { + GuiUtil.copyToClipboard(c2.getText()); + GuiUtil.showPopup(c2, I18n.translate("popup.copied"), e.getXOnScreen(), e.getYOnScreen()); + } + } + }); + addRow(c1, c2); + } + public ConvertingTextField addConvertingTextField(String c1, String c2) { ConvertingTextField textField = new ConvertingTextField(c2); addRow(new JLabel(c1), textField.getUi()); @@ -226,6 +241,10 @@ public class IdentifierPanel { addRow(new JLabel(c1), GuiUtil.unboldLabel(new JLabel(c2))); } + public void addCopiableStringRow(String c1, String c2) { + addCopiableRow(new JLabel(c1), GuiUtil.unboldLabel(new JLabel(c2))); + } + public JComboBox addModifierRow(String c1, Consumer changeListener) { if (!project.isRenamable(e)) return null; 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 7f08fc2..e26c29b 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 @@ -9,6 +9,7 @@ import cuchaz.enigma.utils.Os; import javax.swing.*; import java.awt.*; +import java.awt.datatransfer.StringSelection; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.font.TextAttribute; @@ -53,6 +54,24 @@ public class GuiUtil { return label; } + /** + * Puts the provided {@code text} in the system clipboard. + */ + public static void copyToClipboard(String text) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null); + } + + public static void showPopup(JComponent component, String text, int x, int y) { + // from https://stackoverflow.com/questions/39955015/java-swing-show-tooltip-as-a-message-dialog + JToolTip tooltip = new JToolTip(); + tooltip.setTipText(text); + Popup p = PopupFactory.getSharedInstance().getPopup(component, tooltip, x + 10, y); + p.show(); + Timer t = new Timer(1000, e -> p.hide()); + t.setRepeats(false); + t.start(); + } + public static void showToolTipNow(JComponent component) { // HACKHACK: trick the tooltip manager into showing the tooltip right now ToolTipManager manager = ToolTipManager.sharedInstance(); diff --git a/enigma/src/main/resources/lang/en_us.json b/enigma/src/main/resources/lang/en_us.json index fd21faa..903faa7 100644 --- a/enigma/src/main/resources/lang/en_us.json +++ b/enigma/src/main/resources/lang/en_us.json @@ -108,6 +108,8 @@ "info_panel.tree.implementations": "Implementations", "info_panel.tree.calls": "Call Graph", + "popup.copied": "Copied!", + "log_panel.messages": "Messages", "log_panel.users": "Users", diff --git a/enigma/src/main/resources/lang/fr_fr.json b/enigma/src/main/resources/lang/fr_fr.json index 43bea4d..42edd6b 100644 --- a/enigma/src/main/resources/lang/fr_fr.json +++ b/enigma/src/main/resources/lang/fr_fr.json @@ -36,6 +36,7 @@ "menu.view.themes": "Thèmes", "menu.view.themes.default": "Par défaut", "menu.view.themes.darcula": "Darcula", + "menu.view.themes.metal": "Métal", "menu.view.themes.system": "Système", "menu.view.themes.none": "Aucun (JVM par défaut)", "menu.view.languages": "Langues", @@ -107,6 +108,8 @@ "info_panel.tree.implementations": "Implémentations", "info_panel.tree.calls": "Graphique des appels", + "popup.copied": "Copié !", + "log_panel.messages": "Messages", "log_panel.users": "Utilisateurs", @@ -191,6 +194,7 @@ "validation.message.illegal_identifier.long": "Caractère '%2$s' invalide à la position %3$d.", "validation.message.illegal_doc_comment_end": "Un commentaire de Javadoc ne peut pas contenir la séquence de caractères '*/'.", "validation.message.reserved_identifier": "'%s' est un identifiant réservé.", + "validation.message.unknown_record_getter": "Impossible de trouver un accesseur de composant de record correspondant pour %s", "crash.title": "%s - Rapport de plantage", "crash.summary": "%s a planté ! =(", -- cgit v1.2.3