Skip to content

Commit

Permalink
FontDialog and FontFamilyDialog load font collections from mac os pre…
Browse files Browse the repository at this point in the history
…ferences.
  • Loading branch information
wrandelshofer committed Jan 13, 2024
1 parent cb4d008 commit 089a1a5
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 391 deletions.
8 changes: 8 additions & 0 deletions org.jhotdraw8.fxcontrols/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.jhotdraw8.geom</artifactId>
</dependency>
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.jhotdraw8.os</artifactId>
</dependency>
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.jhotdraw8.icollection</artifactId>
</dependency>
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.jhotdraw8.os</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
/*
* @(#)module-info.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
Expand All @@ -19,9 +20,11 @@
requires transitive org.jhotdraw8.collection;
requires transitive org.jhotdraw8.base;
requires transitive org.jhotdraw8.fxbase;
requires transitive org.jhotdraw8.os;
requires transitive org.jhotdraw8.color;
requires transitive org.jhotdraw8.geom;
requires org.jhotdraw8.icollection;
requires java.logging;
exports org.jhotdraw8.fxcontrols.dock;
exports org.jhotdraw8.fxcontrols.colorchooser;
exports org.jhotdraw8.fxcontrols.spi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*
* @(#)DefaultFontChooserModelFactory.java
* @(#)DefaultFontCollectionsFactory.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
*/
package org.jhotdraw8.fxcontrols.fontchooser;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.text.Font;
import org.jhotdraw8.annotation.NonNull;
import org.jhotdraw8.application.resources.ModulepathResources;
Expand All @@ -18,47 +17,33 @@
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;

/**
* DefaultFontChooserModelFactory.
* DefaultFontCollectionsFactory.
*
* @author Werner Randelshofer
*/
public class DefaultFontChooserModelFactory {
public class DefaultFontCollectionsFactory implements FontCollectionsFactory {

public DefaultFontChooserModelFactory() {
public DefaultFontCollectionsFactory() {
}

public FontChooserModel create() {
final FontChooserModel model = new FontChooserModel();
model.setFontCollections(generateCollections(loadFonts()));
return model;
}

public @NonNull CompletableFuture<FontChooserModel> createAsync() {
CompletableFuture<FontChooserModel> future = new CompletableFuture<>();
Task<FontChooserModel> task = new Task<FontChooserModel>() {
@Override
protected FontChooserModel call() throws Exception {
return create();
}

@Override
protected void failed() {
future.completeExceptionally(getException());
}

@Override
protected void succeeded() {
future.complete(getValue());
}
};
ForkJoinPool.commonPool().execute(task);
return future;
/**
* Creates a FontChooserModel.
* <p>
* This is an IO intensive operation and should therefore not be called on the JavaFX Application Thread.
*
* @return a FontChooserModel.
*/
public List<FontCollection> create() {
return generateCollections(loadFonts());
}

/**
* Loads all Fonts that are available to JavaFX.
*
* @return the list of fonts
*/
protected @NonNull List<FontFamily> loadFonts() {
List<FontFamily> allFamilies = new ArrayList<>();

Expand Down Expand Up @@ -92,18 +77,26 @@ protected void succeeded() {
return allFamilies;
}

/**
* Groups a list of font families into collections.
* <p>
* This implementation uses a hardcoded set of collections.
*
* @param families a list of font families
* @return a collection of font families
*/
protected @NonNull ObservableList<FontCollection> generateCollections(@NonNull List<FontFamily> families) {
ObservableList<FontCollection> root = FXCollections.observableArrayList();
ObservableList<FontCollection> collections = FXCollections.observableArrayList();

final Resources labels = ModulepathResources.getResources(FontDialog.class.getModule(), "org.jhotdraw8.fxcontrols.spi.labels");

// All fonts
FontCollection allFonts = new FontCollection(labels.getString("FontCollection.allFonts"), true, families);
root.add(allFonts);
collections.add(allFonts);

// Web core fonts
// https://en.wikipedia.org/wiki/Core_fonts_for_the_Web
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.web"), true, collectFamiliesNamed(families,
"Arial",
"Arial Black",
Expand All @@ -119,7 +112,7 @@ protected void succeeded() {

// PDF Standard Fonts
// https://en.wikipedia.org/wiki/Portable_Document_Format#Standard_Type_1_Fonts_(Standard_14_Fonts)
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.pdf"), true, collectFamiliesNamed(families,
"Courier",
"Helvetica",
Expand All @@ -128,7 +121,7 @@ protected void succeeded() {
"Zapf Dingbats")));

// Java System fonts
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.system"), true, collectFamiliesNamed(families,
"Dialog",
"DialogInput",
Expand All @@ -137,7 +130,7 @@ protected void succeeded() {
"Serif",
"System")));
// Serif fonts
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.serif"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Adobe Caslon Pro",
Expand Down Expand Up @@ -221,7 +214,7 @@ protected void succeeded() {
"KodchiangUPC",
"Narkisim")));
// Sans Serif
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.sansSerif"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Abadi MT Condensed Extra Bold",
Expand Down Expand Up @@ -308,7 +301,7 @@ protected void succeeded() {
"Segoe UI")));

// Scripts
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.script"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Apple Chancery",
Expand Down Expand Up @@ -386,7 +379,7 @@ protected void succeeded() {
"Segoe Script")));

// Monospaced
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.monospaced"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Andale Mono",
Expand Down Expand Up @@ -415,7 +408,7 @@ protected void succeeded() {
"Rod")));

