Skip to content

Commit 806f3de

Browse files
committed
Clean up MaterialDesignIconView to be more generic
1 parent 6cc3eba commit 806f3de

9 files changed

Lines changed: 499 additions & 424 deletions

File tree

Generator/src/main/java/fr/flowarg/materialdesigniconfx/Generator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ public class MaterialDesignIcon_GEN_%s {
106106
""".formatted(version, version.replace('.', '_'));
107107
final String enumValues = generateEnumValues(file);
108108
final String endClass1 = """
109-
public static final List<GlyphIcons> GLYPH_ICONS;
109+
public static final List<MaterialDesignIconGlyphs<?>> GLYPH_ICONS;
110110
111111
static {
112-
final List<GlyphIcons> icons = new ArrayList<>();
112+
final List<MaterialDesignIconGlyphs<?>> icons = new ArrayList<>();
113113
""";
114114

115115
final StringBuilder listAdder = new StringBuilder();
@@ -149,7 +149,7 @@ private static String generateEnumValues(List<String> file)
149149
}
150150

151151
final String startEnum = """
152-
public enum %s implements GlyphIcons {
152+
public enum %s implements MaterialDesignIconGlyphs<%s> {
153153
154154
""";
155155

@@ -170,6 +170,12 @@ public String unicode() {
170170
public String fontFamily() {
171171
return "'Material Design Icons'";
172172
}
173+
174+
@Override
175+
public %s getDefaultGlyph()
176+
{
177+
return %s.%s;
178+
}
173179
}
174180
175181
""";
@@ -182,9 +188,9 @@ public String fontFamily() {
182188
});
183189

184190
LETTERS.forEach((character, stringBuilder) -> {
185-
enumValues.append(startEnum.formatted(character));
191+
enumValues.append(startEnum.formatted(character, character));
186192
enumValues.append(stringBuilder);
187-
enumValues.append(endEnum.formatted(character));
193+
enumValues.append(endEnum.formatted(character, character, character, stringBuilder.toString().split("\\(")[0].trim()));
188194
});
189195

190196
return enumValues.toString();

README.MD

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1+
[version]: https://img.shields.io/maven-central/v/fr.flowarg/materialdesignfontfx.svg?label=Download
2+
[download]: https://central.sonatype.com/artifact/fr.flowarg/materialdesignfontfx
3+
4+
[ ![version][] ][download]
5+
16
# MaterialDesignFont for JavaFx
27

38
This is the updated version of the [Jensd's lib](https://bitbucket.org/Jerady/fontawesomefx/src/master/fontawesomefx/fontawesomefx-materialdesignfont/).
49

510
It's made for Java 17.
611

7-
## Help Wanted
8-
Feel free to open a PR if you want to help me with this project.
9-
10-
11-
Because of the limitations imposed by Java, it is not possible to create enums that are too large, so I had to split `MaterialDesignIcon` into multiple enums. I've chosen to separate them with the first letter.
12+
## Download
13+
MaterialDesignFontFX is available on Maven Central.
1214

13-
I seriously need help with the `MaterialDesignIconView` class to be compatible with the new enums without making one class per letter.
15+
### Maven
16+
```xml
17+
<dependency>
18+
<groupId>fr.flowarg</groupId>
19+
<artifactId>materialdesignfontfx</artifactId>
20+
<version>VERSION</version>
21+
</dependency>
22+
```
1423

15-
I need a generic solution so we can remove this slow reflection mess in the demo app.
24+
### Gradle
25+
```gradle
26+
dependencies {
27+
implementation 'fr.flowarg:materialdesignfontfx:VERSION'
28+
}
29+
```
1630

1731
## Usage
1832
```java
@@ -25,8 +39,8 @@ public class Main extends Application
2539
public void start(Stage primaryStage)
2640
{
2741
// scene logic
28-
var accountIcon = new MaterialDesignIconView.A(MaterialDesignIcon.ACCOUNT);
29-
var yinYangIcon = new MaterialDesignIconView.Y(MaterialDesignIcon.YIN_YANG);
42+
var accountIcon = new MaterialDesignIconView<>(MaterialDesignIcon.A.ACCOUNT);
43+
var yinYangIcon = new MaterialDesignIconView<>(MaterialDesignIcon.Y.YIN_YANG);
3044
// add the icons to your UI
3145
}
3246
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply plugin: 'idea'
1717
apply plugin: 'maven-publish'
1818
apply plugin: 'signing'
1919

20-
version '7.2.96-0' // material design font version - revision
20+
version '7.2.96-1' // material design font version - revision
2121

2222
java {
2323
withJavadocJar()
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/**
2+
* Copyright (c) 2016 Jens Deters http://www.jensd.de
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*
16+
*/
17+
package fr.flowarg.materialdesignfontfx;
18+
19+
import de.jensd.fx.glyphs.GlyphIconUtils;
20+
import de.jensd.fx.glyphs.GlyphIcons;
21+
import javafx.beans.property.SimpleStringProperty;
22+
import javafx.beans.property.StringProperty;
23+
import javafx.css.*;
24+
import javafx.fxml.FXML;
25+
import javafx.scene.text.Font;
26+
import javafx.scene.text.Text;
27+
28+
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
33+
34+
/**
35+
* A GlyphIcon represents an Icon Node.
36+
*
37+
* @author Jens Deters
38+
* @param <T> The type of GlyphIcons enum.
39+
*/
40+
public abstract class GlyphIcon<T extends Enum<T> & GlyphIcons> extends Text {
41+
42+
public final static Double DEFAULT_ICON_SIZE = 12.0;
43+
public final static String DEFAULT_FONT_SIZE = "1em";
44+
45+
private StringProperty glyphStyle; // needed as setStyle() is final in javafx.scene.text.Text
46+
private String glyphFontFamily;
47+
private String unicode;
48+
private SimpleStyleableObjectProperty<String> glyphName;
49+
private SimpleStyleableObjectProperty<Number> glyphSize;
50+
public final Class<T> typeOfT;
51+
52+
@FXML
53+
public void init() {
54+
}
55+
56+
public GlyphIcon(Class<T> iconType) {
57+
this(iconType, true);
58+
}
59+
60+
public GlyphIcon(Class<T> iconType, boolean initProperties) {
61+
this.typeOfT = iconType;
62+
if(initProperties)
63+
initProperties();
64+
}
65+
66+
protected void initProperties() {
67+
getStyleClass().addAll("glyph-icon");
68+
glyphSizeProperty().addListener(observable -> updateSize());
69+
glyphStyleProperty().addListener(observable -> updateStyle());
70+
glyphNameProperty().addListener(observable -> updateIcon());
71+
setIcon(getDefaultGlyph());
72+
}
73+
74+
// convenience method
75+
public final GlyphIcon<T> setStyleClass(String styleClass) {
76+
getStyleClass().add(styleClass);
77+
return this;
78+
}
79+
80+
public final StringProperty glyphStyleProperty() {
81+
if (glyphStyle == null) {
82+
glyphStyle = new SimpleStringProperty("");
83+
}
84+
return glyphStyle;
85+
}
86+
87+
public final String getGlyphStyle() {
88+
return glyphStyleProperty().getValue();
89+
}
90+
91+
public final void setGlyphStyle(String style) {
92+
if (!getGlyphStyle().isEmpty() && !getGlyphStyle().endsWith(";")) {
93+
style = ";".concat(style);
94+
}
95+
glyphStyleProperty().setValue(getGlyphStyle().concat(style));
96+
}
97+
98+
public final SimpleStyleableObjectProperty<String> glyphNameProperty() {
99+
if (glyphName == null)
100+
glyphName = new SimpleStyleableObjectProperty<>(StyleableProperties.GLYPH_NAME, GlyphIcon.this, "glyphName");
101+
return glyphName;
102+
}
103+
104+
public final String getGlyphName() {
105+
return glyphNameProperty().getValue();
106+
}
107+
108+
public final void setGlyphName(String glyphName) {
109+
glyphNameProperty().setValue(glyphName);
110+
}
111+
112+
public final String getGlyphFontFamily() {
113+
return glyphFontFamily;
114+
}
115+
116+
public final SimpleStyleableObjectProperty<Number> glyphSizeProperty() {
117+
if (glyphSize == null) {
118+
glyphSize = new SimpleStyleableObjectProperty<>(StyleableProperties.GLYPH_SIZE, GlyphIcon.this, "glyphSize");
119+
glyphSize.setValue(DEFAULT_ICON_SIZE);
120+
}
121+
return glyphSize;
122+
}
123+
124+
public final Number getGlyphSize() {
125+
return glyphSizeProperty().getValue();
126+
}
127+
128+
public final void setGlyphSize(Number size) {
129+
size = (size == null) ? DEFAULT_ICON_SIZE : size;
130+
glyphSizeProperty().setValue(size);
131+
}
132+
133+
// kept for compability reasons and for SceneBuilder/FXML support
134+
public final String getSize() {
135+
return getGlyphSize().toString();
136+
}
137+
138+
// kept for compability reasons and for SceneBuilder/FXML support
139+
public final void setSize(String sizeExpr) {
140+
Number s = convert(sizeExpr);
141+
setGlyphSize(s);
142+
}
143+
144+
public final void setIcon(T glyph) {
145+
setGlyphName(glyph.name());
146+
glyphFontFamily = glyph.fontFamily();
147+
unicode = glyph.unicode();
148+
}
149+
150+
public String unicode(){
151+
return unicode;
152+
}
153+
154+
abstract public T getDefaultGlyph();
155+
156+
private void updateSize() {
157+
Font f = new Font(getFont().getFamily(), getGlyphSize().doubleValue());
158+
setFont(f);
159+
setGlyphStyle(String.format("-fx-font-family: %s; -fx-font-size: %s;", getGlyphFontFamily(), getGlyphSize().doubleValue()));
160+
}
161+
162+
void updateIcon() {
163+
GlyphIcons icon = getDefaultGlyph();
164+
try {
165+
icon = Enum.valueOf(typeOfT, getGlyphName());
166+
} catch (Exception e) {
167+
String msg = String.format("Icon '%s' not found. Using '%s' (default) instead", getGlyphName(), getDefaultGlyph());
168+
Logger.getLogger(GlyphIcon.class.getName()).log(Level.SEVERE, msg);
169+
}
170+
setText(icon.unicode());
171+
}
172+
173+
private void updateStyle() {
174+
setStyle(getGlyphStyle());
175+
}
176+
177+
// CSS
178+
private static class StyleableProperties {
179+
180+
private static final CssMetaData<GlyphIcon<?>, String> GLYPH_NAME
181+
= new CssMetaData<>("-glyph-name", StyleConverter.getStringConverter(), "BLANK")
182+
{
183+
184+
@Override
185+
public boolean isSettable(GlyphIcon<?> styleable)
186+
{
187+
return styleable.glyphName == null || !styleable.glyphName.isBound();
188+
}
189+
190+
@Override
191+
public StyleableProperty<String> getStyleableProperty(GlyphIcon<?> styleable)
192+
{
193+
return styleable.glyphNameProperty();
194+
}
195+
196+
@Override
197+
public String getInitialValue(GlyphIcon<?> styleable)
198+
{
199+
return "BLANK";
200+
}
201+
};
202+
203+
private static final CssMetaData<GlyphIcon<?>, Number> GLYPH_SIZE
204+
= new CssMetaData<>("-glyph-size", StyleConverter.getSizeConverter(), DEFAULT_ICON_SIZE)
205+
{
206+
@Override
207+
public boolean isSettable(GlyphIcon<?> styleable)
208+
{
209+
return styleable.glyphSize == null || !styleable.glyphSize.isBound();
210+
}
211+
212+
@Override
213+
public StyleableProperty<Number> getStyleableProperty(GlyphIcon<?> styleable)
214+
{
215+
return styleable.glyphSizeProperty();
216+
}
217+
218+
@Override
219+
public Number getInitialValue(GlyphIcon<?> styleable)
220+
{
221+
return DEFAULT_ICON_SIZE;
222+
}
223+
};
224+
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
225+
226+
static {
227+
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Text.getClassCssMetaData());
228+
Collections.addAll(styleables, GLYPH_NAME, GLYPH_SIZE);
229+
STYLEABLES = Collections.unmodifiableList(styleables);
230+
}
231+
}
232+
233+
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
234+
return StyleableProperties.STYLEABLES;
235+
}
236+
237+
@Override
238+
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
239+
return getClassCssMetaData();
240+
}
241+
242+
public Number convert(String sizeString) {
243+
return GlyphIconUtils.convert(sizeString, getFont());
244+
}
245+
246+
}

0 commit comments

Comments
 (0)