8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.embed.swing; 27 28 import com.sun.javafx.geom.BaseBounds; 29 import com.sun.javafx.geom.transform.BaseTransform; 30 import com.sun.javafx.scene.NodeHelper; 31 import com.sun.javafx.sg.prism.NGNode; 32 import com.sun.javafx.util.Utils; 33 import javafx.embed.swing.SwingNode; 34 import javafx.scene.Node; 35 36 /** 37 * Used to access internal methods of SwingNode. 38 */ 39 public class SwingNodeHelper extends NodeHelper { 40 41 private static final SwingNodeHelper theInstance; 42 private static SwingNodeAccessor swingNodeAccessor; 43 44 static { 45 theInstance = new SwingNodeHelper(); 46 Utils.forceInit(SwingNode.class); 47 } 48 49 private static SwingNodeHelper getInstance() { 50 return theInstance; 51 } 52 53 public static void initHelper(SwingNode swingNode) { 54 setHelper(swingNode, getInstance()); 55 } 59 return swingNodeAccessor.doCreatePeer(node); 60 } 61 62 @Override 63 protected void updatePeerImpl(Node node) { 64 super.updatePeerImpl(node); 65 swingNodeAccessor.doUpdatePeer(node); 66 } 67 68 @Override 69 protected BaseBounds computeGeomBoundsImpl(Node node, BaseBounds bounds, 70 BaseTransform tx) { 71 return swingNodeAccessor.doComputeGeomBounds(node, bounds, tx); 72 } 73 74 @Override 75 protected boolean computeContainsImpl(Node node, double localX, double localY) { 76 return swingNodeAccessor.doComputeContains(node, localX, localY); 77 } 78 79 public static void setSwingNodeAccessor(final SwingNodeAccessor newAccessor) { 80 if (swingNodeAccessor != null) { 81 throw new IllegalStateException(); 82 } 83 84 swingNodeAccessor = newAccessor; 85 } 86 87 public interface SwingNodeAccessor { 88 NGNode doCreatePeer(Node node); 89 void doUpdatePeer(Node node); 90 BaseBounds doComputeGeomBounds(Node node, BaseBounds bounds, BaseTransform tx); 91 boolean doComputeContains(Node node, double localX, double localY); 92 } 93 94 } | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.embed.swing; 27 28 import java.util.concurrent.locks.ReentrantLock; 29 30 import com.sun.javafx.geom.BaseBounds; 31 import com.sun.javafx.geom.transform.BaseTransform; 32 import com.sun.javafx.scene.NodeHelper; 33 import com.sun.javafx.sg.prism.NGNode; 34 import com.sun.javafx.util.Utils; 35 import javafx.embed.swing.SwingNode; 36 import javafx.scene.Node; 37 38 import java.util.Set; 39 import java.util.HashSet; 40 import javafx.application.Platform; 41 import javax.swing.SwingUtilities; 42 import com.sun.javafx.tk.Toolkit; 43 44 /** 45 * Used to access internal methods of SwingNode. 46 */ 47 public class SwingNodeHelper extends NodeHelper { 48 49 private static final SwingNodeHelper theInstance; 50 private static SwingNodeAccessor swingNodeAccessor; 51 52 static { 53 theInstance = new SwingNodeHelper(); 54 Utils.forceInit(SwingNode.class); 55 } 56 57 private static SwingNodeHelper getInstance() { 58 return theInstance; 59 } 60 61 public static void initHelper(SwingNode swingNode) { 62 setHelper(swingNode, getInstance()); 63 } 67 return swingNodeAccessor.doCreatePeer(node); 68 } 69 70 @Override 71 protected void updatePeerImpl(Node node) { 72 super.updatePeerImpl(node); 73 swingNodeAccessor.doUpdatePeer(node); 74 } 75 76 @Override 77 protected BaseBounds computeGeomBoundsImpl(Node node, BaseBounds bounds, 78 BaseTransform tx) { 79 return swingNodeAccessor.doComputeGeomBounds(node, bounds, tx); 80 } 81 82 @Override 83 protected boolean computeContainsImpl(Node node, double localX, double localY) { 84 return swingNodeAccessor.doComputeContains(node, localX, localY); 85 } 86 87 public static Object getLightweightFrame(SwingNode node) { 88 return swingNodeAccessor.getLightweightFrame(node); 89 } 90 91 public static ReentrantLock getPaintLock(SwingNode node) { 92 return swingNodeAccessor.getPaintLock(node); 93 } 94 95 public static void setImageBuffer(SwingNode node, final int[] data, 96 final int x, final int y, 97 final int w, final int h, final int linestride, 98 final double scaleX, final double scaleY) { 99 swingNodeAccessor.setImageBuffer(node, data, x, y, w, h, 100 linestride, scaleX, scaleY); 101 } 102 103 public static void setImageBounds(SwingNode node, final int x, final int y, 104 final int w, final int h) { 105 swingNodeAccessor.setImageBounds(node, x, y, w, h); 106 } 107 108 public static void repaintDirtyRegion(SwingNode node, final int dirtyX, final int dirtyY, 109 final int dirtyWidth, final int dirtyHeight) { 110 swingNodeAccessor.repaintDirtyRegion(node, dirtyX, dirtyY, 111 dirtyWidth, dirtyHeight); 112 } 113 114 public static void ungrabFocus(SwingNode node, boolean postUngrabEvent) { 115 swingNodeAccessor.ungrabFocus(node, postUngrabEvent); 116 } 117 118 public static void setSwingPrefWidth(SwingNode node, int swingPrefWidth) { 119 swingNodeAccessor.setSwingPrefWidth(node, swingPrefWidth);; 120 } 121 122 public static void setSwingPrefHeight(SwingNode node, int swingPrefHeight) { 123 swingNodeAccessor.setSwingPrefHeight(node, swingPrefHeight); 124 } 125 126 public static void setSwingMaxWidth(SwingNode node, int swingMaxWidth) { 127 swingNodeAccessor.setSwingMaxWidth(node, swingMaxWidth); 128 } 129 130 public static void setSwingMaxHeight(SwingNode node, int swingMaxHeight) { 131 swingNodeAccessor.setSwingMaxHeight(node, swingMaxHeight); 132 } 133 134 public static void setSwingMinWidth(SwingNode node, int swingMinWidth) { 135 swingNodeAccessor.setSwingMinWidth(node, swingMinWidth); 136 } 137 138 public static void setSwingMinHeight(SwingNode node, int swingMinHeight) { 139 swingNodeAccessor.setSwingMinHeight(node, swingMinHeight); 140 } 141 142 public static void setGrabbed(SwingNode node, boolean grab) { 143 swingNodeAccessor.setGrabbed(node, grab); 144 } 145 146 /** 147 * If called from the FX Application Thread 148 * invokes a runnable directly blocking the calling code 149 * Otherwise 150 * uses Platform.runLater without blocking 151 * 152 * @param runnable {@code Runnable} to be invoked 153 */ 154 public static void runOnFxThread(Runnable runnable) { 155 if (Platform.isFxApplicationThread()) { 156 runnable.run(); 157 } else { 158 Platform.runLater(runnable); 159 } 160 } 161 162 /** 163 * If called from the event dispatch thread 164 * invokes a runnable directly blocking the calling code 165 * Otherwise 166 * uses SwingUtilities.invokeLater without blocking 167 * 168 * @param r {@code Runnable} to be invoked 169 */ 170 public static void runOnEDT(final Runnable r) { 171 if (SwingUtilities.isEventDispatchThread()) { 172 r.run(); 173 } else { 174 SwingUtilities.invokeLater(r); 175 } 176 } 177 178 private static final Set<Object> eventLoopKeys = new HashSet<>(); 179 180 /** 181 * The runnable is responsible for entering the nested event loop. 182 * 183 * @param nestedLoopKey the Object that identifies the nested event loop, 184 * which must not be null 185 * @param r {@code Runnable} to be invoked 186 */ 187 public static void runOnEDTAndWait(Object nestedLoopKey, Runnable r) { 188 Toolkit.getToolkit().checkFxUserThread(); 189 190 if (SwingUtilities.isEventDispatchThread()) { 191 r.run(); 192 } else { 193 eventLoopKeys.add(nestedLoopKey); 194 SwingUtilities.invokeLater(r); 195 Toolkit.getToolkit().enterNestedEventLoop(nestedLoopKey); 196 } 197 } 198 199 /** 200 * The runnable is responsible for leaving the nested event loop. 201 * 202 * @param nestedLoopKey the Object that identifies the nested event loop, 203 * which must not be null 204 */ 205 public static void leaveFXNestedLoop(Object nestedLoopKey) { 206 if (!eventLoopKeys.contains(nestedLoopKey)) return; 207 208 if (Platform.isFxApplicationThread()) { 209 Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null); 210 } else { 211 Platform.runLater(() -> { 212 Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null); 213 }); 214 } 215 216 eventLoopKeys.remove(nestedLoopKey); 217 } 218 219 public static void setSwingNodeAccessor(final SwingNodeAccessor newAccessor) { 220 if (swingNodeAccessor != null) { 221 throw new IllegalStateException(); 222 } 223 224 swingNodeAccessor = newAccessor; 225 } 226 227 public interface SwingNodeAccessor { 228 NGNode doCreatePeer(Node node); 229 void doUpdatePeer(Node node); 230 BaseBounds doComputeGeomBounds(Node node, BaseBounds bounds, BaseTransform tx); 231 boolean doComputeContains(Node node, double localX, double localY); 232 Object getLightweightFrame(SwingNode node); 233 ReentrantLock getPaintLock(SwingNode node); 234 void setImageBuffer(SwingNode node, final int[] data, 235 final int x, final int y, 236 final int w, final int h, final int linestride, 237 final double scaleX, final double scaleY); 238 void setImageBounds(SwingNode node, final int x, final int y, 239 final int w, final int h); 240 void repaintDirtyRegion(SwingNode node, final int dirtyX, final int dirtyY, 241 final int dirtyWidth, final int dirtyHeight); 242 void ungrabFocus(SwingNode node, boolean postUngrabEvent); 243 void setSwingPrefWidth(SwingNode node, int swingPrefWidth); 244 void setSwingPrefHeight(SwingNode node, int swingPrefHeight); 245 void setSwingMaxWidth(SwingNode node, int swingMaxWidth); 246 void setSwingMaxHeight(SwingNode node, int swingMaxHeight); 247 void setSwingMinWidth(SwingNode node, int swingMinWidth); 248 void setSwingMinHeight(SwingNode node, int swingMinHeight); 249 void setGrabbed(SwingNode node, boolean grab); 250 } 251 252 } |