33import static com .ss .rlib .util .ObjectUtils .notNull ;
44import com .ss .editor .plugin .api .property .PropertyDefinition ;
55import com .ss .editor .ui .css .CSSClasses ;
6+ import com .ss .editor .ui .util .AutoCompleteComboBoxListener ;
67import com .ss .rlib .ui .util .FXUtils ;
78import com .ss .rlib .util .StringUtils ;
89import com .ss .rlib .util .VarTable ;
910import javafx .scene .control .ComboBox ;
10- import javafx .scene .control .ListCell ;
1111import javafx .scene .control .SingleSelectionModel ;
12+ import javafx .util .StringConverter ;
1213import org .jetbrains .annotations .NotNull ;
1314import org .jetbrains .annotations .Nullable ;
1415
1516import java .awt .*;
17+ import java .util .Arrays ;
1618
1719/**
1820 * The control to choose string value from list.
2224public class AwtFontPropertyEditorControl extends PropertyEditorControl <Font > {
2325
2426 @ NotNull
25- private class FontCell extends ListCell < Font > {
27+ private static final GraphicsEnvironment GRAPHICS_ENVIRONMENT = GraphicsEnvironment . getLocalGraphicsEnvironment ();
2628
27- @ Override
28- protected void updateItem (@ Nullable final Font font , final boolean empty ) {
29- super .updateItem (font , empty );
29+ @ NotNull
30+ private static final Font [] FONTS = GRAPHICS_ENVIRONMENT .getAllFonts ();
3031
31- if (font == null ) {
32- setText (StringUtils .EMPTY );
33- return ;
34- }
32+ @ NotNull
33+ private static final StringConverter <Font > STRING_CONVERTER = new StringConverter <Font >() {
3534
36- setText (font .getFontName ());
35+ @ Override
36+ public String toString (@ Nullable final Font font ) {
37+ return font == null ? StringUtils .EMPTY : font .getFontName ();
3738 }
38- }
39+
40+ @ Override
41+ public Font fromString (@ NotNull final String fontName ) {
42+ return Arrays .stream (FONTS )
43+ .filter (font -> font .getFontName ().equals (fontName ))
44+ .findAny ().orElse (null );
45+ }
46+ };
3947
4048 /**
4149 * The list of available options of the string value.
@@ -52,15 +60,16 @@ protected AwtFontPropertyEditorControl(@NotNull final VarTable vars, @NotNull fi
5260 protected void createComponents () {
5361 super .createComponents ();
5462
55- final GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment ();
56-
5763 comboBox = new ComboBox <>();
58- comboBox .setCellFactory (param -> new FontCell ());
59- comboBox .setButtonCell (new FontCell ());
6064 comboBox .getSelectionModel ().selectedItemProperty ().addListener ((observable , oldValue , newValue ) -> change ());
6165 comboBox .prefWidthProperty ().bind (widthProperty ().multiply (DEFAULT_FIELD_W_PERCENT ));
62- comboBox .getItems ().addAll (environment .getAllFonts ());
66+ comboBox .getItems ().addAll (FONTS );
67+ comboBox .setVisibleRowCount (20 );
68+ comboBox .setConverter (STRING_CONVERTER );
69+
70+ AutoCompleteComboBoxListener .install (comboBox );
6371
72+ FXUtils .addClassesTo (comboBox .getEditor (), CSSClasses .TRANSPARENT_TEXT_FIELD , CSSClasses .TEXT_FIELD_IN_COMBO_BOX );
6473 FXUtils .addClassTo (comboBox , CSSClasses .ABSTRACT_PARAM_CONTROL_COMBO_BOX );
6574 FXUtils .addToPane (comboBox , this );
6675 }
@@ -82,10 +91,10 @@ protected void reload() {
8291 }
8392
8493 @ Override
85- protected void change () {
94+ protected void changeImpl () {
8695 final ComboBox <Font > comboBox = getComboBox ();
8796 final SingleSelectionModel <Font > selectionModel = comboBox .getSelectionModel ();
8897 setPropertyValue (selectionModel .getSelectedItem ());
89- super .change ();
98+ super .changeImpl ();
9099 }
91100}
0 commit comments