Skip to content

Commit

Permalink
fix(gui): text on popup action change if need update comment & fixes …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
MrIkso and skylot authored Dec 20, 2024
1 parent ff0fbba commit 8345edf
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 46 deletions.
65 changes: 49 additions & 16 deletions jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jadx.gui.ui.codearea;

import java.awt.event.ActionEvent;
import java.util.Objects;

import javax.swing.event.PopupMenuEvent;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -45,34 +49,63 @@ public CommentAction(CodeArea codeArea) {

@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;
}

/**
Expand Down Expand Up @@ -132,7 +165,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;
}
Expand Down
33 changes: 3 additions & 30 deletions jadx-gui/src/main/java/jadx/gui/ui/dialog/CommentDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -79,25 +71,6 @@ private static void updateCommentsData(CodeArea codeArea, Consumer<List<ICodeCom
}
}

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;
Expand Down Expand Up @@ -217,7 +190,7 @@ public void keyPressed(KeyEvent e) {
}
pack();
if (!codeArea.getMainWindow().getSettings().loadWindowPos(this)) {
setSize(800, 140);
setSize(400, 340);
}
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_id_ID.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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" 검색
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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”
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8345edf

Please sign in to comment.