refactor(gui): extract common dialog code into new parent class
This commit is contained in:
@@ -4,13 +4,8 @@ public class ExportProjectProperties {
|
||||
private boolean skipSources;
|
||||
private boolean skipResources;
|
||||
private boolean asGradleMode;
|
||||
private boolean useGradleKts;
|
||||
private String exportPath;
|
||||
|
||||
public ExportProjectProperties() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isSkipSources() {
|
||||
return skipSources;
|
||||
}
|
||||
@@ -35,14 +30,6 @@ public class ExportProjectProperties {
|
||||
this.asGradleMode = asGradleMode;
|
||||
}
|
||||
|
||||
public boolean isUseGradleKts() {
|
||||
return useGradleKts;
|
||||
}
|
||||
|
||||
public void setUseGradleKts(boolean useGradleKts) {
|
||||
this.useGradleKts = useGradleKts;
|
||||
}
|
||||
|
||||
public String getExportPath() {
|
||||
return exportPath;
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ import jadx.gui.utils.shortcut.ShortcutsController;
|
||||
import jadx.gui.utils.ui.ActionHandler;
|
||||
import jadx.gui.utils.ui.NodeLabel;
|
||||
|
||||
public class MainWindow extends JFrame implements ExportProjectDialog.ExportProjectDialogListener {
|
||||
public class MainWindow extends JFrame {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MainWindow.class);
|
||||
|
||||
private static final String DEFAULT_TITLE = "jadx-gui";
|
||||
@@ -809,7 +809,7 @@ public class MainWindow extends JFrame implements ExportProjectDialog.ExportProj
|
||||
}
|
||||
|
||||
private void exportProject() {
|
||||
ExportProjectDialog dialog = new ExportProjectDialog(this, this);
|
||||
ExportProjectDialog dialog = new ExportProjectDialog(this, this::saveAll);
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
|
||||
@@ -823,8 +823,8 @@ public class MainWindow extends JFrame implements ExportProjectDialog.ExportProj
|
||||
decompilerArgs.setSkipSources(exportProjectProperties.isSkipSources());
|
||||
decompilerArgs.setSkipResources(exportProjectProperties.isSkipResources());
|
||||
}
|
||||
|
||||
backgroundExecutor.execute(new ExportTask(this, wrapper, new File(exportProjectProperties.getExportPath())));
|
||||
File saveDir = new File(exportProjectProperties.getExportPath());
|
||||
backgroundExecutor.execute(new ExportTask(this, wrapper, saveDir));
|
||||
}
|
||||
|
||||
public void initTree() {
|
||||
@@ -1750,9 +1750,4 @@ public class MainWindow extends JFrame implements ExportProjectDialog.ExportProj
|
||||
public JadxGuiEventsImpl events() {
|
||||
return events;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProjectExportCalled(ExportProjectProperties exportProjectProperties) {
|
||||
saveAll(exportProjectProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,11 @@ import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -35,9 +33,8 @@ import jadx.gui.settings.JadxProject;
|
||||
import jadx.gui.ui.codearea.CodeArea;
|
||||
import jadx.gui.utils.NLS;
|
||||
import jadx.gui.utils.TextStandardActions;
|
||||
import jadx.gui.utils.UiUtils;
|
||||
|
||||
public class CommentDialog extends JDialog {
|
||||
public class CommentDialog extends CommonDialog {
|
||||
private static final long serialVersionUID = -1865682124935757528L;
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CommentDialog.class);
|
||||
@@ -96,7 +93,7 @@ public class CommentDialog extends JDialog {
|
||||
}
|
||||
return;
|
||||
}
|
||||
CommentStyle style = ((CommentStyle) styleCombo.getSelectedItem());
|
||||
CommentStyle style = (CommentStyle) styleCombo.getSelectedItem();
|
||||
ICodeComment newComment = new JadxCodeComment(comment.getNodeRef(), comment.getCodeRef(), newCommentStr, style);
|
||||
if (updateComment) {
|
||||
updateCommentsData(codeArea, list -> {
|
||||
@@ -122,7 +119,7 @@ public class CommentDialog extends JDialog {
|
||||
commentArea = new JTextArea();
|
||||
TextStandardActions.attach(commentArea);
|
||||
commentArea.setEditable(true);
|
||||
commentArea.setFont(codeArea.getMainWindow().getSettings().getFont());
|
||||
commentArea.setFont(mainWindow.getSettings().getFont());
|
||||
commentArea.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
|
||||
commentArea.addKeyListener(new KeyAdapter() {
|
||||
@@ -188,15 +185,7 @@ public class CommentDialog extends JDialog {
|
||||
} else {
|
||||
setTitle(NLS.str("comment_dialog.title.add"));
|
||||
}
|
||||
pack();
|
||||
setMinimumSize(getSize());
|
||||
if (!codeArea.getMainWindow().getSettings().loadWindowPos(this)) {
|
||||
setSize(400, 340);
|
||||
}
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
setModalityType(ModalityType.APPLICATION_MODAL);
|
||||
UiUtils.addEscapeShortCutToDispose(this);
|
||||
commonWindowInit();
|
||||
}
|
||||
|
||||
protected JPanel initButtonsPanel() {
|
||||
@@ -230,10 +219,4 @@ public class CommentDialog extends JDialog {
|
||||
buttonPane.add(cancelButton);
|
||||
return buttonPane;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
codeArea.getMainWindow().getSettings().saveWindowPos(this);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package jadx.gui.ui.dialog;
|
||||
|
||||
import java.awt.Dimension;
|
||||
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jadx.gui.ui.MainWindow;
|
||||
import jadx.gui.utils.UiUtils;
|
||||
|
||||
public abstract class CommonDialog extends JDialog {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CommonDialog.class);
|
||||
|
||||
protected final MainWindow mainWindow;
|
||||
|
||||
public CommonDialog(MainWindow mainWindow) {
|
||||
super(mainWindow);
|
||||
this.mainWindow = mainWindow;
|
||||
}
|
||||
|
||||
protected void commonWindowInit() {
|
||||
setModalityType(ModalityType.APPLICATION_MODAL);
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
UiUtils.addEscapeShortCutToDispose(this);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
pack();
|
||||
Dimension minSize = getSize();
|
||||
setMinimumSize(minSize);
|
||||
if (!mainWindow.getSettings().loadWindowPos(this)) {
|
||||
setSize(incByPercent(minSize.getWidth(), 30), incByPercent(minSize.getHeight(), 30));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
try {
|
||||
mainWindow.getSettings().saveWindowPos(this);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Failed to save window size and position", e);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
private static int incByPercent(double value, int percent) {
|
||||
return (int) (value * (1 + percent * 0.01));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.awt.event.ItemEvent;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.Box;
|
||||
@@ -14,16 +15,10 @@ import javax.swing.BoxLayout;
|
||||
import javax.swing.GroupLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jadx.gui.settings.ExportProjectProperties;
|
||||
import jadx.gui.ui.MainWindow;
|
||||
@@ -33,55 +28,35 @@ import jadx.gui.utils.NLS;
|
||||
import jadx.gui.utils.TextStandardActions;
|
||||
import jadx.gui.utils.ui.DocumentUpdateListener;
|
||||
|
||||
public class ExportProjectDialog extends JDialog {
|
||||
public class ExportProjectDialog extends CommonDialog {
|
||||
|
||||
public interface ExportProjectDialogListener {
|
||||
void onProjectExportCalled(ExportProjectProperties exportProjectProperties);
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ExportProjectDialog.class);
|
||||
private final ExportProjectDialogListener exportProjectDialogListener;
|
||||
private final ExportProjectProperties exportProjectProperties = new ExportProjectProperties();
|
||||
private final MainWindow mainWindow;
|
||||
private JTextField pathField;
|
||||
private final Consumer<ExportProjectProperties> exportListener;
|
||||
|
||||
public ExportProjectDialog(MainWindow mainWindow, ExportProjectDialogListener exportProjectDialogListener) {
|
||||
public ExportProjectDialog(MainWindow mainWindow, Consumer<ExportProjectProperties> exportListener) {
|
||||
super(mainWindow);
|
||||
this.mainWindow = mainWindow;
|
||||
this.exportProjectDialogListener = exportProjectDialogListener;
|
||||
this.exportListener = exportListener;
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
|
||||
JPanel contentPane = makeContentPane();
|
||||
|
||||
JPanel buttonPane = initButtonsPanel();
|
||||
|
||||
Container container = getContentPane();
|
||||
// contentPane.add(topPanel, BorderLayout.NORTH);
|
||||
container.add(contentPane, BorderLayout.CENTER);
|
||||
container.add(buttonPane, BorderLayout.PAGE_END);
|
||||
|
||||
setTitle(NLS.str("export_dialog.title"));
|
||||
|
||||
pack();
|
||||
setSize(400, 250);
|
||||
setLocationRelativeTo(mainWindow);
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
setModalityType(ModalityType.MODELESS);
|
||||
commonWindowInit();
|
||||
}
|
||||
|
||||
private JPanel makeContentPane() {
|
||||
|
||||
JPanel mainPanel = new JPanel();
|
||||
|
||||
// top layout
|
||||
JLabel label = new JLabel(NLS.str("export_dialog.save_path"));
|
||||
JTextField pathField = new JTextField();
|
||||
pathField.setText(mainWindow.getSettings().getLastSaveFilePath().toString());
|
||||
pathField.getDocument().addDocumentListener(new DocumentUpdateListener(ev -> setExportProjectPath(pathField)));
|
||||
new TextStandardActions(pathField);
|
||||
TextStandardActions.attach(pathField);
|
||||
|
||||
JButton browseButton = makeEditorBrowseButton(pathField);
|
||||
|
||||
@@ -111,9 +86,9 @@ public class ExportProjectDialog extends JDialog {
|
||||
skipSources.setEnabled(!isSelected);
|
||||
});
|
||||
|
||||
exportOptionsPanel.add(exportAsGradleProject);
|
||||
exportOptionsPanel.add(resourceDecode);
|
||||
exportOptionsPanel.add(skipSources);
|
||||
exportOptionsPanel.add(exportAsGradleProject);
|
||||
|
||||
// build group box layout
|
||||
JPanel groupBoxPanel = new JPanel();
|
||||
@@ -128,6 +103,7 @@ public class ExportProjectDialog extends JDialog {
|
||||
.addComponent(exportOptionsPanel));
|
||||
|
||||
// main layout
|
||||
JPanel mainPanel = new JPanel();
|
||||
GroupLayout layout = new GroupLayout(mainPanel);
|
||||
mainPanel.setLayout(layout);
|
||||
layout.setAutoCreateGaps(true);
|
||||
@@ -147,7 +123,6 @@ public class ExportProjectDialog extends JDialog {
|
||||
.addComponent(pathField)
|
||||
.addComponent(browseButton))
|
||||
.addComponent(groupBoxPanel));
|
||||
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
@@ -158,9 +133,7 @@ public class ExportProjectDialog extends JDialog {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected JPanel initButtonsPanel() {
|
||||
|
||||
JButton cancelButton = new JButton(NLS.str("common_dialog.cancel"));
|
||||
cancelButton.addActionListener(event -> dispose());
|
||||
|
||||
@@ -200,8 +173,7 @@ public class ExportProjectDialog extends JDialog {
|
||||
NLS.str("message.errorTitle"), JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
exportProjectDialogListener.onProjectExportCalled(exportProjectProperties);
|
||||
setVisible(false);
|
||||
exportListener.accept(exportProjectProperties);
|
||||
dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,14 +9,12 @@ import javax.swing.BorderFactory;
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JPopupMenu;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -30,15 +28,13 @@ import jadx.gui.treemodel.JRenameNode;
|
||||
import jadx.gui.ui.MainWindow;
|
||||
import jadx.gui.utils.NLS;
|
||||
import jadx.gui.utils.TextStandardActions;
|
||||
import jadx.gui.utils.UiUtils;
|
||||
import jadx.gui.utils.pkgs.JRenamePackage;
|
||||
import jadx.gui.utils.ui.DocumentUpdateListener;
|
||||
import jadx.gui.utils.ui.NodeLabel;
|
||||
|
||||
public class RenameDialog extends JDialog {
|
||||
public class RenameDialog extends CommonDialog {
|
||||
private static final long serialVersionUID = -3269715644416902410L;
|
||||
|
||||
private final transient MainWindow mainWindow;
|
||||
private final transient JRenameNode node;
|
||||
private transient JTextField renameField;
|
||||
private transient JButton renameBtn;
|
||||
@@ -68,7 +64,6 @@ public class RenameDialog extends JDialog {
|
||||
|
||||
private RenameDialog(MainWindow mainWindow, JRenameNode node) {
|
||||
super(mainWindow);
|
||||
this.mainWindow = mainWindow;
|
||||
this.node = node.replace();
|
||||
initUI();
|
||||
}
|
||||
@@ -161,6 +156,7 @@ public class RenameDialog extends JDialog {
|
||||
lbl.setLabelFor(nodeLabel);
|
||||
|
||||
renameField = new JTextField(40);
|
||||
renameField.setFont(mainWindow.getSettings().getFont());
|
||||
renameField.getDocument().addDocumentListener(new DocumentUpdateListener(ev -> checkNewName(renameField.getText())));
|
||||
renameField.addActionListener(e -> rename());
|
||||
new TextStandardActions(renameField);
|
||||
@@ -187,20 +183,6 @@ public class RenameDialog extends JDialog {
|
||||
contentPane.add(buttonPane, BorderLayout.PAGE_END);
|
||||
|
||||
setTitle(NLS.str("popup.rename"));
|
||||
if (!mainWindow.getSettings().loadWindowPos(this)) {
|
||||
setSize(800, 80);
|
||||
}
|
||||
// always pack (ignore saved windows sizes)
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
setModalityType(ModalityType.APPLICATION_MODAL);
|
||||
UiUtils.addEscapeShortCutToDispose(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
mainWindow.getSettings().saveWindowPos(this);
|
||||
super.dispose();
|
||||
commonWindowInit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user