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 public class LightweightFrameWrapper {
  45     
  46     JLightweightFrame lwFrame;
  47 
  48     public LightweightFrameWrapper() {
  49         lwFrame = new JLightweightFrame();
  50     }
  51 
  52     private JLightweightFrame getLightweightFrame() {
  53         return lwFrame;
  54     }
  55 
  56     public void notifyDisplayChanged(final int scaleFactor) {
  57         lwFrame.notifyDisplayChanged(scaleFactor, scaleFactor);
  58     }
  59 
  60     public void overrideNativeWindowHandle(long handle, Runnable closeWindow) {
  61         lwFrame.overrideNativeWindowHandle(handle, closeWindow);
  62     }
  63 
  64     public void setHostBounds(int x, int y, int w, int h) {
  65         lwFrame.setHostBounds(x, y, w, h);
  66     }
  67 
  68     public void dispose() {
  69         lwFrame.dispose();
  70     }
  71 
  72     public void addWindowFocusListener(WindowFocusListener listener) {
  73         lwFrame.addWindowFocusListener(listener);
  74     }
  75 
  76     public void setVisible(boolean visible) {
  77         lwFrame.setVisible(visible);
  78     }
  79 
  80     public void setBounds(int x, int y, int w, int h) {
  81         lwFrame.setBounds(x, y, w, h);
  82     }
  83 
  84     public void setContent(final LightweightContentWrapper lwCntWrapper) {
  85         lwFrame.setContent(lwCntWrapper.getContent());
  86     }
  87 
  88     public void emulateActivation(boolean activate) {
  89         lwFrame.emulateActivation(activate);
  90     }
  91 
  92     public MouseEvent createMouseEvent(LightweightFrameWrapper lwFrame, 
  93                           int swingID, long swingWhen, int swingModifiers,
  94                           int relX, int relY, int absX, int absY,
  95                           int clickCount, boolean swingPopupTrigger, 
  96                           int swingButton) {
  97         return new java.awt.event.MouseEvent(lwFrame.getLightweightFrame(), 
  98                                              swingID, swingWhen, 
  99                                              swingModifiers,
 100                                              relX, relY, absX, absY, clickCount,
 101                                              swingPopupTrigger, swingButton);
 102     }
 103 
 104     public MouseWheelEvent createMouseWheelEvent(LightweightFrameWrapper lwFrame,
 105                           int swingModifiers, int x, int y, int wheelRotation) {
 106         return  new MouseWheelEvent(lwFrame.getLightweightFrame(),
 107                           java.awt.event.MouseEvent.MOUSE_WHEEL,
 108                           System.currentTimeMillis(),
 109                           swingModifiers, x, y, 0, 0, 0, false,
 110                           MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, wheelRotation);
 111     }
 112 
 113     public KeyEvent createKeyEvent(LightweightFrameWrapper lwFrame,
 114                            int swingID, long swingWhen, int swingModifiers, 
 115                            int swingKeyCode, char swingChar) {
 116         return new java.awt.event.KeyEvent(lwFrame.getLightweightFrame(),
 117                             swingID, swingWhen, swingModifiers, swingKeyCode, 
 118                             swingChar);
 119     }
 120 
 121     public AWTEvent createUngrabEvent(LightweightFrameWrapper lwFrame) {
 122         return new UngrabEvent(lwFrame.getLightweightFrame());
 123     }
 124 
 125     public Component findComponentAt(LightweightFrameWrapper cont, int x, int y, boolean ignoreEnabled) {
 126         Container lwframe = cont.getLightweightFrame();
 127         return AWTAccessor.getContainerAccessor().findComponentAt(lwframe, x, y, ignoreEnabled); 
 128     }
 129 
 130     public boolean isCompEqual(Component c, LightweightFrameWrapper lwFrame) {
 131         return c != lwFrame.getLightweightFrame();
 132     }
 133 }