| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.gui; |
| | 3 | |
| | 4 | import javafx.application.Platform; |
| | 5 | import javafx.embed.swing.JFXPanel; |
| | 6 | import javafx.scene.Group; |
| | 7 | import javafx.scene.Node; |
| | 8 | import javafx.scene.Scene; |
| | 9 | |
| | 10 | /** |
| | 11 | * This wrapper class wraps arbitrary JavaFX Nodes, so that they can easily be |
| | 12 | * added to a Swing UI. |
| | 13 | * |
| | 14 | * @author Taylor Smock |
| | 15 | * @param <T> Some class that extends {@link Node} |
| | 16 | * @since xxx |
| | 17 | */ |
| | 18 | public class JavaFXWrapper<T extends Node> extends JFXPanel { |
| | 19 | T node; |
| | 20 | |
| | 21 | /** |
| | 22 | * @param node The JavaFX node that will be returned later with |
| | 23 | * {@link JavaFXWrapper#getNode}. |
| | 24 | */ |
| | 25 | public JavaFXWrapper(T node) { |
| | 26 | super(); |
| | 27 | this.node = node; |
| | 28 | Platform.runLater(() -> initFX()); |
| | 29 | } |
| | 30 | |
| | 31 | private void initFX() { |
| | 32 | Scene scene = createScene(); |
| | 33 | setScene(scene); |
| | 34 | } |
| | 35 | |
| | 36 | private Scene createScene() { |
| | 37 | Group group = new Group(); |
| | 38 | Scene scene = new Scene(group); |
| | 39 | group.getChildren().add(node); |
| | 40 | return scene; |
| | 41 | } |
| | 42 | |
| | 43 | /** |
| | 44 | * Get the JavaFX {@link Node} |
| | 45 | * |
| | 46 | * @return The Node passed to the class during construction |
| | 47 | */ |
| | 48 | public T getNode() { |
| | 49 | return node; |
| | 50 | } |
| | 51 | } |