fix(gui): text on popup action change if need update comment & fixes default size on comment diallog (PR #2376)

* fix(gui):text on popup action change if need update comment & fixed default comment dialog size (#2155)

* don't add new action, just rename labels

---------

Co-authored-by: Skylot <118523+skylot@users.noreply.github.com>
This commit is contained in:
Yaroslav
2024-12-20 22:21:57 +02:00
committed by GitHub
parent ff0fbba720
commit 8345edf76b
11 changed files with 61 additions and 46 deletions
@@ -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.JavaMethod;
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.ICodeNodeRef;
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;
@@ -34,9 +37,10 @@ public class CommentAction extends CodeAreaAction implements DefaultPopupMenuLis
private static final long serialVersionUID = 4753838562204629112L;
private static final Logger LOG = LoggerFactory.getLogger(CommentAction.class);
private final boolean enabled;
private ICodeComment actionComment;
private final boolean enabled;
private @Nullable ICodeComment actionComment;
private boolean updateComment;
public CommentAction(CodeArea codeArea) {
super(ActionModel.CODE_COMMENT, codeArea);
@@ -45,34 +49,63 @@ public class CommentAction extends CodeAreaAction implements DefaultPopupMenuLis
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
if (enabled) {
ICodeComment codeComment = getCommentRef(UiUtils.getOffsetAtMousePosition(codeArea));
setEnabled(codeComment != null);
this.actionComment = codeComment;
if (enabled && updateCommentAction(UiUtils.getOffsetAtMousePosition(codeArea))) {
setNameAndDesc(updateComment ? NLS.str("popup.update_comment") : NLS.str("popup.add_comment"));
setEnabled(true);
} else {
setEnabled(false);
}
}
private boolean updateCommentAction(int pos) {
ICodeComment codeComment = getCommentRef(pos);
if (codeComment == null) {
actionComment = null;
return false;
}
ICodeComment exitsComment = searchForExistComment(codeComment);
if (exitsComment != null) {
actionComment = exitsComment;
updateComment = true;
} else {
actionComment = codeComment;
updateComment = false;
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
if (!enabled) {
return;
}
if (JadxGuiAction.isSource(e)) {
showCommentDialog(getCommentRef(codeArea.getCaretPosition()));
} else {
showCommentDialog(this.actionComment);
updateCommentAction(codeArea.getCaretPosition());
}
}
private void showCommentDialog(ICodeComment codeComment) {
if (codeComment == null) {
if (actionComment == null) {
UiUtils.showMessageBox(codeArea.getMainWindow(), NLS.str("msg.cant_add_comment"));
return;
}
CommentDialog.show(codeArea, codeComment);
CommentDialog.show(codeArea, actionComment, updateComment);
}
private @Nullable ICodeComment searchForExistComment(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 +165,7 @@ public class CommentAction extends CodeAreaAction implements DefaultPopupMenuLis
}
}
} catch (Exception e) {
LOG.error("Failed to add comment at: " + pos, e);
LOG.error("Failed to add comment at: {}", pos, e);
}
return null;
}
@@ -3,14 +3,12 @@ package jadx.gui.ui.dialog;
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) {
CommentDialog dialog = new CommentDialog(codeArea, comment, updateComment);
dialog.setVisible(true);
}
@@ -79,25 +71,6 @@ public class CommentDialog extends JDialog {
}
}
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;
}
private final transient CodeArea codeArea;
private final transient ICodeComment comment;
private final transient boolean updateComment;
@@ -217,7 +190,7 @@ public class CommentDialog extends JDialog {
}
pack();
if (!codeArea.getMainWindow().getSettings().loadWindowPos(this)) {
setSize(800, 140);
setSize(400, 340);
}
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
@@ -318,6 +318,7 @@ popup.go_to_declaration=Zur Erklärung gehen
popup.exclude=Ausschließen
popup.exclude_packages=Pakete ausschließen
popup.add_comment=Kommentar
#popup.update_comment=Update comment
popup.search_comment=Kommentar suchen
popup.rename=Umbennen
popup.search=Suche "%s"
@@ -318,6 +318,7 @@ popup.go_to_declaration=Go to declaration
popup.exclude=Exclude
popup.exclude_packages=Exclude packages
popup.add_comment=Comment
popup.update_comment=Update comment
popup.search_comment=Search comments
popup.rename=Rename
popup.search=Search "%s"
@@ -318,6 +318,7 @@ popup.xposed=Copiar como fragmento de xposed
#popup.exclude=Exclude
#popup.exclude_packages=Exclude packages
#popup.add_comment=Comment
#popup.update_comment=Update comment
#popup.search_comment=Search comments
popup.rename=Renombrar
#popup.search=Search "%s"
@@ -318,6 +318,7 @@ popup.go_to_declaration=Pergi ke Deklarasi
popup.exclude=Kecualikan
popup.exclude_packages=Kecualikan paket
popup.add_comment=Komentar
#popup.update_comment=Update comment
popup.search_comment=Cari komentar
popup.rename=Ganti nama
popup.search=Cari "%s"
@@ -318,6 +318,7 @@ popup.go_to_declaration=선언문으로 이동
popup.exclude=제외
popup.exclude_packages=패키지 제외
popup.add_comment=주석
#popup.update_comment=Update comment
popup.search_comment=주석 검색
popup.rename=이름 바꾸기
popup.search="%s" 검색
@@ -318,6 +318,7 @@ popup.go_to_declaration=Ir para declaração
popup.exclude=Ignorar
popup.exclude_packages=Pacotes ignorados
popup.add_comment=Comentar
#popup.update_comment=Update comment
popup.search_comment=Buscar comentários
popup.rename=Renomear
popup.search=Buscar "%s"
@@ -318,6 +318,7 @@ popup.go_to_declaration=Перейти к объявлению
popup.exclude=Исключить
popup.exclude_packages=Исключить пакеты
popup.add_comment=Комментарий
#popup.update_comment=Update comment
popup.search_comment=Поиск комментариев
popup.rename=Переименовать
popup.search=Найти "%s"
@@ -318,6 +318,7 @@ popup.go_to_declaration=跳到声明
popup.exclude=排除此包
popup.exclude_packages=排除包
popup.add_comment=添加注释
#popup.update_comment=Update comment
popup.search_comment=搜索注释
popup.rename=重命名
popup.search=搜索 “%s”
@@ -318,6 +318,7 @@ popup.go_to_declaration=前往宣告
popup.exclude=排除
popup.exclude_packages=排除套件
popup.add_comment=註解
#popup.update_comment=Update comment
popup.search_comment=搜尋註解
popup.rename=重新命名
popup.search=搜尋 "%s"