1 /*
   2  * Copyright (c) 2010, 2014, 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 public void setMinimumSize(int minWidth, int minHeight) {
  82         // This is a no-op for embedded stages
  83     }
  84 
  85     @Override public void setMaximumSize(int maxWidth, int maxHeight) {
  86         // This is a no-op for embedded stages
  87     }
  88 
  89     @Override
  90     protected void setPlatformEnabled(boolean enabled) {
  91         super.setPlatformEnabled(enabled);
  92         host.setEnabled(enabled);
  93     }
  94 
  95     @Override
  96     public void setIcons(List icons) {
  97         if (QuantumToolkit.verbose) {
  98             System.err.println("EmbeddedStage.setIcons");
  99         }
 100     }
 101 
 102     @Override
 103     public void setTitle(String title) {
 104         if (QuantumToolkit.verbose) {
 105             System.err.println("EmbeddedStage.setTitle " + title);
 106         }
 107     }
 108 
 109     @Override
 110     public void setVisible(boolean visible) {
 111         host.setEmbeddedStage(visible ? this : null);
 112         super.setVisible(visible);
 113     }
 114 
 115     @Override
 116     public void setOpacity(float opacity) {
 117 //        host.setOpacity(opacity);
 118     }
 119 
 120     @Override
 121     public void setIconified(boolean iconified) {
 122         if (QuantumToolkit.verbose) {
 123             System.err.println("EmbeddedScene.setIconified " + iconified);
 124         }
 125     }
 126 
 127     @Override
 128     public void setMaximized(boolean maximized) {
 129         if (QuantumToolkit.verbose) {
 130             System.err.println("EmbeddedScene.setMaximized " + maximized);
 131         }
 132     }
 133 
 134     @Override
 135     public void setAlwaysOnTop(boolean alwaysOnTop) {
 136         if (QuantumToolkit.verbose) {
 137             System.err.println("EmbeddedScene.setAlwaysOnTop " + alwaysOnTop);
 138         }
 139     }
 140 
 141     @Override
 142     public void setResizable(boolean resizable) {
 143         if (QuantumToolkit.verbose) {
 144             System.err.println("EmbeddedStage.setResizable " + resizable);
 145         }
 146     }
 147 
 148     @Override
 149     public void setFullScreen(boolean fullScreen) {
 150         if (QuantumToolkit.verbose) {
 151             System.err.println("EmbeddedStage.setFullScreen " + fullScreen);
 152         }
 153     }
 154 
 155     @Override
 156     public void requestFocus() {
 157         if (!host.requestFocus()) {
 158             return;
 159         }
 160         super.requestFocus();
 161     }
 162 
 163     @Override
 164     public void toBack() {
 165         if (QuantumToolkit.verbose) {
 166             System.err.println("EmbeddedStage.toBack");
 167         }
 168     }
 169 
 170     @Override
 171     public void toFront() {
 172         if (QuantumToolkit.verbose) {
 173             System.err.println("EmbeddedStage.toFront");
 174         }
 175     }
 176 
 177     @Override public boolean grabFocus() {
 178         return host.grabFocus();
 179     }
 180 
 181     @Override public void ungrabFocus() {
 182         host.ungrabFocus();
 183     }
 184 
 185     private void notifyStageListener(final Runnable r) {
 186         AccessControlContext acc = getAccessControlContext();
 187         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 188             r.run();
 189             return null;
 190         }, acc);
 191     }
 192     private void notifyStageListenerLater(final Runnable r) {
 193         Platform.runLater(() -> notifyStageListener(r));
 194     }
 195 
 196     // EmbeddedStageInterface methods
 197 
 198     @Override
 199     public void setLocation(final int x, final int y) {
 200         Runnable r = () -> {
 201             if (stageListener != null) {
 202                 stageListener.changedLocation(x, y);
 203             }
 204         };
 205         // setLocation() can be called on both FX and Swing/SWT/etc threads
 206         if (Toolkit.getToolkit().isFxUserThread()) {
 207             notifyStageListener(r);
 208         } else {
 209             notifyStageListenerLater(r);
 210         }
 211     }
 212 
 213     @Override
 214     public void setSize(final int width, final int height) {
 215         Runnable r = () -> {
 216             if (stageListener != null) {
 217                 stageListener.changedSize(width, height);
 218             }
 219         };
 220         // setSize() can be called on both FX and Swing/SWT/etc threads
 221         if (Toolkit.getToolkit().isFxUserThread()) {
 222             notifyStageListener(r);
 223         } else {
 224             notifyStageListenerLater(r);
 225         }
 226     }
 227 
 228     @Override
 229     public void setFocused(final boolean focused, final int focusCause) {
 230         Runnable r = () -> {
 231             if (stageListener != null) {
 232                 stageListener.changedFocused(focused,
 233                         AbstractEvents.focusCauseToPeerFocusCause(focusCause));
 234             }
 235         };
 236         // setFocused() can be called on both FX and Swing/SWT/etc threads
 237         if (Toolkit.getToolkit().isFxUserThread()) {
 238             notifyStageListener(r);
 239         } else {
 240             notifyStageListenerLater(r);
 241         }
 242     }
 243     
 244     @Override
 245     public void focusUngrab() {
 246         Runnable r = () -> {
 247             if (stageListener != null) {
 248                 stageListener.focusUngrab();
 249             }
 250         };
 251         if (Toolkit.getToolkit().isFxUserThread()) {
 252             notifyStageListener(r);
 253         } else {
 254             notifyStageListenerLater(r);
 255         }
 256     }
 257 
 258     @Override
 259     public void requestInput(String text, int type, double width, double height, 
 260                                 double Mxx, double Mxy, double Mxz, double Mxt,
 261                                 double Myx, double Myy, double Myz, double Myt, 
 262                                 double Mzx, double Mzy, double Mzz, double Mzt) {
 263         throw new UnsupportedOperationException("Not supported yet.");
 264     }
 265 
 266     @Override
 267     public void releaseInput() {
 268         throw new UnsupportedOperationException("Not supported yet.");
 269     }
 270 
 271     @Override public void setRTL(boolean b) {
 272     }
 273     
 274 }