| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.plugins.javafx.gui; |
| | 3 | |
| | 4 | import java.awt.Dimension; |
| | 5 | import java.lang.reflect.InvocationTargetException; |
| | 6 | |
| | 7 | import org.openstreetmap.josm.tools.Logging; |
| | 8 | |
| | 9 | import javafx.application.Platform; |
| | 10 | import javafx.embed.swing.JFXPanel; |
| | 11 | import javafx.scene.Group; |
| | 12 | import javafx.scene.Node; |
| | 13 | import javafx.scene.Scene; |
| | 14 | |
| | 15 | /** |
| | 16 | * This wrapper class wraps arbitrary JavaFX Nodes, so that they can easily be |
| | 17 | * added to a Swing UI. |
| | 18 | * |
| | 19 | * @author Taylor Smock |
| | 20 | * @param <T> Some class that extends {@link Node} |
| | 21 | * @since xxx |
| | 22 | * |
| | 23 | */ |
| | 24 | public class JavaFxWrapper<T extends Node> extends JFXPanel { |
| | 25 | private static final long serialVersionUID = -4414342147357482212L; |
| | 26 | transient T node; |
| | 27 | |
| | 28 | /** |
| | 29 | * <p> |
| | 30 | * <b>Implementation note</b>: when the first {@code JFXPanel} object is |
| | 31 | * created, it implicitly initializes the JavaFX runtime. This is the preferred |
| | 32 | * way to initialize JavaFX in Swing. Since this class extends JFXPanel, the |
| | 33 | * JavaFX node should be passed as a class type and not initialized. |
| | 34 | * |
| | 35 | * @param node The JavaFX node that will be returned later with |
| | 36 | * {@link JavaFxWrapper#getNode}. |
| | 37 | */ |
| | 38 | public JavaFxWrapper(Class<T> node) { |
| | 39 | super(); |
| | 40 | try { |
| | 41 | initialize(node.getConstructor().newInstance()); |
| | 42 | } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException |
| | 43 | | NoSuchMethodException | SecurityException e) { |
| | 44 | Logging.logWithStackTrace(Logging.LEVEL_ERROR, e); |
| | 45 | } |
| | 46 | } |
| | 47 | |
| | 48 | /** |
| | 49 | * <p> |
| | 50 | * <b>Implementation note</b>: when the first {@code JFXPanel} object is |
| | 51 | * created, it implicitly initializes the JavaFX runtime. This is the preferred |
| | 52 | * way to initialize JavaFX in Swing. If this is the first call, please use the |
| | 53 | * {@code JavaFxWrapper(Class<T> node)} constructor instead. |
| | 54 | * |
| | 55 | * @param node The JavaFX node that will be returned later with |
| | 56 | * {@link JavaFxWrapper#getNode}. |
| | 57 | */ |
| | 58 | public JavaFxWrapper(T node) { |
| | 59 | super(); |
| | 60 | initialize(node); |
| | 61 | } |
| | 62 | |
| | 63 | /** |
| | 64 | * This holds common initialization code |
| | 65 | * |
| | 66 | * @param node The node that should be set to this.node |
| | 67 | */ |
| | 68 | private void initialize(T node) { |
| | 69 | this.node = node; |
| | 70 | Platform.runLater(this::initFX); |
| | 71 | Platform.setImplicitExit(false); |
| | 72 | this.setFocusTraversalKeysEnabled(node.isFocusTraversable()); |
| | 73 | } |
| | 74 | |
| | 75 | private void initFX() { |
| | 76 | Scene scene = createScene(); |
| | 77 | setScene(scene); |
| | 78 | } |
| | 79 | |
| | 80 | private Scene createScene() { |
| | 81 | Group group = new Group(); |
| | 82 | Scene scene = new Scene(group); |
| | 83 | group.getChildren().add(node); |
| | 84 | return scene; |
| | 85 | } |
| | 86 | |
| | 87 | /** |
| | 88 | * Get the JavaFX {@link Node} |
| | 89 | * |
| | 90 | * @return The Node passed to the class during construction |
| | 91 | */ |
| | 92 | public T getNode() { |
| | 93 | return node; |
| | 94 | } |
| | 95 | |
| | 96 | @Override |
| | 97 | public Dimension getMinimumSize() { |
| | 98 | if (isMinimumSizeSet()) { |
| | 99 | return super.getMinimumSize(); |
| | 100 | } |
| | 101 | Dimension size = null; |
| | 102 | if (node != null) { |
| | 103 | size = new Dimension(); |
| | 104 | size.setSize(node.minWidth(-1), node.minHeight(-1)); |
| | 105 | } |
| | 106 | return (size != null) ? size : super.getMinimumSize(); |
| | 107 | } |
| | 108 | |
| | 109 | @Override |
| | 110 | public Dimension getPreferredSize() { |
| | 111 | if (isPreferredSizeSet()) { |
| | 112 | return super.getPreferredSize(); |
| | 113 | } |
| | 114 | Dimension dimension = new Dimension(); |
| | 115 | dimension.setSize(node.prefWidth(-1), node.prefHeight(-1)); |
| | 116 | return dimension; |
| | 117 | } |
| | 118 | } |