1 /*
   2  * Copyright (c) 2010, 2015, 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 com.sun.javafx.tk.quantum;
  27 
  28 import java.security.AccessControlContext;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.List;
  32 
  33 import com.sun.javafx.embed.AbstractEvents;
  34 import com.sun.javafx.embed.EmbeddedStageInterface;
  35 import com.sun.javafx.embed.HostInterface;
  36 import com.sun.javafx.tk.TKScene;
  37 import com.sun.javafx.tk.Toolkit;
  38 import javafx.application.Platform;
  39 
  40 final class EmbeddedStage extends GlassStage implements EmbeddedStageInterface {
  41 
  42     private HostInterface host;
  43 
  44     public EmbeddedStage(HostInterface host) {
  45         this.host = host;
  46     }
  47 
  48     // TKStage methods
  49 
  50     @Override
  51     public TKScene createTKScene(boolean depthBuffer, boolean msaa, AccessControlContext acc) {
  52         EmbeddedScene scene = new EmbeddedScene(host, depthBuffer, msaa);
  53         scene.setSecurityContext(acc);
  54         return scene;
  55     }
  56 
  57     @Override
  58     public void setScene(TKScene scene) {
  59         if (scene != null) {
  60             assert scene instanceof EmbeddedScene;
  61         }
  62         super.setScene(scene);
  63     }
  64 
  65     @Override
  66     public void setBounds(float x, float y, boolean xSet, boolean ySet,
  67                           float w, float h, float cw, float ch,
  68                           float xGravity, float yGravity)
  69     {
  70         if (QuantumToolkit.verbose) {
  71             System.err.println("EmbeddedStage.setBounds: x=" + x + " y=" + y + " xSet=" + xSet + " ySet=" + ySet +
  72                                " w=" + w + " h=" + " cw=" + cw + " ch=" + ch);
  73         }
  74         float newW = w > 0 ? w : cw;
  75         float newH = h > 0 ? h : ch;
  76         if ((newW > 0) && (newH > 0)) {
  77             host.setPreferredSize((int)newW, (int)newH);
  78         }
  79     }
  80 
  81     @Override
  82     public float getUIScale() {
  83         return 1.0f;
  84     }
  85 
  86     @Override
  87     public float getRenderScale() {
  88         TKScene scene = getScene();
  89         if (scene instanceof EmbeddedScene) {
  90             return ((EmbeddedScene) scene).getRenderScale();
  91         }
  92         return 1.0f;
  93     }
  94 
  95     @Override public void setMinimumSize(int minWidth, int minHeight) {
  96         // This is a no-op for embedded stages
  97     }
  98 
  99     @Override public void setMaximumSize(int maxWidth, int maxHeight) {
 100         // This is a no-op for embedded stages
 101     }
 102 
 103     @Override
 104     protected void setPlatformEnabled(boolean enabled) {
 105         super.setPlatformEnabled(enabled);
 106         host.setEnabled(enabled);
 107     }
 108 
 109     @Override
 110     public void setIcons(List icons) {
 111         if (QuantumToolkit.verbose) {
 112             System.err.println("EmbeddedStage.setIcons");
 113         }
 114     }
 115 
 116     @Override
 117     public void setTitle(String title) {
 118         if (QuantumToolkit.verbose) {
 119             System.err.println("EmbeddedStage.setTitle " + title);
 120         }
 121     }
 122 
 123     @Override
 124     public void setVisible(boolean visible) {
 125         host.setEmbeddedStage(visible ? this : null);
 126         super.setVisible(visible);
 127     }
 128 
 129     @Override
 130     public void setOpacity(float opacity) {
 131 //        host.setOpacity(opacity);
 132     }
 133 
 134     @Override
 135     public void setIconified(boolean iconified) {
 136         if (QuantumToolkit.verbose) {
 137             System.err.println("EmbeddedScene.setIconified " + iconified);
 138         }
 139     }
 140 
 141     @Override
 142     public void setMaximized(boolean maximized) {
 143         if (QuantumToolkit.verbose) {
 144             System.err.println("EmbeddedScene.setMaximized " + maximized);
 145         }
 146     }
 147 
 148     @Override
 149     public void setAlwaysOnTop(boolean alwaysOnTop) {
 150         if (QuantumToolkit.verbose) {
 151             System.err.println("EmbeddedScene.setAlwaysOnTop " + alwaysOnTop);
 152         }
 153     }
 154 
 155     @Override
 156     public void setResizable(boolean resizable) {
 157         if (QuantumToolkit.verbose) {
 158             System.err.println("EmbeddedStage.setResizable " + resizable);
 159         }
 160     }
 161 
 162     @Override
 163     public void setFullScreen(boolean fullScreen) {
 164         if (QuantumToolkit.verbose) {
 165             System.err.println("EmbeddedStage.setFullScreen " + fullScreen);
 166         }
 167     }
 168 
 169     @Override
 170     public void requestFocus() {
 171         if (!host.requestFocus()) {
 172             return;
 173         }
 174         super.requestFocus();
 175     }
 176 
 177     @Override
 178     public void toBack() {
 179         if (QuantumToolkit.verbose) {
 180             System.err.println("EmbeddedStage.toBack");
 181         }
 182     }
 183 
 184     @Override
 185     public void toFront() {
 186         if (QuantumToolkit.verbose) {
 187             System.err.println("EmbeddedStage.toFront");
 188         }
 189     }
 190 
 191     @Override public boolean grabFocus() {
 192         return host.grabFocus();
 193     }
 194 
 195     @Override public void ungrabFocus() {
 196         host.ungrabFocus();
 197     }
 198 
 199     private void notifyStageListener(final Runnable r) {
 200         AccessControlContext acc = getAccessControlContext();
 201         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 202             r.run();
 203             return null;
 204         }, acc);
 205     }
 206     private void notifyStageListenerLater(final Runnable r) {
 207         Platform.runLater(() -> notifyStageListener(r));
 208     }
 209 
 210     // EmbeddedStageInterface methods
 211 
 212     @Override
 213     public void setLocation(final int x, final int y) {
 214         Runnable r = () -> {
 215             if (stageListener != null) {
 216                 stageListener.changedLocation(x, y);
 217             }
 218         };
 219         // setLocation() can be called on both FX and Swing/SWT/etc threads
 220         if (Toolkit.getToolkit().isFxUserThread()) {
 221             notifyStageListener(r);
 222         } else {
 223             notifyStageListenerLater(r);
 224         }
 225     }
 226 
 227     @Override
 228     public void setSize(final int width, final int height) {
 229         Runnable r = () -> {
 230             if (stageListener != null) {
 231                 stageListener.changedSize(width, height);
 232             }
 233         };
 234         // setSize() can be called on both FX and Swing/SWT/etc threads
 235         if (Toolkit.getToolkit().isFxUserThread()) {
 236             notifyStageListener(r);
 237         } else {
 238             notifyStageListenerLater(r);
 239         }
 240     }
 241 
 242     @Override
 243     public void setFocused(final boolean focused, final int focusCause) {
 244         Runnable r = () -> {
 245             if (stageListener != null) {
 246                 stageListener.changedFocused(focused,
 247                         AbstractEvents.focusCauseToPeerFocusCause(focusCause));
 248             }
 249         };
 250         // setFocused() can be called on both FX and Swing/SWT/etc threads
 251         if (Toolkit.getToolkit().isFxUserThread()) {
 252             notifyStageListener(r);
 253         } else {
 254             notifyStageListenerLater(r);
 255         }
 256     }
 257 
 258     @Override
 259     public void focusUngrab() {
 260         Runnable r = () -> {
 261             if (stageListener != null) {
 262                 stageListener.focusUngrab();
 263             }
 264         };
 265         if (Toolkit.getToolkit().isFxUserThread()) {
 266             notifyStageListener(r);
 267         } else {
 268             notifyStageListenerLater(r);
 269         }
 270     }
 271 
 272     @Override
 273     public void requestInput(String text, int type, double width, double height,
 274                                 double Mxx, double Mxy, double Mxz, double Mxt,
 275                                 double Myx, double Myy, double Myz, double Myt,
 276                                 double Mzx, double Mzy, double Mzz, double Mzt) {
 277         throw new UnsupportedOperationException("Not supported yet.");
 278     }
 279 
 280     @Override
 281     public void releaseInput() {
 282         throw new UnsupportedOperationException("Not supported yet.");
 283     }
 284 
 285     @Override public void setRTL(boolean b) {
 286     }
 287 
 288 }