// Decorative
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.decorative"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Academy Engraved LET",
Expand Down Expand Up @@ -509,7 +502,7 @@ protected void succeeded() {
"Slimbach-BlackItalic",
"Snap ITC" // Fonts on Windows Vista:
)));
root.add(
collections.add(
new FontCollection(labels.getString("FontCollection.symbols"), collectFamiliesNamed(families,
// Fonts on Mac OS X 10.5:
"Apple Symbols",
Expand All @@ -535,7 +528,7 @@ protected void succeeded() {
// Fonts on Windows Vista:
)));

return root;
return collections;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.jhotdraw8.annotation.NonNull;
import org.jhotdraw8.annotation.Nullable;
Expand All @@ -17,7 +18,7 @@
*/
public class FontChooserModel {

private final ListProperty<FontCollection> fontCollections = new SimpleListProperty<>();
private final ListProperty<FontCollection> fontCollections = new SimpleListProperty<>(FXCollections.observableArrayList());

public FontChooserModel() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jhotdraw8.fxcontrols.fontchooser;

import javafx.application.Platform;
import org.jhotdraw8.annotation.NonNull;
import org.jhotdraw8.os.macos.MacOSPreferencesUtil;

import java.util.function.Supplier;

public class FontChooserModelFactories {
private static Supplier<FontCollectionsFactory> singleton;

static {
if (MacOSPreferencesUtil.isMacOs()) singleton = MacOSFontCollectionsFactory::new;
else singleton = DefaultFontCollectionsFactory::new;
}

private FontChooserModelFactories() {
}

public static Supplier<FontCollectionsFactory> getSingleton() {
return singleton;
}

public static void setSingleton(Supplier<FontCollectionsFactory> singleton) {
FontChooserModelFactories.singleton = singleton;
}

public static @NonNull FontChooserModel create() {
FontChooserModel model = new FontChooserModel();
if (Platform.isFxApplicationThread()) {
singleton.get().createAsync().thenAccept(m -> {
model.getFontCollections().addAll(m);
});
} else {
model.getFontCollections().addAll(singleton.get().create());
}
return model;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jhotdraw8.fxcontrols.fontchooser;

import javafx.application.Platform;
import javafx.concurrent.Task;
import org.jhotdraw8.annotation.NonNull;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface FontCollectionsFactory {
@NonNull List<FontCollection> create();

/**
* Creates a FontChooserModel asynchronously in a worker thread, and completes the returned
* {@link CompletableFuture} on the JavaFX Application Thread.
*
* @return a {@link CompletableFuture}.
*/
default @NonNull CompletableFuture<List<FontCollection>> createAsync() {
CompletableFuture<List<FontCollection>> future = new CompletableFuture<>();
Task<List<FontCollection>> task = new Task<List<FontCollection>>() {
@Override
protected List<FontCollection> call() throws Exception {
return create();
}

@Override
protected void failed() {
Platform.runLater(() -> future.completeExceptionally(getException()));
}

@Override
protected void succeeded() {
Platform.runLater(() -> future.complete(getValue()));
}
};
new Thread(task).start();
return future;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ public FontDialog() {

public static @Nullable FontChooserModel getModel() {
if (model == null) {
model = new PreferencesFontChooserModelFactory().create();
model = FontChooserModelFactories.create();
}
return model;
}

private @Nullable FontFamilySize onButton(@Nullable ButtonType buttonType) {
new PreferencesFontChooserModelFactory().writeModelToPrefs(controller.getModel());
if (buttonType != null && buttonType.getButtonData() == ButtonData.OK_DONE) {
String selectedFontName = controller.getSelectedFontName();
double fontSize = controller.getFontSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jhotdraw8.application.resources.Resources;

import java.net.URL;
import java.util.Collections;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
Expand Down Expand Up @@ -61,10 +60,6 @@ public class FontFamilyChooserController {
public FontFamilyChooserController() {
}

private @NonNull FontCollection createFontCollection() {
final Resources labels = ModulepathResources.getResources(FontDialog.class.getModule(), "org.jhotdraw8.fxcontrols.spi.labels");
return new FontCollection(labels.getString("FontCollection.unnamed"), Collections.emptyList());
}

public double getFontSize() {
return fontSize.get();
Expand Down Expand Up @@ -105,7 +100,7 @@ private void initDoubleClickBehavior() {
familyList.setOnMousePressed(onMouseHandler);
}

private void initListCellsWithDragAndDropBehavior() {
private void initListCells() {
familyList.setCellFactory(lv -> {
final TextFieldListCell<FontFamily> listCell = new TextFieldListCell<>();
return listCell;
Expand Down Expand Up @@ -171,8 +166,11 @@ private void initPreferencesBehavior() {

private void initUpdateViewFromModelBehavior() {
model.addListener((o, oldv, newv) -> {
if (oldv != null) {
collectionList.itemsProperty().unbind();
}
if (newv != null) {
collectionList.setItems(newv.getFontCollections());
collectionList.itemsProperty().bind(newv.fontCollectionsProperty());
}
});
}
Expand All @@ -190,7 +188,11 @@ void initialize() {
initListSelectionBehavior();
initDoubleClickBehavior();
initPreferencesBehavior();
initListCellsWithDragAndDropBehavior();
initListCells();

collectionList.itemsProperty().addListener((o, oldv, newv) -> {
System.out.println("collectionList change=" + newv);
});

}

Expand Down
Loading

0 comments on commit 089a1a5

Please sign in to comment.