1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 jdk.swing.interop;
  27 
  28 import java.awt.AWTEvent;
  29 import java.awt.Container;
  30 import java.awt.Component;
  31 import java.awt.event.WindowFocusListener;
  32 import java.awt.event.MouseEvent;
  33 import java.awt.event.MouseWheelEvent;
  34 import java.awt.event.KeyEvent;
  35 import sun.awt.LightweightFrame;
  36 import sun.awt.UngrabEvent;
  37 import sun.awt.AWTAccessor;
  38 import sun.swing.JLightweightFrame;
  39 
  40 /**
  41  * This class wraps sun.swing.JLightweightFrame and implements
  42  * APIs to be used by FX swing interop to access and use JLightweightFrame APIs.
  43  *
  44  * @since 11
  45  */
  46 public class LightweightFrameWrapper {
  47     
  48     JLightweightFrame lwFrame;
  49 
  50     public LightweightFrameWrapper() {
  51         lwFrame = new JLightweightFrame();
  52     }
  53 
  54     private JLightweightFrame getLightweightFrame() {
  55         return lwFrame;
  56     }
  57 
  58     public void notifyDisplayChanged(final int scaleFactor) {
  59         if (lwFrame != null) {
  60             lwFrame.notifyDisplayChanged(scaleFactor, scaleFactor);
  61         }
  62     }
  63 
  64     /**
  65      * {@code overrideNativeWindowHandle()} is package private but
  66      * part of the interface of this class. It supports providing a 
  67      * foreign native window handle (i.e. an FX window handle) to AWT, 
  68      * and as such is intended to be called via JNI code, 
  69      * not by Java code, so it is not public.
  70      */
  71     void overrideNativeWindowHandle(long handle, Runnable closeWindow) {
  72         if (lwFrame != null) {
  73             lwFrame.overrideNativeWindowHandle(handle, closeWindow);
  74         }
  75     }
  76 
  77     public void setHostBounds(int x, int y, int w, int h) {
  78         if (lwFrame != null) {
  79             lwFrame.setHostBounds(x, y, w, h);
  80         }
  81     }
  82 
  83     public void dispose() {
  84         if (lwFrame != null) {
  85             lwFrame.dispose();
  86         }
  87     }
  88 
  89     public void addWindowFocusListener(WindowFocusListener listener) {
  90         if (lwFrame != null) {
  91             lwFrame.addWindowFocusListener(listener);
  92         }
  93     }
  94 
  95     public void setVisible(boolean visible) {
  96         if (lwFrame != null) {
  97             lwFrame.setVisible(visible);
  98         }
  99     }
 100 
 101     public void setBounds(int x, int y, int w, int h) {
 102         if (lwFrame != null) {
 103             lwFrame.setBounds(x, y, w, h);
 104         }
 105     }
 106 
 107     public void setContent(final LightweightContentWrapper lwCntWrapper) {
 108         if (lwFrame != null) {
 109             lwFrame.setContent(lwCntWrapper.getContent());
 110         }
 111     }
 112 
 113     public void emulateActivation(boolean activate) {
 114         if (lwFrame != null) {
 115             lwFrame.emulateActivation(activate);
 116         }
 117     }
 118 
 119     public MouseEvent createMouseEvent(LightweightFrameWrapper lwFrame, 
 120                           int swingID, long swingWhen, int swingModifiers,
 121                           int relX, int relY, int absX, int absY,
 122                           int clickCount, boolean swingPopupTrigger, 
 123                           int swingButton) {
 124         return new java.awt.event.MouseEvent(lwFrame.getLightweightFrame(), 
 125                                              swingID, swingWhen, 
 126                                              swingModifiers,
 127                                              relX, relY, absX, absY, clickCount,
 128                                              swingPopupTrigger, swingButton);
 129     }
 130 
 131     public MouseWheelEvent createMouseWheelEvent(LightweightFrameWrapper lwFrame,
 132                           int swingModifiers, int x, int y, int wheelRotation) {
 133         return  new MouseWheelEvent(lwFrame.getLightweightFrame(),
 134                           java.awt.event.MouseEvent.MOUSE_WHEEL,
 135                           System.currentTimeMillis(),
 136                           swingModifiers, x, y, 0, 0, 0, false,
 137                           MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, wheelRotation);
 138     }
 139 
 140     public KeyEvent createKeyEvent(LightweightFrameWrapper lwFrame,
 141                            int swingID, long swingWhen, int swingModifiers, 
 142                            int swingKeyCode, char swingChar) {
 143         return new java.awt.event.KeyEvent(lwFrame.getLightweightFrame(),
 144                             swingID, swingWhen, swingModifiers, swingKeyCode, 
 145                             swingChar);
 146     }
 147 
 148     public AWTEvent createUngrabEvent(LightweightFrameWrapper lwFrame) {
 149         return new UngrabEvent(lwFrame.getLightweightFrame());
 150     }
 151 
 152     public Component findComponentAt(LightweightFrameWrapper cont, int x, int y, boolean ignoreEnabled) {
 153         Container lwframe = cont.getLightweightFrame();
 154         return AWTAccessor.getContainerAccessor().findComponentAt(lwframe, x, y, ignoreEnabled); 
 155     }
 156 
 157     public boolean isCompEqual(Component c, LightweightFrameWrapper lwFrame) {
 158         return c != lwFrame.getLightweightFrame();
 159     }
 160 }