|
| 1 | +package one.jpro.platform.css; |
| 2 | + |
| 3 | +import javafx.application.Platform; |
| 4 | +import javafx.scene.Parent; |
| 5 | +import javafx.scene.Scene; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.PrintWriter; |
| 10 | +import java.net.URL; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | + |
| 15 | +/** |
| 16 | + * Java-friendly API for applying dynamic CSS strings to a {@link Parent} or {@link Scene}. |
| 17 | + * <p> |
| 18 | + * CSS content is written to temporary files and registered as stylesheets. |
| 19 | + * Each call replaces the previous CSS for that target. |
| 20 | + * <p> |
| 21 | + * <strong>Usage:</strong> |
| 22 | + * <pre>{@code |
| 23 | + * DynamicCSSUtil.setCssString(scene, ".button { -fx-background-color: red; }"); |
| 24 | + * // later: |
| 25 | + * DynamicCSSUtil.setCssString(scene, ".button { -fx-background-color: blue; }"); |
| 26 | + * }</pre> |
| 27 | + */ |
| 28 | +public class DynamicCSSUtil { |
| 29 | + |
| 30 | + private static final String KEY_PROP = "DynamicCSSUtil.key"; |
| 31 | + private static final String URL_PROP = "DynamicCSSUtil.url"; |
| 32 | + |
| 33 | + private static final AtomicInteger counter = new AtomicInteger(0); |
| 34 | + private static final Map<String, File> files = new HashMap<>(); |
| 35 | + private static final File tempDir; |
| 36 | + |
| 37 | + static { |
| 38 | + try { |
| 39 | + tempDir = File.createTempFile("dynamiccss", ""); |
| 40 | + tempDir.delete(); |
| 41 | + tempDir.mkdirs(); |
| 42 | + tempDir.deleteOnExit(); |
| 43 | + } catch (IOException e) { |
| 44 | + throw new RuntimeException("Failed to create temp dir for dynamic CSS", e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sets or updates a dynamic CSS string on a {@link Scene}. |
| 50 | + */ |
| 51 | + public static void setCssString(Scene scene, String css) { |
| 52 | + removePrevious(scene.getProperties(), scene.getStylesheets()); |
| 53 | + if (css != null && !css.isEmpty()) { |
| 54 | + addNew(css, scene.getProperties(), scene.getStylesheets()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sets or updates a dynamic CSS string on a {@link Parent} node. |
| 60 | + */ |
| 61 | + public static void setCssString(Parent parent, String css) { |
| 62 | + removePrevious(parent.getProperties(), parent.getStylesheets()); |
| 63 | + if (css != null && !css.isEmpty()) { |
| 64 | + addNew(css, parent.getProperties(), parent.getStylesheets()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void removePrevious(javafx.collections.ObservableMap<Object, Object> props, |
| 69 | + java.util.List<String> stylesheets) { |
| 70 | + String prevKey = (String) props.get(KEY_PROP); |
| 71 | + String prevURL = (String) props.get(URL_PROP); |
| 72 | + if (prevURL != null) { |
| 73 | + stylesheets.remove(prevURL); |
| 74 | + props.remove(KEY_PROP); |
| 75 | + props.remove(URL_PROP); |
| 76 | + Platform.runLater(() -> unregister(prevKey)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void addNew(String css, |
| 81 | + javafx.collections.ObservableMap<Object, Object> props, |
| 82 | + java.util.List<String> stylesheets) { |
| 83 | + String key = "dcss" + counter.incrementAndGet(); |
| 84 | + File file = new File(tempDir, key + ".css"); |
| 85 | + try (PrintWriter w = new PrintWriter(file)) { |
| 86 | + w.write(css); |
| 87 | + } catch (IOException e) { |
| 88 | + throw new RuntimeException("Failed to write CSS file", e); |
| 89 | + } |
| 90 | + file.deleteOnExit(); |
| 91 | + synchronized (files) { |
| 92 | + files.put(key, file); |
| 93 | + } |
| 94 | + |
| 95 | + String url; |
| 96 | + try { |
| 97 | + url = file.toURI().toURL().toString(); |
| 98 | + } catch (Exception e) { |
| 99 | + throw new RuntimeException(e); |
| 100 | + } |
| 101 | + |
| 102 | + props.put(KEY_PROP, key); |
| 103 | + props.put(URL_PROP, url); |
| 104 | + stylesheets.add(url); |
| 105 | + } |
| 106 | + |
| 107 | + private static void unregister(String key) { |
| 108 | + synchronized (files) { |
| 109 | + File f = files.remove(key); |
| 110 | + if (f != null) f.delete(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments