import java.util.concurrent.ThreadLocalRandom;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * Displays a label with a random floating-point number between 0 and 1, and a button for 
 * randomizing the number.
 * 
 * @version 1 (event handler is created as a named object of a named class)
 *
 * @author Drue Coles
 */
public class NumberServer extends Application {

    private final Label label = new Label();

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setPadding(new Insets(10));
        root.setHgap(10);
        Scene scene = new Scene(root);

        // Add button and label to root pane.
        Button button = new Button("serve");
        root.add(button, 0, 0);
        root.add(label, 1, 0);
        randomizeLabel();

        // Style the button and label.
        String style = "-fx-font-size:24; -fx-font-weight: bold; -fx-text-fill: #1f2f4f";
        button.setStyle(style);
        label.setStyle(style);

        // Event handler is a named object of a named class. The code can be trivially 
        // modified to create the handler as an anonymous object of a named class. 
        class ButtonHandler implements EventHandler<ActionEvent> {

            @Override
            public void handle(ActionEvent event) {
                randomizeLabel();
            }
        }
        ButtonHandler buttonHandler = new ButtonHandler();
        button.setOnAction(buttonHandler);

        primaryStage.setTitle("Number Server");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * Randomizes the label with a floating-point number between 0 and 1.
     */
    private void randomizeLabel() {
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        String num = String.format("%1.8f", rand.nextDouble());
        label.setText(num);
    }

    public static void main(String[] args) {
        launch(args);
    }
}