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.Graphics;
  29 import java.awt.Component;
  30 import java.awt.Container;
  31 import java.awt.Toolkit;
  32 import java.awt.Window;
  33 import java.awt.AWTEvent;
  34 import java.awt.EventQueue;
  35 import java.awt.image.BufferedImage;
  36 import sun.awt.SunToolkit;
  37 import sun.awt.AppContext;
  38 import sun.awt.AWTAccessor;
  39 import sun.awt.UngrabEvent;
  40 import sun.awt.image.IntegerComponentRaster;
  41 import sun.java2d.SunGraphics2D;
  42 import sun.java2d.SurfaceData;
  43 
  44 /**
  45  * This class provides static utility methods to be used by FX swing interop 
  46  * to access and use jdk internal classes like SunToolkit, AppContext, AWTAccessor, 
  47  * UngrabEvent, IntegerComponentRaster, SunGraphics2D and SurfaceData.
  48  */
  49 public class SwingInterOpUtils {
  50     public static double getDefaultScaleX(Graphics g) {
  51         if (g instanceof SunGraphics2D) {
  52             SurfaceData sd = ((SunGraphics2D) g).surfaceData;
  53             return sd.getDefaultScaleX();
  54         }
  55         return 1.0;
  56     }
  57 
  58     public static double getDefaultScaleY(Graphics g) {
  59         if (g instanceof SunGraphics2D) {
  60             SurfaceData sd = ((SunGraphics2D) g).surfaceData;
  61             return sd.getDefaultScaleY();
  62         }
  63         return 1.0;
  64     }
  65 
  66      public static int[] getData(BufferedImage bimg) {
  67         IntegerComponentRaster icr =
  68                         (IntegerComponentRaster) bimg.getRaster();
  69         int data[] = icr.getDataStorage();
  70         return data;
  71     }
  72 
  73     public static int getOffset(BufferedImage bimg) {
  74         IntegerComponentRaster icr =
  75                 (IntegerComponentRaster) bimg.getRaster();
  76         int offset = icr.getDataOffset(0);
  77         return offset;
  78     }
  79 
  80     public static int getScanlineStride(BufferedImage bimg) {
  81         IntegerComponentRaster icr =
  82                 (IntegerComponentRaster) bimg.getRaster();
  83         int scan = icr.getScanlineStride();
  84         return scan;
  85     }
  86 
  87     public static void postEvent(Object target, java.awt.AWTEvent e) {
  88         AppContext context = SunToolkit.targetToAppContext(target);
  89         if (context != null) {
  90             SunToolkit.postEvent(context, e);
  91         }
  92     }
  93 
  94     public static void grab(Toolkit toolkit, Window window) {
  95         if (toolkit instanceof SunToolkit) {
  96             ((SunToolkit)toolkit).grab(window);
  97         }
  98     }
  99 
 100     public static void ungrab(Toolkit toolkit, Window window) {
 101         if (toolkit instanceof SunToolkit) {
 102             ((SunToolkit)toolkit).ungrab(window);
 103         }
 104     }
 105 
 106     public static boolean isUngrabEvent(AWTEvent e) {
 107         return e instanceof UngrabEvent;
 108     }
 109 
 110     public static final int GRAB_EVENT_MASK = SunToolkit.GRAB_EVENT_MASK;
 111 
 112 }