Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed exception about missing custom css file #7292

Merged
merged 17 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue with the style of highlighted check boxes while searching in preferences. [#7226](https://github.com/JabRef/jabref/issues/7226)
- We fixed an issue where the option "Move file to file directory" was disabled in the entry editor for all files [#7194](https://github.com/JabRef/jabref/issues/7194)
- We fixed an issue where application dialogs were opening in the wrong display when using multiple screens [#7273](https://github.com/JabRef/jabref/pull/7273)
- We fixed an issue where an exception would be displayed for previewing and preferences when a custom theme has been configured but is missing [#7177](https://github.com/JabRef/jabref/issues/7177)

### Removed

Expand Down
22 changes: 1 addition & 21 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.jabref.gui.preview;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
Expand All @@ -19,7 +16,6 @@
import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.StateManager;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
Expand All @@ -30,7 +26,6 @@
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -126,22 +121,7 @@ public PreviewViewer(BibDatabaseContext database, DialogService dialogService, S
}

public void setTheme(Theme theme) {
if (theme.getType() == Theme.Type.DARK) {
// We need to load the css file manually, due to a bug in the jdk
// https://bugs.openjdk.java.net/browse/JDK-8240969
// TODO: Remove this workaround as soon as openjfx 16 is released
URL url = JabRefFrame.class.getResource(theme.getPath().getFileName().toString());
String dataUrl = "data:text/css;charset=utf-8;base64," +
Base64.getEncoder().encodeToString(StringUtil.getResourceFileAsString(url).getBytes());

previewView.getEngine().setUserStyleSheetLocation(dataUrl);
} else if (theme.getType() != Theme.Type.LIGHT) {
try {
previewView.getEngine().setUserStyleSheetLocation(theme.getPath().toUri().toURL().toExternalForm());
} catch (MalformedURLException ex) {
LOGGER.error("Cannot set custom theme, invalid url", ex);
}
}
theme.ifAdditionalStylesheetPresent(location -> previewView.getEngine().setUserStyleSheetLocation(location));
}

private void highlightSearchPattern() {
Expand Down
68 changes: 50 additions & 18 deletions src/main/java/org/jabref/gui/util/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Optional;
import java.util.function.Consumer;

import javafx.scene.Scene;

Expand Down Expand Up @@ -36,8 +38,13 @@ public enum Type {
private static final Logger LOGGER = LoggerFactory.getLogger(Theme.class);

private final Type type;

// String and URL formats of the path to the css.
// in general, call method additionalCssToLoad(), to ensure file existence checks are performed
private final Path pathToCss;
private final Optional<URL> additionalCssToLoad;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private final Optional<URL> cssUrl;

private final PreferencesService preferencesService;

public Theme(String path, PreferencesService preferencesService) {
Expand All @@ -47,31 +54,31 @@ public Theme(String path, PreferencesService preferencesService) {
if (StringUtil.isBlank(path) || BASE_CSS.equalsIgnoreCase(path)) {
// Light theme
this.type = Type.LIGHT;
this.additionalCssToLoad = Optional.empty();
this.cssUrl = Optional.empty();
} else {
Optional<URL> cssResource = Optional.ofNullable(JabRefFrame.class.getResource(path));
if (cssResource.isPresent()) {
URL url = JabRefFrame.class.getResource(path);
if (url != null) {
// Embedded dark theme
this.type = Type.DARK;
} else {
// Custom theme
this.type = Type.CUSTOM;
if (Files.exists(pathToCss)) {
try {
cssResource = Optional.of(pathToCss.toUri().toURL());
} catch (MalformedURLException e) {
cssResource = Optional.empty();
}
try {
url = pathToCss.toUri().toURL();
} catch (MalformedURLException e) {
url = null;
}
}

if (cssResource.isPresent()) {
additionalCssToLoad = cssResource;
LOGGER.debug("Using css {}", path);
} else {
additionalCssToLoad = Optional.empty();
LOGGER.warn("Cannot load css {}", path);
}
this.cssUrl = Optional.ofNullable(url);
}
}

private Optional<URL> additionalCssToLoad() {
if (type == Type.CUSTOM && !Files.exists(pathToCss)) {
docrjp marked this conversation as resolved.
Show resolved Hide resolved
return Optional.empty();
} else {
return cssUrl;
}
}

Expand All @@ -83,7 +90,7 @@ public void installCss(Scene scene, FileUpdateMonitor fileUpdateMonitor) {
AppearancePreferences appearancePreferences = preferencesService.getAppearancePreferences();

addAndWatchForChanges(scene, JabRefFrame.class.getResource(BASE_CSS), fileUpdateMonitor, 0);
additionalCssToLoad.ifPresent(file -> addAndWatchForChanges(scene, file, fileUpdateMonitor, 1));
additionalCssToLoad().ifPresent(file -> addAndWatchForChanges(scene, file, fileUpdateMonitor, 1));

if (appearancePreferences.shouldOverrideDefaultFontSize()) {
scene.getRoot().setStyle("-fx-font-size: " + appearancePreferences.getMainFontSize() + "pt;");
Expand Down Expand Up @@ -134,4 +141,29 @@ public Type getType() {
public Path getPath() {
return pathToCss;
}

/**
* This method allows callers to consume the theme's additional stylesheet. The consumer is only called if there is
* a stylesheet that is additional to the base stylesheet, and either embedded in the application or present on
* the file system.
*
* @param consumer called with the stylesheet location if there is an additional stylesheet present. The location
* is local URL (e.g. {@code 'data:'} or {@code 'file:'})
*/
public void ifAdditionalStylesheetPresent(Consumer<String> consumer) {

final Optional<String> location;
if (type == Theme.Type.DARK) {
// We need to load the css file manually, due to a bug in the jdk
// https://bugs.openjdk.java.net/browse/JDK-8240969
// TODO: Remove this workaround, and update javadoc to include jrt: URL prefix, as soon as openjfx 16 is released
URL url = JabRefFrame.class.getResource(pathToCss.getFileName().toString());
location = Optional.of("data:text/css;charset=utf-8;base64," +
Base64.getEncoder().encodeToString(StringUtil.getResourceFileAsString(url).getBytes()));
} else {
location = additionalCssToLoad().map(URL::toExternalForm);
}

location.ifPresent(consumer);
}
}