未验证 提交 18070eb7 编写于 作者: S Skylot

fix(gui): allow to select file on mapping export

上级 84868917
...@@ -57,6 +57,18 @@ public class JadxProject { ...@@ -57,6 +57,18 @@ public class JadxProject {
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
} }
public @Nullable Path getWorkingDir() {
if (projectPath != null) {
return projectPath.toAbsolutePath().getParent();
}
List<Path> files = data.getFiles();
if (!files.isEmpty()) {
Path path = files.get(0);
return path.toAbsolutePath().getParent();
}
return null;
}
@Nullable @Nullable
public Path getProjectPath() { public Path getProjectPath() {
return projectPath; return projectPath;
...@@ -166,7 +178,7 @@ public class JadxProject { ...@@ -166,7 +178,7 @@ public class JadxProject {
Path path = files.get(0); Path path = files.get(0);
return path.resolveSibling(path.getFileName() + ".cache"); return path.resolveSibling(path.getFileName() + ".cache");
} }
throw new JadxRuntimeException("Can't get working dir"); throw new JadxRuntimeException("Failed to build cache dir");
} }
public boolean isEnableLiveReload() { public boolean isEnableLiveReload() {
......
...@@ -44,6 +44,7 @@ import javax.swing.Action; ...@@ -44,6 +44,7 @@ import javax.swing.Action;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem; import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
...@@ -85,7 +86,6 @@ import jadx.api.JavaNode; ...@@ -85,7 +86,6 @@ import jadx.api.JavaNode;
import jadx.api.ResourceFile; import jadx.api.ResourceFile;
import jadx.api.plugins.utils.CommonFileUtils; import jadx.api.plugins.utils.CommonFileUtils;
import jadx.core.Jadx; import jadx.core.Jadx;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.ListUtils; import jadx.core.utils.ListUtils;
import jadx.core.utils.StringUtils; import jadx.core.utils.StringUtils;
import jadx.core.utils.files.FileUtils; import jadx.core.utils.files.FileUtils;
...@@ -362,17 +362,28 @@ public class MainWindow extends JFrame { ...@@ -362,17 +362,28 @@ public class MainWindow extends JFrame {
} }
private void exportMappings(MappingFormat mappingFormat) { private void exportMappings(MappingFormat mappingFormat) {
RootNode rootNode = wrapper.getDecompiler().getRoot(); FileDialog fileDialog = new FileDialog(this, FileDialog.OpenMode.CUSTOM_SAVE);
fileDialog.setTitle(NLS.str("file.export_mappings_as"));
Thread exportThread = new Thread(() -> { Path workingDir = project.getWorkingDir();
new MappingExporter(rootNode).exportMappings( Path baseDir = workingDir != null ? workingDir : settings.getLastSaveFilePath();
Paths.get(project.getProjectPath().getParent().toString(), if (mappingFormat.hasSingleFile()) {
"mappings" + (mappingFormat.hasSingleFile() ? "." + mappingFormat.fileExt : "")), fileDialog.setSelectedFile(baseDir.resolve("mappings." + mappingFormat.fileExt));
project.getCodeData(), mappingFormat); fileDialog.setFileExtList(Collections.singletonList(mappingFormat.fileExt));
}); fileDialog.setSelectionMode(JFileChooser.FILES_ONLY);
} else {
backgroundExecutor.execute(NLS.str("progress.export_mappings"), exportThread); fileDialog.setCurrentDir(baseDir);
update(); fileDialog.setSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
List<Path> paths = fileDialog.show();
if (paths.size() != 1) {
return;
}
Path savePath = paths.get(0);
LOG.info("Export mappings to: {}", savePath.toAbsolutePath());
backgroundExecutor.execute(NLS.str("progress.export_mappings"),
() -> new MappingExporter(wrapper.getDecompiler().getRoot())
.exportMappings(savePath, project.getCodeData(), mappingFormat),
s -> update());
} }
void open(List<Path> paths) { void open(List<Path> paths) {
......
...@@ -27,7 +27,12 @@ import jadx.gui.utils.NLS; ...@@ -27,7 +27,12 @@ import jadx.gui.utils.NLS;
public class FileDialog { public class FileDialog {
public enum OpenMode { public enum OpenMode {
OPEN, ADD, SAVE_PROJECT, EXPORT OPEN,
ADD,
SAVE_PROJECT,
EXPORT,
CUSTOM_SAVE,
CUSTOM_OPEN
} }
private final MainWindow mainWindow; private final MainWindow mainWindow;
...@@ -44,6 +49,26 @@ public class FileDialog { ...@@ -44,6 +49,26 @@ public class FileDialog {
initForMode(mode); initForMode(mode);
} }
public void setTitle(String title) {
this.title = title;
}
public void setFileExtList(List<String> fileExtList) {
this.fileExtList = fileExtList;
}
public void setSelectionMode(int selectionMode) {
this.selectionMode = selectionMode;
}
public void setSelectedFile(Path path) {
this.selectedFile = path;
}
public void setCurrentDir(Path currentDir) {
this.currentDir = currentDir;
}
public List<Path> show() { public List<Path> show() {
FileChooser fileChooser = buildFileChooser(); FileChooser fileChooser = buildFileChooser();
int ret = isOpen ? fileChooser.showOpenDialog(mainWindow) : fileChooser.showSaveDialog(mainWindow); int ret = isOpen ? fileChooser.showOpenDialog(mainWindow) : fileChooser.showSaveDialog(mainWindow);
...@@ -66,10 +91,6 @@ public class FileDialog { ...@@ -66,10 +91,6 @@ public class FileDialog {
return currentDir; return currentDir;
} }
public void setSelectedFile(Path path) {
this.selectedFile = path;
}
private void initForMode(OpenMode mode) { private void initForMode(OpenMode mode) {
switch (mode) { switch (mode) {
case OPEN: case OPEN:
...@@ -101,6 +122,14 @@ public class FileDialog { ...@@ -101,6 +122,14 @@ public class FileDialog {
currentDir = mainWindow.getSettings().getLastSaveFilePath(); currentDir = mainWindow.getSettings().getLastSaveFilePath();
isOpen = false; isOpen = false;
break; break;
case CUSTOM_SAVE:
isOpen = false;
break;
case CUSTOM_OPEN:
isOpen = true;
break;
} }
} }
...@@ -110,7 +139,7 @@ public class FileDialog { ...@@ -110,7 +139,7 @@ public class FileDialog {
fileChooser.setFileSelectionMode(selectionMode); fileChooser.setFileSelectionMode(selectionMode);
fileChooser.setMultiSelectionEnabled(isOpen); fileChooser.setMultiSelectionEnabled(isOpen);
fileChooser.setAcceptAllFileFilterUsed(true); fileChooser.setAcceptAllFileFilterUsed(true);
if (!fileExtList.isEmpty()) { if (Utils.notEmpty(fileExtList)) {
String description = NLS.str("file_dialog.supported_files") + ": (" + Utils.listToString(fileExtList) + ')'; String description = NLS.str("file_dialog.supported_files") + ": (" + Utils.listToString(fileExtList) + ')';
fileChooser.setFileFilter(new FileNameExtensionFilter(description, fileExtList.toArray(new String[0]))); fileChooser.setFileFilter(new FileNameExtensionFilter(description, fileExtList.toArray(new String[0])));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册