| | 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> { |
| | 19 | JFXPanel jfxPanel; |
| | 20 | T node; |
| | 21 | |
| | 22 | /** |
| | 23 | * @param node The JavaFX node that will be returned later with |
| | 24 | * {@link JavaFXWrapper#getNode}. |
| | 25 | */ |
| | 26 | public JavaFXWrapper(T node) { |
| | 27 | jfxPanel = new JFXPanel(); |
| | 28 | this.node = node; |
| | 29 | Platform.runLater(() -> initFX()); |
| | 30 | } |
| | 31 | |
| | 32 | private void initFX() { |
| | 33 | Scene scene = createScene(); |
| | 34 | jfxPanel.setScene(scene); |
| | 35 | } |
| | 36 | |
| | 37 | private Scene createScene() { |
| | 38 | Group group = new Group(); |
| | 39 | Scene scene = new Scene(group); |
| | 40 | group.getChildren().add(node); |
| | 41 | return scene; |
| | 42 | } |
| | 43 | |
| | 44 | /** |
| | 45 | * Get the JFXPanel to add to a Swing UI |
| | 46 | * |
| | 47 | * @return The JFXPanel |
| | 48 | */ |
| | 49 | public JFXPanel getPanel() { |
| | 50 | return jfxPanel; |
| | 51 | } |
| | 52 | |
| | 53 | /** |
| | 54 | * Get the JavaFX {@link Node} |
| | 55 | * |
| | 56 | * @return The Node passed to the class during construction |
| | 57 | */ |
| | 58 | public T getNode() { |
| | 59 | return node; |
| | 60 | } |
| | 61 | } |