| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 
 | package cssregen;
 
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
 
public class Main extends Application {
 
    private Path cssFile;
 
    @Override
    public void init() throws Exception {
        cssFile = Files.createTempFile("temp", ".css");
        assert (cssFile != null);
    }
 
    @Override
    public void start(final Stage primaryStage) throws MalformedURLException {
        final StackPane root = new StackPane();
        final Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
        startUpdateAsync(scene.getStylesheets());
    }
 
    private ScheduledService<String> cssUpdateService;
 
    private void startUpdateAsync(final List<String> styleSheets) {
        cssUpdateService = new ScheduledService<String>() {
            @Override
            protected Task<String> createTask() {
                return new Task<String>() {
                    @Override
                    protected String call() throws Exception {
                        final String[] colors = {"red", "blue", "green", "pink", "cyan", "yellow", "purple", "orange"};
                        final int index = (int) (colors.length * Math.random());
                        final String color = colors[index];
                        try (final OutputStream out = Files.newOutputStream(cssFile);
                                final PrintWriter writer = new PrintWriter(out)) {
                            writer.println(".root {");
                            writer.printf("    -fx-background-color: %s;%n", color);
                            writer.println("}");
                        }
                        return cssFile.toUri().toURL().toExternalForm();
                    }
                };
            }
        };
        cssUpdateService.setDelay(Duration.seconds(2));
        cssUpdateService.setPeriod(Duration.seconds(2));
        cssUpdateService.setOnSucceeded(workerStateEvent -> {
            Logger.getGlobal().log(Level.INFO, "Update successful");
            final String css = (String) workerStateEvent.getSource().getValue();
            final int index = styleSheets.indexOf(css);
            if (index > -1) {
                styleSheets.remove(index);
                styleSheets.add(index, css);
            } else {
                styleSheets.add(css);
            }
        });
        cssUpdateService.setOnFailed(workerStateEvent -> {
            final Throwable ex = workerStateEvent.getSource().getException();
            Logger.getGlobal().log(Level.SEVERE, ex.getMessage(), ex);
        });
        cssUpdateService.start();
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
} |