|
| 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