Skip to content
forked from JabRef/jabref

Commit

Permalink
Add functionality to let the user import custom CSS file JabRef#5790
Browse files Browse the repository at this point in the history
* Add CSS file type, add button in preferences to import a custom CSS file, started on import functionality

* Change so that the log uses format specifiers instead of string concatenation

* Add RadioButton for toggling custom theme

* Add preference for setting the path to custom CSS theme

* Load custom CSS if toggled

* Add missing language keys

* Remove check if the current theme is applied again, the check is removed since we don't need it

* Save path to the custom CSS file in program preferences
  • Loading branch information
nilsstre authored Feb 26, 2020
1 parent 8c1b35c commit bcf8004
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<Label styleClass="sectionHeader" text="%Visual theme"/>
<RadioButton fx:id="themeLight" text="%Light theme" toggleGroup="$theme"/>
<RadioButton fx:id="themeDark" text="%Dark theme" toggleGroup="$theme"/>
<RadioButton fx:id="customTheme" text="%Custom theme" toggleGroup="$theme" />
</fx:root>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class AppearanceTabView extends AbstractPreferenceTabView<AppearanceTabVi
@FXML public TextField fontSize;
@FXML public RadioButton themeLight;
@FXML public RadioButton themeDark;
@FXML public RadioButton customTheme;

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();

Expand All @@ -43,7 +44,7 @@ public void initialize () {

themeLight.selectedProperty().bindBidirectional(viewModel.themeLightProperty());
themeDark.selectedProperty().bindBidirectional(viewModel.themeDarkProperty());

customTheme.selectedProperty().bindBidirectional(viewModel.customThemeProperty());
validationVisualizer.setDecoration(new IconValidationDecorator());
Platform.runLater(() -> validationVisualizer.initVisualization(viewModel.fontSizeValidationStatus(), fontSize));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class AppearanceTabViewModel implements PreferenceTabViewModel {
private final StringProperty fontSizeProperty = new SimpleStringProperty();
private final BooleanProperty themeLightProperty = new SimpleBooleanProperty();
private final BooleanProperty themeDarkProperty = new SimpleBooleanProperty();
private final BooleanProperty themeCustomProperty = new SimpleBooleanProperty();

private final DialogService dialogService;
private final JabRefPreferences preferences;
Expand Down Expand Up @@ -60,11 +61,17 @@ public void setValues() {
case ThemeLoader.DARK_CSS:
themeLightProperty.setValue(false);
themeDarkProperty.setValue(true);
themeCustomProperty.setValue(false);
break;
case ThemeLoader.MAIN_CSS:
default:
themeLightProperty.setValue(true);
themeDarkProperty.setValue(false);
themeCustomProperty.setValue(false);
break;
default:
themeLightProperty.setValue(false);
themeDarkProperty.setValue(false);
themeCustomProperty.setValue(true);
}
}

Expand All @@ -87,6 +94,9 @@ public void storeSettings() {
} else if (themeDarkProperty.getValue() && !preferences.get(JabRefPreferences.FX_THEME).equals(ThemeLoader.DARK_CSS)) {
restartWarnings.add(Localization.lang("Theme changed to dark theme."));
preferences.put(JabRefPreferences.FX_THEME, ThemeLoader.DARK_CSS);
} else if (themeCustomProperty.getValue()) {
restartWarnings.add(Localization.lang("Theme change to a custom theme."));
preferences.put(JabRefPreferences.FX_THEME, preferences.getPathToCustomTheme());
}
}

