diff --git a/jadx-gui/src/main/java/jadx/gui/ui/action/ActionModel.java b/jadx-gui/src/main/java/jadx/gui/ui/action/ActionModel.java index d0b3a61b6e4..888abca172c 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/action/ActionModel.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/action/ActionModel.java @@ -84,6 +84,8 @@ public enum ActionModel { Shortcut.keyboard(KeyEvent.VK_D)), CODE_COMMENT(CODE_AREA, "popup.add_comment", "popup.add_comment", null, Shortcut.keyboard(KeyEvent.VK_SEMICOLON)), + UPDATE_CODE_COMMENT(CODE_AREA, "popup.update_comment", "popup.update_comment", null, + Shortcut.keyboard(KeyEvent.VK_SEMICOLON)), CODE_COMMENT_SEARCH(CODE_AREA, "popup.search_comment", "popup.search_comment", null, Shortcut.keyboard(KeyEvent.VK_SEMICOLON, UiUtils.ctrlButton())), CODE_RENAME(CODE_AREA, "popup.rename", "popup.rename", null, diff --git a/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java b/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java index bc4af24ffde..9454246fb32 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java @@ -17,7 +17,7 @@ public class JadxGuiAction extends ActionHandler implements IShortcutAction { private static final String COMMAND_PREFIX = "JadxGuiAction.Command."; - private final ActionModel actionModel; + private ActionModel actionModel; private final String id; private JComponent shortcutComponent = null; private KeyStroke addedKeyStroke = null; @@ -53,6 +53,11 @@ public JadxGuiAction(String id) { updateProperties(); } + public void setActionModel(ActionModel actionModel) { + this.actionModel = actionModel; + updateProperties(); + } + private void updateProperties() { if (actionModel == null) { return; diff --git a/jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java b/jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java index cc5db54df62..efd63e3bef9 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java @@ -1,6 +1,7 @@ package jadx.gui.ui.codearea; import java.awt.event.ActionEvent; +import java.util.Objects; import javax.swing.event.PopupMenuEvent; @@ -13,6 +14,7 @@ import jadx.api.JavaNode; import jadx.api.data.ICodeComment; import jadx.api.data.impl.JadxCodeComment; +import jadx.api.data.impl.JadxCodeData; import jadx.api.data.impl.JadxCodeRef; import jadx.api.data.impl.JadxNodeRef; import jadx.api.metadata.ICodeAnnotation; @@ -22,6 +24,7 @@ import jadx.api.metadata.annotations.InsnCodeOffset; import jadx.api.metadata.annotations.NodeDeclareRef; import jadx.gui.JadxWrapper; +import jadx.gui.settings.JadxProject; import jadx.gui.treemodel.JClass; import jadx.gui.ui.action.ActionModel; import jadx.gui.ui.action.JadxGuiAction; @@ -35,6 +38,7 @@ public class CommentAction extends CodeAreaAction implements DefaultPopupMenuLis private static final Logger LOG = LoggerFactory.getLogger(CommentAction.class); private final boolean enabled; + private boolean updateComment; private ICodeComment actionComment; @@ -47,13 +51,31 @@ public CommentAction(CodeArea codeArea) { public void popupMenuWillBecomeVisible(PopupMenuEvent e) { if (enabled) { ICodeComment codeComment = getCommentRef(UiUtils.getOffsetAtMousePosition(codeArea)); - setEnabled(codeComment != null); - this.actionComment = codeComment; + if (codeComment != null) { + actionComment = getActionComment(codeComment); + setEnabled(true); + } else { + setActionModel(ActionModel.CODE_COMMENT); + setEnabled(false); + } } else { setEnabled(false); } } + private ICodeComment getActionComment(ICodeComment codeComment) { + ICodeComment exitsComment = searchForExistComment(codeArea, codeComment); + if (exitsComment != null) { + setActionModel(ActionModel.UPDATE_CODE_COMMENT); + updateComment = true; + return exitsComment; + } else { + setActionModel(ActionModel.CODE_COMMENT); + updateComment = false; + return codeComment; + } + } + @Override public void actionPerformed(ActionEvent e) { if (!enabled) { @@ -61,7 +83,7 @@ public void actionPerformed(ActionEvent e) { } if (JadxGuiAction.isSource(e)) { - showCommentDialog(getCommentRef(codeArea.getCaretPosition())); + showCommentDialog(getActionComment(getCommentRef(codeArea.getCaretPosition()))); } else { showCommentDialog(this.actionComment); } @@ -72,7 +94,26 @@ private void showCommentDialog(ICodeComment codeComment) { UiUtils.showMessageBox(codeArea.getMainWindow(), NLS.str("msg.cant_add_comment")); return; } - CommentDialog.show(codeArea, codeComment); + CommentDialog.show(codeArea, codeComment, updateComment); + } + + private static ICodeComment searchForExistComment(CodeArea codeArea, ICodeComment blankComment) { + try { + JadxProject project = codeArea.getProject(); + JadxCodeData codeData = project.getCodeData(); + if (codeData == null || codeData.getComments().isEmpty()) { + return null; + } + for (ICodeComment comment : codeData.getComments()) { + if (Objects.equals(comment.getNodeRef(), blankComment.getNodeRef()) + && Objects.equals(comment.getCodeRef(), blankComment.getCodeRef())) { + return comment; + } + } + } catch (Exception e) { + LOG.error("Error searching for exists comment", e); + } + return null; } /** @@ -132,7 +173,7 @@ private ICodeComment getCommentRef(int pos) { } } } catch (Exception e) { - LOG.error("Failed to add comment at: " + pos, e); + LOG.error("Failed to add comment at: {}", pos, e); } return null; } diff --git a/jadx-gui/src/main/java/jadx/gui/ui/dialog/CommentDialog.java b/jadx-gui/src/main/java/jadx/gui/ui/dialog/CommentDialog.java index 0b813bc6c1a..8de84b26a04 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/dialog/CommentDialog.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/dialog/CommentDialog.java @@ -3,14 +3,12 @@ import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; -import java.awt.Dialog; import java.awt.Dimension; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.function.Consumer; import javax.swing.BorderFactory; @@ -44,14 +42,8 @@ public class CommentDialog extends JDialog { private static final Logger LOG = LoggerFactory.getLogger(CommentDialog.class); - public static void show(CodeArea codeArea, ICodeComment blankComment) { - ICodeComment existComment = searchForExistComment(codeArea, blankComment); - Dialog dialog; - if (existComment != null) { - dialog = new CommentDialog(codeArea, existComment, true); - } else { - dialog = new CommentDialog(codeArea, blankComment, false); - } + public static void show(CodeArea codeArea, ICodeComment comment, boolean updateComment) { + var dialog = new CommentDialog(codeArea, comment, updateComment); dialog.setVisible(true); } @@ -79,25 +71,6 @@ private static void updateCommentsData(CodeArea codeArea, Consumer