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

Try to fix dark theme path #5497

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.preview;

import java.net.URL;
import java.util.Base64;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -27,6 +29,7 @@
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 @@ -116,9 +119,14 @@ public PreviewViewer(BibDatabaseContext database, DialogService dialogService, S

public void setTheme(String theme) {
if (theme.equals(ThemeLoader.DARK_CSS)) {
previewView.getEngine().setUserStyleSheetLocation(JabRefFrame.class.getResource(ThemeLoader.DARK_CSS).toString());
// We need to load the css file manually, due to a bug in the jdk
// TODO: Remove this workaround as soon as https://github.com/openjdk/jfx/pull/22 is merged
URL url = JabRefFrame.class.getResource(ThemeLoader.DARK_CSS);
String dataUrl = "data:text/css;charset=utf-8;base64," +
Base64.getEncoder().encodeToString(StringUtil.getResourceFileAsString(url).getBytes());

previewView.getEngine().setUserStyleSheetLocation(dataUrl);
}

}

private void highlightSearchPattern() {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/jabref/model/strings/StringUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.jabref.model.strings;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -9,6 +16,7 @@
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jabref.architecture.ApacheCommonsLang3Allowed;

Expand Down Expand Up @@ -721,4 +729,19 @@ public static boolean containsIgnoreCase(String text, String searchString) {
public static String substringBetween(String str, String open, String close) {
return StringUtils.substringBetween(str, open, close);
}

public static String getResourceFileAsString(URL resource) {
try {
URLConnection conn = resource.openConnection();
conn.connect();

try (InputStream inputStream = new BufferedInputStream(conn.getInputStream());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
} catch (IOException e) {
throw new RuntimeException(e);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a runtime exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this should never happen but I still want to inform the user (instead of only logging it).

}
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/jabref/model/strings/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void StringUtilClassIsSmall() throws Exception {
Path path = Paths.get("src", "main", "java", StringUtil.class.getName().replace('.', '/') + ".java");
int lineCount = Files.readAllLines(path, StandardCharsets.UTF_8).size();

assertTrue(lineCount <= 725, "StringUtil increased in size to " + lineCount + ". "
assertTrue(lineCount <= 749, "StringUtil increased in size to " + lineCount + ". "
+ "We try to keep this class as small as possible. "
+ "Thus think twice if you add something to StringUtil.");
}
Expand Down