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

fix java.nio.file.FileSystemNotFoundException and reorganize code #4416

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Changes from all commits
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
41 changes: 19 additions & 22 deletions src/main/java/org/jabref/gui/util/ThemeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import javafx.scene.Parent;
import javafx.scene.Scene;

import org.jabref.JabRefException;
import org.jabref.gui.JabRefFrame;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;
Expand Down Expand Up @@ -39,21 +37,25 @@ public class ThemeLoader {
public static final String DEFAULT_MAIN_CSS = "Base.css";
private static final String DEFAULT_PATH_MAIN_CSS = JabRefFrame.class.getResource(DEFAULT_MAIN_CSS).toExternalForm();
private static final Logger LOGGER = LoggerFactory.getLogger(ThemeLoader.class);
private String cssProperty = System.getProperty("jabref.theme.css");
private String cssToLoad = System.getProperty("jabref.theme.css");
private final FileUpdateMonitor fileUpdateMonitor;

public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRefPreferences) throws JabRefException {
public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRefPreferences) {
this.fileUpdateMonitor = Objects.requireNonNull(fileUpdateMonitor);

if (StringUtil.isNullOrEmpty(cssProperty)) {
String cssFileName = jabRefPreferences.get(JabRefPreferences.FX_THEME);
if (cssFileName != null) {
try {
cssProperty = Paths.get(JabRefFrame.class.getResource(cssFileName).toURI()).toString();
} catch (URISyntaxException e) {
LOGGER.warn("can't get css file URI");
throw new JabRefException("can't set custom theme");
}
if (!StringUtil.isNullOrEmpty(cssToLoad)) {
LOGGER.info("using css from system " + cssToLoad);
return;
}

// otherwise load css from preference
String cssFileName = jabRefPreferences.get(JabRefPreferences.FX_THEME);
if (cssFileName != null) {
try {
cssToLoad = JabRefFrame.class.getResource(cssFileName).toExternalForm();
LOGGER.info("using css " + cssToLoad);
} catch (Exception e) {
LOGGER.warn("can't get css file path of " + cssFileName);
}
}
}
Expand All @@ -64,15 +66,10 @@ public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRef
* Changes in the css file lead to a redraw of the scene using the new css file.
*/
public void installBaseCss(Scene scene, JabRefPreferences preferences) {
if (StringUtil.isNotBlank(cssProperty)) {
final Path path = Paths.get(cssProperty);
if (Files.isReadable(path)) {
String cssUrl = path.toUri().toString();
addAndWatchForChanges(scene, cssUrl, 0);
} else {
LOGGER.warn(path.toAbsolutePath() + " is not readable");
}
if (!StringUtil.isNullOrEmpty(cssToLoad)) {
addAndWatchForChanges(scene, cssToLoad, 0);
} else {
LOGGER.warn("using the last default css " + DEFAULT_PATH_MAIN_CSS);
addAndWatchForChanges(scene, DEFAULT_PATH_MAIN_CSS, 0);
}

Expand All @@ -88,7 +85,7 @@ private void addAndWatchForChanges(Scene scene, String cssUrl, int index) {
try {
// If -Djabref.theme.css is defined and the resources are not part of a .jar bundle,
// we watch the file for changes and turn on live reloading
if (!cssUrl.startsWith("jar:") && cssProperty != null) {
if (!cssUrl.startsWith("jar:")) {
Path cssFile = Paths.get(new URL(cssUrl).toURI());
LOGGER.info("Enabling live reloading of " + cssFile);
fileUpdateMonitor.addListenerForFile(cssFile, () -> {
Expand Down