Expand All @@ -113,4 +123,5 @@ public boolean validateSettings() {

public BooleanProperty themeDarkProperty() { return themeDarkProperty; }

public BooleanProperty customThemeProperty() { return themeCustomProperty; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
</Button>
<Button maxWidth="Infinity" onAction="#showAllPreferences" text="%Show preferences"/>
<Button maxWidth="Infinity" onAction="#resetPreferences" text="%Reset preferences"/>
<Button maxWidth="Infinity" onAction="#importStyle" text="%Import CSS file">
<tooltip>
<Tooltip text="%Import custom CSS file" />
</tooltip>
</Button>
</VBox>
</VBox>
<ScrollPane fx:id="preferencesContainer" maxHeight="Infinity" maxWidth="Infinity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ private void savePreferencesAndCloseDialog() {

@FXML
void resetPreferences() { viewModel.resetPreferences(); }

@FXML
void importStyle() {viewModel.importCSSFile();}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,19 @@ public void setValues() {
preferencesTab.setValues();
}
}

public void importCSSFile() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.CSS)
.withDefaultExtension(StandardFileType.CSS)
.withInitialDirectory(preferences.setLastPreferencesExportPath()).build();

dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> {

preferences.setPathToCustomTheme(file.toAbsolutePath().toString());

dialogService.showWarningDialogAndWait(Localization.lang("Import CSS"),
Localization.lang("You must restart JabRef for this to come into effect."));
});
}
}
26 changes: 19 additions & 7 deletions src/main/java/org/jabref/gui/util/ThemeLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.util;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -48,9 +49,10 @@ public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRef

String cssVmArgument = System.getProperty("jabref.theme.css");
String cssPreferences = jabRefPreferences.get(JabRefPreferences.FX_THEME);

