Skip to content

Commit

Permalink
check for file presence in isInitialized
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinayagarwal committed Aug 18, 2024
1 parent 8c5f379 commit c52b2d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,27 @@ public class DownloadbleEmojiSpriteLoader implements EmojiSpriteLoader {
private static final String EMOJI_PNG_URL = "https://github.com/iamcal/emoji-data/blob/" + COMMIT_NUMBER + "/sheets-clean/sheet_apple_%s_clean.png?raw=true";

private static final String LOCAL_PATH = System.getProperty("user.home") + "/.gluon/emoji/" + COMMIT_NUMBER;
private static final int[] EMOJI_SIZES = new int[] { 20, 32, 64 };
private boolean initialized;

@Override
public boolean isInitialized() {
return initialized;
if (!initialized) {
for (int size : EMOJI_SIZES) {
String fileName = "sheet_apple_" + size + ".png";
if (!Files.exists(Paths.get(LOCAL_PATH, fileName))) {
return false;
}
}
}
return initialized = true;
}

@Override
public CompletableFuture<Boolean> initialize() {
return CompletableFuture.supplyAsync(() -> {
try {
downloadSprites(20, 32, 64);
downloadSprites(EMOJI_SIZES);
initialized = true;
return true;
} catch (Exception e) {
Expand All @@ -56,6 +65,7 @@ private void downloadSprites(int... sizes) {
downloadFile(new URI(String.format(EMOJI_PNG_URL, size)).toURL(), localPath);
}
} catch (IOException | URISyntaxException e) {
LOG.severe("Download sprite failed: " + e);
throw new RuntimeException("Unable to load local image file", e);
}
}
Expand All @@ -70,6 +80,7 @@ public Image loadEmojiSprite(int size) {
try {
return new Image(new FileInputStream(localPath.toFile()));
} catch (IOException e) {
LOG.severe("Loading of local image file failed: " + e);
throw new RuntimeException("Unable to load local image file", e);
}
}
Expand Down
13 changes: 8 additions & 5 deletions samples/nodes/src/main/java/com/gluonhq/emoji/samples/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@
import com.gluonhq.emoji.Emoji;
import com.gluonhq.emoji.EmojiData;
import com.gluonhq.emoji.EmojiLoaderFactory;
import com.gluonhq.emoji.EmojiSpriteLoader;
import com.gluonhq.emoji.util.EmojiImageUtils;
import com.gluonhq.emoji.util.TextUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Demo extends Application {

@Override
public void start(Stage primaryStage) {

ProgressIndicator progressIndicator = new ProgressIndicator();
Scene scene = new Scene(new StackPane(progressIndicator), 1230, 800);
TextFlow textFlow = new TextFlow();
ScrollPane pane = new ScrollPane(textFlow);
Scene scene = new Scene(pane, 1230, 800);
textFlow.prefWidthProperty().bind(scene.widthProperty().subtract(30));
primaryStage.setScene(scene);
primaryStage.setTitle("Emoji nodes: " +
Expand All @@ -75,7 +75,10 @@ public void start(Stage primaryStage) {
Tooltip.install(node, new Tooltip("Emoji: " + emoji.getName() + "\n" + unified)));
});
// Add nodes to textFlow
Platform.runLater(() -> textFlow.getChildren().addAll(emojiNodes));
Platform.runLater(() -> {
textFlow.getChildren().addAll(emojiNodes);
scene.setRoot(new ScrollPane(textFlow));
});
}
});
}
Expand Down

0 comments on commit c52b2d7

Please sign in to comment.