Skip to content

Commit

Permalink
avoid repeat adding the same css
Browse files Browse the repository at this point in the history
  • Loading branch information
Fancy Z committed Oct 25, 2018
1 parent a3a1531 commit 613db98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/jabref/gui/util/ThemeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
*/
public class ThemeLoader {

private static final String DEFAULT_PATH_MAIN_CSS = JabRefFrame.class.getResource("Base.css").toExternalForm();
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 final FileUpdateMonitor fileUpdateMonitor;
Expand All @@ -57,25 +58,31 @@ public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRef
}
}


/**
* Installs the base css file as a stylesheet in the given scene.
* Changes in the css file lead to a redraw of the scene using the new css file.
*/
public void installBaseCss(Scene scene, JabRefPreferences preferences) {
addAndWatchForChanges(scene, DEFAULT_PATH_MAIN_CSS, 0);

if (StringUtil.isNotBlank(cssProperty)) {
final Path path = Paths.get(cssProperty);
if (Files.isReadable(path)) {
String cssUrl = path.toUri().toString();
addAndWatchForChanges(scene, cssUrl, 1);
addAndWatchForChanges(scene, cssUrl, 0);
}else{
LOGGER.warn(path.toAbsolutePath() + " is not readable");
}
}else{
addAndWatchForChanges(scene, DEFAULT_PATH_MAIN_CSS, 0);
}

preferences.getFontSize().ifPresent(size -> scene.getRoot().setStyle("-fx-font-size: " + size + "pt;"));
}

private void addAndWatchForChanges(Scene scene, String cssUrl, int index) {
// avoid repeat add
if (scene.getStylesheets().contains(cssUrl)) return;

scene.getStylesheets().add(index, cssUrl);

try {
Expand All @@ -86,7 +93,6 @@ private void addAndWatchForChanges(Scene scene, String cssUrl, int index) {
LOGGER.info("Enabling live reloading of " + cssFile);
fileUpdateMonitor.addListenerForFile(cssFile, () -> {
LOGGER.info("Reload css file " + cssFile);

DefaultTaskExecutor.runInJavaFXThread(() -> {
scene.getStylesheets().remove(cssUrl);
scene.getStylesheets().add(index, cssUrl);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.jabref.gui.maintable.ColumnPreferences;
import org.jabref.gui.maintable.MainTablePreferences;
import org.jabref.gui.preferences.ImportSettingsTab;
import org.jabref.gui.util.ThemeLoader;
import org.jabref.logic.bibtex.FieldContentParserPreferences;
import org.jabref.logic.bibtex.LatexFieldFormatterPreferences;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences;
Expand Down Expand Up @@ -781,7 +782,7 @@ private JabRefPreferences() {
+ "</dd>__NEWLINE__<p></p></font>");

// set default theme
defaults.put(JabRefPreferences.FX_THEME, "Base.css");
defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.DEFAULT_MAIN_CSS);
setLanguageDependentDefaultValues();
}

Expand Down

0 comments on commit 613db98

Please sign in to comment.