Ticket #19169: 19169.patch
File 19169.patch, 4.0 KB (added by , 4 years ago) |
---|
-
src/org/openstreetmap/josm/plugins/javafx/gui/JavaFxWrapper.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.javafx.gui; 3 4 3 import java.awt.Dimension; 4 import java.util.concurrent.ExecutionException; 5 import java.util.concurrent.FutureTask; 5 6 6 7 import org.openstreetmap.josm.tools.Logging; 7 8 … … 23 24 transient T node; 24 25 25 26 /** 27 * Catch exceptions in the JavaFX thread (only instantiated with the JavaFxWrapper). 28 * Since most exceptions should be seen through the `future.get()` method in initialize, this is (mostly) safe. 29 */ 30 private static class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { 31 private final Thread.UncaughtExceptionHandler currentHandler; 32 public UncaughtExceptionHandler() { 33 currentHandler = Thread.currentThread().getUncaughtExceptionHandler(); 34 Thread.currentThread().setUncaughtExceptionHandler(this); 35 } 36 @Override 37 public void uncaughtException(Thread t, Throwable e) { 38 if (currentHandler != null && !(e instanceof NoClassDefFoundError)) { 39 currentHandler.uncaughtException(t, e); 40 } else { 41 Logging.error(e); 42 } 43 } 44 } 45 46 private static UncaughtExceptionHandler handler; 47 48 /** 26 49 * <p> 27 50 * <b>Implementation note</b>: when the first {@code JFXPanel} object is 28 51 * created, it implicitly initializes the JavaFX runtime. This is the preferred … … 31 54 * 32 55 * @param node The JavaFX node that will be returned later with 33 56 * {@link JavaFxWrapper#getNode}. 57 * @throws ExecutionException If something happened during execution. If this happens, fall back to something else! 34 58 */ 35 public JavaFxWrapper(Class<T> node) {59 public JavaFxWrapper(Class<T> node) throws ExecutionException { 36 60 try { 37 61 initialize(node.getConstructor().newInstance()); 38 62 } catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) { … … 49 73 * 50 74 * @param node The JavaFX node that will be returned later with 51 75 * {@link JavaFxWrapper#getNode}. 76 * @throws ExecutionException If something happened during execution. If this happens, fall back to something else! 52 77 */ 53 public JavaFxWrapper(T node) {78 public JavaFxWrapper(T node) throws ExecutionException { 54 79 initialize(node); 55 80 } 56 81 … … 58 83 * This holds common initialization code 59 84 * 60 85 * @param node The node that should be set to this.node 86 * @throws ExecutionException If something happened during execution. If this happens, fall back to something else! 61 87 */ 62 private void initialize(T node) {88 private void initialize(T node) throws ExecutionException { 63 89 this.node = node; 64 Platform.runLater(this::initFX); 90 FutureTask<Scene> task = new FutureTask<>(this::initFX); 91 Platform.runLater(task); 65 92 Platform.setImplicitExit(false); 66 93 this.setFocusTraversalKeysEnabled(node.isFocusTraversable()); 94 try { 95 task.get(); 96 } catch (InterruptedException e) { 97 Thread.currentThread().interrupt(); 98 Logging.error(e); 99 } 67 100 } 68 101 69 private void initFX() { 102 /** 103 * @return The scene to be used for initializing JavaFX 104 */ 105 protected Scene initFX() { 106 initializeExceptionHandler(); 70 107 Scene scene = createScene(); 71 108 setScene(scene); 109 return scene; 72 110 } 73 111 112 private static void initializeExceptionHandler() { 113 if (handler == null) 114 handler = new UncaughtExceptionHandler(); 115 } 116 74 117 private Scene createScene() { 75 118 Group group = new Group(); 76 119 Scene scene = new Scene(group);