Deduplication

This commit is contained in:
Sebastian Stenzel
2019-08-19 16:27:35 +02:00
parent 644f2d9a89
commit 3d61fab8b6
3 changed files with 57 additions and 23 deletions

View File

@@ -0,0 +1,49 @@
package org.cryptomator.ui.common;
import javafx.scene.text.Font;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
public class FontLoader {
private static final Logger LOG = LoggerFactory.getLogger(FontLoader.class);
private static final double DEFAULT_FONT_SIZE = 12;
public static Font load(String resourcePath) throws FontLoaderException {
try (InputStream in = FontLoader.class.getResourceAsStream(resourcePath)) {
if (in == null) {
throw new FontLoaderException(resourcePath);
} else {
return load(resourcePath, in);
}
} catch (IOException e) {
throw new FontLoaderException(resourcePath, e);
}
}
private static Font load(String resourcePath, InputStream in) throws FontLoaderException {
Font font = Font.loadFont(in, DEFAULT_FONT_SIZE);
if (font != null) {
LOG.debug("Loaded family: {}", font.getFamily());
return font;
} else {
throw new FontLoaderException(resourcePath);
}
}
public static class FontLoaderException extends IOException {
private FontLoaderException(String resourceName) {
super("Failed to load font: " + resourceName);
}
private FontLoaderException(String resourceName, Throwable cause) {
super("Failed to load font: " + resourceName, cause);
}
}
}

View File

@@ -1,6 +1,5 @@
package org.cryptomator.ui.controls;
import com.google.common.base.Preconditions;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
@@ -8,11 +7,10 @@ import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import org.cryptomator.ui.common.FontLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
/**
@@ -30,15 +28,9 @@ public class FontAwesome5IconView extends Text {
private DoubleProperty glyphSize = new SimpleDoubleProperty(this, "glyphSize", DEFAULT_GLYPH_SIZE);
static {
try (InputStream in = FontAwesome5IconView.class.getResourceAsStream(FONT_PATH)) {
Preconditions.checkNotNull(in, "Resource not found: " + FONT_PATH);
FONT = Font.loadFont(in, DEFAULT_GLYPH_SIZE);
if (FONT != null) {
LOG.debug("Loaded family: {}", FONT.getFamily());
} else {
throw new IllegalStateException("Failed to load font.");
}
} catch (IOException e) {
try {
FONT = FontLoader.load(FONT_PATH);
} catch (FontLoader.FontLoaderException e) {
throw new UncheckedIOException(e);
}
}

View File

@@ -4,8 +4,8 @@ import javafx.beans.binding.BooleanBinding;
import javafx.fxml.FXML;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.cryptomator.ui.common.FontLoader;
import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.fxapp.FxApplication;
import org.cryptomator.ui.fxapp.UpdateChecker;
@@ -14,8 +14,6 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.io.InputStream;
@MainWindowScoped
public class MainWindowController implements FxController {
@@ -63,14 +61,9 @@ public class MainWindowController implements FxController {
}
private void loadFont(String resourcePath) {
try (InputStream in = getClass().getResourceAsStream(resourcePath)) {
Font font = Font.loadFont(in, 12.0);
if (font == null) {
LOG.warn("Error loading font from path: " + resourcePath);
} else {
LOG.debug("Loaded font {}", font.getFamily());
}
} catch (IOException e) {
try {
FontLoader.load(resourcePath);
} catch (FontLoader.FontLoaderException e) {
LOG.warn("Error loading font from path: " + resourcePath, e);
}
}