8000 Remember theme across tabs within the same session · scijava/script-editor@41c8c3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 41c8c3e

Browse files
committed
Remember theme across tabs within the same session
Reported by @acardona, #56 (comment)
1 parent 7c55b2d commit 41c8c3e

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/main/java/org/scijava/ui/swing/script/EditorPane.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
6464
import org.fife.ui.rsyntaxtextarea.Style;
6565
import org.fife.ui.rsyntaxtextarea.SyntaxScheme;
66+
import org.fife.ui.rsyntaxtextarea.Theme;
6667
import org.fife.ui.rtextarea.Gutter;
6768
import org.fife.ui.rtextarea.GutterIconInfo;
6869
import org.fife.ui.rtextarea.RTextArea;
@@ -837,8 +838,7 @@ public void convertSpacesToTabs() {
837838
public static final String DEFAULT_THEME = "default";
838839

839840
/**
840-
* Loads and applies the preferences for the tab (theme excluded).
841-
* @see TextEditor#applyTheme(String)
841+
* Loads and applies the preferences for the tab
842842
*/
843843
public void loadPreferences() {
844844
if (prefService == null) {
@@ -862,13 +862,47 @@ public void loadPreferences() {
862862
setKeylessAutoCompletion(prefService.getBoolean(getClass(), AUTOCOMPLETE_KEYLESS_PREFS, true)); // true for backwards compatibility with IJ1 macro
863863
setFallbackAutoCompletion(prefService.getBoolean(getClass(), AUTOCOMPLETE_FALLBACK_PREFS, false));
864864
setMarkOccurrences(prefService.getBoolean(getClass(), MARK_OCCURRENCES_PREFS, false));
865+
applyTheme(themeName());
866+
}
867+
}
868+
869+
/**
870+
* Applies a theme to this pane.
871+
*
872+
* @param theme either "default", "dark", "druid", "eclipse", "idea", "monokai",
873+
* "vs"
874+
* @throws IllegalArgumentException If {@code theme} is not a valid option, or
875+
* the resource could not be loaded
876+
*/
877+
public void applyTheme(final String theme) throws IllegalArgumentException {
878+
try {
879+
applyTheme(getTheme(theme));
880+
} catch (final Exception ex) {
881+
throw new IllegalArgumentException(ex);
865882
}
866883
}
867884

868885
public String themeName() {
869886
return prefService.get(getClass(), THEME_PREFS, DEFAULT_THEME);
870887
}
871888

889+
private void applyTheme(final Theme th) throws IllegalArgumentException {
890+
// themes include font size, so we'll need to reset that
891+
final float existingFontSize = getFontSize();
892+
th.apply(this);
893+
setFontSize(existingFontSize);
894+
updateBookmarkIcon(); // update bookmark icon color
895+
}
896+
897+
private static Theme getTheme(final String theme) throws IllegalArgumentException {
898+
try {
899+
return Theme
900+
.load(TextEditor.class.getResourceAsStream("/org/fife/ui/rsyntaxtextarea/themes/" + theme + ".xml"));
901+
} catch (final Exception ex) {
902+
throw new IllegalArgumentException(ex);
903+
}
904+
}
905+
872906
public String loadFolders() {
873907
return prefService.get(getClass(), FOLDERS_PREFS, System.getProperty("user.home"));
874908
}

src/main/java/org/scijava/ui/swing/script/TextEditor.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@
132132

133133
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
134134
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
135-
import org.fife.ui.rsyntaxtextarea.Theme;
136135
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
137136
import org.scijava.Context;
138137
import org.scijava.app.AppService;
@@ -1494,9 +1493,11 @@ private JMenu applyThemeMenu() {
14941493
item.addActionListener(e -> {
14951494
try {
14961495
applyTheme(v, false);
1496+
// make the choice available for the next tab
1497+
prefService.put(EditorPane.class, EditorPane.THEME_PREFS, v);
14971498
} catch (final IllegalArgumentException ex) {
14981499
JOptionPane.showMessageDialog(TextEditor.this,
1499-
"An exception occured. Theme could not be loaded");
1500+
"An exception occured. Theme could not be loaded.");
15001501
ex.printStackTrace();
15011502
}
15021503
});
@@ -1519,21 +1520,22 @@ public void applyTheme(final String theme) throws IllegalArgumentException {
15191520

15201521
private void applyTheme(final String theme, final boolean updateUI) throws IllegalArgumentException {
15211522
try {
1522-
final Theme th = Theme
1523-
.load(getClass().getResourceAsStream("/org/fife/ui/rsyntaxtextarea/themes/" + theme + ".xml"));
15241523
for (int i = 0; i < tabbed.getTabCount(); i++) {
1525-
// themes include font size, so we'll need to reset that
1526-
final EditorPane ep = getEditorPane(i);
1527-
final float existingFontSize = ep.getFontSize();
1528-
th.apply(ep);
1529-
ep.setFontSize(existingFontSize);
1530-
ep.updateBookmarkIcon(); // update bookmark icon color
1524+
getEditorPane(i).applyTheme(theme);
15311525
}
15321526
} catch (final Exception ex) {
1527+
activeTheme = "default";
1528+
updateThemeControls("default");
1529+
writeError("Could not load theme. See Console for details.");
1530+
updateThemeControls(activeTheme);
15331531
throw new IllegalArgumentException(ex);
15341532
}
1535-
this.activeTheme = theme;
1536-
if (updateUI && themeRadioGroup != null) {
1533+
activeTheme = theme;
1534+
if (updateUI) updateThemeControls(theme);
1535+
}
1536+
1537+
private void updateThemeControls(final String theme) {
1538+
if (themeRadioGroup != null) {
15371539
final Enumeration<AbstractButton> choices = themeRadioGroup.getElements();
15381540
while (choices.hasMoreElements()) {
15391541
final AbstractButton choice = choices.nextElement();

0 commit comments

Comments
 (0)

Footer

© 2025 GitHub, Inc.
0