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 addNotify() {
  77         lwFrame.addNotify();
  78     }
  79 
  80     public void setVisible(boolean visible) {
  81         lwFrame.setVisible(visible);
  82     }
  83 
  84     public void setBounds(int x, int y, int w, int h) {
  85         lwFrame.setBounds(x, y, w, h);
  86     }
  87 
  88     public void setContent(final LightweightContentWrapper lwCntWrapper) {
  89         lwFrame.setContent(lwCntWrapper.getContent());
  90     }
  91 
  92     public void emulateActivation(boolean activate) {
  93         lwFrame.emulateActivation(activate);
  94     }
  95 
  96     public MouseEvent createMouseEvent(LightweightFrameWrapper lwFrame, 
  97                           int swingID, long swingWhen, int swingModifiers,
  98                           int relX, int relY, int absX, int absY,
  99                           int clickCount, boolean swingPopupTrigger, 
 100                           int swingButton) {
 101         return new java.awt.event.MouseEvent(lwFrame.getLightweightFrame(), 
 102                                              swingID, swingWhen, 
 103                                              swingModifiers,
 104                                              relX, relY, absX, absY, clickCount,
 105                                              swingPopupTrigger, swingButton);
 106     }
 107 
 108     public MouseWheelEvent createMouseWheelEvent(LightweightFrameWrapper lwFrame,
 109                           int swingModifiers, int x, int y, int wheelRotation) {
 110         return  new MouseWheelEvent(lwFrame.getLightweightFrame(),
 111                           java.awt.event.MouseEvent.MOUSE_WHEEL,
 112                           System.currentTimeMillis(),
 113                           swingModifiers, x, y, 0, 0, 0, false,
 114                           MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, wheelRotation);
 115     }
 116 
 117     public KeyEvent createKeyEvent(LightweightFrameWrapper lwFrame,
 118                            int swingID, long swingWhen, int swingModifiers, 
 119                            int swingKeyCode, char swingChar) {
 120         return new java.awt.event.KeyEvent(lwFrame.getLightweightFrame(),
 121                             swingID, swingWhen, swingModifiers, swingKeyCode, 
 122                             swingChar);
 123     }
 124 
 125     public AWTEvent createUngrabEvent(LightweightFrameWrapper lwFrame) {
 126         return new UngrabEvent(lwFrame.getLightweightFrame());
 127     }
 128 
 129     public Component findComponentAt(LightweightFrameWrapper cont, int x, int y, boolean ignoreEnabled) {
 130         Container lwframe = cont.getLightweightFrame();
 131         return AWTAccessor.getContainerAccessor().findComponentAt(lwframe, x, y, ignoreEnabled); 
 132     }
 133 
 134     public boolean isCompEqual(Component c, LightweightFrameWrapper lwFrame) {
 135         return c != lwFrame.getLightweightFrame();
 136     }
 137 }