ひらおかゆみのなげやりブログ

もう、なげやりです…

JavaFX Utilities

Happy birthday to me!

Hi, I'm Yumi Hiraoka a.k.a. "Yumix". Today, I'll be age 22. But I digress...

This post is written as 19th day's post in JavaFX Advent Calendar 2012. This is a translated version of my 19th day's post in JavaFX Advent Calendar 2012, titled "JavaFXのユーティリティたち". Yesterday we read a great post of Mr. Yuichi Sakuraba (@skrb) titled "Binding Sample".

Today, we look at the utilities of JavaFX. They are based on JavaFX, but so unspectacular things as if small asteroid "Itokawa" before voyage of "Hayabusa". I begin JavaFX 5 days ago. So I cannot great things as other people, but I do as hard as possible to write the post.

 

Section 1. StringConverter

JavaFX has StringConverter and a lot of it's subclasses. They're look like java.util.Format class and coexist with it. StringConverte differs from Format in that the method names (Format has parse/format method, on the other hand, StringConverter has fromString/toString methods) and converting from String (as fromString method) doesn't throw any checked exceptions. Because of StringConverter declared as an abstract class, we use the following subclasses.

For basic types:

meaningful number:

Date and Time:

General purpose:

Special purpose:
They almost all have the default constructor excepts FormatStringConverter and NumberAxis.DefaultFormatter. Then the converters for meaningful number or date/time accept pattern or locale. FormatStringConverter uses any purpose even if the Format object is possible to convert.

See the below sample programs.

[DateTimeConverters.java]

package org.yumix.javafxutil.converter;

import java.util.*;

import javafx.scene.paint.*;
import javafx.util.converter.*;

public class DateTimeConverters {

  public static void main(String args) {
    // 2012/12/19 14:46:00 JST
    Date now = new Date(1355895960000L);
    
    // Date
    DateStringConverter dsc = new DateStringConverter();
    System.out.println("Date");
    System.out.println(dsc.toString(now));
    System.out.println(dsc.fromString("2012/12/19"));
    
    // Date and Time
    DateTimeStringConverter dtsc = new DateTimeStringConverter();
    System.out.println("\nDate and Time");
    System.out.println(dtsc.toString(now));
    System.out.println(dtsc.fromString("2012/12/19 14:46:00"));
    
    // Time
    TimeStringConverter tsc = new TimeStringConverter();
    System.out.println("\nTime");
    System.out.println(tsc.toString(now));
    System.out.println(tsc.fromString("14:46:00"));
    
    // Date and Time (ISO)
    DateTimeStringConverter isodtsc = new DateTimeStringConverter("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    System.out.println("\nDate and Time (ISO)");
    System.out.println(isodtsc.toString(now));
    System.out.println(isodtsc.fromString("2012-12-19T14:46:00.000+09:00"));
  }
    
}

[NumberConverters.java]

package org.yumix.javafxutil.converter;

import java.util.*;

import javafx.util.converter.*;

public class NumberConverters {

  public static void main(String args) {
    // Number
    NumberStringConverter nsc = new NumberStringConverter();
    System.out.println("Number");
    System.out.println(nsc.toString(10));
    System.out.println(nsc.fromString("10"));
    
    // Currency
    CurrencyStringConverter csc = new CurrencyStringConverter();
    System.out.println("\nCurrency (Default)");
    System.out.println(csc.toString(1200));
    System.out.println(csc.fromString("¥1,200"));
    
    // Currency (U.S.)
    CurrencyStringConverter uscsc = new CurrencyStringConverter(Locale.US);
    System.out.println("\nCurrency (U.S.)");
    System.out.println(uscsc.toString(1200));
    System.out.println(uscsc.fromString("$1,200.00"));
    
    // Currency (Germany)
    CurrencyStringConverter gercsc = new CurrencyStringConverter(Locale.GERMANY);
    System.out.println("\nCurrency (Germany)");
    System.out.println(gercsc.toString(1200));
    System.out.println(gercsc.fromString("1.200,00 €"));
    
    // Percentage
    PercentageStringConverter psc = new PercentageStringConverter();
    System.out.println("\nPercentage");
    System.out.println(psc.toString(0.15));
    System.out.println(psc.fromString("15%"));
  }

}

All the source codes (Eclipse project with e(fx)clipse) are hosted to the following URL on GitHub.

https://github.com/yumix/JavaFXUtilities 

 

Section 2. Builder

JavaFX has many classes implements Builder interface mainly UI parts. When using builder patterns on JavaFX, the builder classes have same operations. For example,

Color color = ColorBuilder.create().green(0.5).blue(0.75).build();

Builder classes on Java APIs has different operations each other. As  an example, StringBuilder class builds with toString method, but Jersey's WebResource class builds with get, post, put, delete, head and options methods. At least, the builder classes operations are unified on JavaFX. And It's able to get any builder class from JavaFXBuilderFactory class. If we define a builder class implements Builder interface, it is used by same operations as JavaFX APIs. It's useful thing, I think.

  

Section 3. Callback

Using obvious phrase, Builder interface is "so what?". But Callback interface is "so what?" strongly. Why must it be standardized?

When implement a callback function by Java, almost all use an anonymous inner-class implements a simple interface. I have no idea, so I use a snippet from @aoe_tk's post

pagenation.setPageFactory(new Callback() {
  @Override
  public Node call(Integer idx) {
    return createPage(idx);
  }
});

Maybe, using Lambda of Java 8, we rewrite it the following (It's my guess):

pagenation.setPageFactory(idx -> createPage(idx)); 

(I think it is too wel-prepared when I rewrite the above code to below using Lambda.)

I think that Callback interface aim at Lambda. It might be standardized because interfaces Lambda readiness are restricted.

Reference: http://www.slideshare.net/skrb/lambda-15544054

 

Conclusion

JavaFX's main features are beautiful CGs and UIs. But the small utilities that support behind main features are so important because they're based on JavaFX.

Tomorrow we'll read @cocoatomo's post, titled "use JavaFX from Jython".