if (StringUtil.isNotBlank(cssVmArgument)) {
// First priority: VM argument
LOGGER.info("Using css from VM option: " + cssVmArgument);
LOGGER.info("Using css from VM option: {}", cssVmArgument);
URL cssVmUrl = null;
try {
cssVmUrl = Paths.get(cssVmArgument).toUri().toURL();
Expand All @@ -60,13 +62,23 @@ public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRef
additionalCssToLoad = Optional.ofNullable(cssVmUrl);
} else if (StringUtil.isNotBlank(cssPreferences) && !MAIN_CSS.equalsIgnoreCase(cssPreferences)) {
// Otherwise load css from preference
URL cssResource = JabRefFrame.class.getResource(cssPreferences);
if (cssResource != null) {
LOGGER.debug("Using css " + cssResource);
additionalCssToLoad = Optional.of(cssResource);
Optional<URL> cssResource = Optional.empty();
if (DARK_CSS.equals(cssPreferences)) {
cssResource = Optional.ofNullable(JabRefFrame.class.getResource(cssPreferences));
} else {
try {
cssResource = Optional.of(new File(cssPreferences).toURI().toURL());
} catch (MalformedURLException e) {
LOGGER.warn("Cannot load css {}", cssResource);
}
}

if (cssResource.isPresent()) {
LOGGER.debug("Using css {}", cssResource);
additionalCssToLoad = cssResource;
} else {
additionalCssToLoad = Optional.empty();
LOGGER.warn("Cannot load css " + cssPreferences);
LOGGER.warn("Cannot load css {}", cssPreferences);
}
} else {
additionalCssToLoad = Optional.empty();
Expand Down Expand Up @@ -104,7 +116,7 @@ private void addAndWatchForChanges(Scene scene, URL cssFile, int index) {
});
}
} catch (IOException | URISyntaxException | UnsupportedOperationException e) {
LOGGER.error("Could not watch css file for changes " + cssFile, e);
LOGGER.error("Could not watch css file for changes {}", cssFile, e);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/util/StandardFileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public enum StandardFileType implements FileType {
XML("xml"),
JSON("json"),
XMP("xmp"),
ZIP("zip");
ZIP("zip"),
CSS("css");

private final List<String> extensions;

Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class JabRefPreferences implements PreferencesService {

/* contents of the defaults HashMap that are defined in this class.
* There are more default parameters in this map which belong to separate preference classes.
*/
*/
public static final String TEXSTUDIO_PATH = "TeXstudioPath";
public static final String WIN_EDT_PATH = "winEdtPath";
public static final String TEXMAKER_PATH = "texmakerPath";
Expand Down Expand Up @@ -403,6 +403,8 @@ public class JabRefPreferences implements PreferencesService {
private static final int EXPORTER_FILENAME_INDEX = 1;
private static final int EXPORTER_EXTENSION_INDEX = 2;

private static final String PATH_TO_CUSTOM_THEME = "pathToCustomTheme";

// The only instance of this class:
private static JabRefPreferences singleton;
/**
Expand Down Expand Up @@ -759,6 +761,7 @@ private JabRefPreferences() {

// set default theme
defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.MAIN_CSS);
defaults.put(JabRefPreferences.PATH_TO_CUSTOM_THEME, "");

setLanguageDependentDefaultValues();
}
Expand Down Expand Up @@ -968,6 +971,14 @@ private List<Field> getCustomTabFieldNames() {
return customFields;
}

public void setPathToCustomTheme(String path) {
put(PATH_TO_CUSTOM_THEME, path);
}

public String getPathToCustomTheme() {
return get(PATH_TO_CUSTOM_THEME);
}

public void setLanguageDependentDefaultValues() {
// Entry editor tab 0:
defaults.put(CUSTOM_TAB_NAME + "_def0", Localization.lang("General"));
Expand Down Expand Up @@ -1129,7 +1140,7 @@ public BibDatabaseMode getDefaultBibDatabaseMode() {
* Set the default value for a key. This is useful for plugins that need to add default values for the prefs keys
* they use.
*
* @param key The preferences key.
* @param key The preferences key.
* @param value The default value.
*/
public void putDefaultValue(String key, Object value) {
Expand All @@ -1139,7 +1150,7 @@ public void putDefaultValue(String key, Object value) {
/**
* Stores a color in preferences.
*
* @param key The key for this setting.
* @param key The key for this setting.
* @param color The Color to store.
*/
public void putColor(String key, Color color) {
Expand Down Expand Up @@ -1356,7 +1367,7 @@ public void exportPreferences(Path file) throws JabRefException {
prefs.exportSubtree(os);
} catch (BackingStoreException | IOException ex) {
throw new JabRefException("Could not export preferences", Localization.lang("Could not export preferences"),
ex);
ex);
}
}

Expand Down Expand Up @@ -2097,7 +2108,7 @@ public void saveCustomEntryTypes() {
}

private void saveCustomEntryTypes(BibDatabaseMode bibDatabaseMode) {
List<BibEntryType> customBiblatexBibTexTypes = Globals.entryTypesManager.getAllTypes(bibDatabaseMode).stream()
List<BibEntryType> customBiblatexBibTexTypes = Globals.entryTypesManager.getAllTypes(bibDatabaseMode).stream()
.map(entryType -> entryType).collect(Collectors.toList());

storeBibEntryTypes(customBiblatexBibTexTypes, bibDatabaseMode);
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ Importing=Importing

Importing\ in\ unknown\ format=Importing in unknown format

Import\ CSS=Import CSS

Import\ CSS\ file=Import CSS file

Import\ custom\ CSS\ file=Import custom CSS file

Theme\ change\ to\ a\ custom\ theme.=Theme change to a custom theme.

Include\ subgroups\:\ When\ selected,\ view\ entries\ contained\ in\ this\ group\ or\ its\ subgroups=Include subgroups: When selected, view entries contained in this group or its subgroups

Independent\ group\:\ When\ selected,\ view\ only\ this\ group's\ entries=Independent group: When selected, view only this group's entries
Expand Down Expand Up @@ -2041,6 +2049,7 @@ Font=Font
Visual\ theme=Visual theme
Light\ theme=Light theme
Dark\ theme=Dark theme
Custom\ theme=Custom theme
Overwrite\ existing\ keys=Overwrite existing keys
Key\ patterns=Key patterns
Size\:=Size:
Expand Down

0 comments on commit bcf8004

Please sign in to comment.