Skip to content

Commit

Permalink
fix(gui):text on popup action change if need update comment & fixed d…
Browse files Browse the repository at this point in the history
…efault comment dialog size (#2155)
  • Loading branch information
MrIkso committed Dec 19, 2024
1 parent ff0fbba commit a6f55a1
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 36 deletions.
2 changes: 2 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/action/ActionModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/action/JadxGuiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 46 additions & 5 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 @@ -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;

Expand All @@ -47,21 +51,39 @@ 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) {
return;
}

if (JadxGuiAction.isSource(e)) {
showCommentDialog(getCommentRef(codeArea.getCaretPosition()));
showCommentDialog(getActionComment(getCommentRef(codeArea.getCaretPosition())));
} else {
showCommentDialog(this.actionComment);
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
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) {
var 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 a6f55a1

Please sign in to comment.