Skip to content

Commit

Permalink
fix(gui): editor theme loading and error/fallback handling improved
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz committed May 8, 2022
1 parent a717392 commit 9a7eaf8
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -71,6 +72,7 @@
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -112,6 +114,7 @@
import jadx.gui.treemodel.JRoot;
import jadx.gui.ui.codearea.AbstractCodeArea;
import jadx.gui.ui.codearea.AbstractCodeContentPanel;
import jadx.gui.ui.codearea.EditorTheme;
import jadx.gui.ui.codearea.EditorViewState;
import jadx.gui.ui.dialog.ADBDialog;
import jadx.gui.ui.dialog.AboutDialog;
Expand Down Expand Up @@ -1298,15 +1301,32 @@ public void setLocationAndPosition() {
}

private void setEditorTheme(String editorThemePath) {
try {
URL themeUrl = getClass().getResource(editorThemePath);
if (themeUrl != null) {
try (InputStream is = themeUrl.openStream()) {
editorTheme = Theme.load(is);
return;
}
}
Path themePath = Paths.get(editorThemePath);
if (Files.isRegularFile(themePath)) {
try (InputStream is = Files.newInputStream(themePath)) {
editorTheme = Theme.load(is);
return;
}
}
} catch (Exception e) {
LOG.error("Failed to load editor theme: {}", editorThemePath, e);
}
LOG.warn("Falling back to default editor theme", editorThemePath);
editorThemePath = EditorTheme.getDefaultTheme().getPath();
try (InputStream is = getClass().getResourceAsStream(editorThemePath)) {
editorTheme = Theme.load(is);
return;
} catch (Exception e) {
LOG.error("Can't load editor theme from classpath: {}", editorThemePath);
try (InputStream is = new FileInputStream(editorThemePath)) {
editorTheme = Theme.load(is);
} catch (Exception ex) {
LOG.error("Can't load editor theme from file: {}", editorThemePath);
}
LOG.error("Failed to load default editor theme: {}", editorThemePath, e);
editorTheme = new Theme(new RSyntaxTextArea());
}
}

Expand Down

0 comments on commit 9a7eaf8

Please sign in to comment.