Commits

Jonathan Giles committed 367f950

Make Icon a public class, clean up IconFont class. The big problem now is the pseudo-builder syntax in IconFont. Note that in HelloIconFont I set one icon green, but that flows through to subsequent ones too. I'm not sure if this is desirable or not...

  • Participants
  • Parent commits 7f5e1e5

Comments (2)

Files changed (4)

File src/main/java/org/controlsfx/iconfont/Icon.java

+package org.controlsfx.iconfont;
+
+import javafx.scene.control.Label;
+import javafx.scene.paint.Color;
+
+public class Icon extends Label {
+
+    private final String fontFamily;
+    private final Character character;
+    private double size;
+    private Color color;
+
+    public Icon(String fontFamily, Character character, double size, Color color) {
+        super(character.toString());
+
+        this.fontFamily = fontFamily;
+        this.character = character;
+        this.size = size;
+        this.color = color;
+
+        getStyleClass().add("icon-font");
+        updateStyle();
+    }
+    
+    public void setSize(double size) {
+        this.size = size;
+        updateStyle();
+    }
+    
+    public void setColor(Color color) {
+        this.color = color;
+        updateStyle();
+    }
+    
+    private void updateStyle() {
+        StringBuilder css = new StringBuilder("-fx-font-family: "+ fontFamily +"; -fx-font-size: " + size + ";");
+        if (color == null) {
+            css.append("-icons-color: -fx-text-background-color;");
+        } else {
+            css.append("-icons-color: rgb(");
+            css.append((int)(color.getRed()*255));
+            css.append(",");
+            css.append((int)(color.getGreen()*255));
+            css.append(",");
+            css.append((int)(color.getBlue())*255);
+            css.append(");");
+        }
+        setStyle(css.toString());
+    }
+}

File src/main/java/org/controlsfx/iconfont/IconFont.java

 
 import com.sun.javafx.css.StyleManager;
 
-import javafx.scene.Node;
-import javafx.scene.control.Label;
 import javafx.scene.paint.Color;
 import javafx.scene.text.Font;
 
 	
 	private final String fontName;
 	
-	private Character character;
 	private double size = DEFAULT_ICON_SIZE;
 	private Color color;
 	
 		Font.loadFont(urlStr, DEFAULT_ICON_SIZE);
 	}
 	
-	public IconFont create(char c) {
-	    this.character = c;
-	    
-	    // reset other properties to default
-	    size = DEFAULT_ICON_SIZE;
-	    color = null;
-	    
-	    return this;
-	}
-	
-	public IconFont size(double size) {
+	public IconFont fontSize(double size) {
 	    this.size = size;
 	    return this;
 	}
 	
-	public IconFont color(Color color) {
+	public IconFont fontColor(Color color) {
 	    this.color = color;
 	    return this;
 	}
 	
-	public Node build() {
-	    return new Icon(character, size, color);
-	}
-	
-	
-	private class Icon extends Label {
-		
-		private final Character _character;
-		private final double _size;
-		private final Color _color;
-		
-		public Icon(Character character, double size, Color color) {
-			super(character.toString());
-			
-			this._character = character;
-			this._size = size;
-			this._color = color;
-			
-			StringBuilder css = new StringBuilder("-fx-font-family: "+ fontName +"; -fx-font-size: " + _size + ";");
-			if (color == null) {
-			    css.append("-icons-color: -fx-text-background-color;");
-			} else {
-			    css.append("-icons-color: rgb(");
-			    css.append((int)(_color.getRed()*255));
-			    css.append(",");
-			    css.append((int)(_color.getGreen()*255));
-			    css.append(",");
-			    css.append((int)(_color.getBlue())*255);
-			    css.append(");");
-			}
-		    setStyle(css.toString());
-		    getStyleClass().add("icon-font");
-		}
+	public Icon create(char character) {
+	    return new Icon(fontName, character, size, color);
 	}
 }

File src/main/java/org/controlsfx/iconfont/IconFontRegistry.java

 	
 	public static Node glyph( String fontName, String glyphName ) {
 		FontIconPack pack = pack(fontName);
-		return pack.getFont().create( pack.getGlyphs().get(glyphName)).build();
+		return pack.getFont().create(pack.getGlyphs().get(glyphName));
 	}
 	
 	public static Node glyph( String fontAndGlyph ) {

File src/samples/java/org/controlsfx/samples/HelloIconFont.java

 		root.getChildren().add(title);
 		ToolBar toolbar = new ToolBar(
 				new Button("", glyph("FontAwesome:TRASH")), 
-				new Button("", fontAwesome.create(FAW_GEAR).color(Color.RED).build()), 
+				new Button("", fontAwesome.fontColor(Color.RED).create(FAW_GEAR)), 
 				new Button("", glyph("FontAwesome:STAR")),
 				new Button("", FontAwesomePack.Glyph.ANCHOR.create()));
 		root.getChildren().add(toolbar);
 		title = new Label("Using IcoMoon (Local)");
 		root.getChildren().add(title);
 		toolbar = new ToolBar(
-				new Button("", icoMoon.create(IM_BOLD).size(16).build()),
-				new Button("", icoMoon.create(IM_UNDERSCORED).size(32).build()), 
-				new Button("", icoMoon.create(IM_ITALIC).size(48).build()));
+				new Button("", icoMoon.fontSize(16).create(IM_BOLD)),
+				new Button("", icoMoon.fontColor(Color.GREEN).fontSize(32).create(IM_UNDERSCORED)), 
+				new Button("", icoMoon.fontSize(48).create(IM_ITALIC)));
 		root.getChildren().add(toolbar);
 		return root;